diff options
author | Mike Crute <mike@crute.us> | 2019-02-13 04:28:30 +0000 |
---|---|---|
committer | Mike Crute <mike@crute.us> | 2019-02-13 04:28:30 +0000 |
commit | 776709255538ac65188cc23d320d3141c03857e0 (patch) | |
tree | e781a9f76f18834f604e9243bdf90d990cdd6f99 /ping_tester | |
parent | ecc64d22bd029ed624f7d744eedaea223daebb1f (diff) | |
download | dockerfiles-776709255538ac65188cc23d320d3141c03857e0.tar.bz2 dockerfiles-776709255538ac65188cc23d320d3141c03857e0.tar.xz dockerfiles-776709255538ac65188cc23d320d3141c03857e0.zip |
Add ping tester
Diffstat (limited to 'ping_tester')
-rw-r--r-- | ping_tester/Dockerfile | 9 | ||||
-rw-r--r-- | ping_tester/Makefile | 11 | ||||
-rwxr-xr-x | ping_tester/ping_test.py | 96 |
3 files changed, 116 insertions, 0 deletions
diff --git a/ping_tester/Dockerfile b/ping_tester/Dockerfile new file mode 100644 index 0000000..702b596 --- /dev/null +++ b/ping_tester/Dockerfile | |||
@@ -0,0 +1,9 @@ | |||
1 | FROM alpine:latest | ||
2 | |||
3 | RUN set -euxo pipefail; \ | ||
4 | apk --no-cache add python3; \ | ||
5 | python3 -m pip install boto3; | ||
6 | |||
7 | COPY ping_test.py /usr/bin/ | ||
8 | |||
9 | ENTRYPOINT [ "/usr/bin/ping_test.py" ] | ||
diff --git a/ping_tester/Makefile b/ping_tester/Makefile new file mode 100644 index 0000000..8e15cbe --- /dev/null +++ b/ping_tester/Makefile | |||
@@ -0,0 +1,11 @@ | |||
1 | IMAGE=docker.crute.me/ping_tester:latest | ||
2 | |||
3 | all: | ||
4 | docker build -t $(IMAGE) . | ||
5 | |||
6 | all-no-cache: | ||
7 | docker build --no-cache -t $(IMAGE) . | ||
8 | |||
9 | publish: | ||
10 | docker push $(IMAGE) | ||
11 | |||
diff --git a/ping_tester/ping_test.py b/ping_tester/ping_test.py new file mode 100755 index 0000000..6e19b3f --- /dev/null +++ b/ping_tester/ping_test.py | |||
@@ -0,0 +1,96 @@ | |||
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 | val = patt.search(out.stdout.decode("us-ascii")).groupdict() | ||
32 | |||
33 | client.put_metric_data( | ||
34 | Namespace="VPNLatency", | ||
35 | MetricData=[ | ||
36 | { | ||
37 | "MetricName": "PingRTT", | ||
38 | "Dimensions": [ | ||
39 | { | ||
40 | "Name": "From Location", | ||
41 | "Value": from_location, | ||
42 | }, | ||
43 | { | ||
44 | "Name": "To Location", | ||
45 | "Value": to_location, | ||
46 | } | ||
47 | ], | ||
48 | "Timestamp": now, | ||
49 | "StatisticValues": { | ||
50 | "SampleCount": sample_count, | ||
51 | "Sum": float(val["avg"]) * sample_count, | ||
52 | "Minimum": float(val["min"]), | ||
53 | "Maximum": float(val["max"]), | ||
54 | }, | ||
55 | "Unit": "Milliseconds" | ||
56 | }, | ||
57 | { | ||
58 | "MetricName": "PingRTT", | ||
59 | "Dimensions": [ | ||
60 | { | ||
61 | "Name": "From Location", | ||
62 | "Value": from_location, | ||
63 | }, | ||
64 | ], | ||
65 | "Timestamp": now, | ||
66 | "StatisticValues": { | ||
67 | "SampleCount": sample_count, | ||
68 | "Sum": float(val["avg"]) * sample_count, | ||
69 | "Minimum": float(val["min"]), | ||
70 | "Maximum": float(val["max"]), | ||
71 | }, | ||
72 | "Unit": "Milliseconds" | ||
73 | }, | ||
74 | { | ||
75 | "MetricName": "PingRTT", | ||
76 | "Dimensions": [ | ||
77 | { | ||
78 | "Name": "To Location", | ||
79 | "Value": to_location, | ||
80 | } | ||
81 | ], | ||
82 | "Timestamp": now, | ||
83 | "StatisticValues": { | ||
84 | "SampleCount": sample_count, | ||
85 | "Sum": float(val["avg"]) * sample_count, | ||
86 | "Minimum": float(val["min"]), | ||
87 | "Maximum": float(val["max"]), | ||
88 | }, | ||
89 | "Unit": "Milliseconds" | ||
90 | }, | ||
91 | ] | ||
92 | ) | ||
93 | |||
94 | |||
95 | if __name__ == "__main__": | ||
96 | main() | ||