#!/usr/bin/python ''' ************************************************************************ * pingtest.py - (C) 2015 James Nonnemaker - james (AT) nonnemaker.us * * This program is merely to demonstrate my python coding skills/style * It actually does function, this program will read a lisst of servers * from a MySQL server and ping each one to see if it is online. It * will create a web page with the results of the ping test. ************************************************************************ ''' sql_server = "localhost" sql_user = "sqluser" sql_pass = "p@ssword3" sql_db = "network_uptime" import MySQLdb import os import os.path import datetime if os.path.exists("/tmp/ping.html"): os.remove("/tmp/ping.html") file = open("/tmp/ping.html","w") d = datetime.datetime.now() file.write("\n") file.write("

Network Ping Monitor

\n") file.write("

Updated: %s

\n" % d) file.write("\n") db = MySQLdb.connect(sql_server,sql_user,sql_pass,sql_db) cursor = db.cursor() sql = "select server_ip,down_count from hosts" cursor.execute(sql) results = cursor.fetchall() for row in results: response = os.system("ping -c 1 " + row[0] + " > /dev/null 2>&1") if response == 0: file.write("\n") update_sql = "update hosts set down_count = '0' where server_ip = '" + row[0] + "'" cursor.execute(update_sql) db.commit() else: down_count = row[1]+1; file.write("\n") update_sql = "update hosts set down_count = down_count + 1 where server_ip = '" + row[0] + "'" cursor.execute(update_sql) db.commit() #for row file.write("
" + row[0] + "Host is up0
" + row[0] + "Host is down" + str(down_count) + "
\n\n") file.close() db.close()