Example #1
0
 def processArgs(self, argv):
     allOK = True
     self.progdir = os.path.dirname(sys.argv[0])
     for arg in argv[1:]:
         if (arg.startswith("--auth=")):
             self.authFilename = arg.split("=")[1]
         elif (arg.startswith("--starting")):
             self.starting = arg.split("=")[1]
             (ok, error) = cputils.verifyISO8601(self.starting)
             if not ok:
                 print >> sys.stderr, error
                 allOk = False
         elif (arg.startswith("--ending")):
             self.ending = arg.split("=")[1]
             (ok, error) = cputils.verifyISO8601(self.ending)
             if not ok:
                 print >> sys.stderr, error
                 allOk = False
         elif (arg.startswith("--base=")):
             self.base = arg.split("=")[1]
         elif (arg.startswith("--reportModule")):
             self.reportModule = arg.split("=")[1]
         elif (arg == "-h") or (arg == "-?"):
             allOK = False
         else:
             print >>sys.stderr, "Unknown argument: %s" % arg
             allOK = False
     return allOK
Example #2
0
def do_run():
    config = get_input_config()
    #TODO , poll for data and print output to STD OUT
    #if error , logging.error & sys.exit(2)
    url = config['name']
    apiURL = config['endpoint']
    authKeyID = config['auth_id']
    proxy = None
    if ('proxy' in config) and (config['proxy'] != ''):
        proxy = config['proxy']
        logging.info("cphalo: proxy=%s" % proxy)
    else:
        logging.info("cphalo: no proxy")
    authSecret = config['auth_secret']
    logging.info("cphalo: apiURL=%s" % apiURL)
    apiCon = cpapi.CPAPI()
    (apiCon.key_id, apiCon.secret) = (authKeyID, authSecret)
    if apiURL.endswith('/'):
        apiURL = apiURL[:-1]  # strip off final / if present
    apiCon.base_url = apiURL
    if (proxy != None):
        apiCon.setProxy(proxy)
    logging.info("cphalo: config= %s" % config)
    timestamp = load_checkpoint(config, url)
    logging.info("cphalo: load_checkpoint= %s" % timestamp)

    if not timestamp:
        if 'startdate' in config:
            (ok, msg) = cputils.verifyISO8601(config['startdate'])
            if not ok:
                logging.error("Formatting Error -- start date: %s" % msg)
                sys.exit(1)
            timestamp = config['startdate']
        else:
            timestamp = datetime.datetime.today().date() - datetime.timedelta(
                days=90)

    logging.info("cphalo: startingTimestamp=%s" % timestamp)
    timestamp = processEventBatches(timestamp, authKeyID, authSecret)
    logging.info("cphalo: lastEventTimestamp=%s" % timestamp)
    if (timestamp != None):
        # # timestamp = cputils.getNowAsISO8601() # use this for current system time
        # if type(timestamp) is datetime.date:
        #     timeObj = timestamp
        # else:
        #     timeObj = cputils.strToDate(timeObj)
        # if (timeObj != None):
        #     twoMillisecond = datetime.timedelta(0,0,2000)
        #     newTimeObj = timeObj + twoMillisecond
        #     timestamp = cputils.formatTimeAsISO8601(newTimeObj)
        logging.info("cphalo: checkpointTimestamp=%s" % timestamp)
        save_checkpoint(config, url, timestamp)
    else:
        logging.info("cphalo: no events, not updating checkpoint")
Example #3
0
def do_validate():
    config = get_validation_config()
    #TODO
    #if error , print_validation_error & sys.exit(2)
    if not isPresent(config, 'auth_id'):
        print_validation_error("Missing Authorization ID")
        sys.exit(2)
    if not isPresent(config, 'auth_secret'):
        print_validation_error("Missing Authorization Secret")
        sys.exit(2)
    if not isPresent(config, 'endpoint'):
        print_validation_error("Missing Portal/Endpoint URL")
        sys.exit(2)
    if isPresent(config, 'startdate'):
        timestamp = config['startdate']
        (ok, msg) = cputils.verifyISO8601(timestamp)
        if (not ok):
            print_validation_error("start date: %s" % msg)
            sys.exit(2)
def processCmdLineArgs(argv):
    """ Process the script-specific command line arguments.

        A description of these arguments can be found in the printUsage() function.
    """
    global oneEventPerLine, verbose, outputFormat, outputDestination, lastTimestamp, configDir
    argsOK = True
    for arg in argv:
        if ((arg == '-?') or (arg == "-h")):
            printUsage(os.path.basename(argv[0]))
            return True
        elif ((arg == '-b') or (arg == '--one-batch-per-line')):
            oneEventPerLine = False
        elif (arg == '-v'):
            verbose = True
        elif (arg.startswith('--starting=')):
            lastTimestamp = arg[11:]
            (ok, error) = cputils.verifyISO8601(lastTimestamp)
            if not ok:
                print >> sys.stderr, error
                return True
        elif (arg.startswith('--auth=')):
            filename = arg[7:]
            if len(authFilenameList) > 0:
                print >> sys.stderr, "Error: Only one auth filename allowed"
                return True
            else:
                authFilenameList.append(filename)
        elif (arg.startswith('--cfgdir=') or arg.startswith('--configdir=')):
            i = arg.index('=') + 1
            configDir = arg[i:]
        elif (arg.startswith('--jsonfile=')):
            outputFormat = 'json-file'
            outputDestination = arg[11:]
        elif (arg.startswith('--kvfile=')):
            outputFormat = 'kv-file'
            outputDestination = arg[9:]
        elif (arg.startswith('--kv')):
            outputFormat = 'kv-file'
            outputDestination = None
        elif (arg.startswith('--txtsyslog')):
            if (syslogAvailable):
                if (arg.startswith('--txtsyslog=')):
                    outputFormat = 'txt-file'
                    outputDestination = arg[12:]
                else:
                    outputFormat = 'txt-syslog'
                    outputDestination = 'localhost'
            else:
                syslogNotAvailable()
        elif (arg.startswith('--kvsyslog')) and (not isWindows):
            if (syslogAvailable):
                outputFormat = 'kv-syslog'
                outputDestination = 'localhost'
            else:
                syslogNotAvailable()
        elif (arg != argv[0]):
            print >> sys.stderr, "Unrecognized argument: %s" % arg
            argsOK = False
    if not argsOK:
        print >> sys.stderr, "Run \"%s -h\" to see usage info." % os.path.basename(argv[0])
        return True
    if (outputFormat == None):
        print >> sys.stderr, "No output type selected, must choose one"
        printUsage(argv[0])
        return True
    else:
        return False