DerEddy

cosm.py

Jan 9th, 2013
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.82 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # cosm.py Copyrigth 2012 Itxaka Serrano Garcia <[email protected]>  
  3. # licensed under the GPL2  
  4. # see the full license at http://www.gnu.org/licenses/gpl-2.0.txt  
  5. # edited by Der-Eddy from elitepvpers
  6.  
  7. import json, subprocess, os, time
  8.  
  9. #add your privat stuff here
  10. feed = "your feed"
  11. apikey = "your api key"
  12.  
  13. hdd = subprocess.check_output(["df | grep rootfs | awk '{print $2,$4,$5}'"], shell=True)
  14. hdd = hdd.split()
  15. hdd = int(hdd[1]) / 1024
  16.  
  17. cpu = subprocess.check_output(["vmstat | awk '{print $13}'"], shell=True)
  18. cpu = cpu.split()[1]
  19.  
  20. cpu_temp = subprocess.check_output(["sudo /opt/vc/bin/vcgencmd measure_temp | cut -c6-9"], shell=True)
  21.  
  22. mem = subprocess.check_output(["cat /proc/meminfo | grep Mem | awk '{print $2}'"], shell=True)
  23. mem = mem.split()
  24. mem_total = int(mem[0]) / 1024
  25. mem_free = int(mem[1]) / 1024
  26. mem_used = mem_total - mem_free
  27.  
  28. mhz = float(subprocess.check_output(["cat /proc/cpuinfo | grep BogoMIPS | awk '{print $3}'"], shell=True))
  29. mhz = int(round(mhz, -2))
  30.  
  31. up = float(subprocess.check_output(["cat /proc/uptime | cut -d'.' -f1-1"], shell=True))
  32. up = round(up / 60 / 60, 3)
  33.  
  34. ps = subprocess.check_output(["ps -A | awk '{print $1}'"], shell=True)
  35. ps = len(ps) - 1
  36.  
  37. 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},{"id":"cpu_temp","current_value":cpu_temp},{"id":"cpu_frequency","current_value":mhz},{"id":"uptime","current_value":up},{"id":"processes","current_value":ps}]})
  38. with open("temp.json", "w") as f:
  39.     f.write(data)
  40. subprocess.call(['curl --request PUT --data-binary @temp.json --header "X-ApiKey: {0}" --verbose http://api.cosm.com/v2/feeds/{1}'.format(apikey, feed)], shell=True)
  41.  
  42. os.remove("temp.json")
Advertisement
Add Comment
Please, Sign In to add comment