Files
heartbeat/selfcheck
T
2006-11-08 15:16:41 +00:00

266 lines
5.8 KiB
Python
Executable File

#!/usr/bin/env python
#
RCSID="$Id: selfcheck,v 1.2 2006/11/08 15:16:41 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"<B>PROBLEM: no login info configured, use \n'pppoectl %s myauthproto=pap myauthname=\"<account>\" myauthsecret=\"<password>\"'</B>"
return
def checkall(IF):
Res="The interface is"
if "UP" in IF.flags:
Res+=" up"
else:
Res=".\n<B>PROBLEM: the interface is down, use 'ifconfig %s up'</B>" % (PPPIF)
return(Res)
if IF.foundauthinfo:
Res+=", has authentication information"
else:
Res+=".\n<B>PROBLEM: pppoe has no authentication information.</B>"
return(Res)
if IF.foundinet:
Res+=", is configured"
else:
Res+=".\n<B>PROBLEM: %s is not configured, use 'ifconfig %s inet 0.0.0.0 0.0.0.1'</B>" % (PPPIF, PPPIF)
return(Res)
if IF.ipaddr != '0.0.0.0':
Res+=", has an IP address"
else:
Res+=".\n<B>PROBLEM: The inteface has no address</B>"
return(Res)
if IF.ipgw != '0.0.0.1':
Res+=", has an IP gateway"
else:
Res+=".\n<B>PROBLEM: The interfaces has no gateway</B>"
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+=".\n<B>PROBLEM: The gateway is not reachable.</B>"
return(Res)
Res+=".\n\n<B> All appears to be well.</B>"
return(Res)
#
# Main
#
uname=os.uname()
lines=[]
if sys.stdin.isatty():
uri='/selfcheck'
else:
while 1:
l2=sys.stdin.readline()
# print "<pre>[",len(l2),l2[:-2],"]</pre>"
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 """
<html>
<head>
<title>ADSL Check</title>
</head>
<body>
<H2>ADSL Check</H2>
<pre>"""
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 "<br>"
print "<B>Checking interface %s</B>" % PPPIF
IF=Pppoe(PPPIF)
print checkall(IF)
print "<br>"
print "<B>Additional information</B>"
print "<BR><B>Users</B>"
sys.stdout.flush()
os.system("w")
print "<br>"
print "<B>Interface data</B>"
print IF
print
print "<br>"
if 0:
print "<B>Ping</B>"
sys.stdout.flush()
ec=os.system("/sbin/ping -n -q -c 3 204.29.161.37")
if ec != 0:
print "<B> ping failed!</B>"
else:
print "<B>Traceroute</B>"
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 "<B> traceroute failed!</B>"
print "<br>"
print "<B>Routing Table</B>"
sys.stdout.flush()
os.system("/usr/bin/netstat -rnLfinet")
print "<br>"
print "<B>Relevant log entries</B>"
sys.stdout.flush()
os.system("grep %s: /var/log/messages" % IF.interface)
if DBG:
for l in lines:
print l
print "</pre>"
print "All done"
print "</body>"
print "</html>"