Simple linux resource monitor

por | 14 enero, 2013

from: http://joelslowik.blogspot.mx/2013/01/monitor-linux-resources-easy-way.html

Simple linux resource monitor

I use my Raspberry Pi  (running on raspbmc) primarily to host and serve media via NFS for yet another Pi running XBMC. I also use it as a private subversion server for all of my unfinished projects as well as a downloader for all of my legitimate downloading needs. At times, especially during the evenings, all of these services running at the same time causes some bottlenecks. At any other time I really don’t know what’s going on. Is the Pi busy? idle? To answer these questions I want to monitor my Pis resources.Now I know that there are plenty of resource monitoring software packages out in the ether already, including packages already provided within the O/S. I felt that the tools I could use either provided too much data, too complex to install (for the time I wanted to invest), or too simple of a presentation for what I’d like:

  • 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.
Most importantly? I’m setting this up for a Raspberry Pi so it wouldn’t feel right not to splash some code together myself to get what I want.

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:

  1. Clone a copy of my code from GitHub onto the server you want to monitor: https://github.com/JoelSlowik/LoadMonitor
  2. Make a copy of my google spreadsheet: https://docs.google.com/spreadsheet/ccc?key=0As-0hIWBcQmFdGZzSW1YNGdldjVybnJHYkJFeUFHRGc
    1. In the sheet go to File -> Make a Copy
  3. 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.
  4. 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.
Now everything should be ready to go. You may have to edit the ENTRIES dictionary in the event those values change. They shouldn’t change unless you edit the form but you can confirm this by going to the live form and viewing the source for each entry field. To test, run «python Main.py» in a command prompt. If everything works an entry should exist in the google spreadsheet.
When everything is confirmed to be working Main.py can then be setup to run as a cronjob; mine is setup to run every minute.
  1. Run «crontab -e» in a command prompt
  2. Paste something like this into the crontab «* * * * * python /yourScriptLocation/Main.py»
Few insights:

  1. 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.
  2. 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.
  3. 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
  4. 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.
  5. With the exception of CPU temp, all of the resources this script monitors looks at /proc/ kernel files.