SHARE
TWEET

cosm.py

itxakapi Dec 22nd, 2012 408 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # cosm.py Copyrigth 2012 Itxaka Serrano Garcia <itxakaserrano@gmail.com>
  2. # licensed under the GPL2
  3. # see the full license at http://www.gnu.org/licenses/gpl-2.0.txt
  4. #
  5. # You only need to add 2 things, YOUR_API KEY HERE and YOUR_FEED_NUMBER_HERE
  6. # also, you can change your stream ids, in that case change the id names in the "data = json.dumps..." line
  7.  
  8. import json, subprocess, os
  9.  
  10. hdd = subprocess.check_output(["df | grep rootfs | awk '{print $2,$4,$5}'"], shell=True)
  11. hdd = hdd.split()
  12. hdd = int(hdd[1]) / 1024
  13.  
  14. cpu = subprocess.check_output(["vmstat | awk '{print $13}'"], shell=True)
  15. cpu = cpu.split()[1]
  16.  
  17. mem = subprocess.check_output(["cat /proc/meminfo | grep Mem | awk '{print $2}'"], shell=True)
  18. mem = mem.split()
  19. mem_total = int(mem[0]) / 1024
  20. mem_free = int(mem[1]) / 1024
  21. mem_used = mem_total - mem_free
  22.  
  23. 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}]})
  24. with open("temp.tmp", "w") as f:
  25.         f.write(data)
  26.  
  27. 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)
  28.  
  29. os.remove("temp.tmp")
RAW Paste Data
Want to get better at Python?
Learn to code Python in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top