def request_dashboard_api(): sources = [ 'cpu', 'memory', 'disk', 'mysql', 'mean', 'deviation_pos', 'deviation_neg' ] t = time.time() response = DW.cache_key_get('roledefs') warnings = DW.cache_key_get('ro_warnings') server_dict = ResourceDashboard().create_server_list( literal_eval(response), 'production_servers' ) response_dict = { 'response': { 'value': {} }, 'warnings': {} } response_dict['warnings'] = literal_eval(warnings) error_dict = {} for server in server_dict: response_dict['response']['value'][server] = {} for source in sources: key = 'ro_%s_%s' % (source, server) try: response = DW.cache_key_get(key) response_dict['response']['value'][server][source] = literal_eval(response) except Exception, e: error_dict[key] = str(e)
def request_admin_api(): print request.method if request.method == 'GET': return jsonify(ResourceAdmin()._show_help()) else: print request.json if not 'server_name' in request.json \ or not 'action' in request.json \ or not 'network' in request.json: return jsonify({'error': 'missing required data'}) response_object = GenericDataObject(request.json) roledefs = literal_eval(DW.cache_key_get('roledefs')) if response_object.action == 'remove': changed_roles = {} try: for name in roledefs: if response_object.server_name in name: host = roledefs[name][response_object.network] for name in roledefs: if host in roledefs[name]: roledefs[name].remove(host) changed_roles[name] = roledefs[name] DW.cache_key_set('roledefs', roledefs) except Exception, e: return jsonify({'error':e}) return jsonify({ 'host':host, 'changed_roles': changed_roles }) elif response_object.action == 'list': return jsonify(roledefs)
def request_admin_api(): print request.method if request.method == 'GET': return jsonify(ResourceAdmin()._show_help()) else: print request.json if not 'server_name' in request.json \ or not 'action' in request.json \ or not 'network' in request.json: return jsonify({'error': 'missing required data'}) response_object = GenericDataObject(request.json) roledefs = literal_eval(DW.cache_key_get('roledefs')) if response_object.action == 'remove': changed_roles = {} try: for name in roledefs: if response_object.server_name in name: host = roledefs[name][response_object.network] for name in roledefs: if host in roledefs[name]: roledefs[name].remove(host) changed_roles[name] = roledefs[name] DW.cache_key_set('roledefs', roledefs) except Exception, e: return jsonify({'error': e}) return jsonify({'host': host, 'changed_roles': changed_roles}) elif response_object.action == 'list': return jsonify(roledefs)
class ResourceRemoveAlertServer(AxolResource): """docstring for ResourceRemoveAlertServer Must implement: _show_help self.methods = {<method_type>: function} self.source = {keyword} request_{keyword}_api calculate_new_fields """ required_post = { 'server_name': (True, u's'), 'profile': (False, u's') } def __init__(self): super(ResourceRemoveAlertServer, self).__init__() self.source = 'remove_alert_servers' self.local = True def _show_help(self): return { 'Help': { 'api': '/api/remove_alert_servers', 'method': 'POST', 'required data': { 'server_name': '<name of the server to remove>' }, 'version': api } } @staticmethod @app.route('/api/remove_alert_server', methods=['POST', 'GET']) def api_remove_alert_server(): role = 'production_servers' if request.method == 'GET': return jsonify(ResourceRemoveAlertServer()._show_help()) try: data = CommonResource.handle_request(request, ResourceRemoveAlertServer.required_post) except Exception, e: CommonLogger.log(e, 'remove_alert_server', 'api_remove_alert_server') return jsonify({'response': {'error': str(e)}}) try: roledefs = literal_eval(DW.cache_key_get('roledefs')) server_dict = ResourceRemoveAlertServer.create_server_list(roledefs, role) if server_dict[data.server_name] in roledefs[role]: roledefs[role].remove(server_dict[data.server_name]) DW.cache_key_set('roledefs', roledefs) if data.server_name in server_dict.keys(): del server_dict[data.server_name] except Exception, e: CommonLogger.log(e, 'remove_alert_server', 'api_remove_alert_server') return jsonify({'response': {'error': str(e)}})
def run_task(self, task): print 'RUN TASK 1 ##############' print task for server in task.value: for _type in self.sources: key = '%s_%s' % (_type, str(server)) try: cache_data = DW.cache_key_get(key) if type(eval(str(cache_data))) is dict: try: task.value[server][_type] = GenericDataObject(eval(str(cache_data))) if task.value[server][_type].warnings[server] != None: print 'RED FLAG: %s %s' % (server, _type) print task.value[server][_type].warnings else: task.value[server][_type].health_indicator = CommonMath.adaptive_filtration( task.value[server][_type].normalized_indicator, task.value[server][_type].multiplier, task.value[server][_type].threshold_red, task.value[server][_type].scale ) except Exception, e: print 'ERROR 1: %s' % e except Exception, e: CommonLogger.log(e, 'create_health_metrics', 'run_task-<%s-%s>' % (server, _type)) print 'ERROR 2: %s' % e return jsonify({'response': {'error': str(e)}})
def run_task(self, task): print 'RUN TASK 1 ##############' print task for server in task.value: for _type in self.sources: key = '%s_%s' % (_type, str(server)) try: cache_data = DW.cache_key_get(key) if type(eval(str(cache_data))) is dict: try: task.value[server][_type] = GenericDataObject( eval(str(cache_data))) if task.value[server][_type].warnings[ server] != None: print 'RED FLAG: %s %s' % (server, _type) print task.value[server][_type].warnings else: task.value[server][ _type].health_indicator = CommonMath.adaptive_filtration( task.value[server] [_type].normalized_indicator, task.value[server][_type].multiplier, task.value[server] [_type].threshold_red, task.value[server][_type].scale) except Exception, e: print 'ERROR 1: %s' % e except Exception, e: CommonLogger.log(e, 'create_health_metrics', 'run_task-<%s-%s>' % (server, _type)) print 'ERROR 2: %s' % e return jsonify({'response': {'error': str(e)}})
def populate_cache(self, response): server_dict = {} print response print '##################' response['api'] = api response['api']['api_name'] = 'health' for item in response['value']: print '1111' print response['value'][item] response['value'][item] = response['value'][item] for item_2 in response['value'][item]: if item_2 != 'name' and item_2 != 'warnings': print '2222' print response['value'][item][item_2] response['value'][item][item_2] = response['value'][item][item_2] key = 'ro_%s_%s' % (item_2, item) print '-------------------' print key DW.cache_key_set(key, response['value'][item][item_2])
def populate_cache(self, response): server_dict = {} print response print '##################' response['api'] = api response['api']['api_name'] = 'health' for item in response['value']: print '1111' print response['value'][item] response['value'][item] = response['value'][item] for item_2 in response['value'][item]: if item_2 != 'name' and item_2 != 'warnings': print '2222' print response['value'][item][item_2] response['value'][item][item_2] = response['value'][item][ item_2] key = 'ro_%s_%s' % (item_2, item) print '-------------------' print key DW.cache_key_set(key, response['value'][item][item_2])
class ResourceGetAlertServers(AxolResource): """docstring for ResourceGetAlertServers Must implement: _show_help self.methods = {<method_type>: function} self.source = {keyword} request_{keyword}_api calculate_new_fields """ required_post = {'profile': (False, u's')} def __init__(self): super(ResourceGetAlertServers, self).__init__() self.source = 'get_alert_servers' self.local = True def _show_help(self): return { 'Help': { 'api': '/api/get_alert_servers', 'method': 'POST', 'required data': 'None', 'version': api } } @staticmethod @app.route('/api/get_alert_servers', methods=['POST', 'GET']) def api_get_alert_servers(): if request.method == 'GET': return jsonify(ResourceGetAlertServers()._show_help()) try: data = CommonResource.handle_request( request, ResourceGetAlertServers.required_post) except Exception, e: CommonLogger.log(e, 'get_alert_servers', 'api_get_alert_servers') return jsonify({'response': {'error': str(e)}}) try: roledefs = literal_eval(DW.cache_key_get('roledefs')) server_dict = ResourceGetAlertServers.create_server_list( roledefs, 'production_servers') except Exception, e: CommonLogger.log(e, 'get_alert_servers', 'api_get_alert_servers') return jsonify({'response': {'error': str(e)}})
def run_task(self, AxolTask): AxolTask.name = 'alerts' AxolTask.api = api AxolTask.api['api_name'] = 'send_warnings_alerts' AxolTask.value = {} warnings = {} for source in self.sources: warnings[source] = [] key = '%s_warnings' % source print key try: value = literal_eval(DW.cache_key_get(key)) except Exception, e: print 'ERROR RT: %s' % e for server in value: if type(value[server]) is dict: if server in AxolTask.value: AxolTask.value[server][source] = value[server]['error'] else: AxolTask.value[server] = {source: value[server]['error']}
class ResourceAdmin(AxolResource): """docstring for ResourceProcessor Must implement: _show_help self.source = {keyword} request_{keyword}_api calculate_new_fields """ def __init__(self): super(ResourceAdmin, self).__init__() self.source = 'version' self.local = True def _show_help(self): return { 'Help': { 'roles administration': { 'api': '/api/system/admin/roles', 'available_methods': { 'POST': { 'required data': [ 'network=<internal, external>', 'action=<remove, list, list_ec2, restore>', 'server_name=<name of server>' ] } } } } } @staticmethod @app.route('/api/manage_alerts', methods=['POST', 'GET']) def request_admin_api(): print request.method if request.method == 'GET': return jsonify(ResourceAdmin()._show_help()) else: print request.json if not 'server_name' in request.json \ or not 'action' in request.json \ or not 'network' in request.json: return jsonify({'error': 'missing required data'}) response_object = GenericDataObject(request.json) roledefs = literal_eval(DW.cache_key_get('roledefs')) if response_object.action == 'remove': changed_roles = {} try: for name in roledefs: if response_object.server_name in name: host = roledefs[name][response_object.network] for name in roledefs: if host in roledefs[name]: roledefs[name].remove(host) changed_roles[name] = roledefs[name] DW.cache_key_set('roledefs', roledefs) except Exception, e: return jsonify({'error': e}) return jsonify({'host': host, 'changed_roles': changed_roles}) elif response_object.action == 'list': return jsonify(roledefs) elif response_object.action == 'restore': roledefs = generate_base_roles() DW.cache_key_set('roledefs', roledefs) return jsonify(roledefs)
def process_notification(name, AxolTask, key): notification_status = DW.cache_key_get(key) print '<Notification Status>: %s' % notification_status if notification_status == None: default = { 'warn_threshold': 0, 'silence_threshold': 0 } DW.cache_key_set(key, default) notification_status = DW.cache_key_get(key) ns = literal_eval(notification_status) ns['warn_threshold'] += 1 if (7 >= ns['warn_threshold'] > 4 and ns['silence_threshold'] == 0): self.send_notice( name=AxolTask.name, data=AxolTask.value, group='default', alert_type=['email', 'text'] ) DW.cache_key_set(key, ns) return 1 elif (ns['warn_threshold'] > 4 and ns['silence_threshold'] <= 12): ns['silence_threshold'] += 1 DW.cache_key_set(key, ns) return 1 elif ns['silence_threshold'] > 12: ns['warn_threshold'] = 5 ns['silence_threshold'] = 0 DW.cache_key_set(key, ns) return 1 DW.cache_key_set(key, ns) return 1
key = 'notification_threshold' if len(AxolTask.value) > 0: for server in AxolTask.value: if server != 'status': if AxolTask.value[server] != []: print '<Task Warnings>: %s' % AxolTask.value[server] try: process_notification(server, AxolTask, key) except Exception, e: CommonLogger.log(e, 'axol_resource', 'process_notification') else: default = { 'warn_threshold': 0, 'silence_threshold': 0 } DW.cache_key_set(key, default) return 1 def process_queue(self, result): t = 0 while True: if result.ready() == True: g = result.get() return g, t break else: t = t + 0.001 time.sleep(0.001) if t >= 10: tmp = { 'Response': {
def process_notification(name, AxolTask, key): notification_status = DW.cache_key_get(key) print "<Notification Status>: %s" % notification_status if notification_status == None: default = {"warn_threshold": 0, "silence_threshold": 0} DW.cache_key_set(key, default) notification_status = DW.cache_key_get(key) ns = literal_eval(notification_status) ns["warn_threshold"] += 1 if 7 >= ns["warn_threshold"] > 4 and ns["silence_threshold"] == 0: self.send_notice(name=AxolTask.name, data=AxolTask.value, group="default", alert_type=["email", "text"]) DW.cache_key_set(key, ns) return 1 elif ns["warn_threshold"] > 4 and ns["silence_threshold"] <= 12: ns["silence_threshold"] += 1 DW.cache_key_set(key, ns) return 1 elif ns["silence_threshold"] > 12: ns["warn_threshold"] = 5 ns["silence_threshold"] = 0 DW.cache_key_set(key, ns) return 1 DW.cache_key_set(key, ns) return 1
DW.cache_key_set(key, ns) return 1 key = "notification_threshold" if len(AxolTask.value) > 0: for server in AxolTask.value: if server != "status": if AxolTask.value[server] != []: print "<Task Warnings>: %s" % AxolTask.value[server] try: process_notification(server, AxolTask, key) except Exception, e: CommonLogger.log(e, "axol_resource", "process_notification") else: default = {"warn_threshold": 0, "silence_threshold": 0} DW.cache_key_set(key, default) return 1 def process_queue(self, result): t = 0 while True: if result.ready() == True: g = result.get() return g, t break else: t = t + 0.001 time.sleep(0.001) if t >= 10: tmp = {"Response": {"failed_request": True}} return tmp, t