-
Notifications
You must be signed in to change notification settings - Fork 4
/
openvpn_supervisor.py
140 lines (94 loc) · 4.06 KB
/
openvpn_supervisor.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python
# MIT License
#
# Copyright (c) 2018 James Gebbie-Rayet
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Blah"""
import logging
import os
import subprocess
import time
def loggingsetup():
"""Blah"""
logger = logging.getLogger("openvpn-supervisor")
logger.setLevel(logging.INFO)
logpath = os.path.join(
"/var/log/openvpn-supervisor", "openvpn-supervisor.log")
logformat = logging.Formatter(
'%(asctime)s - %(message)s', '%Y-%m-%d %H:%M:%S')
logformatter = logging.FileHandler(logpath, mode="w")
logformatter.setFormatter(logformat)
logger.addHandler(logformatter)
return logger
def openvpnservicerestart():
"""Blah"""
status = sendtoshell("service openvpn restart")
return status
def sendtoshell(cmd):
"""Blah"""
proc = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
stdout, stderr = proc.communicate()
errcode = proc.returncode
# Format the stdout to utf-8 for python 3.
if not isinstance(stdout, str):
stdout = stdout.decode("utf-8")
# Format the stderr to utf-8 for python 3.
if not isinstance(stdout, str):
stderr = stderr.decode("utf-8")
return stdout, stderr, errcode
def main():
"""Blah"""
notdone = True
logger = loggingsetup()
logger.info("Starting the OpenVPN Supervisor.")
logger.info("This simple software utility is designed to monitor the "
"status of an OpenVPN connection and restart the OpenVPN "
"service when the connection becomes stale.")
logger.info("This software utility was developed by James Gebbie-Rayet "
"and is made available under the MIT license terms.")
time.sleep(300)
logger.info("Monitoring started - logs will appear at "
"/var/log/openvpn-supervisor/log")
while notdone:
# Ping out
status = sendtoshell("ping -c 1 -w2 www.google.com")
# If can't ping, issue restart.
if status[2] > 0:
logger.info("Stale VPN connection detected - restarting OpenVPN "
"service.")
status = openvpnservicerestart()
if status[2] == 0:
logger.info("OpenVPN has been successfully restarted.")
else:
notdone = False
logger.info("ERROR - OpenVPN could not be restarted, here is "
"the output of stdout, stderr and the errorcode:"
"\n\nstdout:\n{0}\n\nstderr:{1}\n\nerrcode = {2}"
.format(status[0], status[1], status[2]))
logger.info("If the error message is to do with 'permissions' "
"or 'access denied' then you need to make sure "
"that this utility is running as root.")
time.sleep(120)
logger.info("Exiting the OpenVPN Supervisor.")
logger.info("Goodbye for now!")
if __name__ == "__main__":
main()