-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping.py
executable file
·50 lines (44 loc) · 1.2 KB
/
ping.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
#!/usr/bin/env python
from pinger import Pinger
from enum import Enum
from datetime import datetime
import os
import time
import logging
import notification
class State(Enum):
INITIALIZING = 1
ONLINE = 2
OFFLINE = 3
if __name__ == "__main__":
logArgs = {
'format': '%(asctime)s:%(levelname)s:%(process)d:%(message)s',
'level': logging.INFO
}
logFile = os.environ.get('LOG_FILE')
if logFile != None:
logArgs['filename'] = logFile
logging.basicConfig(**logArgs)
pinger = Pinger("8.8.8.8")
pinger.start()
state = State.INITIALIZING
while True:
time.sleep(15)
pingSuccessful = pinger.check()
oldState = state
if state == State.INITIALIZING:
if pingSuccessful:
state = State.ONLINE
elif state == State.ONLINE:
if not pingSuccessful:
state = State.OFFLINE
offlineDate = datetime.now()
elif state == State.OFFLINE:
if pingSuccessful:
state = State.INITIALIZING
notification.send(f"Internet connection restored, offline since {offlineDate.isoformat()}")
else:
logging.error(f"invalid state: {state}")
state = State.INITIALIZING
if oldState != state:
logging.info(f"{oldState} -> {state}")