Last active
February 3, 2024 17:54
-
-
Save simonhearne/7a8ef5a792050b89bd708fb754be0bb4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import datetime | |
import psutil | |
from influxdb import InfluxDBClient | |
# influx configuration - edit these | |
ifuser = "grafana" | |
ifpass = "<yourpassword>" | |
ifdb = "home" | |
ifhost = "127.0.0.1" | |
ifport = 8086 | |
measurement_name = "system" | |
# take a timestamp for this measurement | |
time = datetime.datetime.utcnow() | |
# collect some stats from psutil | |
disk = psutil.disk_usage('/') | |
mem = psutil.virtual_memory() | |
load = psutil.getloadavg() | |
# format the data as a single measurement for influx | |
body = [ | |
{ | |
"measurement": measurement_name, | |
"time": time, | |
"fields": { | |
"load_1": load[0], | |
"load_5": load[1], | |
"load_15": load[2], | |
"disk_percent": disk.percent, | |
"disk_free": disk.free, | |
"disk_used": disk.used, | |
"mem_percent": mem.percent, | |
"mem_free": mem.free, | |
"mem_used": mem.used | |
} | |
} | |
] | |
# connect to influx | |
ifclient = InfluxDBClient(ifhost,ifport,ifuser,ifpass,ifdb) | |
# write the measurement | |
ifclient.write_points(body) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
remove trailing comma