change stats of connectivity loss

This commit is contained in:
2015-03-06 18:19:31 +01:00
parent b7ca714f86
commit 1adfacfed7
2 changed files with 120 additions and 23 deletions
+43 -22
View File
@@ -40,7 +40,7 @@ htab = {}
msgs = []
num = 0
upcount = 0
#AEW upcount = 0
PORT = 50003
TPORT = 50004
THOST = ""
@@ -52,11 +52,8 @@ GRACE = 2
os.environ['TZ'] = 'EST5EDT'
stdscr = None
win = None
msgw = None
msgwB = None
msgwHeight = 10
tsfm=["%H","%d","%U"]
lastfm=["","",""]
def handler(signum, frame):
global running, sig
@@ -100,7 +97,7 @@ class Host:
self.interval = 0
self.doesack = -1
self.cmds = []
self.hdwcounts = [0,0,0]
self.hdwcounts = [[0,0],[0,0],[0,0]]
num += 1
@@ -114,7 +111,7 @@ class Host:
try:
a=self.hdwcounts
except:
self.hdwcounts = [0,0,0]
self.hdwcounts = [[self.doesack,self.upcount],[self.doesack,self.upcount],[self.doesack,self.upcount]]
def getstate(self):
return self.state
@@ -124,12 +121,26 @@ class Host:
state = "<b>%s</b>" % self.state
else:
state = "%s" % self.state
return state
def dispstats(self):
if self.doesack != -1:
if self.upcount > 0:
return "%s(%0.1f%%) %s %s " % (state, (self.doesack * 100.0) / self.upcount, self.doesack, self.upcount)
# return "(%0.1f%%) %s %s %s " % ((self.doesack * 100.0) / self.upcount, self.doesack, self.upcount, self.hdwcounts)
r = ""
for v in xrange(3):
a,u = self.hdwcounts[v]
if (self.upcount - u) != 0:
vs = "%0.0f" % (100.0 - (((self.doesack - a) * 100.0) / (self.upcount - u)))
if vs == "0":
vs=""
else:
vs="-"
r+= "<td>%s</td>" % vs
return r
else:
return "%s(%s)" % (state, self.doesack)
return state
return "<td>(%s)</td><td></td><td></td>" % (self.doesack)
return "<td>N/A</td><td></td<td></td>>"
# set new state, return number of secs in previous state
def newstate(self, state, when=0):
@@ -200,7 +211,7 @@ answer
""" % D
# log("DBG: nsup %s" % nsup)
cmd = ["/usr/bin/nsupdate", "-k", "/etc/dhcpc/K%(domain)s.+157+00000." % D, "-v"]
cmd = ["/usr/local/bin/nsupdate", "-k", "/etc/dhcpc/K%(domain)s.+157+00000." % D, "-v"]
# log("DBG: cmd %s" % cmd)
try:
p = Popen(cmd, shell=False, bufsize=1, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
@@ -287,13 +298,6 @@ def log(m, service="heartbeat"):
m2 = msg
logf.write(m2)
logf.flush()
if msgw is not None:
y, x = msgw.getyx()
# if y > = msgwHeight-1:
# msgw.scroll()
msgw.addstr(msg)
msgw.clrtoeol()
msgw.refresh()
pickleit()
@@ -319,6 +323,7 @@ def fromaddr(name, addr, boot, interval, acks):
err = nsupdate(name, addr)
if err:
m += ", DNS failed: %s" % err
email("error: nsupdate failed", m)
else:
m += ", DNS updated."
log(m)
@@ -344,7 +349,7 @@ def fromaddr(name, addr, boot, interval, acks):
#
#
def readsock(sock):
global htab, win
global htab
data, addr = sock.recvfrom(1024)
pairs = string.split(data, ';')
boot = 0
@@ -460,11 +465,12 @@ class HtmlHandler(SocketServer.BaseRequestHandler):
res=self.buildhead(refresh=60)
res.append("<H2>Heartbeat status</h2><h4> %s (%s)</H4>" % (time.strftime("%H:%M:%S", time.localtime(now)), os.environ.get('TZ', 'CET-1CDT')))
res.append("<table>")
res.append("<tr><th>Host</th><th>State</th><th>IP Addr</th><th>Last change</th></tr>\n")
res.append("<tr><th>Host</th><th>State</th><th>Hr</th><th>Dy</th><th>Wk</th><th>IP Addr</th><th>Last change</th></tr>\n")
hosts_sorted = hosts.keys()
hosts_sorted.sort()
for h in hosts_sorted:
res.append("<tr><td>%-24s</td><td>%-7s</td><td>%-16s</td><td>%-17s</td></tr>\n" % (h, hosts[h].dispstate(), hosts[h].addr, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(hosts[h].statetime))))
res.append("<tr><td>%-24s</td><td>%-7s</td>%s<td>%-16s</td><td>%-17s</td></tr>\n" % \
(h, hosts[h].dispstate(), hosts[h].dispstats(), hosts[h].addr, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(hosts[h].statetime))))
res.append("</table>")
res.append("<h4>Log of Events</h4>")
for m in msgs[len(msgs)-30:]:
@@ -603,6 +609,7 @@ def pickleit():
pick.dump(hosts)
pick.dump(htab)
pick.dump(msgs)
pick.dump(lastfm)
pickf.close()
@@ -730,6 +737,10 @@ if os.path.exists(PICKFILE):
hosts = pick.load()
htab = pick.load()
msgs = pick.load()
try:
lastfm = pick.load()
except:
lastfm = ["","",""]
pickf.close()
except:
os.unlink(PICKFILE)
@@ -812,6 +823,16 @@ while running:
serv.handle_request()
else:
print("what happend just now")
# check hour/day/week
for v in xrange(3):
fm=tsfm[v]
ts=time.strftime(tsfm[v], time.localtime(now))
if ts != lastfm[v]:
lastfm[v]=ts
for h in hosts.keys():
hosts[h].hdwcounts[v] = [hosts[h].doesack, hosts[h].upcount]
if now >= next:
next = now+1
checkoverdue()