-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathping.py
More file actions
42 lines (36 loc) · 1.69 KB
/
ping.py
File metadata and controls
42 lines (36 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import subprocess
import configuration
from typing import List
from hostentry import HostEntry
class Ping(object):
def __init__(self, hostentry: HostEntry) -> None:
self.hostentry = hostentry
self.ping_ok = False
self.ping_size = -1
self.ping_time = -1
def ping(self) -> None:
try:
ping_cmd = [configuration.PingExecutableLocation, '-c', '1', self.hostentry.ip]
output = subprocess.run(ping_cmd, shell=False, stdout=subprocess.PIPE, encoding='utf-8').stdout
for line in output.split('\n'):
if not line:
break
stripped_line = str(line).strip()
if 'icmp_seq' in stripped_line and self.hostentry.ip in stripped_line:
ping_size, _, _, ipv4, _, _, time_string, _ = stripped_line.split(' ')
self.ping_size = int(ping_size)
self.ping_ok = True
self.ping_time = float(time_string.split('=')[1])
except Exception as exc:
print(exc)
self.ping_ok = False
self.ping_size = -1
self.ping_time = -1
def export_prometheus_metric(self) -> List[str]:
metric_collection = []
for hostname in self.hostentry.hostnames:
ping_status = '1' if self.ping_ok else '0'
metric_collection.append('nde_ping_ok{target_hostname="' + hostname + '"} ' + str(ping_status))
metric_collection.append('nde_ping_size{target_hostname="' + hostname + '"} ' + str(self.ping_size))
metric_collection.append('nde_ping_time{target_hostname="' + hostname + '"} ' + str(self.ping_time))
return metric_collection