nagios-plugins/z_server/z_protocol.py

35 lines
1.6 KiB
Python

import nagios
import socket_protocol
# from devdi.topic import topic_by_props
DID_FOLLOWS_SETPOINT = 'follow_setpoint'
DID_BATTERY_LEVEL = 'battery'
DID_HEARTBEAT = 'heartbeat'
DID_LINKQUALITY = 'linkquality'
class server(socket_protocol.pure_json_protocol):
def __init__(self, *args, **kwargs):
if 'devices' in kwargs:
self.__devices__ = kwargs.pop('devices')
socket_protocol.pure_json_protocol.__init__(self, *args, **kwargs)
#
if not self.__comm_inst__.IS_CLIENT:
self.register_callback(socket_protocol.SID_READ_REQUEST, DID_FOLLOWS_SETPOINT, self.device_status)
self.register_callback(socket_protocol.SID_READ_REQUEST, DID_BATTERY_LEVEL, self.device_status)
self.register_callback(socket_protocol.SID_READ_REQUEST, DID_LINKQUALITY, self.device_status)
self.register_callback(socket_protocol.SID_READ_REQUEST, DID_HEARTBEAT, self.device_status)
def device_status(self, msg):
if msg.get_status() == socket_protocol.STATUS_OKAY:
try:
dev = self.__devices__.get_str(**msg.get_data())
except:
return socket_protocol.STATUS_CALLBACK_ERROR, {"status": nagios.Nagios.UNKNOWN, "msg": "Device does not exist."}
if dev is None:
return socket_protocol.STATUS_SERVICE_OR_DATA_UNKNOWN, {"status": nagios.Nagios.UNKNOWN, "msg": "Device does not exist."}
else:
return socket_protocol.STATUS_OKAY, dev.status(msg.get_data_id())
else:
return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, {"status": nagios.Nagios.UNKNOWN, "msg": "Socket protocol error."}