def service_perf_data_live_query(db,site,log_split): if log_split[0] == "CURRENT SERVICE STATE": host_ip = log_split[12] description=log_split[11] elif log_split[0] == "SERVICE ALERT" or log_split[0] == "INITIAL SERVICE STATE": host_ip = log_split[12] description=log_split[11] elif log_split[0] == "SERVICE FLAPPING ALERT": host_ip = log_split[11] description=log_split[9] if 'invent' not in log_split[5]: query = "GET services\nColumns: service_perf_data\nFilter: service_description ~ %s\n" %(log_split[5]) perf_data= rrd_main.get_from_socket(site, query) perf_data = rrd_migration.get_threshold(perf_data) for ds in perf_data.iterkeys(): cur =perf_data.get(ds).get('cur') war =perf_data.get(ds).get('war') crit =perf_data.get(ds).get('cric') serv_event_dict=dict(sys_timestamp=int(log_split[1]),device_name=log_split[4],severity=log_split[8], description=log_split[11],min_value=0,max_value=0,avg_value =0,current_value=cur, data_source = ds,warning_threshold=war, critical_threshold =crit ,check_timestamp = int(log_split[1]), ip_address=host_ip,service_name=log_split[5],site_name=site) mongo_functions.mongo_db_insert(db,serv_event_dict,"serv_event") else: query = "GET services\nColumns: service_plugin_output\nFilter: service_description ~ %s\n" %(log_split[5]) perf_data= rrd_main.get_from_socket(site, query) current_value = perf_data.split('- ')[1].strip('\n') serv_event_dict=dict(sys_timestamp=int(log_split[1]),device_name=log_split[4],severity=log_split[8], description=log_split[11],min_value=0,max_value=0,avg_value =0, current_value=current_value, data_source = log_split[5],warning_threshold=0, critical_threshold =0 ,check_timestamp = int(log_split[1]), ip_address=host_ip,service_name=log_split[5],site_name=site) mongo_functions.mongo_db_insert(db,serv_event_dict,"serv_event")
def extract_nagios_events_live(mongo_host, mongo_db, mongo_port): db = None perf_data = {} file_path = os.path.dirname(os.path.abspath(__file__)) path = [path for path in file_path.split('/')] war = None crit = None cur = None if 'sites' not in path: raise Exception, "File is not in omd specific directory" else: site = path[path.index('sites')+1] db = rrd_migration.mongo_conn( host=mongo_host, port=mongo_port, db_name=mongo_db ) utc_time = datetime(1970, 1,1,5,30) #start_epoch = get_latest_event_entry(db_type = 'mongodb',db=db) #if start_epoch == None: start_time = datetime.now() - timedelta(minutes=1) start_epoch = int((start_time - utc_time).total_seconds()) end_time = datetime.now() end_epoch = int((end_time - utc_time).total_seconds()) # sustracting 5.30 hours host_event_dict ={} serv_event_dict={} query = "GET log\nColumns: log_type log_time log_state_type log_state host_name service_description "\ "options host_address current_service_perf_data\nFilter: log_time > %s\nFilter: class = 0\nFilter: class = 1\n"\ "Filter: class = 2\nFilter: class = 3\nFilter: class = 4\nFilter: class = 6\nOr: 6\n" %(start_epoch) output= rrd_main.get_from_socket(site, query) for log_attr in output.split('\n'): log_split = [log_split for log_split in log_attr.split(';')] if log_split[0] == "CURRENT SERVICE STATE": service_perf_data_live_query(db,site,log_split) elif log_split[0] == "SERVICE ALERT" or log_split[0] == "INITIAL SERVICE STATE": service_perf_data_live_query(db,site,log_split) elif log_split[0] == "SERVICE FLAPPING ALERT": service_perf_data_live_query(db,site,log_split) query = "GET log\nColumns: log_type log_time log_state_type log_state host_name service_description "\ "options host_address current_host_perf_data\nFilter: log_time > %s\nFilter: class = 0\n"\ "Filter: class = 1\nFilter: class = 2\nFilter: class = 3\nFilter: class = 4\nFilter: class = 6\nOr: 6\n" %(start_epoch) output= rrd_main.get_from_socket(site, query) for log_attr in output.split('\n'): log_split = [log_split for log_split in log_attr.split(';')] if log_split[0] == "CURRENT HOST STATE": network_perf_data_live_query(db,site,log_split) elif log_split[0] == "HOST ALERT" or log_split[0] == "INITIAL HOST STATE": network_perf_data_live_query(db,site,log_split) elif log_split[0] == "HOST FLAPPING ALERT": network_perf_data_live_query(db,site,log_split)
def inventory_perf_data(site, hostlist): invent_check_list = [] invent_service_dict = {} db = mongo_functions.mongo_db_conn(site, "nocout") for host in hostlist[0]: query = "GET hosts\nColumns: host_services\nFilter: host_name = %s\n" % ( host) query_output = rrd_main.get_from_socket(site, query).strip() service_list = [ service_name for service_name in query_output.split(',') ] for service in service_list: if service.endswith('_invent'): invent_check_list.append(service) for service in invent_check_list: query_string = "GET services\nColumns: service_state plugin_output host_address\nFilter: " + \ "service_description = %s\nFilter: host_name = %s\nOutputFormat: json\n" % (service,host) query_output = json.loads( rrd_main.get_from_socket(site, query_string).strip()) plugin_output = str(query_output[0][1].split('- ')[1]) service_state = (query_output[0][0]) if service_state == 0: service_state = "OK" elif service_state == 1: service_state = "WARNING" elif service_state == 2: service_state = "CRITICAL" elif service_state == 3: service_state = "UNKNOWN" host_ip = str(query_output[0][2]) current_time = int(time.time()) invent_service_dict = dict(sys_timestamp=current_time, check_timestamp=current_time, device_name=str(host), service_name=service, current_value=plugin_output, min_value=0, max_value=0, avg_value=0, data_source=service_name, severity=service_state, site_name=site, warning_threshold=0, critical_threshold=0, ip_address=host_ip) mongo_functions.mongo_db_insert(db, invent_service_dict, "inventory_services") invent_service_dict = {}
def snmp_alarm_extraction(): try: query = "GET hosts\nColumns: host_address\nOutputFormat: json\n" utc_time = datetime(1970, 1, 1) #Shifting the fetching time to -15mins, for the time being end_time = datetime.now() - timedelta(minutes=60) end_time = end_time.strftime("%Y-%m-%d %H:%M:%S") #start_time = end_time - timedelta(minutes=5) file_path = os.path.dirname(os.path.abspath(__file__)) path = [path for path in file_path.split('/')] if len(path) <= 4 or 'sites' not in path: raise Exception, "Place the file in appropriate omd site" else: site = path[path.index('sites') + 1] output = json.loads(rrd_main.get_from_socket(site, query)) for host_ip in output: modified_query = "select * from snmptt where agentip='%s' and traptime >='%s';" % ( host_ip[0], end_time) trap_result = mysql_functions.mysql_execute( modified_query, "snmptt") mongo_db_store(site, trap_result) except SyntaxError, e: raise MKGeneralException(_("Can not get snmp alarm outputs: %s") % (e))
def status_perf_data_main(): try: configs = parse_config_obj() for section, options in configs.items(): site = options.get('site') query = "GET hosts\nColumns: host_name\nOutputFormat: json\n" output = json.loads(rrd_main.get_from_socket(site,query)) status_perf_data(site,output) except SyntaxError, e: raise MKGeneralException(("Can not get performance data: %s") % (e))
def status_perf_data(site,hostlist): status_check_list = [] status_service_dict = {} db = mongo_functions.mongo_db_conn(site,"nocout") for host in hostlist[0]: query = "GET hosts\nColumns: host_services\nFilter: host_name = %s\n" %(host) query_output = rrd_main.get_from_socket(site,query).strip() service_list = [service_name for service_name in query_output.split(',')] for service in service_list: if service.endswith('_status'): status_check_list.append(service) for service in status_check_list: query_string = "GET services\nColumns: service_state service_perf_data host_address\nFilter: " + \ "service_description = %s\nFilter: host_name = %s\nOutputFormat: json\n" % (service,host) query_output = json.loads(rrd_main.get_from_socket(site,query_string).strip()) perf_data_output = str(query_output[0][1]) service_state = (query_output[0][0]) host_ip = str(query_output[0][2]) current_time = int(time.time()) if service_state == 0: service_state = "OK" elif service_state == 1: service_state = "WARNING" elif service_state == 2: service_state = "CRITICAL" elif service_state == 3: service_state = "UNKNOWN" perf_data = rrd_migration.get_threshold(perf_data_output) for ds in perf_data.iterkeys(): cur =perf_data.get(ds).get('cur') war =perf_data.get(ds).get('war') crit =perf_data.get(ds).get('cric') status_service_dict = dict (sys_timestamp=current_time,check_timestamp=current_time,device_name=str(host), service_name=service,current_value=cur,min_value=0,max_value=0,avg_value=0, data_source=ds,severity=service_state,site_name=site,warning_threshold=war, critical_threshold=crit,ip_address=host_ip) mongo_functions.mongo_db_insert(db,status_service_dict,"status_services") query_output = json.loads(rrd_main.get_from_socket(site,query_string).strip()) status_service_dict = {}
def extract_nagios_events_live(): db = None file_path = os.path.dirname(os.path.abspath(__file__)) path = [path for path in file_path.split('/')] if 'sites' not in path: raise Exception, "File is not in omd specific directory" else: site = path[path.index('sites')+1] utc_time = datetime(1970, 1,1) start_time = datetime.now() - timedelta(minutes=10) end_time = datetime.now() start_epoch = int((start_time - utc_time).total_seconds()) end_epoch = int((end_time - utc_time).total_seconds()) # sustracting 5.30 hours start_epoch -= 19800 end_epoch -= 19800 host_event_dict ={} serv_event_dict={} db = mongo_functions.mongo_db_conn(site,"nocout_event_log") query = "GET log\nColumns: log_type log_time log_state_type log_state host_name service_description options host_address\nFilter: log_time >= %s\nFilter: class = 0\nFilter: class = 1\nFilter: class = 2\nFilter: class = 3\nFilter: class = 4\nFilter: class = 6\nOr: 6\n" %(start_epoch) output= rrd_main.get_from_socket(site, query) #print output for log_attr in output.split('\n'): log_split = [log_split for log_split in log_attr.split(';')] print log_split if log_split[0] == "CURRENT HOST STATE": host_ip = log_split[11] #host_ip = log_split[10].split(':')[0] #host_ip = host_ip.split('-')[1] host_event_dict=dict(time=log_split[1],host_name=log_split[4],status=log_split[7], state_type=log_split[2],discription=log_split[11], ip_address=host_ip,event_type_name=log_split[0]) mongo_functions.mongo_db_insert(db,host_event_dict,"host_event") elif log_split[0] == "CURRENT SERVICE STATE": host_ip = log_split[12] #host_ip = log_split[11].split(':')[0] #host_ip = host_ip.split('-')[1] serv_event_dict=dict(time=log_split[1],host_name=log_split[4],status=log_split[8], state_type=log_split[2],discription=log_split[11], ip_address=host_ip,event_type_name=log_split[0],event_name=log_split[5]) #print serv_event_dict mongo_functions.mongo_db_insert(db,serv_event_dict,"serv_event") elif log_split[0] == "HOST ALERT": host_ip = log_split[11] #host_ip = log_split[10].split(':')[0] #host_ip = host_ip.split('-')[1] host_event_dict=dict(time=log_split[1],host_name=log_split[4],status=log_split[7], state_type=log_split[2],discription=log_split[10], ip_address=host_ip,event_type_name=log_split[0]) #print host_event_dict mongo_functions.mongo_db_insert(db,host_event_dict,"host_event") elif log_split[0] == "HOST FLAPPING ALERT": host_ip = log_split[11] host_event_dict=dict(time=log_split[1],host_name=log_split[4],status=log_split[7], state_type=None,discription=log_split[8], ip_address=host_ip,event_type_name=log_split[0]) mongo_functions.mongo_db_insert(db,host_event_dict,"host_event") elif log_split[0] == "SERVICE ALERT": host_ip = log_split[11] #host_ip = log_split[11].split(':')[0] #host_ip = host_ip.split('-')[1] serv_event_dict=dict(time=log_split[1],host_name=log_split[4],status=log_split[8], state_type=log_split[2],discription=log_split[11], ip_address=host_ip,event_type_name=log_split[0],event_name=log_split[5]) #print serv_event_dict mongo_functions.mongo_db_insert(db,serv_event_dict,"serv_event") elif log_split[0] == "SERVICE FLAPPING ALERT": serv_event_dict=dict(time=log_split[1],host_name=log_split[4],status=log_split[8], state_type=None,discription=log_split[9], ip_address=None,event_type_name=log_split[0],event_name=log_split[5]) mongo_functions.mongo_db_insert(db,serv_event_dict,"serv_event") elif log_split[0] == "HOST NOTIFICATION": host_ip = log_split[11] #host_ip = log_split[10].split(':')[0] #host_ip = host_ip.split('-')[1] host_event_dict=dict(time=log_split[1],host_name=log_split[4],status=log_split[7], state_type=log_split[2],discription=log_split[10], ip_address=host_ip,event_type_name=log_split[0]) #print host_event_dict mongo_functions.mongo_db_insert(db,host_event_dict,"notification_event") elif log_split[0] == "SERVICE NOTIFICATION": host_ip = log_split[12] #host_ip = log_split[11].split(':')[0] #host_ip = host_ip.split('-')[1] serv_event_dict=dict(time=log_split[1],host_name=log_split[4],status=log_split[9], state_type=log_split[2],discription=log_split[11], ip_address=host_ip,event_type_name=log_split[0],event_name=log_split[5]) #print serv_event_dict mongo_functions.mongo_db_insert(db,serv_event_dict,"notification_event")