Instantly share code, notes, and snippets.

@Kwarf /rrdlog.py Secret
Created Feb 7, 2015

Embed
What would you like to do?
Quick hack to benchmark Steam OS
#!/usr/bin/python2
# !!! IMPORTANT !!!
# This is just something I threw together when benchmarking my newly built
# Steam Machine, using the ASRock Q1900DC-ITX, don't expect it to work on
# anything else. For all I know it might not even work at all. This is my first
# time using rrdtool, and probably my second python script longer than 10 lines
# Check out my blog post for more info:
# http://blog.kwarf.com/2015/02/building-a-steam-machine-part-1/
# The graphs in the post were generated using the following commands:
# rrdtool graph network.png -W http://blog.kwarf.com/2015/02/building-a-steam-machine-part-1/ -s end-15m -e `rrdtool last log.rrd` -w 900 -h 200 DEF:downbytes=log.rrd:down:AVERAGE CDEF:down=downbytes,8,* AREA:down#00ff00:down DEF:upbytes=log.rrd:up:AVERAGE CDEF:up=upbytes,8,* AREA:up#ff0000:up
# rrdtool graph cores.png -W http://blog.kwarf.com/2015/02/building-a-steam-machine-part-1/ -s end-15m -e `rrdtool last log.rrd` -w 900 -h 200 DEF:core0=log.rrd:core0:AVERAGE LINE:core0#ff0000:core0 DEF:core1=log.rrd:core1:AVERAGE LINE:core1#00ff00:core1 DEF:core2=log.rrd:core2:AVERAGE LINE:core2#0000ff:core2 DEF:core3=log.rrd:core3:AVERAGE LINE:core3#ffff00:core3
# rrdtool graph perf.png -W http://blog.kwarf.com/2015/02/building-a-steam-machine-part-1/ -s end-15m -e `rrdtool last log.rrd` -w 900 -h 200 -l 0 -u 100 -r DEF:core0=log.rrd:core0:AVERAGE DEF:core1=log.rrd:core1:AVERAGE DEF:core2=log.rrd:core2:AVERAGE DEF:core3=log.rrd:core3:AVERAGE CDEF:cpu=core0,core1,+,core2,+,core3,+,4,/ LINE:cpu#00ff00:cpu DEF:mem=log.rrd:mem:AVERAGE LINE:mem#0000ff:mem DEF:temp=log.rrd:temp:AVERAGE LINE:temp#ff0000:temp
import os
import psutil
import rrdtool
import threading
FILENAME = "log.rrd"
def core_temp(core):
# For some reason the core temperatures are in temp2 to temp5,
# verify for yourself by checking the _label files, mine says Core 0 to 3
path = "/sys/class/hwmon/hwmon1/temp{}_input".format(core + 2)
return int(open(path, "r").read()) / 1000
# Get the temperature of the hottest core
def core_high_temp():
temp = 0
for i in range(0, psutil.NUM_CPUS):
ct = core_temp(i)
if ct > temp:
temp = ct
return temp
def tick():
threading.Timer(1, tick).start()
cpu = psutil.cpu_percent(percpu=True)
mem = psutil.phymem_usage().percent
temp = core_high_temp()
net = psutil.network_io_counters(pernic=True).get("eth0")
down = net.bytes_recv
up = net.bytes_sent
update_string = "N:{}:{}:{}:{}:{}:{}:{}:{}".format(
cpu[0], cpu[1], cpu[2], cpu[3], mem, temp, down, up
)
rrdtool.update(FILENAME, update_string)
# Create RRD database for 1 second log interval, 900 samples (15 minutes)
rrdtool.create(
FILENAME,
"--step", "1",
"DS:core0:GAUGE:2:0:100",
"DS:core1:GAUGE:2:0:100",
"DS:core2:GAUGE:2:0:100",
"DS:core3:GAUGE:2:0:100",
"DS:mem:GAUGE:2:0:100",
"DS:temp:GAUGE:2:0:100",
"DS:down:COUNTER:2:U:U",
"DS:up:COUNTER:2:U:U",
"RRA:AVERAGE:0.5:1:900"
)
tick()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment