File: //usr/lib/python2.7/site-packages/lap/weatherman.py
import json
import netifaces
import os
import psutil
import socket
import sys
import time
import xmpp
socket.setdefaulttimeout(10)
def check_log(message, logfile, logseek):
f = file(logfile)
f.seek(logseek)
for line in f.readlines():
if message in line:
return True
return False
def check_ha(iface):
try:
netifaces.ifaddresses(iface)
return True
except Exception, e:
return False
def check_process():
for name in psutil.get_pid_list():
p = psutil.Process(name).cmdline
if "weatherman" in p:
return True
return False
def __run__(params):
iface = params.get("iface")
if check_ha(iface):
if check_process():
jid_to = params.get("jid_to")
logfile = params.get("logfile")
start = time.time()
jid = xmpp.protocol.JID(params.get("jid_from"))
cl = xmpp.Client(jid.getDomain(),debug=[])
logseek = os.stat(logfile).st_size
if not cl.connect():
return [2, "Could not connect to xmpp server"]
if not cl.auth(jid.getNode(), params.get("jid_password"), resource=jid.getResource()):
return [2, "Could not authenticate on xmpp server"]
message = json.dumps({"timestamp": int(time.time()), "name": params.get("jid_data"), "value": 88.8})
if not cl.send(xmpp.protocol.Message(jid_to, message)):
return [2, "Could not send message to: %s" % jid_to]
cl.disconnect()
time.sleep(1.0)
if not check_log(message, logfile, logseek=logseek):
return [2, "Message not received on weatherman"]
total = time.time() - start
return[0, "OK - Total runtime: %.2f" % total]
else:
return[2, "Weatherman not running"]
else:
return[0, "OK"]