-
-
Save rickyhewitt/fb66b6b593f39acb8df5686f4112c753 to your computer and use it in GitHub Desktop.
Simple OpenVPN Status
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# | |
# OpenVPNStatus class adapted from | |
# https://gist.github.com/gregarmer/5a6c096be858580da889 | |
# Gregory Armer (https://sigterm.sh/) | |
# | |
class OpenVPNStatus: | |
def __init__(self): | |
STATUS = "/etc/openvpn/openvpn-status.log" | |
status_file = open(STATUS, 'r') | |
self.stats = status_file.readlines() | |
status_file.close() | |
self.hosts = [] | |
self.headers = { | |
'cn': 'Common Name', | |
'virt': 'Virtual Address', | |
'real': 'Real Address', | |
'sent': 'Sent', | |
'recv': 'Received', | |
'since': 'Connected Since' | |
} | |
def byte2str(self, size): | |
sizes = [ | |
(1<<50L, 'PB'), | |
(1<<40L, 'TB'), | |
(1<<30L, 'GB'), | |
(1<<20L, 'MB'), | |
(1<<10L, 'KB'), | |
(1, 'B') | |
] | |
for f, suf in sizes: | |
if size >= f: | |
break | |
return "%.2f %s" % (size / float(f), suf) | |
def results(self): | |
for line in self.stats: | |
cols = line.split(',') | |
if len(cols) == 5 and not line.startswith('Common Name'): | |
host = {} | |
host['cn'] = cols[0] | |
host['real'] = cols[1].split(':')[0] | |
host['recv'] = self.byte2str(int(cols[2])) | |
host['sent'] = self.byte2str(int(cols[3])) | |
host['since'] = cols[4].strip() | |
self.hosts.append(host) | |
if len(cols) == 4 and not line.startswith('Virtual Address'): | |
for h in self.hosts: | |
if h['cn'] == cols[1]: | |
h['virt'] = cols[0] | |
results = { | |
# "headers": self.headers, | |
"hosts": self.hosts | |
} | |
return results | |
if __name__=="__main__": | |
print OpenVPNStatus().results() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment