Created
October 5, 2014 03:22
-
-
Save imerr/576efc36e751acc7a31b to your computer and use it in GitHub Desktop.
Simple linux system monitoring using /proc
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/python | |
import threading | |
import requests | |
TIMER_CYCLE = 10 | |
config = {'net': {'device': 'eth0'}}#execfile("/etc/imer-monitor/config.py", {}) | |
def update(): | |
global config | |
threading.Timer(TIMER_CYCLE, update).start() | |
with open("/sys/class/net/{}/statistics/rx_bytes".format(config['net']['device']), "r") as frx, open("/sys/class/net/{}/statistics/tx_bytes".format(config['net']['device']), "r") as ftx: | |
rx = int(frx.read()) | |
tx = int(ftx.read()) | |
update.network = ((rx-update.network[2])/TIMER_CYCLE, (tx-update.network[3])/TIMER_CYCLE, rx, tx) | |
with open("/proc/meminfo", "r") as mem: | |
tmp = 0 | |
total = 0 | |
for i in mem: | |
sline = i.split() | |
if str(sline[0]) == 'MemTotal:': | |
total = int(sline[1]) | |
elif str(sline[0]) in ('MemFree:', 'Buffers:', 'Cached:'): | |
tmp += int(sline[1]) | |
update.memory = total - tmp | |
with open("/proc/loadavg", "r") as load: | |
update.load = load.read().split(" ")[0] | |
with open("/proc/stat", "r") as cpu: | |
c = cpu.readline() | |
stats = filter(None, (c.split(" ")[1:])) | |
total = 0 | |
for a in stats: | |
total += int(a) | |
idle = int(stats[3])+int(stats[4]) | |
update.cpu['pct'] = ((total-update.cpu['prev']['total'])-(idle-update.cpu['prev']['idle']))/float(total-update.cpu['prev']['total']) | |
update.cpu['prev']['total'] = total | |
update.cpu['prev']['idle'] = idle | |
if update.first: | |
update.first = False | |
return | |
print "{0}b/s {1}kB {2}% {3}".format(update.network[0]+update.network[1], update.memory, update.cpu['pct'], update.load) | |
#requests.post(config['report_url'], data = {'key':config['key'],'network':update.network[0]+update.network[1], 'memory': update.memory, 'load': update.load, 'cpu': update.cpu['pct']}) | |
update.first = True | |
update.network = [0,0,0,0] | |
update.memory = 0 | |
update.cpu = { | |
'pct':0.0, | |
'prev': {'idle':0, 'total':0} | |
} | |
update.load = 0.0 | |
update() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment