-
Notifications
You must be signed in to change notification settings - Fork 0
/
uptime.py
executable file
·114 lines (98 loc) · 3.92 KB
/
uptime.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#! /usr/bin/env python
from datetime import datetime
import logging
import requests
from socket import gethostname
import sys
import yaml
yamlconf = '/path/to/config.yml'
rebooted = False
try:
if sys.argv[1] == 'reboot':
rebooted = True
except IndexError:
pass
now = datetime.now().strftime("%m/%d/%Y %H:%M:%S")
with open(yamlconf) as fr:
config = yaml.load(fr)
log = logging.getLogger('uptime.py')
log.setLevel(logging.INFO)
fh = logging.FileHandler(config['Logging']['logfile'])
fh.setLevel(logging.INFO)
frmt = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s",
"%Y/%m/%d %H:%M:%S")
fh.setFormatter(frmt)
log.addHandler(fh)
tg_api_url = 'http://api.telegram.org/bot'
tg_api_token = config['Bots']['botname']['token']
tg_api_method = 'sendMessage'
tg_api_bot_url = tg_api_url + tg_api_token + '/' + tg_api_method
tg_chat_id = config['Bots']['botname']['chat_id']
my_host = gethostname()
log_check = config['Online']['check']
log_latest = config['Online']['latest']
log_status = config['Online']['status']
check_url = 'https://google.com'
def notify_telegram_bot(status, latest, check):
text_msg = ("*HOST UPTIME*\nHost: %s\nStatus: %s\nLatest: %s\nCheck: %s"
% (my_host, status, latest, check))
msg_data = {'chat_id': tg_chat_id,
'text': text_msg,
'parse_mode': 'Markdown'}
requests.get(tg_api_bot_url, params=msg_data)
try:
response = requests.get(check_url).status_code
if response == 200:
online_status = 'Online'
else:
online_status = 'Offline'
except requests.exceptions.ConnectionError:
online_status = 'Offline'
with open(yamlconf, 'w+') as fw:
if rebooted and online_status == 'Online':
log.info("Status CHANGED: Reboot")
notify_telegram_bot('Reboot', log_latest, now)
config['Host']['hostname'] = my_host
config['Online']['check'] = now
config['Online']['latest'] = now
config['Online']['status'] = 'Online'
fw.write(yaml.dump(config, default_flow_style=False))
elif rebooted and online_status == 'Offline':
log.warn("Status CHANGED: Reboot")
config['Host']['hostname'] = my_host
config['Online']['check'] = now
config['Online']['status'] = 'Reboot'
fw.write(yaml.dump(config, default_flow_style=False))
elif log_status == 'Reboot' and online_status == 'Offline':
log.warn("Status remains: Offline")
config['Host']['hostname'] = my_host
config['Online']['check'] = now
fw.write(yaml.dump(config, default_flow_style=False))
elif log_status == 'Online' and online_status == 'Online':
log.info("Status remains: Online")
config['Host']['hostname'] = my_host
config['Online']['check'] = now
config['Online']['latest'] = now
fw.write(yaml.dump(config, default_flow_style=False))
elif log_status == 'Online' and online_status == 'Offline':
log.warn("Status CHANGED: Offline")
config['Host']['hostname'] = my_host
config['Online']['check'] = now
config['Online']['status'] = 'Offline'
fw.write(yaml.dump(config, default_flow_style=False))
elif log_status != 'Online' and online_status == 'Online':
log.info("Status CHANGED: Online")
if log_status == 'Reboot':
notify_telegram_bot('Reboot', log_latest, now)
else:
notify_telegram_bot('Online', log_latest, now)
config['Host']['hostname'] = my_host
config['Online']['check'] = now
config['Online']['latest'] = now
config['Online']['status'] = 'Online'
fw.write(yaml.dump(config, default_flow_style=False))
elif log_status == 'Offline' and online_status == 'Offline':
log.warn("Status remains: Offline")
config['Host']['hostname'] = my_host
config['Online']['check'] = now
fw.write(yaml.dump(config, default_flow_style=False))