from: http://joelslowik.blogspot.mx/2013/01/monitor-linux-resources-easy-way.html
Simple linux resource monitor
- Too much data in that:
- my Pi isn’t an enterprise grade server, it’s just streaming media to my A/V equipment, so I only need a high level summary of what’s going on (CPU, CPU temp, memory usage, network usage, and disk IO)
- Too complex in that:
- Such as with MRTG, I don’t care to compile dependencies specifically for my pi and ultimately adding web hosting as one of the chores of my pi (I don’t have any other server to push this task off to).
- I only wanted to spend an hour, maybe two, hashing something out to give me what I want.
- I want an extremely lean solution that will not eat up much of any resources to use.
- I want to push hosting of the data and presentation of that data off somewhere else. Not only because, again, my resource limitation of the already overburdened pi, but also because I will sometimes want to observe the data outside of my network. If I put the data elsewhere I have a lot less security to worry about (I have 2 kids that I’d rather spend time with than worrying about some hacker) hence remote storage.
- Too simple of a presentation in that:
- I just want a couple of pretty, interactive timeline graphs that present the data; Graphs not numbers.
I decided then to use Google to host my data, present it, and serve it using this very blog (see the «Raspberry Pi server stats» page to see the end result). To do all of the server-side work, I have a couple of Python scripts that utilize various /proc/ kernel files that will compile the CPU load over 5 seconds, memory utilization, disk I/O, CPU temp, and network utilization and send that data to my Google spreadsheet. That spreadsheet contains the graphs which I then publish on this blog. To be really quick about it i’m not using Googles spreadsheet API, rather I am using a form for the spreadsheet and just opening the URL in Python with the required strings.
In short this was an exercise to getting the most value from the least amount of work (I spent more time writing this article and looking back at my code for this than doing any actual coding)
I believe that due to the CPU Temperature monitoring, this package of scripts will be limited to the Raspberry Pi because I did not implement any function to prevent that code from firing if the required module was missing.
To set this up do the following:
- Clone a copy of my code from GitHub onto the server you want to monitor: https://github.com/JoelSlowik/LoadMonitor
- Make a copy of my google spreadsheet: https://docs.google.com/spreadsheet/ccc?key=0As-0hIWBcQmFdGZzSW1YNGdldjVybnJHYkJFeUFHRGc
- In the sheet go to File -> Make a Copy
- In your copy of the spreadsheet, go to «Form -> Go to live form». When you get to the next page, copy the form key. The form key is the obscure text after «formkey=» up to the hash («#») symbol.
- From your cloned copy of my GitHub project, edit the python script GoogleSheet.py from the clone in step 1 and paste the key copied in step 4 into the value FORMKEY.
- Run «crontab -e» in a command prompt
- Paste something like this into the crontab «* * * * * python /yourScriptLocation/Main.py»
- Disk I/O only monitors the «sda1» drive. If that drive doesn’t exist the script will report «0» every time it runs. This code also assumes each sector is 512 bytes.
- CPU Temp can be configured for Celsius, Fahrenheit, and Kelvin (for fun). The script is using Fahrenheit by default because I’m a lazy American who can’t be bothered memorizing Celsius conversion.
- If this package of scripts are ever intended to run on a machine that is not a Raspberry Pi, the CPU Temp code should be ignored
- The network utilization code only monitors the «eth0» network interface. If that interface does not exist or is not used, a «0» will be reported every time the script runs.
- With the exception of CPU temp, all of the resources this script monitors looks at /proc/ kernel files.