actually use web server port for something. filter results by host name

This commit is contained in:
ville rantanen
2018-08-16 12:10:19 +03:00
parent 0b9cebecff
commit 6b668b8d88
2 changed files with 33 additions and 17 deletions

View File

@@ -1,6 +1,6 @@
# Silly alive watchdog
Is [Nando](http://www.imdb.com/title/tt0106246/characters/nm0000160) alive?
Is [Nando](http://www.imdb.com/title/tt0106246/characters/nm0000160) alive?
At server side, run `nandod` with suitable arguments
@@ -9,8 +9,11 @@ At clients, run `nando -i 600` to update status every 10 minutes.
You can send a custom text file with the update, generated with ex. `nando-desc`
Nando clients can query which other clients are online, and what their status is
(whatever you choose to update with the text data)
(whatever you choose to update with the text data)
`nandod` can run a simple HTML server, that lists the nodes that have
returned their alive message. add the name of client at the end of URL
to filter only to that name
## Installer

43
nandod
View File

@@ -42,14 +42,17 @@ class UDPHandler(SocketServer.BaseRequestHandler):
DB.update(data)
reply="OK"
socket.sendto(reply, self.client_address)
class TCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
if not opts.quiet:
print "TCP request: {0}".format(self.client_address[0])
self.data = self.request.recv(1024).strip()
method, path, _ = self.data.splitlines()[0].split()
self.path = path.lstrip("/")
self.request.send('HTTP/1.0 200 OK\r\n')
self.request.send("Content-Type: text/html\r\n\r\n")
self.request.sendall(HTMLDB.HTML_list())
self.request.sendall(HTMLDB.HTML_list(self.path))
self.request.close()
class DataBase:
@@ -60,7 +63,7 @@ class DataBase:
if not os.path.exists(self.DBfile):
self.createDB()
self.conn_init()
def conn_init(self):
self.conn=sqlite3.connect(self.DBfile)
self.db=self.conn.cursor()
@@ -77,7 +80,7 @@ class DataBase:
desc TEXT,\
date INTEGER)')
self.conn_end()
def update(self,id):
try:
host,ip,desc=id.split("|",3)
@@ -94,7 +97,7 @@ class DataBase:
self.db.execute("INSERT OR REPLACE INTO alive(id,ip,date,desc) \
VALUES(?,?,?,?)",(host,ip,int(time.time()),desc))
self.conn_end()
def list_all(self):
self.db=self.conn.cursor()
self.db.execute("SELECT id,ip,date,desc FROM alive ORDER BY id")
@@ -107,7 +110,7 @@ class DataBase:
else:
lost.append( "{0}|{1}|{2}|{3}".format(row[0],row[1], humanize_time(age),row[3]))
return TABLE_HEAD+"\n".join(alive)+"\n---|---|---|---\n"+"\n".join(lost)
def list_alive(self):
self.db=self.conn.cursor()
self.db.execute("SELECT id,ip,date,desc FROM alive ORDER BY date DESC")
@@ -115,9 +118,9 @@ class DataBase:
for row in self.db:
age=int(time.time())-row[2]
if age<ALIVE:
msg.append("{0}|{1}|{2}|{3}".format(row[0],row[1], humanize_time(age),row[3]))
msg.append("{0}|{1}|{2}|{3}".format(row[0],row[1], humanize_time(age),row[3]))
return TABLE_HEAD+"\n".join(msg)
def list_lost(self):
self.db=self.conn.cursor()
self.db.execute("SELECT id,ip,date,desc FROM alive ORDER BY date")
@@ -125,10 +128,11 @@ class DataBase:
for row in self.db:
age=int(time.time())-row[2]
if age>=ALIVE:
msg.append("{0}|{1}|{2}|{3}".format(row[0],row[1], humanize_time(age),row[3]))
msg.append("{0}|{1}|{2}|{3}".format(row[0],row[1], humanize_time(age),row[3]))
return TABLE_HEAD+"\n".join(msg)
def HTML_list(self):
def HTML_list(self, name):
# name = '' means do not filter by name
msg=[]
msg.append('''<html>
<head>
@@ -154,15 +158,21 @@ td, th {
self.db.execute("SELECT id,ip,date,desc FROM alive ORDER BY id")
msg.append('<tr><th>{0}</th><th>{1}</th><th class="right">{2}</th><th>{3}</th></tr>'.format('Host','IP','Age[d h:m:s]','Description'))
for row in self.db:
if name != '':
if name != row[0]:
continue
age=int(time.time())-row[2]
if age <ALIVE:
if age < ALIVE:
msg.append('<tr><td>{0}</td><td>{1}</td><td class="right">{2}</td><td>{3}</td></tr>'.format(row[0],row[1],humanize_time(age),row[3]))
msg.append('</table><h1>Lost</h1><table>')
msg.append('<tr><th>{0}</th><th>{1}</th><th class="right">{2}</th><th>{3}</th></tr>'.format('Host','IP','Age[d h:m:s]','Description'))
self.db.execute("SELECT id,ip,date,desc FROM alive ORDER BY id")
for row in self.db:
if name != '':
if name != row[0]:
continue
age=int(time.time())-row[2]
if age >=ALIVE:
if age >= ALIVE:
msg.append('<tr><td>{0}</td><td>{1}</td><td class="right">{2}</td><td>{3}</td></tr>'.format(row[0],row[1],humanize_time(age),row[3]))
msg.append('</table></body></html>')
@@ -178,9 +188,12 @@ def TCPserve():
global HTMLDB
global HTMLserver
time.sleep(2)
HTMLDB=DataBase(opts.DB)
HTMLserver = SocketServer.TCPServer((opts.HOST, opts.PORT), TCPHandler)
HTMLserver.serve_forever()
try:
HTMLDB=DataBase(opts.DB)
HTMLserver = SocketServer.TCPServer((opts.HOST, opts.WEBPORT), TCPHandler)
HTMLserver.serve_forever()
except:
sys.exit(1)
def humanize_time(secs):
mins, secs = divmod(secs, 60)