Пример #1
0
    def execute(self):
        latest_time = int(time.time())
        earliest_time = latest_time - constants.CLOUDWATCH_TIME_RANGE

        # get cloudwatch kpis
        spl_results = self.get_cloudwatch_kpis('CPUUtilization',
                                               'NetworkIn',
                                               'NetworkOut',
                                               'DiskWriteBytes',
                                               'DiskReadBytes',
                                               earliest_time=earliest_time,
                                               latest_time=latest_time)
        cloudwatch_kpi_list = []
        for spl_result in spl_results:
            cloudwatch_kpi_list.append(self._formatSplResult(spl_result))

        # get feedback data
        feedback_list = json.loads(
            self.read_feebacks(ml_dimension=constants.EC2_DYNAMIC_UP_DOWN))

        # get former recommendation results
        recommendation_result_list = json.loads(
            self.recommendation_kao.query_items(
                {'ml_dimension': constants.EC2_DYNAMIC_UP_DOWN}))

        # get conf
        conf = self.read_conf()
        # execute ml process
        json_arg = {
            'cloudwatch': cloudwatch_kpi_list,
            'feedback': feedback_list,
            'recommendation_results': recommendation_result_list,
            constants.CONF: conf
        }

        try:
            output_list = executor.execute_ml_process(
                'upgrade_downgrade_prediction.py', json_arg)
        except Exception as ex:
            error_logger = util.get_logger(logging.ERROR)
            error_logger.error(ex)
            return ex

        # delete former recommendation results of EC2 Upgrade/Downgrade
        self.recommendation_kao.delete_items_by_condition(
            {'ml_dimension': constants.EC2_DYNAMIC_UP_DOWN})

        # insert recommendation results
        if len(output_list) > 0:
            self.recommendation_kao.batch_insert_items(output_list)

        output_message = 'Insert %d ec2 usage recommendations into kvstore.' % len(
            output_list)
        logger.info(output_message)

        return output_message
    def handleList(self, confInfo):
        search_restriction_arr = util.get_search_restrictions(
            self.getSessionKey())
        prefix = self.callerArgs[PREFIX_ARG][0]
        field = self.callerArgs[FIELD_ARG][0]
        regex_expression = '(?<=[( ])%s\s*=' % field

        new_search_restriction_arr = []

        for search_restriction in search_restriction_arr:
            util.get_logger().info(search_restriction)
            search_restriction, count = re.subn(regex_expression,
                                                '%s%s=' % (prefix, field),
                                                search_restriction)
            new_search_restriction_arr.append(search_restriction)

        confInfo['search_restrictions'].append(
            'search_restrictions', ' OR '.join(new_search_restriction_arr))
        return
__author__ = 'michael'

import splunk.admin as admin
import utils.app_util as util

logger = util.get_logger()


class BaseHandler(admin.MConfigHandler):
    def setup(self, readonly_public=False):
        # By default it is "false", which means the sub-handlers also can not process GET requests when user does not meet the validation rules.
        # When it is set to "true", the "handleList" method can be invoked even current user is not power.
        if self.requestedAction == admin.ACTION_LIST and readonly_public:
            return
        util.validate_aws_admin(self.getSessionKey())

    def _log_request(self):
        logger.info(
            'action %s name %s args %s' %
            (self.requestedAction, self.callerArgs.id, self.callerArgs))
__author__ = 'pezhang'

import anomaly_detection.anomaly_detection_const as const
from datetime import datetime
from anomaly_job import AnomalyJob
import heapq
import utils.app_util as app_util

logger = app_util.get_logger()


class DefaultClock(object):
    """
        Use built-in datetime to get current time
    """
    def now(self):
        return datetime.now()


class AnomalyConfManager(object):
    """
        Configuration file manager. be responsible for reading conf files, getting current hour's task
        and writing conf file with stanza name and correp
    """
    def __init__(self, service, conf_name=const.CONF_NAME, clock=None):
        """
            :param service: used to initialize configuration file handler
            :param conf_name: the name of conf file which stored job settings
            :param clock: tools to get current time, mainly used for UT injection
        """
        self.conf = service.confs[conf_name]
    results, dummyresults, settings = intersplunk.getOrganizedResults()
    session_key = settings['sessionKey']

    # generate local service
    service = LocalServiceManager(app=util.APP_NAME,
                                  session_key=session_key).get_local_service()

    if len(sys.argv) == 2:
        lookup_name = sys.argv[1]

        spl = '| inputlookup %s' % lookup_name

        # get search restrictions of current user
        search_restriction_arr = util.get_search_restrictions(session_key)

        if len(search_restriction_arr) > 0:
            search_restrictions = ' OR '.join(search_restriction_arr)
            spl = '%s | search %s' % (spl, search_restrictions)

        util.get_logger().info(spl)

        results = search.searchAll(spl, sessionKey=session_key)

except:
    import traceback
    stack = traceback.format_exc()
    results = intersplunk.generateErrorResults("Error : Traceback: " +
                                               str(stack))
    util.get_logger().error(str(stack))

intersplunk.outputResults(results)