Sunday 23 December 2012

Tracking our Pi System status in Cosm

Cosm.com is a service that allows you to push data to it or it retrieves data from a device/app/cat and will make some incredible nice graphs with it:




They have a nice API and they accept XML, JSON and CSV to update out data.

So it's a nice platform to control and build graphs for our Raspberry Pi system. With it we can control and track how our system behaves along time without accesing the device.


And with a few lines of python, we have a nice script which will upload cpu(in percentage), free hdd space (rootfs), free and used memory. You can see a couple of examples in the image attached.


Here is the code:
 # cosm.py Copyrigth 2012 Itxaka Serrano Garcia <itxakaserrano@gmail.com>  
 # licensed under the GPL2  
 # see the full license at http://www.gnu.org/licenses/gpl-2.0.txt  
 #  
 # You only need to add 2 things, YOUR_API KEY HERE and YOUR_FEED_NUMBER_HERE  
 # also, you can change your stream ids, in that case change the id names in the "data = json.dumps..." line  
   
 import json, subprocess, os  
   
 hdd = subprocess.check_output(["df | grep rootfs | awk '{print $2,$4,$5}'"], shell=True)  
 hdd = hdd.split()  
 hdd = int(hdd[1]) / 1024  
   
 cpu = subprocess.check_output(["vmstat | awk '{print $13}'"], shell=True)  
 cpu = cpu.split()[1]  
   
 mem = subprocess.check_output(["cat /proc/meminfo | grep Mem | awk '{print $2}'"], shell=True)  
 mem = mem.split()  
 mem_total = int(mem[0]) / 1024  
 mem_free = int(mem[1]) / 1024  
 mem_used = mem_total - mem_free  
   
 data = json.dumps({"version":"1.0.0", "datastreams":[{"id":"hdd","current_value":hdd },{"id":"cpu","current_value":cpu},{"id":"free_mem","current_value":mem_free},{"id":"used_mem","current_value":mem_used}]})  
 with open("temp.tmp", "w") as f:  
     f.write(data)  
   
 subprocess.call(['curl --request PUT --data-binary @temp.tmp --header "X-ApiKey: YOUR_IP_KEY_HERE" http://api.cosm.com/v2/feeds/YOUR_FEED_NUMBER_HERE'], shell=True)  
   
 os.remove("temp.tmp")  




We can get this working in 4 steps.

1 - Copy the code and save it to a file in your Pi under whatever name you want (We will use cosm.py in this example) and store it whenever you want, I usually put them in /home/pi/scripts/

2 - Add in the code your API key for cosm and your Feed name (Instructions on how to get an api key are in the cosm.com page)

3 - Add a new entry in your crontab to run it every X minutes. In my case I run it every 10 minutes so my cron line is
 */10 * * * * python /home/pi/scripts/cosm.py  

4 - Visit your cosm.com feed page and wait for the results

There! That was easy wasn't it?

Be sure to check my feed


8 comments:

  1. Good tutorial!
    is there a way to also display the temperature from /opt/vc/bin/vcgencmd measure_temp ?

    ReplyDelete
    Replies
    1. Sure! Let me check this afternoon and I'll add it.

      Delete
    2. A quick solution for SoC temp:

      cpu_temp = subprocess.check_output(["sudo /opt/vc/bin/vcgencmd measure_temp | cut -c6-9"], shell=True)

      Then add {"id":"cpu_temp","current_value":cpu_temp} to data and voila :)

      Delete
  2. I think the memory test cat /proc/meminfo is not really useful because count also cache used.
    Maybe better use free method ?

    ReplyDelete
  3. i change memory count, removed all lines with mem count and added:

    mem_free = subprocess.check_output(["free -m |grep + | awk '{print $4}'"], shell=True)
    mem_used = subprocess.check_output(["free -m |grep + | awk '{print $3}'"], shell=True)


    what did you think ?

    ReplyDelete
    Replies
    1. Much better than the solution in the original code!
      Just take a look at "www.linuxatemyram.com" to see why... :)

      I also altered a little on the HDD-part, getting both free and used on SD-card AND my USB-drive.

      And just to monitor uptime, I added that as well.
      I should give a nice linear graph - if it get som horizontal lines, it means the RPi has been offline (unable to post to COSM) and if the line drops to zero, the RPi has rebooted. :)

      # cosm.py Copyrigth 2012 Itxaka Serrano Garcia
      # licensed under the GPL2
      # see the full license at http://www.gnu.org/licenses/gpl-2.0.txt
      #
      # You only need to add 2 things, YOUR_API KEY HERE and YOUR_FEED_NUMBER_HERE
      # also, you can change your stream ids, in that case change the id names in the "data = json.dumps..." line

      import json, subprocess, os

      hdd = subprocess.check_output(["df | grep rootfs | awk '{print $2,$4,$5}'"], shell=True)
      hdd = hdd.split()
      hdd = int(hdd[1]) / 1024

      hdd_ext_tmp = subprocess.check_output(["df | grep /dev/sda1 | awk '{print $2,$3,$4,$5}'"], shell=True)
      hdd_ext_tmp = hdd_ext_tmp.split()
      hdd_ext = int(hdd_ext_tmp[2]) / 1024 / 1024
      hdd_ext_used = int(hdd_ext_tmp[1]) / 1024 / 1024

      cpu = subprocess.check_output(["vmstat | awk '{print $13}'"], shell=True)
      cpu = cpu.split()[1]

      mem_free = subprocess.check_output(["free -m |grep + | awk '{print $4}'"], shell=True)
      mem_used = subprocess.check_output(["free -m |grep + | awk '{print $3}'"], shell=True)

      cpu_temp = subprocess.check_output(["sudo /opt/vc/bin/vcgencmd measure_temp | cut -c6-9"], shell=True)

      with open('/proc/uptime', 'r') as f:
      uptime = round(float(f.readline().split()[0])/60/60,2)

      data = json.dumps({"version":"1.0.0", "datastreams":[{"id":"uptime","current_value":uptime },{"id":"hdd_usb_used","current_value":hdd_ext_used },{"id":"hdd_$
      with open("temp.tmp", "w") as f:
      f.write(data)

      Delete