Skip to content

Commit dddc28c

Browse files
committed
CPU, memory and disk usage monitoring methods added
1 parent ea7b385 commit dddc28c

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,21 @@ Proactive monitoring of the system is crucial for ensuring uptime. Critial syste
1313

1414
### Core Temperature
1515

16-
Core temperature of the Raspberry Pi is monitored using ```check_temperature``` method. If the core temperature rises above 80.0'C, the method sends an alsert email.
16+
Core temperature is monitored using ```check_temperature``` method. If the core temperature rises above 80.0'C, the method sends an alert email.
17+
18+
### CPU Usage
19+
20+
CPU usage is monitored using ```check_cpu_usage``` method. If the CPU usage is over 80%, the method sends an alert email.
21+
22+
### Memory Usage
23+
24+
Memory usage is monitored using ```check_memoery_usage``` method. If the available memory is less than 500MB, the method sends an alert email.
25+
26+
### Disk Usage
27+
28+
Disk usage is monitored using ```check_disk_usage``` method. If the available memory is less than 20%, the method sends an alert email.
29+
30+
---
1731

1832
## Credits
1933

system_health_check.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import sys
44
import os
5+
import shutil
6+
import psutil
57
import emails
68
import re
79
from decouple import config
@@ -14,6 +16,20 @@ def check_temperature():
1416
temp = re.findall(r'\d*\.?\d+', measure_temp)
1517
return float(temp[0]) < 80.0
1618

19+
def check_cpu_usage():
20+
cpu_usage = psutil.cpu_percent(1)
21+
return cpu_usage < 80
22+
23+
def check_memory_usage():
24+
memoery_usage = psutil.virtual_memory().available
25+
used = memoery_usage / (1024.0 ** 2)
26+
return used > 500
27+
28+
def check_disk_usage(disk):
29+
disk_usage = shutil.disk_usage(disk)
30+
free = disk_usage.free / disk_usage.total * 100
31+
return free > 20
32+
1733
def send_alert(subject):
1834
sender = SENDER
1935
recipient = RECIPIENT
@@ -22,8 +38,18 @@ def send_alert(subject):
2238
emails.send_email(message)
2339

2440
def main(argv):
25-
subject = "Alert - The core temperature is over 80.0'C"
26-
send_alert(subject)
41+
if not check_temperature():
42+
subject = "Alert - Core temperature is over 80.0'C"
43+
send_alert(subject)
44+
if not check_cpu_usage() :
45+
subject="Alert - CPU usage is over 80%"
46+
send_alert(subject)
47+
if not check_memory_usage():
48+
subject = "Alert - Available memory is less than 500MB"
49+
send_alert(subject)
50+
if not check_disk_usage('/') :
51+
subject = "Alert - Available disk space is less than 20%"
52+
send_alert(subject)
2753

2854
if __name__ == "__main__":
2955
main(sys.argv)

0 commit comments

Comments
 (0)