Skip to content

Instantly share code, notes, and snippets.

@outofjungle
Created June 1, 2020 17:46
Show Gist options
  • Select an option

  • Save outofjungle/cf20ab0f87b3856c832835527655a40a to your computer and use it in GitHub Desktop.

Select an option

Save outofjungle/cf20ab0f87b3856c832835527655a40a to your computer and use it in GitHub Desktop.
Example prometheus script for generating a file with metrics for textfile collector
#!/usr/bin/env python3
from prometheus_client import CollectorRegistry, Gauge, write_to_textfile
from psutil import process_iter
from socket import gethostname
hostname = gethostname()
pid_count = {}
for proc in process_iter(['pid', 'name', 'username']):
if proc.info['username'] not in pid_count:
pid_count[proc.info['username']] = 0
pid_count[proc.info['username']] += 1
registry = CollectorRegistry()
cnt = Gauge('user_pid_count', 'count of pids on the node', ['username', 'hostname'], registry=registry)
ts = Gauge('last_success_unixtime', 'last textfile export time ', registry=registry)
for user, count in pid_count.items():
cnt.labels(user, hostname).set(count)
ts.set_to_current_time()
write_to_textfile('/tmp/user_pid_count.prom', registry)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment