diff options
Diffstat (limited to 'ping_tester/ping_test.py')
-rwxr-xr-x | ping_tester/ping_test.py | 103 |
1 files changed, 0 insertions, 103 deletions
diff --git a/ping_tester/ping_test.py b/ping_tester/ping_test.py deleted file mode 100755 index f6b7238..0000000 --- a/ping_tester/ping_test.py +++ /dev/null | |||
@@ -1,103 +0,0 @@ | |||
1 | #!/usr/bin/env python3 | ||
2 | |||
3 | import os | ||
4 | import re | ||
5 | import sys | ||
6 | import boto3 | ||
7 | import subprocess | ||
8 | from datetime import datetime | ||
9 | |||
10 | |||
11 | def main(sample_count=5): | ||
12 | try: | ||
13 | _, from_location, to_location, hostname = sys.argv | ||
14 | except ValueError: | ||
15 | print("usage: {} <this_location> <to_location> <hostname>".format( | ||
16 | os.path.basename(sys.argv[0]))) | ||
17 | sys.exit(1) | ||
18 | |||
19 | client = boto3.client("cloudwatch") | ||
20 | now = datetime.now() | ||
21 | |||
22 | patt = re.compile( | ||
23 | "round-trip min/avg/max = " | ||
24 | "(?P<min>[0-9]+\.[0-9]+)/(?P<avg>[0-9]+\.[0-9]+)/" | ||
25 | "(?P<max>[0-9]+\.[0-9]+) (?P<unit>.*)") | ||
26 | |||
27 | out = subprocess.run( | ||
28 | ["ping", "-c", str(sample_count), hostname], | ||
29 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
30 | |||
31 | # Prevent failing with an error if the ping fails | ||
32 | match = patt.search(out.stdout.decode("us-ascii")) | ||
33 | if not match: | ||
34 | return 1 | ||
35 | |||
36 | val = match.groupdict() | ||
37 | |||
38 | client.put_metric_data( | ||
39 | Namespace="VPNLatency", | ||
40 | MetricData=[ | ||
41 | { | ||
42 | "MetricName": "PingRTT", | ||
43 | "Dimensions": [ | ||
44 | { | ||
45 | "Name": "From Location", | ||
46 | "Value": from_location, | ||
47 | }, | ||
48 | { | ||
49 | "Name": "To Location", | ||
50 | "Value": to_location, | ||
51 | } | ||
52 | ], | ||
53 | "Timestamp": now, | ||
54 | "StatisticValues": { | ||
55 | "SampleCount": sample_count, | ||
56 | "Sum": float(val["avg"]) * sample_count, | ||
57 | "Minimum": float(val["min"]), | ||
58 | "Maximum": float(val["max"]), | ||
59 | }, | ||
60 | "Unit": "Milliseconds" | ||
61 | }, | ||
62 | { | ||
63 | "MetricName": "PingRTT", | ||
64 | "Dimensions": [ | ||
65 | { | ||
66 | "Name": "From Location", | ||
67 | "Value": from_location, | ||
68 | }, | ||
69 | ], | ||
70 | "Timestamp": now, | ||
71 | "StatisticValues": { | ||
72 | "SampleCount": sample_count, | ||
73 | "Sum": float(val["avg"]) * sample_count, | ||
74 | "Minimum": float(val["min"]), | ||
75 | "Maximum": float(val["max"]), | ||
76 | }, | ||
77 | "Unit": "Milliseconds" | ||
78 | }, | ||
79 | { | ||
80 | "MetricName": "PingRTT", | ||
81 | "Dimensions": [ | ||
82 | { | ||
83 | "Name": "To Location", | ||
84 | "Value": to_location, | ||
85 | } | ||
86 | ], | ||
87 | "Timestamp": now, | ||
88 | "StatisticValues": { | ||
89 | "SampleCount": sample_count, | ||
90 | "Sum": float(val["avg"]) * sample_count, | ||
91 | "Minimum": float(val["min"]), | ||
92 | "Maximum": float(val["max"]), | ||
93 | }, | ||
94 | "Unit": "Milliseconds" | ||
95 | }, | ||
96 | ] | ||
97 | ) | ||
98 | |||
99 | return 0 | ||
100 | |||
101 | |||
102 | if __name__ == "__main__": | ||
103 | sys.exit(main()) | ||