def generateErrorResults(msg):
    """
    Write a error as a nicely formatted event.
    :param msg: The error message.
    :return:
    """
    splunkio.write([{'ERROR':msg, '_raw':'ERROR ' + msg}])
Example #2
0
def execute():
    try:
        keywords, argvals = isp.getKeywordsAndOptions()
        results, dummyresults, settings = isp.getOrganizedResults()
        sessionKey = settings.get('sessionKey')

        if sessionKey == None:
            return vixutils.generateErrorResults(
                'sessionKey not passed to the search command, something\'s very wrong!'
            )

        #check that the command is being executed by the scheduler
        sid = settings.get('sid')
        if not sid.startswith('scheduler_') and not argvals.get(
                'forcerun', '') == '1':
            return vixutils.generateErrorResults(
                'rollercontroller is supposed to be ran by the scheduler, add forcerun=1 to force execution'
            )

        # check if error messaging is disabled
        global ERRMSGS_ENABLED
        ERRMSGS_ENABLED = 'disablemsgs' not in keywords

        providers = erp_launcher.listProviders(sessionKey)
        rollVixes = erp_launcher.listVixes(
            sessionKey, 'disabled=0 AND vix.output.buckets.from.indexes=*')
        rollProviders = filterRollProviders(rollVixes, providers)
        searchString = genSearchString(rollVixes, rollProviders)

        kwargs = {}
        for k in ['owner', 'namespace', 'sessionKey', 'hostPath']:
            if k in settings:
                kwargs[k] = settings[k]

        if not os.path.exists(vixutils.getAppBinJars()):
            # first time we're copying jars, force bundle replication
            kwargs['force_bundle_replication'] = 1

        prepareSearchExecution()

        numRetries = argvals.get("retries", 1)

        for i in range(0, int(numRetries)):
            logger.info("Dispatching the search: %s" % searchString)
            search = splunk.search.dispatch(searchString, **kwargs)
            try:
                streamSearch(search, sessionKey)
            finally:
                cancelSearch(search)

    except Exception as e:
        import traceback
        splunkio.write([{
            "stack": traceback.format_exc(),
            "exception": str(e)
        }])
    finally:
        sys.stdout.flush()
def _messageSH():
    while True:
        try:
            timeout = 1
            messages, shouldExit = _getMessages(timeout)
            splunkio.write(messages)
            if shouldExit:
                break
        except IOError:
            #Calling os._exit here instead of sys.exit because we want to
            #terminate process itself instead of just this thread. No need
            #to cleanup anything since parent process is gone. Also child
            #java process would track byitself if python process is gone.
            os._exit(1)
        except:
            pass
Example #4
0
def postErrors(sessionKey, dicts):
    try:
        errors = itertools.ifilter(resultHasError, dicts)
        for err in errors:
            args = {
                "name": "rollercontroller_" + str(time.time()),
                "severity": "error",
                "value": str(err['_time']) + " " + str(err['_raw'])
            }
            rest.simpleRequest('messages',
                               sessionKey,
                               postargs=args,
                               method='POST',
                               raiseAllErrors=True)
    except Exception, e:
        import traceback
        splunkio.write([{
            "stack": traceback.format_exc(),
            "exception": str(e)
        }])
Example #5
0
def writeResults(results, sessionKey):
    dicts = [resToDict(x) for x in results]
    if len(dicts) is not 0:
        splunkio.write(dicts)
        postErrors(sessionKey, dicts)
Example #6
0
def writeResults(results, sessionKey):
    dicts = map(lambda x: resToDict(x), results)
    if len(dicts) is not 0:
        splunkio.write(dicts)
        postErrors(sessionKey, dicts)