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
+77 -1
View File
@@ -1,11 +1,20 @@
#!/usr/bin/env python
# $Id: hbc,v 1.9 2012/03/29 02:08:36 andreas Exp $
import sys, time, socket, os, signal, getopt, string, select
import sys
import time
import socket
import os
import signal
import getopt
import string
import select
import errno
PORT=50003
INTERVAL=10
False=0
True=1
DBG = False
sock=None
@@ -14,6 +23,60 @@ class NullDevice:
pass
class Flock:
def __init__(self, lock_file):
self.lock_file = lock_file
self.fd = None
self.opid = None
def lock(self):
while 1:
self.fd = None
try:
self.fd = os.open(self.lock_file, os.O_CREAT | os.O_EXCL | os.O_RDWR)
except OSError, e:
if e.errno != errno.EEXIST:
raise
except:
raise
if not self.fd:
if self.oproc():
if DBG: print "process is alive"
os.remove(self.lock_file)
continue
else:
if DBG: print "no pid process??"
if self.fd:
f=os.fdopen(self.fd, 'w').write("%s" % os.getpid())
return self.fd
def unlock(self):
os.remove(self.lock_file)
self.fd=None
def setopid(self):
try:
self.opid=open(self.lock_file).readline()
except:
pass
def oproc(self):
self.setopid()
if not self.opid:
return False
try:
os.kill(int(self.opid), 0)
return True
except:
pass
return False
def handler(signum, frame):
global up
if up == 0:
@@ -178,6 +241,9 @@ if len(msgboot) > 0:
if msgonly:
sys.exit(0)
#
#
if daemon:
pid=os.fork()
if pid > 0:
@@ -197,6 +263,15 @@ if daemon:
os.umask(0)
while True:
lock=Flock('/tmp/hbc.pid')
if lock.lock():
break
if not lock.oproc():
sys.exit(1)
os.kill(lock.opid,15)
time.sleep(1)
up=1
signal.signal(signal.SIGTERM, handler)
signal.signal(signal.SIGHUP, handler)
@@ -238,3 +313,4 @@ for hb_host in hb_hosts:
socksend(msg, (hb_host, hb_port))
time.sleep(1)
sock.close()
lock.unlock()
+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:
return "%s(%s)" % (state, self.doesack)
return state
vs="-"
r+= "<td>%s</td>" % vs
return r
else:
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()