Created
April 20, 2015 18:28
-
-
Save jstnlvns/e53ec5498d4388c313dc to your computer and use it in GitHub Desktop.
Service 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
#! /usr/bin/env python | |
# Tweaked version of Motoma's service monitor script. | |
# https://gist.github.com/Motoma/828694#file-service-monitor-2-py | |
from os import system | |
from urllib2 import urlopen | |
from socket import socket | |
from sys import argv | |
from time import asctime | |
import re | |
def usage(): | |
print('%s <test-type> <server-info> <email-address>\n' % (argv[0])) | |
print('\ttest-type \ttcp or http') | |
print('\tserver-info \thostname:port for tcp') | |
print('\t \thttp://hostname/page for http') | |
print('\temail-address\tusername or [email protected]\n') | |
def tcp_test(server_info): | |
cpos = server_info.find(':') | |
if cpos < 1 or cpos == len(server_info) - 1: | |
print('You need to give server info as hostname:port.') | |
usage() | |
return True | |
try: | |
si = re.split(':',server_info) | |
sock = socket() | |
sock.connect((str(si[0]), int(si[1]))) | |
sock.close | |
return True | |
except: | |
return False | |
def http_test(server_info): | |
try: | |
data = urlopen(server_info).read() | |
return True | |
except: | |
return False | |
def server_test(test_type, server_info): | |
if test_type.lower() == 'tcp': | |
return tcp_test(server_info) | |
elif test_type.lower() == 'http': | |
return http_test(server_info) | |
else: | |
print('Invalid test-type given, please use either tcp or http.') | |
def send_error(test_type, server_info, email_address): | |
subject = '%s: %s %s error' % (asctime(), test_type.upper(), server_info) | |
message = 'There was an error while executing a %s test against %s.' % (test_type.upper(), server_info) | |
system('echo "%s" | mail -s "%s" %s' % (message, subject, email_address)) | |
if __name__ == '__main__': | |
if len(argv) != 4: | |
print('Wrong number of arguments.') | |
usage() | |
elif not server_test(argv[1], argv[2]): | |
send_error(argv[1], argv[2], argv[3]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment