From b88a2b0c18b94cc700f02fa85551429de82a78a7 Mon Sep 17 00:00:00 2001 From: andreas Date: Wed, 8 Nov 2006 13:50:28 +0000 Subject: [PATCH] check state of internet connection --- selfcheck | 265 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 265 insertions(+) create mode 100755 selfcheck diff --git a/selfcheck b/selfcheck new file mode 100755 index 0000000..29a79c2 --- /dev/null +++ b/selfcheck @@ -0,0 +1,265 @@ +#!/usr/bin/env python +# +RCSID="$Id: selfcheck,v 1.1 2006/11/08 13:50:28 andreas Exp $" +# check internet connectivity +# + +import os, sys, string, time, socket + +PPPIF="pppoe0" +DBG=0 + +ADDR="204.29.161.33" +PORT=50003 + +def sendheartbeatmsg(tosend): + iam=socket.gethostname() + sock=socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + msgboot="msg=%s;service=%s;name=%s" % (tosend, "selfcheck", iam) + sock.sendto(msgboot, (ADDR, PORT)) + time.sleep(1) + sock.close() + +class Pppoe: + def __init__(self, interface): + DBG=0 + self.interface=interface + self.foundauthinfo=0 + + fh=os.popen("/sbin/pppoectl %s" % self.interface,"r") + while 1: + l=fh.readline() + if len(l) == 0: + fh.close() + break + if DBG: print l[:-1] + r=string.split(l[:-1]) + if DBG: print r + if r[0] == self.interface+':': + s=string.split(r[1],'=') + self.phase=s[1] + elif r[0][:11] == 'myauthproto': + self.myauthproto=r[0][12:] + self.myauthname=r[1][11:] + self.foundauthinfo=1 + elif r[0] == 'lcp' and r[1] == 'timeout:': + self.lcptimeount=r[2] + elif r[0] == 'idle' and r[1] == 'timeout' and r[2] == "=": + self.idletimeout=r[3] + elif r[0] == 'max-auth-failure' and r[1] == '=': + self.maxauthfailure=r[2] + elif r[0] == 'max-noreceive' and r[1] == '=': + self.maxnoreceive=string.join(r[2:], ' ') + elif r[0] == 'max-alive-missed' and r[1] == '=': + self.maxalivemissed = string.join(r[2:], ' ') + + fh=os.popen("/sbin/pppoectl -d %s" % self.interface,"r") + while 1: + l=fh.readline() + if len(l) == 0: + fh.close() + break + if DBG: print l[:-1] + r=string.split(l[:-1]) + if DBG: print r + if r[0] == self.interface+':': + self.state=string.join(r[3:],' ') + elif r[0] == 'Session' and r[1] == 'ID:': + self.sessionid=r[2] + elif r[0] == 'PADI' and r[1] == 'retries:': + self.PADIretries=r[2] + elif r[0] == 'PADR' and r[1] == 'retries:': + self.PADRretries=r[2] + + + fh=os.popen("/sbin/ifconfig %s" % self.interface,"r") + if DBG: print fh + self.foundinet=0 + while 1: + l=fh.readline() + if len(l) == 0: + fh.close() + break + if DBG: print l[:-1] + r=string.split(l[:-1]) + if DBG: print r + if r[0] == self.interface+':': + s=string.split(r[1],'=') + s1=string.split(s[1],'<') + self.flagshex=s1[0] + self.flags=string.split(s1[1][:-1],',') + self.mtu=r[3] + elif r[0] == 'inet': + self.foundinet=1 + self.ipaddr=r[1] + self.ipgw=r[3] + self.ipmask=r[5] + + + def __repr__(self): + r=[] + keys=self.__dict__.keys() + keys.sort() + for k in keys: + r.append("%-14s: %s" % (k, self.__dict__[k])) + return string.join(r,'\n') + + +def notee(): + if not self.foundauthinfo: + print"PROBLEM: no login info configured, use \n'pppoectl %s myauthproto=pap myauthname=\"\" myauthsecret=\"\"'" + return + +def checkall(IF): + Res="The interface is" + if "UP" in IF.flags: + Res+=" up" + else: + Res=".\nPROBLEM: the interface is down, use 'ifconfig %s up'" % (PPPIF) + return(Res) + + if IF.foundauthinfo: + Res+=", has authentication information" + else: + Res+=".\nPROBLEM: pppoe has no authentication information." + return(Res) + + if IF.foundinet: + Res+=", is configured" + else: + Res+=".\nPROBLEM: %s is not configured, use 'ifconfig %s inet 0.0.0.0 0.0.0.1'" % (PPPIF, PPPIF) + return(Res) + + if IF.ipaddr != '0.0.0.0': + Res+=", has an IP address" + else: + Res+=".\nPROBLEM: The inteface has no address" + return(Res) + + if IF.ipgw != '0.0.0.1': + Res+=", has an IP gateway" + else: + Res+=".\nPROBLEM: The interfaces has no gateway" + return(Res) + + ec=os.system("/sbin/ping -n -c 1 %s >/dev/null 2>&1" % "217.237.157.246") +# ec=os.system("/sbin/ping -n -c 1 %s >/dev/null 2>&1" % IF.ipgw) + if ec == 0: + Res+=".\nThe gateway is reachable." + else: + Res+=".\nPROBLEM: The gateway is not reachable." + return(Res) + + Res+=".\n\n All appears to be well." + return(Res) + + + +# +# Main +# + +uname=os.uname() + +lines=[] +if sys.stdin.isatty(): + uri='/selfcheck' +else: + while 1: + l2=sys.stdin.readline() +# print "
[",len(l2),l2[:-2],"]
" + sys.stdout.flush() + if l2[:-2] == "": + break + lines.append(l2[:-2]) + uri=string.split(lines[0],' ')[1] + if uri != '/favicon.ico': + sendheartbeatmsg(uri) + +date=time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime()) + + +if uri != "/selfcheck": + print "HTTP/1.1 404 Not Found" + print "Date: %s" % date + print "Connection: close" + print "Content-Type: text/html; charset=iso-8859-1" + print "" + print "Nothing here, move along.." + sys.exit(0) + +print """HTTP/1.1 200 OK +Date: %s +Server: Selfcheck/1.0 (Python) +Last-Modified: %s +Accept-Ranges: bytes +Connection: close +Content-Type: text/html; charset=ISO-8859-1""" % (date, date) + +print """ + + +ADSL Check + + +

ADSL Check

+
"""
+print "Current Time:    %s" % date
+print "Machine:         %s" % uname[1]
+print "OS:              %s" % uname[0]+"/"+uname[4]+" "+uname[2]
+print "Uptime:          ",
+sys.stdout.flush()
+os.system("uptime")
+
+print "
" +print "Checking interface %s" % PPPIF + +IF=Pppoe(PPPIF) + +print checkall(IF) + +print "
" +print "Additional information" + +print "
Users" +sys.stdout.flush() +os.system("w") +print "
" +print "Interface data" +print IF +print + +print "
" +if 0: + print "Ping" + sys.stdout.flush() + ec=os.system("/sbin/ping -n -q -c 3 204.29.161.37") + if ec != 0: + print " ping failed!" +else: + print "Traceroute" + sys.stdout.flush() +# ec=os.system("/usr/sbin/traceroute -w 2 204.29.161.37") + ec=os.system("/usr/pkg/sbin/mtr -r -c 2 204.29.161.37") + if ec != 0: + print " traceroute failed!" + +print "
" +print "Routing Table" +sys.stdout.flush() +os.system("/usr/bin/netstat -rnLfinet") + +print "
" +print "Relevant log entries" +sys.stdout.flush() +os.system("grep %s: /var/log/messages" % IF.interface) + + +if DBG: + for l in lines: + print l + +print "
" +print "All done" +print "" +print ""