def __init__(self, push_gateaway, nodename, jobname="Duty"): ''' Arguments --------- push_gateaway: str Push gateway nodename : str Name of node jobname : str default of "Duty" Not important ''' self.push_gateaway = push_gateaway self.registry = pc.CollectorRegistry() self.nodename = nodename self.jobname = jobname ### Defining metrics # Are there heimdall|fredda candidates? self.heimdall_up = pc.Enum('cands_heimdall_up', 'Heimdall candidates present', states=['yes', 'no'], labelnames=['node', 'antenna']) self.fredda_up = pc.Enum('cands_fredda_up', 'Fredda candidates present', states=['yes', 'no'], labelnames=['node', 'antenna']) # How many candidates self.heimdall_n = pc.Gauge('cands_heimdall_num', 'Heimdall candidates number', labelnames=['node', 'antenna']) self.fredda_n = pc.Gauge('cands_fredda_num', 'Fredda candidates number', labelnames=['node', 'antenna'])
def main(): # TODO: Grafana doesn't handle Enum; change to an int lights = prom.Enum("lights_state", "State of plant room lights (commanded)", states=["on", "off"]) prom.start_http_server(9192) while True: lights_control_pin_state = read_pin(11) lights.state("on") if lights_control_pin_state == 1 else lights.state( "off") time.sleep(1)
def _create_metrics(self): """Creates a registry and records metrics""" self.registry = prometheus_client.CollectorRegistry() self.quota_free_count = prometheus_client.Gauge( 'kuryr_quota_free_count', 'Amount of quota available' ' for the network resource', labelnames={'resource'}, registry=self.registry) self.port_quota_per_subnet = prometheus_client.Gauge( 'kuryr_port_quota_per_subnet', 'Amount of ports available' ' on Subnet', labelnames={'subnet_id', 'subnet_name'}, registry=self.registry) self.lbs_members_count = prometheus_client.Gauge( 'kuryr_critical_lb_members_count', 'Amount of members per ' 'critical Load Balancer pool', labelnames={'lb_name', 'lb_pool_name'}, registry=self.registry) self.lbs_state = prometheus_client.Enum('kuryr_critical_lb_state', 'Critical Load Balancer State', labelnames={'lb_name'}, states=[ 'ERROR', 'ACTIVE', 'DELETED', 'PENDING_CREATE', 'PENDING_UPDATE', 'PENDING_DELETE' ], registry=self.registry) buckets = (10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, _INF) self.pod_creation_latency = prometheus_client.Histogram( 'kuryr_pod_creation_latency', 'Time taken for a pod to have' ' Kuryr annotations set', buckets=buckets, registry=self.registry) self.load_balancer_readiness = prometheus_client.Counter( 'kuryr_load_balancer_readiness', 'This counter is increased when ' 'Kuryr notices that an Octavia load balancer is stuck in an ' 'unexpected state', registry=self.registry) self.port_readiness = prometheus_client.Counter( 'kuryr_port_readiness', 'This counter is increased when Kuryr ' 'times out waiting for Neutron to move port to ACTIVE', registry=self.registry)
def _config(self, fields): metrics = {} labels = ['serial_number', 'product_id'] for f in defs.FIELDS: label = f.label.replace('#', '') name = 'victron_%s' % label.lower() kind = f.kind() if isinstance(kind, pint.Quantity): unit = str(kind.units) else: unit = _UNITS.get(f.unit, f.unit) if unit == 'hour * watt': unit = 'wh' if kind == str: metrics[f.label] = prometheus_client.Info(name, f.description, labelnames=labels) elif _is_enum(kind): states = [x.name.lower() for x in kind] metrics[f.label] = prometheus_client.Enum( name, f.description, labelnames=['serial_number', 'product_id'], states=states) metrics[f.label + '_value'] = prometheus_client.Gauge( name + '_value', f.description, labelnames=['serial_number', 'product_id']) else: metrics[f.label] = prometheus_client.Gauge( name, f.description, labelnames=['serial_number', 'product_id'], unit=unit) updated = prometheus_client.Gauge( 'victron_updated', 'Last time a block was received from the device', labelnames=labels) blocks = prometheus_client.Counter( 'victron_blocks', 'Number of blocks received from the device', labelnames=labels) return metrics, updated, blocks
def __init__(self): labels = ['name', 'serial'] # Environmental Sensors self.humidity = prometheus_client.Gauge( 'dyson_humidity_percent', 'Relative humidity (percentage)', labels) self.temperature = prometheus_client.Gauge( 'dyson_temperature_celsius', 'Ambient temperature (celsius)', labels) self.voc = prometheus_client.Gauge( 'dyson_volatile_organic_compounds_units', 'Level of Volatile organic compounds', labels) self.dust = prometheus_client.Gauge('dyson_dust_units', 'Level of Dust', labels) # Operational State # Ignoring: tilt (known values OK), standby_monitoring. self.fan_mode = prometheus_client.Enum('dyson_fan_mode', 'Current mode of the fan', labels, states=['AUTO', 'FAN', 'OFF']) self.fan_state = prometheus_client.Enum( 'dyson_fan_state', 'Current running state of the fan', labels, states=['FAN', 'OFF']) self.fan_speed = prometheus_client.Gauge( 'dyson_fan_speed_units', 'Current speed of fan (-1 = AUTO)', labels) self.oscillation = prometheus_client.Enum('dyson_oscillation_mode', 'Current oscillation mode', labels, states=['ON', 'OFF']) self.focus_mode = prometheus_client.Enum('dyson_focus_mode', 'Current focus mode', labels, states=['ON', 'OFF']) self.heat_mode = prometheus_client.Enum('dyson_heat_mode', 'Current heat mode', labels, states=['HEAT', 'OFF']) self.heat_state = prometheus_client.Enum('dyson_heat_state', 'Current heat state', labels, states=['HEAT', 'OFF']) self.heat_target = prometheus_client.Gauge( 'dyson_heat_target_celsius', 'Heat target temperature (celsius)', labels) self.quality_target = prometheus_client.Gauge( 'dyson_quality_target_units', 'Quality target for fan', labels) self.filter_life = prometheus_client.Gauge( 'dyson_filter_life_seconds', 'Remaining filter life (seconds)', labels)
def flask_app_factory(test_class, test_names=None, service_name=None): '''Create a Prometheus endpoint from a test class and test names to be executed ''' app = Flask(__name__) if test_names is None: test_names = [] if service_name is None: service_name = '{}_status'.format(test_class.__name__) _service_status_enum = prometheus_client.Enum(service_name, 'up/down status of service', states=ServiceStatus.names()) # For each test create a view for test_name in test_names: flask_view = FlaskPrometheusView(_service_status_enum, test_class, test_name=test_name) # Path is made up of the test case class name and name of test # method to be executed. path = '/metrics/{}/{}'.format(test_class.__name__, test_name) app.add_url_rule(path, test_name, flask_view) else: # No test names set - instead run all the tests in the input test case flask_view = FlaskPrometheusView(_service_status_enum, test_class) # Path is made up of the test case class name and name of test # method to be executed. path = '/metrics/{}'.format(test_class.__name__) app.add_url_rule(path, test_class.__name__, flask_view) return app
def __init_metrics(self): namespace = 'homematicip' labelnames = ['room', 'device_label'] detail_labelnames = [ 'device_type', 'firmware_version', 'permanently_reachable' ] event_device_labelnames = ['device_label'] event_group_labelnames = ['group_label'] event_labelnames = ['type', 'window_state', 'sabotage'] self.version_info = prometheus_client.Gauge( name='version_info', documentation='HomematicIP info', labelnames=['api_version'], namespace=namespace) self.metric_temperature_actual = prometheus_client.Gauge( name='temperature_actual', documentation='Actual temperature', labelnames=labelnames, namespace=namespace) self.metric_temperature_setpoint = prometheus_client.Gauge( name='temperature_setpoint', documentation='Set point temperature', labelnames=labelnames, namespace=namespace) self.metric_valve_adaption_needed = prometheus_client.Gauge( name='valve_adaption_needed', documentation='must the adaption re-run?', labelnames=labelnames, namespace=namespace) self.metric_temperature_offset = prometheus_client.Gauge( name='temperature_offset', documentation='the offset temperature for the thermostat', labelnames=labelnames, namespace=namespace) self.metric_valve_position = prometheus_client.Gauge( name='valve_position', documentation= 'the current position of the valve 0.0 = closed, 1.0 max opened', labelnames=labelnames, namespace=namespace) self.metric_humidity_actual = prometheus_client.Gauge( name='humidity_actual', documentation='Actual Humidity', labelnames=labelnames, namespace=namespace) self.metric_last_status_update = prometheus_client.Gauge( name='last_status_update', documentation="Device last status update", labelnames=labelnames, namespace=namespace) self.metric_device_info = prometheus_client.Gauge( name='device_info', documentation='Device information', labelnames=labelnames + detail_labelnames, namespace=namespace) self.metric_power_consumption = prometheus_client.Gauge( name='power_consumption', documentation='Power consumption', labelnames=labelnames, namespace=namespace) self.metric_energy_counter = prometheus_client.Gauge( name='energy_counter', documentation='Energy Counter', labelnames=labelnames, namespace=namespace) self.metric_switch_on = prometheus_client.Gauge( name='switch', documentation='Switch turned on', labelnames=labelnames, namespace=namespace) self.metric_window_state = prometheus_client.Enum( states=["OPEN", "CLOSED"], name='window_state', documentation='Window state', labelnames=labelnames, namespace=namespace) self.metric_device_event = prometheus_client.Counter( name='device_event', documentation='events triggered by a device', labelnames=event_device_labelnames + event_labelnames, namespace=namespace) self.metric_group_event = prometheus_client.Counter( name='group_event', documentation='events triggered by a group', labelnames=event_group_labelnames + event_labelnames, namespace=namespace, )
import prometheus_client import random import re import socket import sys import string import threading import time import yaml api = flask.Flask('rest') stat_config_state = prometheus_client.Enum('config_state', 'Configuration state', ['name'], states=[ 'unknown', 'provisioned', 'changed', 'check-failed', 'removed', 'invalid' ]) stat_run_state = prometheus_client.Enum('runner_state', 'Runner state', ['name'], states=[ 'new', 'config-updated', 'running', 'received-callback', 'succeeded', 'failed', 'triggered', 'disabled' ]) runnable_states = ('new', 'config-updated', 'succeeded', 'failed', 'triggered') pod_ip = socket.gethostbyname(os.environ['HOSTNAME'])
def check(): ''' Updates all data in prometheus ''' check_Cash_Stuff() group_request = get_Infos_From_NiceHash('GET', '/mining/groups/list', {"extendedResponse": "True"}) if 'error_id' in group_request: print(group_request) return rigs = group_request['groups']['']['rigs'] ctr = 0 for r in rigs: if r['name'] == 'JPC': break ctr += 1 second_rig_id = rigs[ctr]["rigId"] stats_request = get_Infos_From_NiceHash('GET', '/mining/rig/stats/unpaid', {'rigId': second_rig_id}) mining_request = get_Infos_From_NiceHash('GET', '/mining/rig2/' + second_rig_id, {}) payout_request = get_Infos_From_NiceHash('GET', '/mining/rigs2', {}) next_payout = datetime.datetime.strptime( payout_request['nextPayoutTimestamp'], '%Y-%m-%dT%H:%M:%SZ') last_payout = datetime.datetime.strptime( payout_request['lastPayoutTimestamp'], '%Y-%m-%dT%H:%M:%S.%fZ') # Timezone adjustment next_payout += datetime.timedelta(hours=1) last_payout += datetime.timedelta(hours=1) if 'error_id' in stats_request or len( stats_request['data']) == 0 or 'error_id' in mining_request: print(group_request, mining_request) return for rig in rigs: name = "rig_status_" + rig['name'] if name not in prometheus_data: prometheus_data[name] = prometheus_client.Enum( name, 'Status of rig: ' + rig['name'], states=[ 'BENCHMARKING', 'MINING', 'STOPPED', 'OFFLINE', 'ERROR', 'PENDING', 'DISABLED', 'TRANSFERRED', 'UNKNOWN' ]) prometheus_data[name].state(rig['status']) prometheus_data['currently_unpaid'].set(stats_request['data'][0][2]) prometheus_data['rig_temperature'].set( mining_request['devices'][1]['temperature']) prometheus_data['rig_load'].set(mining_request['devices'][1]['load']) prometheus_data['rig_power_usage'].set( mining_request['devices'][1]['powerUsage']) prometheus_data['profitability'].set(mining_request['profitability']) prometheus_data['local_profitability'].set( mining_request['localProfitability']) prometheus_data['currency_last_payout_time'].set( round(last_payout.timestamp() * 1000)) prometheus_data['currency_next_payout_time'].set( round(next_payout.timestamp() * 1000)) prometheus_data['worker_hashrate'].set( mining_request['devices'][1]['speeds'][0]['speed'])
"Evohome temperatuur in celsius", ["name", "thermostat", "id", "type"], ) zavail = prom.Gauge( "evohome_zone_available", "Evohome zone availability", ["name", "thermostat", "id"], ) zfault = prom.Gauge( "evohome_zone_fault", "Evohome zone has active fault(s)", ["name", "thermostat", "id"], ) zmode = prom.Enum( "evohome_zone_mode", "Evohome zone mode", ["name", "thermostat", "id"], states=["FollowSchedule", "TemporaryOverride", "PermanentOverride"], ) tcsperm = prom.Gauge( "evohome_temperaturecontrolsystem_permanent", "Evohome temperatureControlSystem is in permanent state", ["id"], ) tcsfault = prom.Gauge( "evohome_temperaturecontrolsystem_fault", "Evohome temperatureControlSystem has active fault(s)", ["id"], ) tcsmode = prom.Enum( "evohome_temperaturecontrolsystem_mode", "Evohome temperatureControlSystem mode",
def main(): logger.info("Evohome exporter for Prometheus") try: username = environ[username_env_var] password = environ[password_env_var] except KeyError: logger.error("Missing environment variables for Evohome credentials:") logger.error(f"\t{username_env_var} - Evohome username") logger.error(f"\t{password_env_var} - Evohome password") exit(1) else: logger.info( f"Evohome credentials read from environment variables ({username})" ) poll_interval = int(environ.get(poll_interval_env_var, 300)) scrape_port = int(environ.get(scrape_port_env_var, 8082)) eht = prom.Gauge( "evohome_temperature_celcius", "Evohome temperatuur in celsius", ["name", "thermostat", "id", "type"], ) zavail = prom.Gauge( "evohome_zone_available", "Evohome zone availability", ["name", "thermostat", "id"], ) zfault = prom.Gauge( "evohome_zone_fault", "Evohome zone has active fault(s)", ["name", "thermostat", "id"], ) zmode = prom.Enum( "evohome_zone_mode", "Evohome zone mode", ["name", "thermostat", "id"], states=["FollowSchedule", "TemporaryOverride", "PermanentOverride"], ) tcsperm = prom.Gauge( "evohome_temperaturecontrolsystem_permanent", "Evohome temperatureControlSystem is in permanent state", ["id"], ) tcsfault = prom.Gauge( "evohome_temperaturecontrolsystem_fault", "Evohome temperatureControlSystem has active fault(s)", ["id"], ) tcsmode = prom.Enum( "evohome_temperaturecontrolsystem_mode", "Evohome temperatureControlSystem mode", ["id"], states=[ "Auto", "AutoWithEco", "AutoWithReset", "Away", "DayOff", "HeatingOff", "Custom", ], ) upd = prom.Gauge("evohome_updated", "Evohome client last updated") up = prom.Gauge("evohome_up", "Evohome client status") prom.start_http_server(scrape_port) try: client = EvohomeClient(username, password) except Exception as e: logger.error( f"ERROR: can't create EvohomeClient\n{type(e).__name__}: {e}") sys.exit(1) logger.info("Logged into Evohome API") loggedin = True lastupdated = 0 tcsalerts = set() zonealerts = dict() oldids = set() labels = {} lastup = False while True: temps = [] newids = set() try: temps = list(client.temperatures()) get_schedules(client) loggedin = True updated = True lastupdated = time.time() except Exception as e: print("{}: {}".format(type(e).__name__, str(e)), file=sys.stderr) temps = [] updated = False loggedin = loginEvohome(client) if loggedin: continue if loggedin and updated: up.set(1) upd.set(lastupdated) tcs = client._get_single_heating_system() sysmode = tcs.systemModeStatus tcsperm.labels(client.system_id).set( float(sysmode.get("isPermanent", True))) tcsmode.labels(client.system_id).state(sysmode.get("mode", "Auto")) if tcs.activeFaults: tcsfault.labels(client.system_id).set(1) for af in tcs.activeFaults: afhd = hashabledict(af) if afhd not in tcsalerts: tcsalerts.add(afhd) logger.warn(f"fault in temperatureControlSystem: {af}") else: tcsfault.labels(client.system_id).set(0) tcsalerts = set() for d in temps: newids.add(d["id"]) labels[d["id"]] = [d["name"], d["thermostat"], d["id"]] if d["temp"] is None: zavail.labels(d["name"], d["thermostat"], d["id"]).set(0) eht.remove(d["name"], d["thermostat"], d["id"], "measured") else: zavail.labels(d["name"], d["thermostat"], d["id"]).set(1) eht.labels(d["name"], d["thermostat"], d["id"], "measured").set(d["temp"]) eht.labels(d["name"], d["thermostat"], d["id"], "setpoint").set(d["setpoint"]) eht.labels(d["name"], d["thermostat"], d["id"], "planned").set( calculate_planned_temperature(schedules[d["id"]])) zmode.labels(d["name"], d["thermostat"], d["id"]).state( d.get("setpointmode", "FollowSchedule")) if d["id"] not in zonealerts.keys(): zonealerts[d["id"]] = set() if d.get("activefaults"): zonefault = 1 for af in d["activefaults"]: afhd = hashabledict(af) if afhd not in zonealerts[d["id"]]: zonealerts[d["id"]].add(afhd) print( "fault in zone {}: {}".format(d["name"], af), file=sys.stderr, ) else: zonefault = 0 zonealerts[d["id"]] = set() zfault.labels(d["name"], d["thermostat"], d["id"]).set(zonefault) lastup = True else: up.set(0) if lastup: tcsperm.remove(client.system_id) tcsfault.remove(client.system_id) tcsmode.remove(client.system_id) lastup = False for i in oldids: if i not in newids: eht.remove(*labels[i] + ["measured"]) eht.remove(*labels[i] + ["setpoint"]) eht.remove(*labels[i] + ["planned"]) zavail.remove(*labels[i]) zmode.remove(*labels[i]) zfault.remove(*labels[i]) oldids = newids time.sleep(poll_interval)
import kubernetes.client.rest import logging import os import prometheus_client import re import shutil import subprocess import sys import threading import time import yaml api = flask.Flask('rest') stat_config_state = prometheus_client.Enum( 'config_state', 'Configuration state', ['name'], states=['new', 'provisioned', 'changed', 'failed', 'removed']) stat_webhook_call_count = prometheus_client.Counter('webhook_call_count', 'Number of webhook calls', ['name']) cluster_vars = None config_queue = None namespace = None provision_configs = {} git_base_path = os.environ.get('GIT_CHECKOUT_DIR', '/opt/openshift-provision/cache/git') ansible_runner_base_path = os.environ.get( 'ANSIBLE_RUNNER_DIR', '/opt/openshift-provision/cache/ansible-runner') default_retry_interval = os.environ.get('RETRY_INTERVAL', '10m') default_run_interval = os.environ.get('RUN_INTERVAL', '30m')