def write_data(data): temperature.set( int(hex(data[9]) + '{:02x}'.format(data[8], 'x'), 16) / 100) humidity.set(int(hex(data[11]) + '{:02x}'.format(data[10], 'x'), 16) / 100) light.set(int(hex(data[13]) + '{:02x}'.format(data[12], 'x'), 16)) barometric.set( int( hex(data[17]) + '{:02x}'.format(data[16], 'x') + '{:02x}'.format( data[15], 'x') + '{:02x}'.format(data[14], 'x'), 16) / 1000) noise.set(int(hex(data[19]) + '{:02x}'.format(data[18], 'x'), 16) / 100) discomfort.set( int(hex(data[25]) + '{:02x}'.format(data[24], 'x'), 16) / 100) heat.set(int(hex(data[27]) + '{:02x}'.format(data[26], 'x'), 16) / 100) etvoc.set(int(hex(data[21]) + '{:02x}'.format(data[20], 'x'), 16)) eco2.set(int(hex(data[23]) + '{:02x}'.format(data[22], 'x'), 16)) seismic.set(int(hex(data[34]) + '{:02x}'.format(data[33], 'x'), 16) / 1000) vibration.set(int(hex(data[28]), 16)) si.set(int(hex(data[30]) + '{:02x}'.format(data[29], 'x'), 16) / 10) pga.set(int(hex(data[32]) + '{:02x}'.format(data[31], 'x'), 16) / 10) write_to_textfile(lib + '/sensor_omron.prom', registry)
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)
def export(): registry = CollectorRegistry() temp_g = Gauge('rpi_temperature', 'Temperatures of the components in degree celsius.', ['node', 'sensor', 'type'], registry=registry) freq_g = Gauge('rpi_frequency', 'Clock frequencies of the components in hertz.', ['node', 'component'], registry=registry) volt_g = Gauge('rpi_voltage', 'Voltages of the components in volts.', ['node', 'component'], registry=registry) mem_g = Gauge('rpi_memory', 'Memory split of CPU and GPU in bytes.', ['node', 'component'], registry=registry) thro_g = Gauge('rpi_throttled', 'Throttled state of the system.', ['node', 'component'], registry=registry) # Get temperatures for folder, subfolders, files in os.walk("/sys/class/thermal/"): for sensor_name in subfolders: temp_celsius = getoutput( "awk '{printf \"%.3f\", $1/1000}' /sys/class/thermal/" + sensor_name + "/temp") sensor_type = getoutput("cat /sys/class/thermal/" + sensor_name + "/type") temp_g.labels(NODENAME, sensor_name, sensor_type).set(temp_celsius) # Get component frequencies for freq_component in [ 'arm', 'core', 'h264', 'isp', 'v3d', 'uart', 'pwm', 'emmc', 'pixel', 'vec', 'hdmi', 'dpi' ]: freq = getoutput("vcgencmd measure_clock " + freq_component).split('=') freq_g.labels(NODENAME, freq_component).set(freq[1]) # Get component voltages for volt_component in ['core', 'sdram_c', 'sdram_i', 'sdram_p']: volt = getoutput("vcgencmd measure_volts " + volt_component).split('=') volt_g.labels(NODENAME, volt_component).set(volt[1].replace("V", "")) # Get memory split of CPU vs GPU for mem_component in ['arm', 'gpu']: mem = getoutput("vcgencmd get_mem " + mem_component).split('=') memory = int(mem[1].replace("M", "")) mem_g.labels(NODENAME, mem_component).set(memory * 1024 * 1024) # Get throttled events thro = getoutput("vcgencmd get_throttled") throttled = int(thro.strip().split("=")[1], 16) under_voltage_detected = 1 if throttled & 1 << 0 else 0 arm_frequency_capped = 1 if throttled & 1 << 1 else 0 currently_throttled = 1 if throttled & 1 << 2 else 0 soft_temperature = 1 if throttled & 1 << 3 else 0 thro_g.labels(NODENAME, 'under_voltage_detected').set(under_voltage_detected) thro_g.labels(NODENAME, 'arm_frequency_capped').set(arm_frequency_capped) thro_g.labels(NODENAME, 'currently_throttled').set(currently_throttled) thro_g.labels(NODENAME, 'soft_temperature').set(soft_temperature) if EXPORT_TYPE == "text": # Write to textfile if os.access(TEXTFILE_DIR, os.W_OK): write_to_textfile(METRICS_FILE, registry=registry) elif EXPORT_TYPE == "gateway": url = GATEWAY_URL.split(':') if (len(url) > 1 and connectable(url[0], int(url[1]))): # Push to gateway push_to_gateway(GATEWAY_URL, job='rpi-exporter', registry=registry)
def run(self, accessor, opts, on_progress=None): """Run some repairs. See command.BaseCommand """ accessor.connect() # TODO: use multiprocessing + progressbar here. Probably remove some # of the current arguments and generate them instead based on a number # of processes to do a full scan. self.metrics_file_path = opts.metrics_file_path if opts.storage_dir: settings = {"path": opts.storage_dir} with metadata_cache.DiskCache(accessor, settings) as cache: cache.repair( shard=opts.shard, nshards=opts.nshards, start_key=opts.start_key, end_key=opts.end_key, ) else: logging.warning( "Skipping disk cache repair because storage_dir is empty") out_fd = sys.stderr if opts.quiet: out_fd = _DEV_NULL if self.pbar is None: start_key = -1 * 2**63 end_key = 2**63 - 1 if opts.start_key is not None: start_key = int(opts.start_key) if opts.end_key is not None: end_key = int(opts.end_key) widgets = [ progressbar.Variable('token', format='(current: {formatted_value})'), ' ', progressbar.Percentage(), ' ', progressbar.SimpleProgress( format='(%s)' % progressbar.SimpleProgress.DEFAULT_FORMAT), ' ', progressbar.Bar(), ' ', progressbar.Timer(), ' ', progressbar.AdaptiveETA(), ] # max_value = end_key - start_key self.pbar = progressbar.ProgressBar(widgets=widgets, fd=out_fd, redirect_stderr=False, min_value=0, max_value=end_key - start_key) self.pbar.start() if on_progress is None: def _on_progress(total, done, token): self.pbar.update(total, token=token) if self.metrics_file_path != "": write_to_textfile(self.metrics_file_path, REGISTRY) on_progress = _on_progress accessor.repair( shard=opts.shard, nshards=opts.nshards, start_key=opts.start_key, end_key=opts.end_key, callback_on_progress=on_progress, ) self.pbar.finish() # Final metric dump if self.metrics_file_path != "": write_to_textfile(self.metrics_file_path, REGISTRY)
def find_physical_interfaces(self): """Find physical interfaces and optionally filter them.""" # https://serverfault.com/a/833577/393474 root = '/sys/class/net' for file in os.listdir(root): path = os.path.join(root, file) if os.path.islink(path) and 'virtual' not in os.readlink(path): if re.match(self.args['interface_regex'], file): yield file if __name__ == '__main__': collector = EthtoolCollector() registry = prometheus_client.CollectorRegistry() registry.register(collector) args = collector.args if args['listen']: (ip, port) = args['listen'].split(':') prometheus_client.start_http_server(port=int(port), addr=ip, registry=registry) while True: time.sleep(3600) if args['textfile_name']: while True: collector.collect() prometheus_client.write_to_textfile(args['textfile_name'], registry) if collector.args['oneshot']: sys.exit(0) time.sleep(args['interval'])
# the number of MD dynamics steps for each cycle n_steps = 1000000 steps = [n_steps for i in range(n_cycles)] # number of parallel simulations n_walkers = 10 # the work mapper # work_mapper = ThreadMapper() work_mapper = Mapper() # create the initial walkers with equal weights with start_action(action_type="Init Walkers") as ctx: init_weight = 1.0 / n_walkers init_walkers = [ Walker(copy(init_state), init_weight) for i in range(n_walkers) ] with start_action(action_type="Init Sim Manager") as ctx: sim_manager = Manager(init_walkers, runner=runner, resampler=resampler, work_mapper=work_mapper) # run the simulation and get the results with start_action(action_type="Simulation") as ctx: final_walkers, _ = sim_manager.run_simulation(n_cycles, steps) write_to_textfile("_output/metrics.prom")
def collect_all(self): for collector in self.gl_collectors: collector.collect_wrapper() write_to_textfile(self.gl_path, self.gl_registry)
def write(self): filename = common.prepare_filename(self.conf['prometheus_output']) pc.write_to_textfile(filename, pc.REGISTRY)
smartstack_yaml = yaml.safe_load(smartstack_file) for instance_name, info in smartstack_yaml.items(): upstream = f"{service}.{instance_name}.egress_cluster" if "endpoint_timeouts" in info: for path, endpoint_timeout in info["endpoint_timeouts"].items(): prom_metric.labels(path, upstream).set(endpoint_timeout) # always record default timeout default_timeout = info.get("timeout_server_ms", DEFAULT_TIMEOUT) prom_metric.labels("/", upstream).set(default_timeout) if __name__ == "__main__": registry = CollectorRegistry() prom_metric = Gauge( "yelpsoaconfig_endpoint_timeouts_ms", "endpoint timeout value defined in yelpsoa-config", ["path", "upstream"], registry=registry, ) # Walk through soa config dir and filter smartstack yaml for root, dirs, files in os.walk(DEFAULT_SOA_DIR): service = root.split("/")[-1] # Avoid confusion of the smartstacks.yaml under autotuned_defaults/ in the future if "autotuned_defaults" == service: continue for f in files: if f == "smartstack.yaml": read_and_write_timeouts_metrics(root, service, prom_metric) write_to_textfile(PROM_OUTPUT_FILE, registry)
def write_metrics(self): """Write metrics in textfile.""" registry = CollectorRegistry() metric_scanned_range = Gauge( 'scanned_range', 'Total of offset ranges scanned', registry=registry) metric_scanned_range.set(self._n_range_size) metric_total = Gauge( 'metric_total', 'Total of metric found in offset ranges scanned', registry=registry) metric_total.set(self._n_count) metric_total_expired = Gauge( 'metric_expired', 'Total of expired metric found in offset ranges scanned', registry=registry) metric_total_expired.set(self._n_count_expired) multiplier = 2**64 / self._n_range_size metric_estimated_total = Gauge( 'metric_estimated_total', 'Estimated total of metric in database', registry=registry) metric_estimated_total.set(int(self._n_count * multiplier)) metric_estimated_total_expired = Gauge( 'metric_estimated_expired', 'Estimated total of expired metric in database', registry=registry) metric_estimated_total_expired.set(int(self._n_count_expired * multiplier)) directories_total = Gauge( 'directories_total', 'Total of directories found in offset ranges scanned', registry=registry) directories_total.set(self._n_dir_count) directories_total_empty = Gauge( 'directories_empty', 'Total of empty directories found in offset ranges scanned', registry=registry) directories_total_empty.set(self._n_dir_empty) directories_estimated_total = Gauge( 'directories_estimated_total', 'Estimated total of directories in database', registry=registry) directories_estimated_total.set(int(self._n_dir_count * multiplier)) directories_estimated_total_empty = Gauge( 'directories_estimated_empty', 'Estimated total of empty directories in database', registry=registry) directories_estimated_total_empty.set(int(self._n_dir_empty * multiplier)) # Final metric dump write_to_textfile(self.metrics_file_path, registry)
labelnames=['container_name', 'metrictype'], registry=registry) g_container_pid = Gauge('lxc_container_container_pid', 'PID of LXC Container', labelnames=['container_name', 'metrictype'], registry=registry) g_network_usage = Gauge( 'lxc_container_container_network_usage', 'Network Usage By Container', labelnames=['container_name', 'metrictype', 'interface', 'operation'], registry=registry) g_container_status = Gauge('lxc_container_container_status', 'Container status', labelnames=['container_name', 'metrictype'], registry=registry) g_total_cpu = Gauge('lxc_container_host_cpu_total', 'Usage of lxc cpu', labelnames=['metrictype'], registry=registry) g_total_mem = Gauge('lxc_container_host_mem_total', 'Usage of lxc cpu', labelnames=['metrictype'], registry=registry) while True: process_metrics(g_cpu,g_mem_usage,g_mem_usage_peak,g_swap_usage,g_swap_usage_peak,\ g_process_count,g_disk_uage,g_container_pid,g_network_usage,g_container_status,\ g_total_cpu,g_total_mem) write_to_textfile('collect.prom', registry)
def write_textfile_collector_file(self) -> None: """Write metrics to the textfile collector path.""" prometheus_client.write_to_textfile(self.prompath, self.registry)
registry = CollectorRegistry() g = Gauge('light', 'LIGHT', ['kind'], registry=registry) try: while True: GPIO.setup(MONITOR_PIN, GPIO.OUT) GPIO.output(MONITOR_PIN, GPIO.LOW) time.sleep(15) count = 0 start_time = time.time() GPIO.setup(MONITOR_PIN, GPIO.IN) while (GPIO.input(MONITOR_PIN) == GPIO.LOW): time.sleep(0.1) end_time = time.time() # capacitor charging time, bigger means darker dur = end_time - start_time # fake lightness light = 100 / dur g.labels('lightness').set(light) write_to_textfile(file_output, registry) print('lightness={:.03f}'.format(light)) except KeyboardInterrupt: print('exit') finally: GPIO.cleanup()
def _collect_to_text(): while True: e = AbucoinsCollector() write_to_textfile('{0}/abucoins_exporter.prom'.format(settings['abucoins_exporter']['prom_folder']), e) time.sleep(int(settings['abucoins_exporter']['interval']))
from prometheus_client import Gauge, write_to_textfile, REGISTRY import RPi.GPIO as GPIO import dht11 import time TMP_PATH = '/textfile/dht11.prom' GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.cleanup() module = dht11.DHT11(pin=24) g1 = Gauge('temperature', 'Gauge') g2 = Gauge('humidity', 'Gauge') while True: result = module.read() if result.is_valid(): g1.set(result.temperature) g2.set(result.humidity) write_to_textfile(TMP_PATH, REGISTRY) time.sleep(1)
def main(): parser = argparse.ArgumentParser( description='Collect SMART information from all physical disks' 'and report as Prometheus metrics') parser.add_argument('--outfile', metavar='FILE.prom', help='Output file (stdout)') parser.add_argument('--syslog', action='store_true', default=False, help='Log to syslog (%(default)s)') parser.add_argument('-d', '--debug', action='store_true', default=False, help='Enable debug logging (%(default)s)') args = parser.parse_args() script_name = parser.prog if args.debug: logging.basicConfig(level=logging.DEBUG) else: logging.basicConfig(level=logging.WARNING) if args.syslog: handler = SysLogHandler(address='/dev/log') handler.setFormatter(logging.Formatter(script_name + ': %(message)s')) root_logger = logging.getLogger() root_logger.handlers = [] root_logger.addHandler(handler) if args.outfile and not args.outfile.endswith('.prom'): parser.error('Output file does not end with .prom') facter_version = get_facter_version() physical_disks = [] raid_drivers = get_fact('raid', facter_version) if raid_drivers is None: log.error('Invalid value for "raid" fact: %r', raid_drivers) return 1 for driver in raid_drivers: handler = DRIVER_HANDLERS.get(driver) if handler is None: continue for pd in handler(): physical_disks.append(pd) # TODO(filippo): handle machines with disks attached to raid controllers _and_ regular sata if not raid_drivers or raid_drivers == ['md']: for pd in noraid_list_pd(): physical_disks.append(pd) log.debug('Gathering SMART data from physical disks: %r', [x.disk_id for x in physical_disks]) registry = CollectorRegistry() collect_smart_metrics(physical_disks, registry) if args.outfile: write_to_textfile(args.outfile, registry) else: sys.stdout.write(generate_latest(registry).decode('utf-8'))
def exportToFile(self, fpath): """export metrics in the registry to a file""" write_to_textfile(fpath, self.registry) return
def _collect_to_text(): e = KrakenCollector() while True: write_to_textfile('{0}/kraken_exporter.prom'.format(settings['kraken_exporter']['prom_folder']), e) time.sleep(int(settings['kraken_exporter']['interval']))
pod='maria78', project='default', period='5').set(17) consumption_service.labels(cluster='ocp1317', service='JBoss', pod='maria78', project='default', period='5').set(5) consumption_service.labels(cluster='ocp1317', service='NgniX', pod='ng1725', project='frontend', period='5').set(7) consumption_total = Counter('consumption_total', 'total consumption per month', ['cluster', 'month'], registry=registry) consumption_total.labels(cluster='ocp1317', month='18-08').inc(5) write_to_textfile('./import/consumption.prom', registry) ''' from prometheus_client import Gauge g = Gauge('my_inprogress_requests', 'Description of gauge') g.inc() # Increment by 1 g.dec(10) # Decrement by given value g.set(4.2) # Set to a given value g.set_to_current_time() '''
registry = CollectorRegistry() g = Gauge('aws_ec2_instance_price', 'price of individual instance types in a region', ['region', 'instance_type'], registry=registry) regions = ["ap-southeast-1"] for region in regions: with open('data/' + region + '.csv') as csvfile: spamreader = csv.reader(csvfile, delimiter=',', quotechar='"') for row in spamreader: if row[1] == "API Name": continue price = row[30].split(" ")[0].lstrip("$") if price == "unavailable": continue row = { "region": region, "instance_type": row[1], "price": float(price) } g.labels(region=row['region'], instance_type=row['instance_type']).set(row['price']) write_to_textfile('metrics', registry)
def run(self, accessor, opts, on_progress=None): """Run some cleanups. See command.BaseCommand """ out_fd = sys.stderr if opts.quiet: out_fd = _DEV_NULL self.metrics_file_path = opts.metrics_file_path if self.pbar is None: start_key = -1 * 2**63 end_key = 2**63 - 1 if opts.start_key is not None: start_key = int(opts.start_key) if opts.end_key is not None: end_key = int(opts.end_key) widgets = [ progressbar.Variable('token', format='(current: {formatted_value})'), ' ', progressbar.Percentage(), ' ', progressbar.SimpleProgress( format='(%s)' % progressbar.SimpleProgress.DEFAULT_FORMAT), ' ', progressbar.Bar(), ' ', progressbar.Timer(), ' ', progressbar.AdaptiveETA(), ] # max_value = end_key - start_key self.pbar = progressbar.ProgressBar(widgets=widgets, fd=out_fd, redirect_stderr=False, min_value=0, max_value=end_key - start_key) self.pbar.start() if on_progress is None: def _on_progress(total, done, token): self.pbar.update(total, token=token) if self.metrics_file_path != "": write_to_textfile(self.metrics_file_path, REGISTRY) on_progress = _on_progress accessor.connect() if opts.clean_cache: if opts.storage_dir: settings = {"path": opts.storage_dir, "ttl": opts.max_age} logging.info("Cleaning cache from %s", settings) with metadata_cache.DiskCache(accessor, settings) as cache: cache.clean() else: logging.error("Cannot clean disk cache because storage_dir" " is empty") if opts.clean_backend: logging.info("Cleaning backend, removing things before %d", opts.max_age) accessor.clean( max_age=opts.max_age, shard=opts.shard, nshards=opts.nshards, start_key=opts.start_key, end_key=opts.end_key, callback_on_progress=on_progress, disable_clean_directories=opts.disable_clean_directories, disable_clean_metrics=opts.disable_clean_metrics, ) if opts.clean_corrupted: # Remove corrupt metrics. now = time.time() def callback(metric, done, total): # TODO: Probably worth removing old metrics here # instead of in the driver... The index doesn't work # well anyway. if metric.updated_on: delta = now - time.mktime(metric.updated_on.timetuple()) else: delta = now if delta > opts.max_age: logging.info("Removing %s (%s)" % (metric.name, delta)) accessor.delete_metric(metric.name) on_progress(done, total) def errback(metric): logging.info("Removing %s" % metric) accessor.delete_metric(metric) logging.info("Cleaning corrupted metrics") accessor.map( callback, shard=opts.shard, nshards=opts.nshards, start_key=opts.start_key, end_key=opts.end_key, errback=errback, ) self.pbar.finish() # Final metric dump if self.metrics_file_path != "": write_to_textfile(self.metrics_file_path, REGISTRY)
for dimension in labeled_prom_metrics["cluster_targets"]: result = computed[dimension["ip"]] dimension["prom_metrics"]["sent"].inc( computed[dimension["ip"]]["sent"]) dimension["prom_metrics"]["received"].inc( computed[dimension["ip"]]["received"]) dimension["prom_metrics"]["rtt"].inc(computed[dimension["ip"]]["rtt"]) dimension["prom_metrics"]["min"].set(computed[dimension["ip"]]["min"]) dimension["prom_metrics"]["max"].set(computed[dimension["ip"]]["max"]) dimension["prom_metrics"]["mdev"].set( computed[dimension["ip"]]["mdev"]) for dimension in labeled_prom_metrics["external_targets"]: result = computed[dimension["host"]] dimension["prom_metrics"]["sent"].inc( computed[dimension["host"]]["sent"]) dimension["prom_metrics"]["received"].inc( computed[dimension["host"]]["received"]) dimension["prom_metrics"]["rtt"].inc( computed[dimension["host"]]["rtt"]) dimension["prom_metrics"]["min"].set( computed[dimension["host"]]["min"]) dimension["prom_metrics"]["max"].set( computed[dimension["host"]]["max"]) dimension["prom_metrics"]["mdev"].set( computed[dimension["host"]]["mdev"]) prometheus_client.write_to_textfile( envs["PROMETHEUS_TEXTFILE_DIR"] + envs["PROMETHEUS_TEXTFILE_PREFIX"] + envs["MY_NODE_NAME"] + ".prom", registry)
agg_counter.labels(contains_values[i] + '_requests').inc() found = True break if not found: agg_counter.labels('others_requests').inc() # Additional statistics file = open(external_indicators_file, 'r') external_indicators = json.load(file) indexed_records = Gauge('opencitations_indexed_records', 'Indexed records', registry=registry) indexed_records.set(external_indicators["indexed_records"]) harvested_data_sources = Gauge('opencitations_harvested_data_sources', 'Harvested data sources', registry=registry) harvested_data_sources.set(external_indicators["harvested_data_sources"]) file.close() # Add the date as info i = Info('opencitations_date', 'Date to which the statistics refers to', registry=registry) date_split = log_file.split(sep)[-1].replace('.txt', '').replace("oc-", "") date_split = date_split.split("-") i.info({'month': date_split[1], 'year': date_split[0]}) # Write the obtained statistics in a file output_file = log_file.split(sep)[-1].replace('.txt', '.prom') write_to_textfile(output_path + output_file, registry)
def _on_progress(total, done, token): self.pbar.update(total, token=token) if self.metrics_file_path != "": write_to_textfile(self.metrics_file_path, REGISTRY)
#!/usr/bin/env python3 import re, subprocess, argparse from prometheus_client import CollectorRegistry, Counter, write_to_textfile from config import * ## Commandline args parser = argparse.ArgumentParser(description="Exports fail2ban-client metrics.") parser.add_argument('-j', '--jail', help="Jail name to be exported", required=True) parser.add_argument('-f', '--file', help="File to be saved. path + name") args = parser.parse_args() ## Regex keys = '|'.join(parseKeys.keys()) pattern = re.compile(r'('+ keys + ')\s*(\d*)') if args.jail: registry = CollectorRegistry() process = subprocess.Popen(['fail2ban-client', 'status', args.jail], stdout=subprocess.PIPE) response = process.communicate()[0].decode('utf-8') match = re.findall(pattern, response) for m in match: print(m[1]) counter = Counter(parseKeys[m[0]][0], parseKeys[m[0]][1], ['jail'] ,registry=registry) counter.labels(args.jail).inc(float(m[1])) write_to_textfile((args.file or (EXPORT_LOCATION + EXPORT_FILE_NAME)).replace('{{jail}}', args.jail), registry)
def write_metrics_to_file(args): registry = BarmanCollector(Barman(), args.servers) prometheus_client.write_to_textfile(args.file, registry) shutil.chown(args.file, user=args.user, group=args.group) os.chmod(args.file, mode=int(args.mode, 8))
def outputs(self, results: Sequence[watcher_models.WatchResult]): logger.debug("Writing results to %s " % self.config.path) self._outputMetrics(results) write_to_textfile(self.config.path, self.registry)