def test_info(self): i = Info('ii', 'A info', ['a', 'b'], registry=self.registry) i.labels('c', 'd').info({'foo': 'bar'}) self.assertEqual( json.loads( """{"ii": {"samples": [{"sample_name": "ii_info", "labels": {"a": "c", "b": "d", "foo": "bar"}, "value": "1.0", "timestamp": null, "exemplar": {}}], "help": "A info", "type": "info"}}""" ), json.loads(self.json_exporter.generate_latest_json()))
def test_info(self): i = Info('ii', 'A info', ['a', 'b'], registry=self.registry) i.labels('c', 'd').info({'foo': 'bar'}) self.assertEqual(b"""# HELP ii A info # TYPE ii info ii_info{a="c",b="d",foo="bar"} 1.0 # EOF """, generate_latest(self.registry))
def put_nvme_info(nvme_list_json): nvme_info = Info('nvme_info', 'Description of NVMe Info', ['device']) for nvme in nvme_list_json['Devices']: nvme_info.labels(nvme['DevicePath']).info( {'ProduceName': nvme['ProductName']}) nvme_info.labels(nvme['DevicePath']).info( {'ModelNumber': nvme['ModelNumber']})
from common.config import LIGHT_DARK_MODES, VERSION from common.logs import MAIN as logger from models.lifecycle import LIFECYCLE from prometheus_client import Info, Gauge, Enum phases = [] for phase in LIFECYCLE: phases.append(phase['name']) try: BUILD = Info('growbox_build', 'Growbox Automation Platform', ['version']) BUILD.labels(version=VERSION) PHASE_NAME = Enum('growbox_phase_name', 'Growbox Current Phase name', states=phases) PHASE_DURATION = Gauge('growbox_phase_duration_seconds', 'Growbox Current Phase duration') PHASE_FROM_START = Gauge('growbox_phase_from_start_seconds', 'Growbox Current Phase seconds from start') PHASE_BEFORE_FINISH = Gauge('growbox_phase_before_finish_seconds', 'Growbox Current Phase seconds before finish') LIGHT_DARK_MODE = Enum('growbox_light_dark_mode', 'Current Growbox Light/Dark mode', states=LIGHT_DARK_MODES) LIGHT_DARK_MODE_DURATION = Gauge( 'growbox_light_dark_mode_duration_seconds', 'Growbox Current Light/Dark mode duration')
def test_info(self): i = Info('ii', 'A info', ['a', 'b'], registry=self.registry) i.labels('c', 'd').info({'foo': 'bar'}) self.assertEqual( b'# HELP ii_info A info\n# TYPE ii_info gauge\nii_info{a="c",b="d",foo="bar"} 1.0\n', generate_latest(self.registry))
def push_job_information(self): ''' Process Bareos job data and send it to the prometheus pushgateway ''' registry = CollectorRegistry() TIME_BUCKETS=(6, 60, 600, 1800, 3600, 10800, 18000, 28800, 86400) bareos_job_status = Enum('bareos_job_status', 'Backup Status', states=self.job_status.values(), labelnames=['instance', 'jobid'], registry=registry) # see https://github.com/bareos/bareos/blob/master/core/src/include/job_level.h bareos_job_level = Enum('bareos_job_level', 'Backup Level', states=self.job_levels.values(), labelnames=['instance', 'jobid'], registry=registry) bareos_job_running_time = Histogram('bareos_job_running_time', 'Job running time', labelnames=['instance', 'jobid'], registry=registry, buckets=TIME_BUCKETS) bareos_job_files = Gauge('bareos_job_files', 'Backed up files', labelnames=['instance', 'jobid'], registry=registry) bareos_job_bytes = Gauge('bareos_job_bytes', 'Backed up bytes', labelnames=['instance', 'jobid'], registry=registry) bareos_job_throughput = Gauge('bareos_job_throughtput', 'Backup throughtput', registry=registry, labelnames=['instance', 'jobid']) # see https://github.com/bareos/bareos/blob/master/core/src/include/job_types.h bareos_job_type = Enum('bareos_job_type', 'Job Type', states=self.job_types.values(), registry=registry, labelnames=['instance', 'jobid']) bareos_job_client = Info('bareos_job_client', 'Client', registry=registry, labelnames=['instance', 'jobid']) bareos_job_priority = Gauge('bareos_job_priority', 'Job Priority', registry=registry, labelnames=['instance', 'jobid']) bareos_job_name = '_'.join(self.jobName.split('.')[:-3]) bareos_job_id = self.jobId if (self.jobStatus == 'E' or self.jobStatus == 'f' or self.jobStatus == 'A') and self.report_failed == False: return bareos_job_status.labels(instance=bareos_job_name, jobid=bareos_job_id).state(self.job_status[self.jobStatus]) bareos_job_running_time.labels(instance=bareos_job_name, jobid=bareos_job_id).observe(self.jobRunningTime) bareos_job_files.labels(instance=bareos_job_name, jobid=bareos_job_id).set(self.jobFiles) bareos_job_bytes.labels(instance=bareos_job_name, jobid=bareos_job_id).set(self.jobBytes) bareos_job_throughput.labels(instance=bareos_job_name, jobid=bareos_job_id).set(self.throughput) bareos_job_priority.labels(instance=bareos_job_name, jobid=bareos_job_id).set(self.Priority) bareos_job_level.labels(instance=bareos_job_name, jobid=bareos_job_id).state(self.job_levels[self.jobLevel]) bareos_job_type.labels(instance=bareos_job_name, jobid=bareos_job_id).state(self.job_types[chr(self.jobType)]) bareos_job_client.labels(instance=bareos_job_name, jobid=bareos_job_id).info({'client': self.jobClient}) if self.use_tls == True or self.use_tls == 'yes': gateway = "https://{}:{}".format(self.gateway_host,self.gateway_port) else: gateway = "{}:{}".format(self.gateway_host,self.gateway_port) bareosdir.DebugMessage(100, "Submitting metrics to {}\n".format(gateway)) try: if self.use_basic_auth: push_to_gateway('{}'.format(gateway), job='bareos', registry=registry, handler=self.authentication_handler) else: push_to_gateway('{}'.format(gateway), job='bareos', registry=registry) except Exception as excp: bareosdir.DebugMessage(100, "Error: Submitting metrics to pushgateway '{}' failed.\n".format(gateway)) bareosdir.DebugMessage(100, "python error was: {}\n".format(excp)) bareosdir.JobMessage(bareosdir.M_INFO, "Failed to submit metrics to pushgateway\n")
def metrics(): REGISTRY = CollectorRegistry() SYSTEM_INFO = Info('awx_system', 'AWX System Information', registry=REGISTRY) ORG_COUNT = Gauge('awx_organizations_total', 'Number of organizations', registry=REGISTRY) USER_COUNT = Gauge('awx_users_total', 'Number of users', registry=REGISTRY) TEAM_COUNT = Gauge('awx_teams_total', 'Number of teams', registry=REGISTRY) INV_COUNT = Gauge('awx_inventories_total', 'Number of inventories', registry=REGISTRY) PROJ_COUNT = Gauge('awx_projects_total', 'Number of projects', registry=REGISTRY) JT_COUNT = Gauge('awx_job_templates_total', 'Number of job templates', registry=REGISTRY) WFJT_COUNT = Gauge('awx_workflow_job_templates_total', 'Number of workflow job templates', registry=REGISTRY) HOST_COUNT = Gauge( 'awx_hosts_total', 'Number of hosts', [ 'type', ], registry=REGISTRY, ) SCHEDULE_COUNT = Gauge('awx_schedules_total', 'Number of schedules', registry=REGISTRY) INV_SCRIPT_COUNT = Gauge('awx_inventory_scripts_total', 'Number of invetory scripts', registry=REGISTRY) USER_SESSIONS = Gauge( 'awx_sessions_total', 'Number of sessions', [ 'type', ], registry=REGISTRY, ) CUSTOM_VENVS = Gauge('awx_custom_virtualenvs_total', 'Number of virtualenvs', registry=REGISTRY) RUNNING_JOBS = Gauge('awx_running_jobs_total', 'Number of running jobs on the Tower system', registry=REGISTRY) PENDING_JOBS = Gauge('awx_pending_jobs_total', 'Number of pending jobs on the Tower system', registry=REGISTRY) STATUS = Gauge( 'awx_status_total', 'Status of Job launched', [ 'status', ], registry=REGISTRY, ) INSTANCE_CAPACITY = Gauge( 'awx_instance_capacity', 'Capacity of each node in a Tower system', [ 'hostname', 'instance_uuid', ], registry=REGISTRY, ) INSTANCE_CPU = Gauge( 'awx_instance_cpu', 'CPU cores on each node in a Tower system', [ 'hostname', 'instance_uuid', ], registry=REGISTRY, ) INSTANCE_MEMORY = Gauge( 'awx_instance_memory', 'RAM (Kb) on each node in a Tower system', [ 'hostname', 'instance_uuid', ], registry=REGISTRY, ) INSTANCE_INFO = Info( 'awx_instance', 'Info about each node in a Tower system', [ 'hostname', 'instance_uuid', ], registry=REGISTRY, ) INSTANCE_LAUNCH_TYPE = Gauge( 'awx_instance_launch_type_total', 'Type of Job launched', [ 'node', 'launch_type', ], registry=REGISTRY, ) INSTANCE_STATUS = Gauge( 'awx_instance_status_total', 'Status of Job launched', [ 'node', 'status', ], registry=REGISTRY, ) INSTANCE_CONSUMED_CAPACITY = Gauge( 'awx_instance_consumed_capacity', 'Consumed capacity of each node in a Tower system', [ 'hostname', 'instance_uuid', ], registry=REGISTRY, ) INSTANCE_REMAINING_CAPACITY = Gauge( 'awx_instance_remaining_capacity', 'Remaining capacity of each node in a Tower system', [ 'hostname', 'instance_uuid', ], registry=REGISTRY, ) LICENSE_INSTANCE_TOTAL = Gauge('awx_license_instance_total', 'Total number of managed hosts provided by your license', registry=REGISTRY) LICENSE_INSTANCE_FREE = Gauge('awx_license_instance_free', 'Number of remaining managed hosts provided by your license', registry=REGISTRY) license_info = get_license() SYSTEM_INFO.info( { 'install_uuid': settings.INSTALL_UUID, 'insights_analytics': str(settings.INSIGHTS_TRACKING_STATE), 'tower_url_base': settings.TOWER_URL_BASE, 'tower_version': get_awx_version(), 'license_type': license_info.get('license_type', 'UNLICENSED'), 'license_expiry': str(license_info.get('time_remaining', 0)), 'pendo_tracking': settings.PENDO_TRACKING_STATE, 'external_logger_enabled': str(settings.LOG_AGGREGATOR_ENABLED), 'external_logger_type': getattr(settings, 'LOG_AGGREGATOR_TYPE', 'None'), } ) LICENSE_INSTANCE_TOTAL.set(str(license_info.get('instance_count', 0))) LICENSE_INSTANCE_FREE.set(str(license_info.get('free_instances', 0))) current_counts = counts(None) ORG_COUNT.set(current_counts['organization']) USER_COUNT.set(current_counts['user']) TEAM_COUNT.set(current_counts['team']) INV_COUNT.set(current_counts['inventory']) PROJ_COUNT.set(current_counts['project']) JT_COUNT.set(current_counts['job_template']) WFJT_COUNT.set(current_counts['workflow_job_template']) HOST_COUNT.labels(type='all').set(current_counts['host']) HOST_COUNT.labels(type='active').set(current_counts['active_host_count']) SCHEDULE_COUNT.set(current_counts['schedule']) INV_SCRIPT_COUNT.set(current_counts['custom_inventory_script']) CUSTOM_VENVS.set(current_counts['custom_virtualenvs']) USER_SESSIONS.labels(type='all').set(current_counts['active_sessions']) USER_SESSIONS.labels(type='user').set(current_counts['active_user_sessions']) USER_SESSIONS.labels(type='anonymous').set(current_counts['active_anonymous_sessions']) all_job_data = job_counts(None) statuses = all_job_data.get('status', {}) for status, value in statuses.items(): STATUS.labels(status=status).set(value) RUNNING_JOBS.set(current_counts['running_jobs']) PENDING_JOBS.set(current_counts['pending_jobs']) instance_data = instance_info(None, include_hostnames=True) for uuid, info in instance_data.items(): hostname = info['hostname'] INSTANCE_CAPACITY.labels(hostname=hostname, instance_uuid=uuid).set(instance_data[uuid]['capacity']) INSTANCE_CPU.labels(hostname=hostname, instance_uuid=uuid).set(instance_data[uuid]['cpu']) INSTANCE_MEMORY.labels(hostname=hostname, instance_uuid=uuid).set(instance_data[uuid]['memory']) INSTANCE_CONSUMED_CAPACITY.labels(hostname=hostname, instance_uuid=uuid).set(instance_data[uuid]['consumed_capacity']) INSTANCE_REMAINING_CAPACITY.labels(hostname=hostname, instance_uuid=uuid).set(instance_data[uuid]['remaining_capacity']) INSTANCE_INFO.labels(hostname=hostname, instance_uuid=uuid).info( { 'enabled': str(instance_data[uuid]['enabled']), 'last_isolated_check': getattr(instance_data[uuid], 'last_isolated_check', 'None'), 'managed_by_policy': str(instance_data[uuid]['managed_by_policy']), 'version': instance_data[uuid]['version'], } ) instance_data = job_instance_counts(None) for node in instance_data: # skipping internal execution node (for system jobs) if node == '': continue types = instance_data[node].get('launch_type', {}) for launch_type, value in types.items(): INSTANCE_LAUNCH_TYPE.labels(node=node, launch_type=launch_type).set(value) statuses = instance_data[node].get('status', {}) for status, value in statuses.items(): INSTANCE_STATUS.labels(node=node, status=status).set(value) return generate_latest(registry=REGISTRY)
class InterfaceCollector(BaseCollector): METRICS_SUBSYSTEM = 'interface' ENDPOINT = 'interfaces' def __init__(self, client: Client): super().__init__(client) self._info = Info( namespace=self.METRICS_NAMESPACE, name=self.METRICS_SUBSYSTEM, labelnames=['interface'], documentation='Information about the physical interfaces', ) self._up = Gauge( namespace=self.METRICS_NAMESPACE, subsystem=self.METRICS_SUBSYSTEM, labelnames=['interface'], name='up', documentation='The status of the interface', ) self._rx_speed = Gauge( namespace=self.METRICS_NAMESPACE, subsystem=self.METRICS_SUBSYSTEM, labelnames=['interface'], name='receive_speed', unit='kbps', documentation= 'Receiving speed of the interface (if applicable else -1)', ) self._tx_speed = Gauge( namespace=self.METRICS_NAMESPACE, subsystem=self.METRICS_SUBSYSTEM, labelnames=['interface'], name='transmit_speed', unit='kbps', documentation= 'Transmit speed of the interface (if applicable else -1)') self._mtu = Gauge( namespace=self.METRICS_NAMESPACE, subsystem=self.METRICS_SUBSYSTEM, labelnames=['interface'], name='mtu', unit='bytes', documentation='The mtu of the interface', ) self._tx_packets = Gauge( namespace=self.METRICS_NAMESPACE, subsystem=self.METRICS_SUBSYSTEM, labelnames=['interface'], name='transmit_packets', unit='total', documentation='Transmitted packets on this interface') self._rx_packets = Gauge( namespace=self.METRICS_NAMESPACE, subsystem=self.METRICS_SUBSYSTEM, labelnames=['interface'], name='receive_packets', unit='total', documentation='Received packets on this interface') self._tx_errors = Gauge(namespace=self.METRICS_NAMESPACE, subsystem=self.METRICS_SUBSYSTEM, labelnames=['interface'], name='transmit_errs', unit='total', documentation='Error count on transmitting') self._rx_errors = Gauge(namespace=self.METRICS_NAMESPACE, subsystem=self.METRICS_SUBSYSTEM, labelnames=['interface'], name='receive_errs', unit='total', documentation='Error count on receiving') self._collisions = Gauge(namespace=self.METRICS_NAMESPACE, subsystem=self.METRICS_SUBSYSTEM, labelnames=['interface'], name='collisions', documentation='Collision count on interface') def _process_data(self, data): interfaces = data['line_status'] for interface in interfaces: name = interface['interface'] del interface['interface'] self._up.labels(name).set(interface['status'] == 'Up') del interface['status'] if interface['media'] == 'WLAN': re_res = re.search(r'([0-9]+)Mbps', interface['speed']) if re_res: speed = int(re_res.group(1)) * 1000 self._rx_speed.labels(name).set(speed) self._tx_speed.labels(name).set(speed) del interface['speed'] elif interface['media'] == 'DSL': re_res = re.search( r'DownStream:([0-9]+)kbps UpStream:([0-9]+)kbps', interface['speed']) if re_res: self._rx_speed.labels(name).set(re_res.group(1)) self._tx_speed.labels(name).set(re_res.group(2)) del interface['speed'] else: self._rx_speed.labels(name).set(-1) self._tx_speed.labels(name).set(-1) self._mtu.labels(name).set(interface['MTU']) del interface['MTU'] self._tx_packets.labels(name).set(interface['tx_packets']) del interface['tx_packets'] self._rx_packets.labels(name).set(interface['rx_packets']) del interface['rx_packets'] self._tx_errors.labels(name).set(interface['tx_errors']) del interface['tx_errors'] self._rx_errors.labels(name).set(interface['rx_errors']) del interface['rx_errors'] self._collisions.labels(name).set(interface['collisions']) del interface['collisions'] self._info.labels(name).info(interface)
'offset': offset })['datastores'] if 'f' in monitor: scluster.labels('Federation', 'Cluster_count').set(len(clusters)) snode.labels('Federation', 'Node_count').set(len(hosts)) svm.labels('Federation', 'VM_count').set(len(vms)) sdatastore.labels('Federation', 'Datastore_count').set(len(datastores)) for x in hosts: cn = (x['name'].split('.')[0]).replace('-', '_') snode.labels(cn, 'State').set(node_state[x['state']]) id_node[x['id']] = x['name'] inode.labels(cn).info({ 'state': x['state'], 'cluster': x['compute_cluster_name'] }) for x in vms: cn = (x['name'].split('.')[0]).replace('-', '_') svm.labels(cn, 'allocated_capacity').set( x['hypervisor_allocated_capacity'] / BtoGB) svm.labels(cn, 'free_space').set( x['hypervisor_free_space'] / BtoGB) svm.labels(cn, 'allocated_cpu').set( x['hypervisor_allocated_cpu']) svm.labels(cn, 'cpu_count').set(x['hypervisor_cpu_count']) svm.labels(cn, 'consumed_cpu').set( x['hypervisor_consumed_cpu']) svm.labels(cn, 'virtual_disk_count').set( x['hypervisor_virtual_disk_count']) svm.labels(cn, 'total_memory').set(
def main() -> None: """Get args, initialise prom client and pipeserial objects, and start the loop. Args: None Returns: None """ # get argparse object and parse args parser = get_parser() args = parser.parse_args() # define the log format used for stdout depending on the requested loglevel if args.loglevel == "DEBUG": console_logformat = "%(asctime)s %(levelname)s mobile_modem_exporter.%(funcName)s():%(lineno)i: %(message)s" else: console_logformat = ( "%(asctime)s %(levelname)s mobile_modem_exporter %(message)s") # configure the log format used for console logging.basicConfig( level=getattr(logging, str(args.loglevel)), format=console_logformat, datefmt="%Y-%m-%d %H:%M:%S %z", ) logger.debug("Initialising serial ports and Prometheus objects...") registry = CollectorRegistry() # mobile_modem_up up = Gauge( "mobile_modem_up", "This metric is always 1 if the mobile_modem scrape worked, 0 if there was a problem getting info from one or more modems.", registry=registry, ) # mobile_modem_build_info build_info = Info( "mobile_modem_build", "Information about the mobile_modem_exporter itself.", registry=registry, ) build_info.info({ "version": __version__, "pipeserial_version": PipeSerial.__version__ }) # mobile_modem_info modem_info = Info( "mobile_modem", "Information about the mobile modem being monitored, including device path, manufacturer, model, revision and serial number.", ["device"], registry=registry, ) # mobile_modem_atcsq_rssi modem_rssi = Gauge( "mobile_modem_atcsq_rssi", "RSSI for the mobile modem as returned by AT+CSQ", ["device"], registry=registry, ) # mobile_modem_ber modem_ber = Gauge( "mobile_modem_atcsq_ber", "BER for the mobile modem as returned by AT+CSQ", ["device"], registry=registry, ) # initialise pipeserial objects devices = [] logger.info("Initialising serial ports...") for device in args.SERIALDEVICE: logger.debug(f"Opening serial port {device} and getting modem info...") pipe = PipeSerial(serialport=device) pipe.open() devices.append(pipe) # get serial device info output = pipe.run("ATI", ["OK"]) manufacturer, model, revision = parse_ati(output) # get serial device serial number output = pipe.run("AT+GSN", ["OK"]) serial = parse_atgsn(output) # set mobile_modem_info for this device modem_info.labels(device=device).info({ "manufacturer": manufacturer, "model": model, "revision": revision, "serial": serial, }) # init done, start loop logger.info( f"Entering main loop, writing metrics for modems {args.SERIALDEVICE} to {args.PROMPATH}, sleeping {args.sleep} seconds between runs..." ) while True: # start out optimistic! up.set(1) for device in devices: logger.debug(f"Getting CSQ from device: {device.ser.name}") output = device.run("AT+CSQ", ["OK"]) try: rssi, ber = parse_atcsq(output) except Exception: logger.exception( "Got an exception while parsing AT+CSQ output") # set up to 0 for this scrape up.set(0) continue logger.debug(f"parsed AT+CSQ output to rssi {rssi} and BER {ber}") modem_rssi.labels(device=device.ser.name).set(rssi) modem_ber.labels(device=device.ser.name).set(ber) # output metrics to textfile exporter path write_to_textfile(args.PROMPATH, registry) logger.debug(f"Sleeping {args.sleep} seconds before next run...") time.sleep(args.sleep)
p = select.poll() p.register(j, j.get_events()) gauge_fps_present = Gauge('fps_present', 'FPS rendered by game', ['vm']) gauge_fps_customer = Gauge('fps_customer', 'FPS got by customer', ['vm']) gauge_latency = Gauge('latency', 'Customer latency', ['vm']) zbx_metrics = [] fps_present, fps_customer, latency = 0, 0, 0 info_session = Info('session', 'GameServer Session Data', ['vm']) start_http_server(8000) zbx = ZabbixSender(zabbix_server) for vm in vms: session[vm] = {'id': '', 'game': '', 'active': True} info_session.labels(vm=vm).info({ 'id': session[vm]['id'], 'game': session[vm]['id'] }) gauge_fps_customer.labels(vm=vm).set(0) gauge_fps_present.labels(vm=vm).set(0) gauge_latency.labels(vm=vm).set(0) while p.poll(): if j.process() != journal.APPEND: continue for entry in j: log_message = entry['MESSAGE'] if log_message != "": zbx_metrics = [] vm = entry['SYSLOG_IDENTIFIER'].split('/')[-1] if ' CreateSession: session_id' in log_message: session[vm]['id'] = log_message.split(' = ')[-1].split(' ')[-1]
from config import METRICS_ENABLED, METRICS_PUSHGATEWAY_URL, \ METRICS_PUSHGATEWAY_USER, METRICS_PUSHGATEWAY_PASSWORD, \ EPAS_REST_USERNAME, BADGE_READER_IP, FTP_SERVER_NAME #################### CONFIGURAZIONI METRICHE PROMETHEUS ######################## CLIENT_REGISTRY = CollectorRegistry() METRICS_LABEL_NAMES = ['instance', 'badgeReader', 'ftp_server'] _CLIENT_INFO = Info('epas_client', 'Tipologia di client e protocollo utilizzato', METRICS_LABEL_NAMES, registry=CLIENT_REGISTRY) CLIENT_INFO = _CLIENT_INFO.labels(EPAS_REST_USERNAME, BADGE_READER_IP, FTP_SERVER_NAME) # Create a metric to track time spent to execute job. _JOB_TIME = Summary('epas_client_job_processing_seconds', 'Time spent executing job', METRICS_LABEL_NAMES, registry=CLIENT_REGISTRY) JOB_TIME = _JOB_TIME.labels(EPAS_REST_USERNAME, BADGE_READER_IP, FTP_SERVER_NAME) _SEND_TIME = Histogram('epas_client_send_stamping_seconds', 'Tempi di invio delle timbrature', METRICS_LABEL_NAMES, registry=CLIENT_REGISTRY) SEND_TIME = _SEND_TIME.labels(EPAS_REST_USERNAME, BADGE_READER_IP, FTP_SERVER_NAME)
class OHM_Exporter(): def __init__(self): self.host = socket.gethostname() self.wmi_connection = wmi.WMI(namespace="root\\OpenHardwareMonitor") # Wait for Open Hardware Monitor while True: if len(self.wmi_connection.Hardware()) > 0: break time.sleep(1) # Wait for Open Hardware Monitor to collect resources time.sleep(5) # Start Exporter start_http_server(9398) self.setupEndpoints() def setupEndpoints(self): self.cpu_identifier = {} self.gpu_identifier = {} self.disk_identifier = {} for x in self.wmi_connection.Hardware(): if x.HardwareType == "RAM": self.ram_identifier = x.identifier ram_name = x.name elif x.HardwareType == "Mainboard": print(x.parent) mainboard_name = x.name self.mainboard_identifier = x.identifier elif x.HardwareType == "CPU": self.cpu_identifier[x.identifier] = x.name elif x.HardwareType == "GpuNvidia" or x.HardwareType == "GpuAti": self.gpu_identifier[x.identifier] = x.name elif x.HardwareType == "HDD": self.disk_identifier[x.identifier] = x.name total_ram = 0 vram = {} for x in self.wmi_connection.Sensor(): if x.parent == self.ram_identifier: if x.name == "Available Memory" or x.name == "Used Memory": total_ram += x.value * 1024 if x.name == "GPU Memory Total": vram[x.parent] = x.value total_ram = int(total_ram) # Cpu self.cpu_info = Info("ohm_cpu", "CPU Info", ["identifier"]) for x in self.cpu_identifier: self.cpu_info.labels(x).info({ "host": self.host, "name": self.cpu_identifier[x] }) self.cpu_temperature_gauge = Gauge( "ohm_cpu_temperature", "Cpu core temperature", ["host", "identifier", "parent", "name"]) self.cpu_load_gauge = Gauge("ohm_cpu_load", "Cpu core load", ["host", "identifier", "parent", "name"]) self.cpu_power_gauge = Gauge("ohm_cpu_power", "Cpu power usage in Watt", ["host", "identifier", "parent", "name"]) # Mainboard self.mainboard_info = Info("ohm_mainboard", "Mainboard Info") self.mainboard_info.info({ "host": self.host, "identifier": self.mainboard_identifier, "name": mainboard_name }) # Memmory self.ram_info = Info("ohm_ram", "RAM Info") self.ram_info.info({ "host": self.host, "identifier": self.ram_identifier, "name": ram_name, "total_memory": str(total_ram) }) self.ram_available_gauge = Gauge("ohm_ram_available", "RAM available", ["host"]) self.ram_used_gauge = Gauge("ohm_ram_used", "RAM used", ["host"]) # GPU self.gpu_info = Info("ohm_gpu", "GPU Info", ["identifier"]) for x in self.gpu_identifier: self.gpu_info.labels(x).info({ "host": self.host, "name": self.gpu_identifier[x], "total_memory": str(vram[x]) }) self.gpu_temperature_gauge = Gauge( "ohm_gpu_temperature", "Gpu core temperature", ["host", "identifier", "parent", "name"]) self.gpu_load_gauge = Gauge("ohm_gpu_load", "Gpu load", ["host", "identifier", "parent", "name"]) self.gpu_fans_gauge = Gauge("ohm_gpu_fans", "Gpu fan speed", ["host", "identifier", "parent", "name"]) self.gpu_vram_available_gauge = Gauge( "ohm_gpu_vram_available", "Gpu vram", ["host", "identifier", "parent", "name"]) self.gpu_vram_used_gauge = Gauge( "ohm_gpu_vram_used", "Gpu vram", ["host", "identifier", "parent", "name"]) # Storage self.disk_usage_gauge = Gauge("ohm_disk_usage", "Disk Usage in %", ["host", "identifier", "name"]) self.disk_temperature_gauge = Gauge("ohm_disk_temperature", "Disk Temperature in °C", ["host", "identifier", "name"]) def update_metrics(self): for x in self.wmi_connection.Sensor(): if x.parent in self.cpu_identifier: if x.SensorType == "Temperature": self.cpu_temperature_gauge.labels(self.host, x.identifier, x.parent, x.name).set(x.value) elif x.SensorType == "Load": self.cpu_load_gauge.labels(self.host, x.identifier, x.parent, x.name).set(round(x.value, 1)) elif x.SensorType == "Power": self.cpu_power_gauge.labels(self.host, x.identifier, x.parent, x.name).set(round(x.value, 1)) elif x.parent == self.ram_identifier: if x.name == "Available Memory": self.ram_available_gauge.labels(self.host).set( int(x.value * 1024)) elif x.name == "Used Memory": self.ram_used_gauge.labels(self.host).set( int(x.value * 1024)) elif x.parent in self.gpu_identifier: if x.SensorType == "Temperature": self.gpu_temperature_gauge.labels(self.host, x.identifier, x.parent, x.name).set(x.value) elif x.SensorType == "Load": self.gpu_load_gauge.labels(self.host, x.identifier, x.parent, x.name).set(x.value) elif x.SensorType == "Fan": self.gpu_fans_gauge.labels(self.host, x.identifier, x.parent, x.name).set(x.value) elif x.name == "GPU Memory Free": self.gpu_vram_available_gauge.labels( self.host, x.identifier, x.parent, x.name).set(round(x.value, 1)) elif x.name == "GPU Memory Used": self.gpu_vram_used_gauge.labels(self.host, x.identifier, x.parent, x.name).set( round(x.value, 1)) elif x.parent in self.disk_identifier: if x.SensorType == "Temperature": self.disk_temperature_gauge.labels( self.host, x.parent, self.disk_identifier[x.parent]).set(x.value) elif x.SensorType == "Load": self.disk_usage_gauge.labels( self.host, x.parent, self.disk_identifier[x.parent]).set(round(x.value, 2))
therm_setpoint_temperature = Gauge( "therm_setpoint_temperature", "Thermostat setpoint temperature", ['id', 'name'], registry=registry) therm_setpoint_temperature.labels( id=term_modules['id'], name=term_modules['name']).set( term_modules['therm_setpoint_temperature']) # therm_setpoint_temperature.set_to_current_time() infos = Info("room_info", "Room informations", ['id'], registry=registry) infos.labels(id=term_modules['id']).info({ 'anticipating': str(term_modules['anticipating']), 'name': term_modules['name'], 'therm_setpoint_mode': term_modules['therm_setpoint_mode'] }) push_to_gateway( os.environ.get("PROM_PUSH_GW", "localhost:9091"), os.environ.get("PROM_PUSH_BATCH", "Netatmo"), registry=registry, ) sleep(os.environ.get("PROM_PUSH_INTERVAL", 60)) except KeyboardInterrupt: print("\n\nKeyboard interuption received. Exiting.") exit()