Skip to content

Instantly share code, notes, and snippets.

@rchakode
Last active December 15, 2022 13:11
Show Gist options
  • Save rchakode/8c362c23876b82c85e997c029b076540 to your computer and use it in GitHub Desktop.
Save rchakode/8c362c23876b82c85e997c029b076540 to your computer and use it in GitHub Desktop.
Sample Python program providing a Prometheus exporter that exposes system CPU and memory usage every 5 minutes.
import prometheus_client
import time
import psutil
UPDATE_PERIOD = 300
SYSTEM_USAGE = prometheus_client.Gauge('system_usage',
'Hold current system resource usage',
['resource_type'])
if __name__ == '__main__':
prometheus_client.start_http_server(9999)
while True:
SYSTEM_USAGE.labels('CPU').set(psutil.cpu_percent())
SYSTEM_USAGE.labels('Memory').set(psutil.virtual_memory()[2])
time.sleep(UPDATE_PERIOD)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment