We had some servers which would occasionally go offline, so I wrote a basic HTTP server monitor using Python and Tkinter (the interface to the Tk GUI library):
# HTTP Server Monitor by Kam-Hung Soh 2008
from csv import reader
from httplib import HTTPConnection
from logging import basicConfig, error, info, INFO
from os.path import exists
from time import strftime
from tkFont import Font
from Tkinter import Button, Frame, Label
CONFIGURATION_PATH = 'HttpServerMonitor.csv'
LOG_PATH = 'HttpServerMonitor.log'
REFRESH_INTERVAL = 60000 # Miliseconds
TIME_FORMAT = '%H:%M:%S %d-%m-%y'
GRID_DEFAULT = {'padx':2, 'pady':2}
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.status_label = {}
self.time_label = {}
self.grid(**GRID_DEFAULT)
self.create_widgets()
def create_widgets(self):
for i, s in enumerate(['Name', 'Host', 'Port', 'Status', 'Last Check']):
Label(self, font=Font(size=10, weight='bold'), text=s).grid(column=i, row=0)
if not exists(CONFIGURATION_PATH):
error("Cannot open,%s" % CONFIGURATION_PATH)
exit(1)
f = open(CONFIGURATION_PATH, "rb")
f.next() # Skip header row
for r, p in enumerate(reader(f)):
row_num = r + 1
for col_num, s in enumerate(p):
Label(self, justify='left', text="%s" % s).grid(column=col_num, row=row_num, sticky='w', **GRID_DEFAULT)
host_name, host, port = p
key = host + ":" + port
self.status_label[key] = Label(self, background='yellow', text='unknown')
self.status_label[key].grid(column=col_num + 1, row=row_num, sticky='w', **GRID_DEFAULT)
self.time_label[key] = Label(self, text='%s' % strftime(TIME_FORMAT))
self.time_label[key].grid(column=col_num + 2, row=row_num, sticky='w', **GRID_DEFAULT)
Button(self, text='Refresh', command=self.refresh).grid(column=4, sticky='e', **GRID_DEFAULT)
def refresh(self):
for key in self.status_label.keys():
self.time_label[key].config(text=strftime(TIME_FORMAT))
label = self.status_label[key]
h = HTTPConnection(key)
try:
h.connect()
label.config(background='green', text='up')
except:
label.config(background='red', text='down')
finally:
h.close()
self.after(REFRESH_INTERVAL, self.refresh)
if __name__ == "__main__":
basicConfig(
datefmt='%Y%m%d_T%H%M%S',
filemode='a',
filename=LOG_PATH,
format='%(asctime)s,%(levelname)s,%(message)s',
level=INFO
)
info('Started')
app = Application()
app.master.title('HTTP Server Monitor')
app.refresh()
app.mainloop()
info('Ended')
This program reads a CSV file specified in CONFIGURATION_PATH constant for a list of servers to monitor. The CSV file has three columns: the display name, the server address and the server's port. The first line of the CSV file is for information only; it is not used by the program. Below is a sample CSV file:
Name,Host,Port My server,myserver.com,80
You can define the time interval between checks by modifying the REFRESH_INTERVAL constant. This constant is in miliseconds, not seconds, so don't set too small a value!
If you using Windows, run it using pythonw HttpServerMonitor.py.
No comments:
Post a Comment