From 19d850306395d225a869bbac5d36ce6ba938ce27 Mon Sep 17 00:00:00 2001 From: Dimitris Rozakis Date: Mon, 17 Jul 2017 00:59:21 +0300 Subject: [PATCH] Use upstream pingparser --- requirements.txt | 3 +- scripts/install.sh | 2 +- vpn-proxy/app/pingparser.py | 127 ------------------------------------ 3 files changed, 3 insertions(+), 129 deletions(-) delete mode 100755 vpn-proxy/app/pingparser.py diff --git a/requirements.txt b/requirements.txt index d33939f..9107a7e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ django netaddr -ipython +ipython==5 +https://github.com/ssteinerx/pingparser/archive/master.zip diff --git a/scripts/install.sh b/scripts/install.sh index 31c60ad..bcdb278 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -39,7 +39,7 @@ apt-get install -yq --no-install-recommends \ python python-pip openvpn uwsgi uwsgi-plugin-python pip install -U pip -pip install -U django netaddr ipython +pip install -r $DIR/vpn-proxy/requirements.txt echo "VPN_SERVER_REMOTE_ADDRESS = \"$VPN_IP\"" > $DIR/vpn-proxy/conf.d/0000-vpn-ip.py SOURCE_CIDRS=`echo "$SOURCE_CIDRS" | sed 's/ /", "/g'` diff --git a/vpn-proxy/app/pingparser.py b/vpn-proxy/app/pingparser.py deleted file mode 100755 index e067a6d..0000000 --- a/vpn-proxy/app/pingparser.py +++ /dev/null @@ -1,127 +0,0 @@ -#!/usr/bin/env python -# coding: utf-8 -""" -Parses the output of the system ping command. - -Code originally taken from https://github.com/gg/pingparser -""" -__version__ = '0.3' - -from optparse import OptionGroup,OptionParser - -import re -import sys - - -def _get_match_groups(ping_output, regex): - match = regex.search(ping_output) - if not match: - raise Exception('Invalid PING output:\n' + ping_output) - return match.groups() - - -def parse(ping_output): - """ - Parses the `ping_output` string into a dictionary containing the following - fields: - - `host`: *string*; the target hostname that was pinged - `sent`: *int*; the number of ping request packets sent - `received`: *int*; the number of ping reply packets received - `packet_loss`: *int*; the percentage of packet loss - `minping`: *float*; the minimum (fastest) round trip ping request/reply - time in milliseconds - `avgping`: *float*; the average round trip ping time in milliseconds - `maxping`: *float*; the maximum (slowest) round trip ping time in - milliseconds - `jitter`: *float*; the standard deviation between round trip ping times - in milliseconds - """ - matcher = re.compile(r'PING ([a-zA-Z0-9.\-]+) \(') - host = _get_match_groups(ping_output, matcher)[0] - - matcher = re.compile(r'(\d+) packets transmitted, (\d+) received, ' - r'(\d+)% packet loss') - sent, received, packet_loss = _get_match_groups(ping_output, matcher) - - try: - matcher = re.compile(r'(\d+.\d+)/(\d+.\d+)/(\d+.\d+)/(\d+.\d+)') - minping, avgping, maxping, jitter = _get_match_groups(ping_output, - matcher) - except: - minping, avgping, maxping, jitter = ['NaN']*4 - - return { - # 'host': host, - 'packets_tx': int(sent), - 'packets_rx': int(received), - 'packets_loss': float(packet_loss), - 'rtt_min': float(minping), - 'rtt_avg': float(avgping), - 'rtt_max': float(maxping) - } - - -def main(argv=sys.argv): - # detects whether input is piped in - ping_output = None - if not sys.stdin.isatty(): - ping_output = sys.stdin.read() - - usage = 'Usage: %prog [OPTIONS] [+FORMAT]\n\n'\ - 'Parses output from the system ping command piped in via stdin.' - parser = OptionParser(usage=usage, version="%prog " + __version__) - - format_group = OptionGroup(parser, - """FORMAT controls the output. Interpreted sequences are: - \t%h host name or IP address - \t%s packets sent - \t%r packets received - \t%p packet_loss - \t%m minimum ping in milliseconds - \t%a average ping in milliseconds - \t%M maximum ping in milliseconds - \t%j jitter in milliseconds - - Default FORMAT is %h,%s,%r,%p,%m,%a,%M,%j""") - parser.add_option_group(format_group) - - (options, args) = parser.parse_args() - - if ping_output is None: - parser.print_help() - sys.exit(1) - - ping_result = parse(ping_output) - - format_replacements = [('%h', 'host'), - ('%s', 'sent'), - ('%r', 'received'), - ('%p', 'packet_loss'), - ('%m', 'minping'), - ('%a', 'avgping'), - ('%M', 'maxping'), - ('%j', 'jitter')] - format_replacements = [(fmt, ping_result[field]) for fmt, field in - format_replacements] - - if len(args) == 0: - output = ','.join(fmt for (fmt, rep) in format_replacements) - elif args[0].startswith('+'): - args[0] = args[0].lstrip('+') - output = ' '.join(args[0:]) - else: - parser.print_help() - - for (fmt, rep) in format_replacements: - output = output.replace(fmt, rep) - - sys.stdout.write(output) - - sys.exit(0) - -if __name__ == "__main__": - try: - main(sys.argv) - except KeyboardInterrupt: - pass