cleanup exit codes

This commit is contained in:
Andreas Billmeier 2021-07-18 12:58:49 +02:00
parent d1dd2a73e2
commit 7a61f17d4a
Signed by: onkelbeh
GPG Key ID: E6DB12C8C550F3C0
1 changed files with 15 additions and 8 deletions

View File

@ -39,16 +39,20 @@ def main():
min = ""
max = ""
# assume the best
STATUS = OK
edevau = True
# Create a TCP socket to OpenVPN management
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((args.HOST, args.PORT))
if not s:
gtfo(3, "Could not connect.")
gtfo(UNKNOWN, "Could not connect.")
try:
if not ovpn_logon(s, args.password):
gtfo(3, "Login failed.")
gtfo(CRITICAL, "Login failed.")
status, clients, routing = ovpn_status2(s)
log.warning(status)
log.warning(clients)
@ -78,10 +82,14 @@ def main():
performancedata['out'] = clients['Bytes Sent'][i]
performancedata['connectedsince'] = clients['Connected Since (time_t)'][i]
i += 1
if performancedata['clients'] == 0:
message += f"No Clients found matching {args.cn}."
STATUS = WARNING
elif args.route:
# search for a specified route
gtfo(3,"Not implemented yet.")
gtfo(UNKNOWN,"Not implemented yet.")
else:
# default: report Clientcount
clientcount = len(clients['Common Name'])
@ -114,7 +122,7 @@ def main():
message += f" {key}={performancedata[key]};;;;"
# go Home
gtfo(0,message)
gtfo(STATUS,message)
def ovpn_status2(s):
@ -122,7 +130,7 @@ def ovpn_status2(s):
request = "status 2\r\n"
s.sendall(request.encode())
except socket.error:
gtfo(3, "Send failed")
gtfo(UNKNOWN, "Send failed")
status = {}
clients = {}
@ -190,9 +198,9 @@ def ovpn_logon(s, password):
request = f"{password}\r\n"
s.sendall(request.encode())
except socket.error:
gtfo(3, "Send Password failed")
gtfo(ERROR, "Send Password failed")
else:
gtfo(3, "Logon requested, none given.")
gtfo(ERROR, "Logon requested, none given.")
elif token == "SUCCESS":
connected = True
break
@ -277,7 +285,6 @@ def Get_Args():
def gtfo(exitcode, message=""):
"""Exit gracefully with exitcode and (optional) message"""
log.debug(f"Exiting with status {exitcode}. Message: {message}")
if message:
print(message)