-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmonitor_riseupvpn.py
57 lines (47 loc) · 1.64 KB
/
monitor_riseupvpn.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
import netifaces
from icmplib import ping, ICMPLibError
"""
checks:
- is there a VPN interface tun0?
- is the default gateway on tun0?
- can I ping the gateway of tun0?
requires:
sudo pacman -S python-netifaces python-icmplib
sudo apt-get install python3-netifaces python3-icmplib
"""
class Py3status:
VPN_INTERFACE = "tun0"
def monitor_host(self):
state_fail = {
'full_text': "VPN: fail",
'color': self.py3.COLOR_BAD,
'cached_until': self.py3.time_in(seconds=10)
}
state_succeed = {
'full_text': "VPN: OK",
'color': self.py3.COLOR_GOOD,
'cached_until': self.py3.time_in(seconds=2 * 60)
}
interfaces = netifaces.interfaces()
if self.VPN_INTERFACE not in interfaces:
self.py3.log(f"VPN interface does not exist ({interfaces})")
return state_fail
gw_ip, interface = netifaces.gateways()['default'][netifaces.AF_INET]
# self.py3.log(gw_ip, interface)
if interface != self.VPN_INTERFACE:
self.py3.log(f"Default gateway interface is not the VPN interface ({interface})")
return state_fail
try:
resp = ping(gw_ip, timeout=2, # noqa: F841
count=1, privileged=False)
# self.py3.log(resp)
return state_succeed
except ICMPLibError as e:
self.py3.log(f"Ping failed: {e}")
return state_fail
if __name__ == "__main__":
"""
Run module in test mode.
"""
from py3status.module_test import module_test
module_test(Py3status)