Smart pinging tool for windows(with coloring,email,and alerting)
One of the routine activities that I am dealing with is finding better ways to monitor critical servers and services, and helping NOC operators to have better tools for their tasks which is mainly watching over platforms that are critical for my company and me as well.
part of monitoring job is to see whether a server is accessible in a network or not, we have a lot of tools to fulfill this need but as an administrator I have to say pinging is something else.
putting aside all the monitoring tools that I operated in our NOC(Network Operation center) department I wanted to keep this beautiful and somehow traditional administration command called 'ping' in the game, so what I did was to come up with a little bit more sophisticated tool which I call py-ping.
It is not a big thing for sure, It is a python 3x script which is going to do the following:
Asking about the Email you want to send errors and IP that you want to ping or actually monitor
if you dont want to send emails to anywhere, simply ignore email part and press enter
When you put the IP address it is going to show you the result as following:
Green lines if your device is accesibale
red lines if your server is not accessible in any ways
so far it is fine but adding two colors is not worth the trouble, right?
that is why I added another feature which almost every monitoring toll needs
Alerting
the way I managed alerting is that you only need to put a sound file named 'alert.mp3' in the same directory as py-ping.exe file( you can choose your own sound, I put one for you just in case), by default the sound will play for 5 seconds but you can change it as you like in the code.
You can download different warnings for as your alert.mp3 here
Emailing
As I figured it out during these years emailing is always a good feature for a monitoring tool, so I tried to bring it to my little and simple ping tool too. at the beginning you will be asked to put an email address as a destination, if you leave it blank it would not be a problem , but if you put your email address there, you will get an email every time your server goes to critical condition
Downloading
you can download the tool from dropbox here
you just need to unzip it and run the py-ping.exe file
and here you can see the code itself, feel free to edit it anyway and share it anywhere you like(I appreciate it if you mention me as well)
import os
import sys
import time
import signal
import smtplib
from termcolor import colored, cprint
from colorama import init
init()
###Email you want to use as your email address
FROM = "[email protected]"
### YOU CAN SET DESTINATIONS FOR SENDING EMAIL TO
TO = [""]
TO[0] = input('Send Email to: ')
###SMTP SERVER
SERVER = "192.168.45.12"
IP = input('IP Address: ')
### title of email
SUBJECT = 'packet lost on ' + IP
PING = 'ping ' + IP + ' -t'
p = os.popen(PING,"r")
### Variable to keep the track of OKs after timeout
got_reply = 0
### Timeout Counter
time_out_counter = 0
### Signal Handler for CRTL + C
def signal_handler(signal, frame):
while True:
print()
print("Thank you guys for using this tool")
print()
print("@meysam")
print("steemit.com")
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
###Function to define green color
def green_color(a):
cprint(a, 'green', attrs=['bold'], file=sys.stderr )
###Function to define red color
def red_color(a):
cprint(a, 'red', attrs=['bold'], file=sys.stderr)
while 1:
line = p.readline()
line = line.rstrip()
### DETECTING NOT OKs
if 'TTL' not in line:
if('Pinging') in line:
print(line)
else:
red_color(line)
time_out_counter += 1
### AFTER GETTING 4 TIMEOUT THE WARNING STARTS FOR 5 SECONDS
if time_out_counter == 4:
os.system('alert.mp3')
### TIME THAT YOU LIKE YOUR WARNING KEEPS GOING
time.sleep(5)
###KILLING THE WMPLAYER
os.system('Taskkill /IM wmplayer.exe /F > null')
### Handle the error if SMTP server is not accessibale
try:
### Emailing
TEXT = 'the server is down'
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
# Send the mail
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()
### RESETING OKs TO ZERO
except:
ignore = 1
if got_reply >= 3:
got_reply = 0
### DETECTING OKs
elif 'TTL' in line:
green_color(line)
got_reply += 1
### IF YOU GET MORE THAN 2 OKs, TIMEOUT COUNTER WILL RESET TO ZERO
if got_reply >= 2:
time_out_counter = 0
### @meysam
note that I didn't do much debugging and error handling, if you find some error I would be happy to hear about it and will try to fix it
This post is 100% powered up
Sun Jan 29 11:01:13 IST 2017
This post has been ranked within the top 80 most undervalued posts in the first half of Jan 29. We estimate that this post is undervalued by $3.09 as compared to a scenario in which every voter had an equal say.
See the full rankings and details in The Daily Tribune: Jan 29 - Part I. You can also read about some of our methodology, data analysis and technical details in our initial post.
If you are the author and would prefer not to receive these comments, simply reply "Stop" to this comment.
thanks