forked from mvanbreeden/TweetRPiStatus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThingspeakRPiStatus.sh
executable file
·92 lines (79 loc) · 2.84 KB
/
ThingspeakRPiStatus.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
import sys
import os
import httplib, urllib
import ConfigParser
os.environ['TERM'] = "bash"
# Return CPU temperature as a float
def getCPUtemperature():
# try:
# s = subprocess.check_output(["/opt/vc/bin/vcgencmd","measure_temp"], shell=False)
# return float(s.split('=')[1][:-3])
# except:
# return 0
if os.path.isfile('/opt/vc/bin/vcgencmd'):
res = os.popen('/opt/vc/bin/vcgencmd measure_temp').readline()
return(res.replace("temp=","").replace("'C\n",""))
else:
return 'N/A'
# Return RAM information (unit=kb) in a list
# Index 0: total RAM
# Index 1: used RAM
# Index 2: free RAM
def getRAMinfo():
p = os.popen('free')
i = 0
while 1:
i = i + 1
line = p.readline()
if i==3:
return(line.split()[1:4])
# Return % of CPU used by user as a character string
def getCPUuse():
# Return the inverse of CPU idle, instead of part of usage
cpuUsePrc=str(100-float(os.popen("top -n1 -b | awk '/Cpu\(s\):/ {print $8}'").readline().strip()))
return(cpuUsePrc)
# Return information about disk space as a list (unit included)
# Index 0: total disk space
# Index 1: used disk space
# Index 2: remaining disk space
# Index 3: percentage of disk used
def getDiskSpace():
p = os.popen("df /")
line = p.readline()
line = p.readline()
return(line.split()[1:5])
#Main program
if __name__ == "__main__":
ramInfo = getRAMinfo()
usedMem = int(ramInfo[1])
freeMem = int(ramInfo[2])
#calculate the free memory percentage
freeMemPerc = int((float(freeMem)/(usedMem+freeMem))*100)
#get hostname
hostname = os.popen('hostname').readline().rstrip()
#build the info string
sysInfoString = hostname + ": CPUTemp " + getCPUtemperature() + ", CPU U " + getCPUuse()+ "%, FreeDisk " + getDiskSpace()[2] + ", FreeMemPrc " + str(freeMemPerc) + "%"
print(sysInfoString)
#read config file for twitter keys (so not to include them on github)
try:
config = ConfigParser.ConfigParser()
if config.read(os.path.dirname(os.path.realpath(__file__)) + "/rpi_status.conf"):
api_key = config.get('ThingspeakAccount', 'api_key')
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
params = urllib.urlencode({
'field1': getCPUtemperature(),
'field2': getCPUuse(),
'field3': getDiskSpace()[2],
'field4': str(freeMemPerc),
'key': api_key
})
conn = httplib.HTTPConnection("api.thingspeak.com:80")
conn.request("POST", "/update", params, headers)
response = conn.getresponse()
conn.close()
else:
print('Error reading config file')
except Exception:
print(Exception)
exit()