Thursday, June 12, 2008

Get system load using Python Script

I have written a simple python script that collect system load information and store in a XML file.


#!/usr/bin/env python

import os
import commands
import time
from time import gmtime, strftime

while 0 < 10:

file_name = strftime("%Y_%m_%d_%H_%M", gmtime())

data = commands.getoutput("w | grep load")
xml_data = ""+ data + ""

print xml_data


if os.path.isfile(file_name + ".xml"):
x=0
else:

f_prev=open(file_name + ".xml", 'a')
f_prev.write("")
f_prev.close

f=open(file_name + ".xml", 'w')
f.write("\n\n")
f.close()


f=open(file_name + ".xml", 'a')

f.write(xml_data+"\n")

f.close()
time.sleep(3)



We will get XML file like this.
<?xml version='1.0' encoding='utf-8'?>
<data>
<load> 01:40:38 up 6:27, 3 users, load average: 0.07, 0.07, 0.03</load>
<load> 01:40:48 up 6:27, 3 users, load average: 0.06, 0.07, 0.03</load>
<load> 01:40:58 up 6:27, 3 users, load average: 0.05, 0.07, 0.03</load>
</data>

1 comment:

xalabin said...

import os

print os.getloadavg()