· 5 years ago · Jan 28, 2021, 04:24 AM
1import urllib.request
2import os
3import subprocess
4
5#
6# Install python3 if it isn't already
7#
8# Adjust python path above accordingly (CLI: which python -or- which python3 -or- which python3.8, etc)
9#
10# Install packages
11# apt install smartmontools
12# apt install lm-sensors
13#
14# Add a cron to run it when desired (Below is every 30 minutes)
15# User who runs the cron needs sudo ability
16# */30 * * * * python3 /path/to/discordnotifier_snapshot_linux.py
17#
18# Turn off what you dont want below, change 1 to 0
19#
20
21Settings = configparser.ConfigParser()
22Settings.read(os.path.join(os.path.dirname(__file__), 'discordnotifier.conf'))
23
24if (not Settings['Settings']['api']):
25 print('Notifier api key missing')
26 exit()
27
28hostname = user = monitorSpace = sensors = mdStat = sdaHours = sdbHours = sdcHours = sdaTemp = sdbTemp = sdcTemp = uptime = top = zfs = ''
29
30# uptime
31if (Settings['Settings']['monitor_uptime'] == 1):
32 process = subprocess.Popen('sudo uptime', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
33 for line in process.stdout:
34 uptime += line.decode();
35
36# machine name
37process = subprocess.Popen('hostname', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
38for line in process.stdout:
39 hostname += line.decode();
40
41# user running the script
42process = subprocess.Popen('whoami', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
43for line in process.stdout:
44 user += line.decode();
45
46# cpu temp
47if (Settings['Settings']['monitor_cpuTemp'] == 1):
48 process = subprocess.Popen('sudo sensors', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
49 for line in process.stdout:
50 sensors += line.decode();
51
52# raid status
53if (Settings['Settings']['monitor_raid'] == 1):
54 process = subprocess.Popen('cat /proc/mdstat', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
55 for line in process.stdout:
56 mdStat += line.decode();
57
58# drive temps
59if (Settings['Settings']['monitor_driveTemp'] == 1):
60 process = subprocess.Popen('sudo smartctl -A /dev/sda | grep Temperature_Celsius', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
61 for line in process.stdout:
62 sdaTemp += line.decode();
63 process = subprocess.Popen('sudo smartctl -A /dev/sdb | grep Temperature_Celsius', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
64 for line in process.stdout:
65 sdbTemp += line.decode();
66 process = subprocess.Popen('sudo smartctl -A /dev/sdc | grep Temperature_Celsius', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
67 for line in process.stdout:
68 sdcTemp += line.decode();
69
70# drive age
71if (Settings['Settings']['monitor_driveAge'] == 1):
72 process = subprocess.Popen('sudo smartctl -A /dev/sda | grep Power_On_Hours', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
73 for line in process.stdout:
74 sdaHours += line.decode();
75 process = subprocess.Popen('sudo smartctl -A /dev/sdb | grep Power_On_Hours', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
76 for line in process.stdout:
77 sdbHours += line.decode();
78 process = subprocess.Popen('sudo smartctl -A /dev/sdc | grep Power_On_Hours', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
79 for line in process.stdout:
80 sdcHours += line.decode();
81
82# space usage
83if (Settings['Settings']['monitor_driveTemp'] == 1):
84 process = subprocess.Popen('df -H', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
85 for line in process.stdout:
86 monitorSpace += line.decode();
87
88# cpu/ram data
89if (Settings['Settings']['monitor_cpuMemory'] == 1):
90 process = subprocess.Popen('top -n 1 -b | awk \'/^%Cpu/{print $2}\' && top -n 1 -b | awk \'/used/ && /buff/ {print $8}{print $1}\'', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
91 for line in process.stdout:
92 top += line.decode();
93
94# space usage
95if (Settings['Settings']['zfs_pool'] == 1):
96 process = subprocess.Popen('zfs list', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
97 for line in process.stdout:
98 zfs += line.decode();
99
100# push out the notification
101postData = urllib.parse.urlencode({'api': Settings['Settings']['api'], 'discordnotifier_eventtype': 'snapshot', 'top': top, 'hostname': hostname, 'user': user, 'monitorSpace': monitorSpace, 'sensors': sensors, 'mdStat': mdStat, 'sdaHours': sdaHours, 'sdbHours': sdbHours, 'sdcHours': sdcHours, 'sdaTemp': sdaTemp, 'sdbTemp': sdbTemp, 'sdcTemp': sdcTemp, 'uptime': uptime, 'zfsPool': zfs, 'zfsPoolName': Settings['Settings']['zfs_pool_name']})
102url = "https://discordnotifier.com/notifier.php"
103try:
104 req = urllib.request.Request(url, postData.encode('ascii'))
105 urllib.request.urlopen(req)
106except OSError:
107 print('\tError - Invalid notifier api key')
108 print('\tFailure')
109except ConnectionError:
110 print('\tError - Invalid notifier api key')
111 print('\tFailure')
112except requests.exceptions.HTTPError as e:
113 print('\tError - '+ e)
114 print('\tFailure')