From 6b668b8d88d1a7f5a25d5e9993246be008b23e80 Mon Sep 17 00:00:00 2001 From: ville rantanen Date: Thu, 16 Aug 2018 12:10:19 +0300 Subject: [PATCH] actually use web server port for something. filter results by host name --- README.md | 7 +++++-- nandod | 43 ++++++++++++++++++++++++++++--------------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 90c3805..544de54 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/nandod b/nandod index fb1abad..79b87b5 100755 --- a/nandod +++ b/nandod @@ -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 HTML_list(self): + def HTML_list(self, name): + # name = '' means do not filter by name msg=[] msg.append(''' @@ -154,15 +158,21 @@ td, th { self.db.execute("SELECT id,ip,date,desc FROM alive ORDER BY id") msg.append('{0}{1}{2}{3}'.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 {0}{1}{2}{3}'.format(row[0],row[1],humanize_time(age),row[3])) msg.append('

Lost

') msg.append(''.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(''.format(row[0],row[1],humanize_time(age),row[3])) msg.append('
{0}{1}{2}{3}
{0}{1}{2}{3}
') @@ -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)