File: //lib/python2.7/site-packages/lap/check_ping_ed.py
import subprocess
import re
def run_ping(host, count):
command = "/bin/ping -f -q -c {0} -n {1}".format(count, host)
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ping = process.communicate()
return(ping)
def get_interface(interface):
if interface:
process = subprocess.Popen("/sbin/ifconfig", stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ifconfig = process.communicate()
hasinterface = [x for x in ifconfig[0].split() if x == interface]
return(hasinterface)
def __run__(params):
try:
intout = params.get("interface", False)
remoteaddr = params.get("ip", False)
maxloss = params.get("maxloss", 50)
maxtime = params.get("maxtime", 3000)
interface = get_interface(intout) if intout else False
if not remoteaddr:
return([2, "Verifique o YAML e informe os valores para IP/HOST?"])
elif not interface:
return([0, "Interface desconhecida - Servidor Stand-by."])
else:
ping = run_ping(remoteaddr, 10)
loss = re.search(r"\d%", ping[0])
time = re.search(r"\dms", ping[0])
if int(loss.group().replace("%", "")) <= maxloss and int(time.group().replace("ms", "")) <= maxtime:
return([0, "OK - Perda de Pacotes: {0} - Latencia: {1}".format(loss.group(), time.group())])
else:
return([2, "Perda de Pacotes: {0} - Latencia: {1}".format(loss.group(), time.group())])
except Exception, err:
return([2, err])