File: //lib/fm-agent/plugins/ping.py
import agent_util
import sys
import os
import time
import string
import re
class PingPlugin(agent_util.Plugin):
textkey = "agent_ping"
label = "Agent Ping"
@classmethod
def get_metadata(self, config):
status = agent_util.SUPPORTED
msg = None
data = {
"ping_status": {
"label": "Ping status",
"options": None,
"option_string": True,
"status": status,
"error_message": msg,
"unit": "boolean",
},
"ping_packet_loss": {
"label": "Ping packet loss",
"options": None,
"option_string": True,
"status": status,
"error_message": msg,
"unit": "percent",
},
"ping_latency": {
"label": "Ping latency",
"options": None,
"option_string": True,
"status": status,
"error_message": msg,
"unit": "ms",
},
}
return data
def check(self, textkey, option, config):
ping_binary_path = agent_util.which("ping")
ping_timeout = 5.0
if "darwin" in sys.platform.lower():
ping_timeout = 5000.0
if textkey == "ping_status":
ret, output = agent_util.execute_command(
"%s -c 1 -W %s %s" % (ping_binary_path, ping_timeout, option)
)
if not ret:
return 1
else:
return None
elif textkey == "ping_packet_loss":
ret, output = agent_util.execute_command(
"%s -c 10 -i 0.2 -W %s %s 2> /dev/null"
% (ping_binary_path, ping_timeout * 2, option)
)
for line in output.split("/n"):
if "packet loss" in line:
pl = float(
re.search(r"([-+]?\d*\.\d+|\d+)% packet loss", line).group(1)
)
return pl
# if no match, default to 100% packet loss
return 100.0
elif textkey == "ping_latency":
ret, output = agent_util.execute_command(
"%s -c 1 -W %s %s" % (ping_binary_path, ping_timeout, option)
)
for line in output.split("/n"):
if "time" in line:
rt = float(re.search(r"time=([-+]?\d*\.\d+|\d+) ms", line).group(1))
return rt
# if no match, default to 0 ms
return None
else:
return None