Created
January 29, 2018 09:11
-
-
Save jamil666/9c37de631b80d55bcb8e6dac4158af80 to your computer and use it in GitHub Desktop.
Windows_WMI_Monitoring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import wmi | |
from socket import * | |
import smtplib | |
from email.mime.text import MIMEText | |
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 = "Remote server IP" | |
username = "remote server login" | |
password = "remote server password" | |
Disk_Space_Threshold = 10 # Threshold value for alarm in GB | |
Memory_Threshold = 250 # Threshold value for alarm in MB | |
CPU_Threshold = 80 # Threshold value for alarm in % | |
try: | |
print ("Establishing connection to %s" %ip) | |
connection = wmi.WMI(ip, user=username, password=password) # Connecting to server | |
print ("Connection established\n") | |
print("\nServer Name:\t\t\t\t%s" %getfqdn(ip)) # Print Server Name | |
for os in connection.Win32_OperatingSystem(): # Print Operating System | |
print("Operating System:\t\t\t%s" %os.Caption) | |
for Memory in connection.Win32_ComputerSystem(): # Print total installed memory | |
Total_Memory = int(Memory.TotalPhysicalMemory) / 1024 / 1024 | |
print("------------------------- Memory -------------------------") | |
print("Total Memory:\t\t\t\t" + round(Total_Memory).__str__() + " MB") | |
for os in connection.Win32_OperatingSystem(): # Print free memory | |
Free_Memory = int(os.FreePhysicalMemory) / 1024 | |
print("Free Memory:\t\t\t\t" + round(Free_Memory).__str__() + " MB") | |
# If free memory less than threshold value print alarm and send email | |
if Free_Memory < Memory_Threshold: | |
print("Free Memory Alarm!") | |
SendMail("You have just %d MB free memory on server %s \n" | |
"\nPlease keep under control!" % (Free_Memory, getfqdn(ip))) | |
print("\n------------------------- CPU -------------------------") | |
for CPU in connection.Win32_Processor(): # Print CPU load | |
print("CPU load: %d%s" % (CPU.LoadPercentage, "%")) | |
if CPU.LoadPercentage > 30: | |
SendMail("Your CPU load is %d %s on server %s \n" | |
"\nPlease keep under control!" % (CPU.LoadPercentage, "%", getfqdn(ip))) | |
break | |
for Disk in connection.Win32_LogicalDisk(): | |
Free_Space = int(Disk.FreeSpace) / 1024 / 1024 / 1024 # Free disk space in GB | |
Disk_Size = int(Disk.Size) / 1024 / 1024 / 1024 # Total disk space in GB | |
print("------------------------- Disk -------------------------") | |
print("Disk Drive:\t\t\t\t\t" + Disk.Caption) | |
print("Disk Size:\t\t\t\t\t" + round(Disk_Size, 2).__str__() + " GB") | |
print("Free Space:\t\t\t\t\t" + round(Free_Space, 2).__str__() + " GB") | |
print("--------------------------------------------------------") | |
# If free disk space less than threshold value print alarm and send email | |
if Free_Space < Disk_Space_Threshold: | |
print("Free Disk space ALARM !!!") | |
SendMail("You have just %d GB free disk space on server %s \n" | |
"\nPlease keep under control!" % (Free_Space, getfqdn(ip))) | |
except wmi.x_wmi: | |
print ("Your Username and Password of " + getfqdn(ip) + " are wrong.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script can monitor remote windows server via WMI