forked from mvanbreeden/TweetRPiStatus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPushRPiStatus.py
executable file
·85 lines (74 loc) · 2.47 KB
/
PushRPiStatus.py
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
#!/usr/bin/env python
import sys
import os
from pushbullet import Pushbullet
import ConfigParser
os.environ['TERM'] = "xterm"
# 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 -h /")
i = 0
while 1:
i = i +1
line = p.readline()
if i==2:
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('./rpi_status.conf'):
api_key = config.get('PushbulletAccount', 'api_key')
print(api_key)
# push the sys info
pb = Pushbullet(api_key=api_key)
push = pb.push_note("RPi Status", sysInfoString)
else:
print('Error reading config file')
except Exception:
print(Exception)
exit()