SHOW:
|
|
- or go back to the newest paste.
| 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 |
| 7 | + | import json, subprocess, os, time, re |
| 8 | ||
| 9 | #add your privat stuff here | |
| 10 | - | feed = "FEED" |
| 10 | + | feed = "YOUR_FEED" |
| 11 | - | apikey = "API_KEY" |
| 11 | + | apikey = "YOUR_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 = subprocess.check_output(["vcgencmd measure_clock arm | cut -d'=' -f2-"], shell=True) | |
| 29 | mhz = int(mhz) / 1000000 | |
| 30 | mhz = int(round(mhz, -2)) | |
| 31 | ||
| 32 | volts = subprocess.check_output(["vcgencmd measure_volts | cut -c6-9"], shell=True) | |
| 33 | ||
| 34 | up = float(subprocess.check_output(["cat /proc/uptime | cut -d'.' -f1-1"], shell=True)) | |
| 35 | up = round(up / 60 / 60, 3) | |
| 36 | ||
| 37 | ps = subprocess.check_output(["ps -A | awk '{print $1}'"], shell=True)
| |
| 38 | ps = len(ps) - 1 | |
| 39 | ||
| 40 | - | 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},{"id":"volts_core","current_value":volts},]})
|
| 40 | + | net = subprocess.check_output(["ifconfig eth0 | grep RX\ bytes"], shell=True) |
| 41 | rx_bytes = re.findall('RX bytes:([0-9]*) ', net)[0]
| |
| 42 | tx_bytes = re.findall('TX bytes:([0-9]*) ', net)[0]
| |
| 43 | rx_bytes = round(float(rx_bytes) / 1024, 1) | |
| 44 | tx_bytes = round(float(tx_bytes) / 1024, 1) | |
| 45 | total_bytes = rx_bytes + tx_bytes | |
| 46 | ||
| 47 | 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},{"id":"volts_core","current_value":volts},{"id":"network_receive","current_value":rx_bytes},{"id":"network_send","current_value":tx_bytes},{"id":"network_total","current_value":total_bytes},]})
| |
| 48 | with open("temp.json", "w") as f:
| |
| 49 | f.write(data) | |
| 50 | 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)
| |
| 51 | ||
| 52 | os.remove("temp.json") |