def get_queue_state(self): q = StateSetMetricFamily('fah_queue_state', 'State of the queue', labels=['id', 'slot', 'core', 'unit']) states = { 'UNKNOWN': False, 'PAUSED': False, 'UPDATING': False, 'FINISHED': False, 'UPLOADED': False, 'RUNNING': False, 'ACTIVE': False, 'ONLINE': False, 'FINISHING': False, 'FAILED': False, 'ERROR': False, 'FAULTY': False, 'SHUTDOWN': False, 'CONNECTING': False, 'OFFLINE': False, 'READY': False, 'DUMP': False, 'DOWNLOAD': False } for queue in self.queue_info: queue_states = states queue_states[queue['state']] = True q.add_metric( [queue['id'], queue['slot'], queue['core'], queue['unit']], queue_states) yield q
def get_metric_authentication(self): """ Shows if the current run was authenticated """ m = StateSetMetricFamily( 'authentication', 'Shows if authentication is enabled', labels=['exchange'], ) try: m.add_metric( [f'{self.exchange.exchange}'], {'enabled': self.exchange.get_enable_authentication()}, ) except AttributeError: pass return m
def get_status_metrics(self, result): metric = StateSetMetricFamily( 'dkron_job_status', 'Dkron job status', labels=["jobname"]) for job in result: name = job['name'] # If there's a null result, we want to export a zero. status = True status_txt = job.get('status') or 'failed' if status_txt == 'failed': status = False metric.add_metric([name], {'success': status}) # job_status = job['status'] return metric
def test_stateset_labels(self): cmf = StateSetMetricFamily('s', 'help', labels=['foo']) cmf.add_metric(['bar'], { 'a': False, 'b': False, }) self.custom_collector(cmf) self.assertEqual( 0, self.registry.get_sample_value('s', { 'foo': 'bar', 's': 'a' })) self.assertEqual( 0, self.registry.get_sample_value('s', { 'foo': 'bar', 's': 'b' }))
def test_stateset(self): self.custom_collector( StateSetMetricFamily('s', 'help', value={ 'a': True, 'b': True, })) self.assertEqual(1, self.registry.get_sample_value('s', {'s': 'a'})) self.assertEqual(1, self.registry.get_sample_value('s', {'s': 'b'}))
def test_simple_stateset(self): families = text_string_to_metric_families("""# TYPE a stateset # HELP a help a{a="bar"} 0 a{a="foo"} 1.0 # EOF """) self.assertEqual([StateSetMetricFamily("a", "help", {'foo': True, 'bar': False})], list(families))
def _status(status: dict) -> StateSetMetricFamily: status_dict = { state: state == status["overall"]["level"] for state in ["available", "degraded"] } status = StateSetMetricFamily("kibana_status", "Kibana Status", value=status_dict) return status
def collect(self): license_counter = CounterMetricFamily("active_licenses", "active license count", labels=("license", )) client_state = StateSetMetricFamily("active_licenses", "active license count", labels=("host", "port", "license", "user")) for license in self._licenses: license_counter.add_metric([license.name], len(license.licenses)) for client in license.licenses: client_state.add_metric( (client.host, str(client.port), license.name, client.user), value={"active": True}) yield (license_counter) yield (client_state)
def _status(status: dict) -> (StateSetMetricFamily, GaugeMetricFamily): status_dict = { state: state == status["overall"]["state"] for state in ["red", "yellow", "green"] } since = datestring_to_timestamp(status["overall"]["since"]) status = StateSetMetricFamily("kibana_status", "Kibana Status", value=status_dict) since = GaugeMetricFamily("kibana_status_since", "Last change of status, in seconds since epoch", value=since) return status, since
def collect(self): data = get_solar_data(self.url, self.username, self.password) g = GaugeMetricFamily( "solar_ac_power_generated_kw", "The amount of power sent to the grid", value=data["generator_ac_power_kW"], ) yield g g = GaugeMetricFamily( "solar_dc_power_generated_kw", "The amount of power generated", value=data["generator_dc_power_kW"], ) yield g g = GaugeMetricFamily( "solar_temperature", "The temperature of the inverter unit", value=data["temperature"], ) yield g g = GaugeMetricFamily( "solar_record_datetime", "The datetime the record was taken", value=int(datetime.now().timestamp()), ) yield g ss = { states[sta]: True if data["status"] == states[sta] else False for sta in states } s = StateSetMetricFamily("solar_state", "The state of the inverter", ss) yield s
def get_schedule_status_metrics(self, result): metric = StateSetMetricFamily( 'dkron_job_schedule_status', 'Dkron job schedule status', labels=["jobname"]) for job in result: name = job['name'] # If there's a null result, we want to export a zero. next_date_str = job.get('next') or '2020-01-01T00:00:00.000Z' next_date = dateutil.parser.parse(next_date_str) diff_date = next_date - datetime.datetime.now(datetime.timezone.utc) if diff_date < datetime.timedelta(minutes=-1): metric.add_metric([name], {'success': False}) else: metric.add_metric([name], {'success': True}) # job_status = job['status'] return metric
def collect(self): # get SID list from os dir sidList = get_sid_list() for sid in sidList: c = consul.Consul(host=os.environ.get('CONSUL_HOST') if os.environ.get('CONSUL_HOST') else '127.0.0.1', port=23345, scheme='http') kvid, kvv = c.kv.get(sid + '_login') if kvv: # get SID login info from consul kvvDict = json.loads(kvv['Value']) conn = None for instance in get_instance_list_by_sid(sid): if not conn and instance['type'] == 'DIALOG': conn = R3rfcconn(r3ashost='127.0.0.1', r3sysnr=instance['sysnr'], r3client=kvvDict['r3client'], r3user=kvvDict['r3user'], r3pwd=kvvDict['r3pwd']) instance_check_cmd = f'su - {sid.lower()}adm -c "sapcontrol -nr {instance["sysnr"]} -function GetProcessList"' instance_check_cmd_args = shlex.split(instance_check_cmd) sp = subprocess.run(instance_check_cmd_args, capture_output=True) output = sp.stdout.decode('utf-8') outputlines = output.splitlines() if 'Red' in output: g_instancestatus = StateSetMetricFamily( "InstanceStatus", 'Instance Status Check in SID', labels=['SID', 'Instance']) g_instancestatus.add_metric([sid, instance["profile"]], {'status': False}) yield g_instancestatus else: g_instancestatus = StateSetMetricFamily( "InstanceStatus", 'Instance Status Check in SID', labels=['SID', 'Instance']) g_instancestatus.add_metric([sid, instance["profile"]], {'status': True}) yield g_instancestatus pass pass if conn: for p in get_instance_servername_list_by_sid(sid): # master identification servername = p['servername'] profile = p['profile'] kvid_master, kvv_master = c.kv.get(sid + '_master') if kvv_master: kvvDict_master = json.loads(kvv_master['Value']) if servername == kvvDict_master['servername']: # during user count, by user type USRLIST = conn.get_user_list() g_usercount = GaugeMetricFamily( "UserCount", 'System Overall User Count', labels=['SID']) g_usercount.add_metric([sid], len(USRLIST)) yield g_usercount # during dump count DUMPLIST = conn.get_dump_list() g_dumpcount = GaugeMetricFamily( "DumpCount", 'System Overall Dump Count', labels=['SID']) g_dumpcount.add_metric([sid], len(DUMPLIST)) yield g_dumpcount # get bk job status job_status = conn.get_bkjob_status_count() g_jobstatus = GaugeMetricFamily( "BKJobCount", 'Current Background Job Count Status', labels=['SID', 'BKJobStatus']) g_jobstatus.add_metric([sid, 'Finish'], job_status['finish']) g_jobstatus.add_metric([sid, 'Running'], job_status['running']) g_jobstatus.add_metric([sid, 'Cancel'], job_status['cancel']) yield g_jobstatus else: c.kv.put(sid + '_master', json.dumps({"servername": servername})) # during user count, by user type USRLIST = conn.get_user_list() g_usercount = GaugeMetricFamily( "UserCount", 'System Overall User Count', labels=['SID']) g_usercount.add_metric([sid], len(USRLIST)) yield g_usercount # during dump count DUMPLIST = conn.get_dump_list() g_dumpcount = GaugeMetricFamily( "DumpCount", 'System Overall Dump Count', labels=['SID']) g_dumpcount.add_metric([sid], len(DUMPLIST)) yield g_dumpcount # get bk job status job_status = conn.get_bkjob_status_count() g_jobstatus = GaugeMetricFamily( "BKJobCount", 'Current Background Job Count Status', labels=['SID', 'BKJobStatus']) g_jobstatus.add_metric([sid, 'Finish'], job_status['finish']) g_jobstatus.add_metric([sid, 'Running'], job_status['running']) g_jobstatus.add_metric([sid, 'Cancel'], job_status['cancel']) yield g_jobstatus # during workprocess count, by wp type wplist = conn.get_server_wp_list(servername) running_dia_count = 0 running_upd_count = 0 running_btc_count = 0 for wp in wplist: if wp['WP_ISTATUS'] != 2: if wp['WP_TYP'] == 'DIA': running_dia_count += 1 pass if wp['WP_TYP'] == 'BTC': running_btc_count += 1 pass if wp['WP_TYP'] == 'UPD': running_upd_count += 1 pass g_wpcount = GaugeMetricFamily( "WorkprocessCount", 'WorkprocessCount of One Instance in SID group by Type', labels=['SID', 'Instance', 'WorkprocessType']) g_wpcount.add_metric([sid, profile, 'DIA'], running_dia_count) g_wpcount.add_metric([sid, profile, 'BTC'], running_btc_count) g_wpcount.add_metric([sid, profile, 'UPD'], running_upd_count) yield g_wpcount # st02 data by instance st02data = conn.get_st02_data() st02data['instance'] = profile g_st02_sapmemory = GaugeMetricFamily( "SAPMemory", 'SAP Memory Current Use % in TCode ST02', labels=[ 'SID', 'Instance', 'SAPMemoryCurrentUsePercent' ]) g_st02_sapmemory.add_metric( [sid, profile, 'PageArea'], round( float(st02data['PAGING_AREA']['CURR_USED']) / float(st02data['PAGING_AREA']['AREA_SIZE']), 4) * 100) g_st02_sapmemory.add_metric( [sid, profile, 'ExtendedMemory'], round( float( st02data['EXTENDED_MEMORY_USAGE']['USED']) / float(st02data['EXTENDED_MEMORY_USAGE'] ['TOTAL']), 4) * 100) g_st02_sapmemory.add_metric( [sid, profile, 'HeapMemory'], round( float(st02data['HEAP_MEMORY_USAGE']['USED']) / float(st02data['HEAP_MEMORY_USAGE']['TOTAL']), 4) * 100) yield g_st02_sapmemory g_st02_callstatics = GaugeMetricFamily( "CallStatistics", 'SAP Call Statistics HitRadio % in TCode ST02', labels=[ 'SID', 'Instance', 'CallStatisticsHitRadioPercent' ]) g_st02_callstatics.add_metric( [sid, profile, 'DIRECT'], st02data['TOTAL_HITRATIO']['DIRECT']) g_st02_callstatics.add_metric( [sid, profile, 'SEQUENTIAL'], st02data['TOTAL_HITRATIO']['SEQUENTIAL']) g_st02_callstatics.add_metric( [sid, profile, 'AVERAGE'], st02data['TOTAL_HITRATIO']['AVERAGE']) yield g_st02_callstatics # es = Elasticsearch(hosts=[{ # "host": "{master_ip}", # "port": 23392 # }]) # es.index( # index="ST02", # body=st02data, # ) # st03 summary data by instance st03detail_summary = conn.get_st03_data_summary() st03detail = {} st03detail['instance'] = profile st03detail['SUMMARY'] = st03detail_summary g_st03detail_steps = GaugeMetricFamily( "DialogSteps", 'Dialog Steps in TCode ST03', labels=['SID', 'Instance', 'DialogSteps']) g_st03detail_averageRespTime = GaugeMetricFamily( "AverageRespTime", 'Average Resp Time in TCode ST03', labels=['SID', 'Instance', 'AverageRespTime']) g_st03detail_averageDBTime = GaugeMetricFamily( "AverageDBTime", 'Average DB Time in TCode ST03', labels=['SID', 'Instance', 'AverageDBTime']) for sumary in st03detail_summary: g_st03detail_steps.add_metric( [sid, profile, sumary['TASKTYPE']], float(sumary['COUNT'])) g_st03detail_averageRespTime.add_metric( [sid, profile, sumary['TASKTYPE']], round( float(sumary['RESPTI']) / float(sumary['COUNT']))) # g_st03detail_averageDBTime.add_metric( # [sid, profile, sumary['TASKTYPE']], # round(sumary['RESPTI'] / sumary['COUNT'])) yield g_st03detail_steps yield g_st03detail_averageRespTime # es.index( # index="ST03", # body=st03detail, # ) conn.close() '''
def collect(self): # get SID list from os dir sidList = get_sid_list() for sid in sidList: c = consul.Consul(host=os.environ.get('CONSUL_HOST') if os.environ.get('CONSUL_HOST') else '127.0.0.1', port=23345, scheme='http') kvid, kvv = c.kv.get(sid + '_login') if kvv: # get SID login info from consul kvvDict = json.loads(kvv['Value']) conn = None for instance in get_instance_list_by_sid(sid): if not conn and instance['type'] == 'DIALOG': conn = R3rfcconn(r3ashost='127.0.0.1', r3sysnr=instance['sysnr'], r3client=kvvDict['r3client'], r3user=kvvDict['r3user'], r3pwd=kvvDict['r3pwd']) instance_check_cmd = f'su - {sid.lower()}adm -c "sapcontrol -nr {instance["sysnr"]} -function GetProcessList"' instance_check_cmd_args = shlex.split(instance_check_cmd) sp = subprocess.run(instance_check_cmd_args, capture_output=True) output = sp.stdout.decode('utf-8') outputlines = output.splitlines() if 'Red' in output: g_instancestatus = StateSetMetricFamily( "InstanceStatus", 'Instance Status Check in SID', labels=['SID', 'Instance']) g_instancestatus.add_metric([sid, instance["profile"]], {'status': False}) yield g_instancestatus else: g_instancestatus = StateSetMetricFamily( "InstanceStatus", 'Instance Status Check in SID', labels=['SID', 'Instance']) g_instancestatus.add_metric([sid, instance["profile"]], {'status': True}) yield g_instancestatus pass pass if conn: for p in get_instance_servername_list_by_sid(sid): # master identification servername = p['servername'] profile = p['profile'] kvid_master, kvv_master = c.kv.get(sid + '_master') if kvv_master: kvvDict_master = json.loads(kvv_master['Value']) if servername == kvvDict_master['servername']: # during user count, by user type USRLIST = conn.get_user_list() g_usercount = GaugeMetricFamily( "UserCount", 'System Overall User Count', labels=['SID']) g_usercount.add_metric([sid], len(USRLIST)) yield g_usercount # during dump count DUMPLIST = conn.get_dump_list() g_dumpcount = GaugeMetricFamily( "DumpCount", 'System Overall Dump Count', labels=['SID']) g_dumpcount.add_metric([sid], len(DUMPLIST)) yield g_dumpcount # get bk job status job_status = conn.get_bkjob_status_count() g_jobstatus = GaugeMetricFamily( "BKJobCount", 'Current Background Job Count Status', labels=['SID', 'BKJobStatus']) g_jobstatus.add_metric([sid, 'Finish'], job_status['finish']) g_jobstatus.add_metric([sid, 'Running'], job_status['running']) g_jobstatus.add_metric([sid, 'Cancel'], job_status['cancel']) yield g_jobstatus else: c.kv.put(sid + '_master', json.dumps({"servername": servername})) # during user count, by user type USRLIST = conn.get_user_list() g_usercount = GaugeMetricFamily( "UserCount", 'System Overall User Count', labels=['SID']) g_usercount.add_metric([sid], len(USRLIST)) yield g_usercount # during dump count DUMPLIST = conn.get_dump_list() g_dumpcount = GaugeMetricFamily( "DumpCount", 'System Overall Dump Count', labels=['SID']) g_dumpcount.add_metric([sid], len(DUMPLIST)) yield g_dumpcount # get bk job status job_status = conn.get_bkjob_status_count() g_jobstatus = GaugeMetricFamily( "BKJobCount", 'Current Background Job Count Status', labels=['SID', 'BKJobStatus']) g_jobstatus.add_metric([sid, 'Finish'], job_status['finish']) g_jobstatus.add_metric([sid, 'Running'], job_status['running']) g_jobstatus.add_metric([sid, 'Cancel'], job_status['cancel']) yield g_jobstatus # during workprocess count, by wp type wplist = conn.get_server_wp_list(servername) running_dia_count = 0 running_upd_count = 0 running_btc_count = 0 for wp in wplist: if wp['WP_ISTATUS'] != 2: if wp['WP_TYP'] == 'DIA': running_dia_count += 1 pass if wp['WP_TYP'] == 'BTC': running_btc_count += 1 pass if wp['WP_TYP'] == 'UPD': running_upd_count += 1 pass g_wpcount = GaugeMetricFamily( "WorkprocessCount", 'WorkprocessCount of One Instance in SID group by Type', labels=['SID', 'Instance', 'WorkprocessType']) g_wpcount.add_metric([sid, profile, 'DIA'], running_dia_count) g_wpcount.add_metric([sid, profile, 'BTC'], running_btc_count) g_wpcount.add_metric([sid, profile, 'UPD'], running_upd_count) yield g_wpcount conn.close() '''