-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_remote_status.py
executable file
·46 lines (40 loc) · 1.86 KB
/
check_remote_status.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/python
import argparse, ConfigParser, psycopg2
from tweet import *
config = ConfigParser.RawConfigParser()
config.read('/home/jessebishop/.pyconfig')
dbhost = config.get('wfdb', 'DBHOST')
dbname = config.get('wfdb', 'DBNAME')
dbuser = config.get('wfdb', 'DBUSER')
p = argparse.ArgumentParser(prog="send_status.py")
p.add_argument('-p', '--piname', dest="piname", required=True, help="The name of the pi being monitored.")
args = p.parse_args()
db = psycopg2.connect(host=dbhost, database=dbname, user=dbuser)
cursor = db.cursor()
query = """SELECT (CURRENT_TIMESTAMP - timestamp) < interval '5 minutes' AS success, notified FROM pi_checkin WHERE pi = '{0}';""".format(args.piname)
try:
cursor.execute(query)
success, notified = cursor.fetchall()[0]
if not success:
if not notified:
#send a tweet
status = """Hey @jessebishop, {0} hasn't been heard from in over 5 minutes!""".format(args.piname)
tweet(status)
query = """UPDATE pi_checkin SET notified = TRUE WHERE pi = '{0}';""".format(args.piname)
cursor.execute(query)
else:
query = """SELECT (CURRENT_TIMESTAMP - timestamp) > interval '1 day' AS check FROM pi_checkin WHERE pi = '{0}';""".format(args.piname)
cursor.execute(query)
check = cursor.fetchall()[0][0]
if check:
status = """Hey @jessebishop, {0} hasn't been heard from in over a day!""".format(args.piname)
tweet(status)
query = """UPDATE pi_checkin SET timestamp = CURRENT_TIMESTAMP - interval '6 minutes' WHERE pi = '{0}';""".format(args.piname)
cursor.execute(query)
else:
query = """UPDATE pi_checkin SET notified = FALSE WHERE pi = '{0}'""".format(args.piname)
cursor.execute(query)
except Exception, msg:
print msg
cursor.close()
db.close()