|
| 1 | +from collections import defaultdict |
| 2 | +from multiping import MultiPing |
| 3 | +import os |
| 4 | +import requests |
| 5 | +import socket |
| 6 | +import time |
| 7 | + |
| 8 | +N_PINGS = 3 |
| 9 | + |
| 10 | +# This is copy-pasted from multiping package; the reason is that we need to get MultiPing |
| 11 | +# instance, because it holds the IP addresses which correspond to the addresses we wanted |
| 12 | +# pinged - and the key in ping results is the IP. |
| 13 | +def multi_ping(dest_addrs, timeout, retry=0, ignore_lookup_errors=False): |
| 14 | + retry_timeout = float(timeout) / (retry + 1) |
| 15 | + |
| 16 | + mp = MultiPing(dest_addrs, ignore_lookup_errors=ignore_lookup_errors) |
| 17 | + |
| 18 | + results = {} |
| 19 | + retry_count = 0 |
| 20 | + while retry_count <= retry: |
| 21 | + # Send a batch of pings |
| 22 | + mp.send() |
| 23 | + single_results, no_results = mp.receive(retry_timeout) |
| 24 | + # Add the results from the last sending of pings to the overall results |
| 25 | + results.update(single_results) |
| 26 | + if not no_results: |
| 27 | + # No addresses left? We are done. |
| 28 | + break |
| 29 | + retry_count += 1 |
| 30 | + |
| 31 | + return results, no_results, mp |
| 32 | + |
| 33 | +def get_addr_for_ip_dict(addrs, mp): |
| 34 | + # This is complicated, and a hack. Still... mp (MultiPing instance) holds two lists, |
| 35 | + # self._dest_addrs and self._unprocessed_targets. List _unprocessed_targets has the addresses |
| 36 | + # that couldn't be resolved. Others were resolved, and _dest_addrs has the IPs in the same |
| 37 | + # order as original addresses. |
| 38 | + resolved_addrs = [a for a in addrs if a not in mp._unprocessed_targets] |
| 39 | + ip_to_addr = {k: v for k, v in zip(mp._dest_addrs, resolved_addrs)} |
| 40 | + return ip_to_addr |
| 41 | + |
| 42 | +def do_ping(addrs): |
| 43 | + results = defaultdict(list) |
| 44 | + mp = None |
| 45 | + ip_to_addr = {} |
| 46 | + for i in range(N_PINGS): |
| 47 | + print(".") |
| 48 | + responses, no_responses, mp = multi_ping(addrs, timeout=2, retry=3, ignore_lookup_errors=True) |
| 49 | + |
| 50 | + # Some addresses (like demo.grafolean.com) resolve to multiple IPs, so each call to multi_ping will |
| 51 | + # resolve differently - we must find the new IP addresses every time: |
| 52 | + ip_to_addr = get_addr_for_ip_dict(addrs, mp) |
| 53 | + |
| 54 | + for no_resp in no_responses: |
| 55 | + addr = ip_to_addr.get(no_resp, no_resp) |
| 56 | + results[addr].append(None) |
| 57 | + for resp, t in responses.items(): |
| 58 | + addr = ip_to_addr.get(resp, resp) |
| 59 | + results[addr].append(t) |
| 60 | + |
| 61 | + if i < N_PINGS - 1: |
| 62 | + time.sleep(1) |
| 63 | + return dict(results) |
| 64 | + |
| 65 | +def send_results_to_grafolean(base_url, account_id, bot_token, results): |
| 66 | + url = '{}/api/accounts/{}/values/?b={}'.format(base_url, account_id, bot_token) |
| 67 | + values = [] |
| 68 | + for ip in results: |
| 69 | + for ping_index, ping_time in enumerate(results[ip]): |
| 70 | + values.append({ |
| 71 | + 'p': 'ping.{}.{}.success'.format(ip.replace('.', '_'), ping_index), |
| 72 | + 'v': 0 if ping_time is None else 1, |
| 73 | + }) |
| 74 | + if ping_time is not None: |
| 75 | + values.append({ |
| 76 | + 'p': 'ping.{}.{}.rtt'.format(ip.replace('.', '_'), ping_index), |
| 77 | + 'v': ping_time, |
| 78 | + }) |
| 79 | + print("Sending results to Grafolean") |
| 80 | + r = requests.post(url, json=values) |
| 81 | + print(r.text) |
| 82 | + r.raise_for_status() |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + addrs = ["8.8.8.8", "youtube.com", "127.0.0.1", "demo.grafolean.com", "grafolean.com", "whateverdoeesndfexist.com"] |
| 86 | + results = do_ping(addrs) |
| 87 | + send_results_to_grafolean(os.environ.get('BACKEND_URL'), 1, os.environ.get('BOT_TOKEN'), results) |
0 commit comments