Last active
October 19, 2022 09:14
-
-
Save dunielpls/ccaed1dcd2c5f6da04b9b26cc1927449 to your computer and use it in GitHub Desktop.
run fping and parse its output, "unreachable" or json stats. requires python 3.5. made for use with zabbix.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import json | |
import subprocess | |
# '1.1.1.1 : xmt/rcv/%loss = 1/1/0%, min/avg/max = 1.35/1.35/1.35' | |
def parse(s): | |
data = {} | |
# sorry, I've been doing too much Erlang lately | |
[host, s1] = s.split(" : ") | |
[stats, ping] = s1.split(", ") | |
# get packet loss % | |
[_, s2] = stats.split(" = ") | |
[sent, recv, loss] = s2.split("/") | |
# get avg latency | |
[_, s3] = ping.split(" = ") | |
[min_, avg, max_] = s3.split("/") | |
return { | |
"host": host, | |
"sent": sent, | |
"recv": recv, | |
"loss": loss.strip("%"), | |
"min": min_, | |
"avg": avg, | |
"max": max_ | |
} | |
def main(host, key): | |
key = key.lower() | |
if key not in "host sent recv loss min avg max".split(): | |
sys.exit(1) | |
cmd = subprocess.run(["/usr/sbin/fping", "-q", "-c2", host], | |
stdout=subprocess.PIPE, | |
stderr=subprocess.STDOUT) | |
output = cmd.stdout.decode("utf-8").strip() | |
parsed = parse(output) | |
print(parsed[key]) | |
sys.exit(0) | |
if __name__ == "__main__": | |
if len(sys.argv) != 3: | |
sys.exit(1) | |
main(sys.argv[1], sys.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment