Skip to content

Instantly share code, notes, and snippets.

@jamil666
Last active September 30, 2023 07:42
Show Gist options
  • Save jamil666/3279cee242ac37fcd8b641e34132d33f to your computer and use it in GitHub Desktop.
Save jamil666/3279cee242ac37fcd8b641e34132d33f to your computer and use it in GitHub Desktop.
CPU_RAM_Disk_Monitoring
import psutil
import smtplib
from email.mime.text import MIMEText
from socket import *
def SendMail(alert):
# Define to/from
sender = 'Your sender email'
recipient = 'Your recipient email'
# Create message
msg = MIMEText(alert)
msg['Subject'] = "Server resources alarm!"
msg['From'] = sender
msg['To'] = recipient
# Create server object with SSL option
server = smtplib.SMTP_SSL('Your mail server', 465)
# Perform operations via server
server.login('Login', 'Password')
server.sendmail(sender, [recipient], msg.as_string())
server.quit()
IP = '127.0.0.1'
Disk_Space_Threshold = 80 # Threshold value for alarm in GB
Memory_Threshold = 80 # Threshold value for alarm in %
CPU_Threshold = 80 # Threshold value for alarm in %
# CPU Load info
print("--------------------------- CPU Info ----------------------------")
print("CPU count: %d" %psutil.cpu_count())
print("CPU load: %d%s" % (psutil.cpu_percent(), "%"))
print("-----------------------------------------------------------------")
# If CPU usage more than 80%, send alarm email
if psutil.cpu_percent() > CPU_Threshold:
print("CPU load: %d%s" % (psutil.cpu_percent(), "%"))
try:
SendMail("Your CPU load is %d %s on server %s \n"
"\nPlease keep under control!" % (psutil.cpu_percent(), "%", getfqdn(IP)))
except:
print("Cannot send email!!!!")
# Memory Load info
print("-------------------------- Memory Info ---------------------------")
print("Total memory:", round(psutil.virtual_memory()[0] / (1024 * 1024 * 1024), 2), "GB")
print("Used memory:", round(psutil.virtual_memory()[3] / (1024 * 1024 * 1024), 2), "GB" )
print("Free memory:", round(psutil.virtual_memory()[4] / (1024 * 1024 * 1024), 2), "GB")
print("Memory load: %d%s" % (psutil.virtual_memory()[2], "%"))
print("------------------------------------------------------------------")
# If Memory usage mote than 80%, send alarm email
if psutil.virtual_memory()[2] > Memory_Threshold:
print("Memory load: %d%s" % (psutil.virtual_memory()[2], "%"))
try:
SendMail("You have just %d GB free memory on server %s \n"
"\nPlease keep under control!" % (round(psutil.virtual_memory()[4] / (1024 * 1024 * 1024), 2), getfqdn(IP)))
except:
print("Cannot send email!!!!")
# Disk Load Info
print("--------------------------- Disk Info ----------------------------")
print("Available volumes:")
for Disk in psutil.disk_partitions():
print(Disk[0], "\tFile system:", Disk[2],
"\t\tTotal Space:", round(psutil.disk_usage(Disk[0]).total / (1024*1024*1024)), "GB",
"\t\t\tUsed Space:", round(psutil.disk_usage(Disk[0]).used / (1024*1024*1024)), "GB (%d%s)" %(psutil.disk_usage(Disk[0]).percent, "%"),
"\t\t\tFree Space:", round(psutil.disk_usage(Disk[0]).free / (1024 * 1024 * 1024)), "GB")
if psutil.disk_usage(Disk[0]).percent > Disk_Space_Threshold:
print("\nFree Disk space ALARM !!! You have just %d GB free disk space on server %s on drive %s \n"
"\nPlease keep under control!" % (round(psutil.disk_usage(Disk[0]).free / (1024 * 1024 * 1024)), getfqdn(IP), Disk[0]))
try:
SendMail("You have just %d GB free disk space on server %s on drive %s \n"
"\nPlease keep under control!" % (round(psutil.disk_usage(Disk[0]).free / (1024 * 1024 * 1024)), getfqdn(IP), Disk[0]))
except:
print("Cannot send email!!!!")
print("\n----------------------- Monitoring check completed ------------------------------")
@jamil666
Copy link
Author

This script can monitor Linux and Windows operating systems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment