Files
heartbeat/monif
T
andreas dd03128ed3 remove dbgprint
full path to hbc
2012-03-28 20:17:08 +00:00

48 lines
924 B
Python
Executable File

#!/usr/bin/env python
# Monitor Interfaces, send hb msg when add changes
import time, os, sys
SLEEP=60
SRV="colo2.wapanafa.org"
DBG=0
HBC="/home/andreas/bin/hbc"
IFS=[]
f=os.popen('/sbin/ifconfig -a 2>/dev/null', 'r')
for l in f.readlines():
if len(l) > 1 and not l[0] in [' ','\t']:
r=l.split()
if DBG: print r
if r[0][-1] == ':':
r[0]=r[0][:-1]
if r[0][:2] == 'lo':
continue
IFS.append(r[0])
if DBG: print IFS
addrs={}
for I in IFS:
addrs[I]=""
while 1:
for I in IFS:
f=os.popen('/sbin/ifconfig %s 2>/dev/null' % I, 'r')
for l in f.readlines():
r=l.split()
if DBG > 1: print "x2", r
if len(r) == 0 or r[0] != 'inet':
continue
if r[1].find('addr:') == 0:
iaddr=r[1][5:]
else:
iaddr=r[1]
if iaddr != addrs[I]:
msg='%s -m "ifadd %s %s" %s' % (HBC, I, iaddr, SRV)
if DBG: print msg
else: os.system(msg)
addrs[I]=iaddr
f.close()
time.sleep(SLEEP)