Ejemplo n.º 1
0
 def listen(self):
     log.info('Start listening for messages, topics = %s' %
              self.getConsumeKeys())
     actionPrefix = self.getActionPrefix()
     heartBeatRequestPrefix = self.getHeartBeatRequestPrefix()
     while not self._shutdown:
         log.notice('Waiting for messages')
         try:
             topic, data, correlationId = self.consumer.get()
         except socket.error as e:
             self.reconnect(e)
             continue
         except amqplib.client_0_8.exceptions.AMQPConnectionException as e:
             self.reconnect(e)
             continue
         except IOError as e:
             self.reconnect(e)
             continue
         except Exception as e:
             log.error('Caught during consumer.get() in listen: %s' %
                       utils.e2str(e))
             raise e
         try:
             self.consumer.ack()
         except Exception as e:
             log.error('Caught during consumer.ack() in listen: %s' %
                       utils.e2str(e))
             raise e
         if self._shutdown:
             log.info(
                 'Not handeling topic: %s because self._shutdown = True' %
                 (topic, ))
         else:
             log.notice('Handeling topic: %s' % topic)
             tmp = topic.split('.')
             handle = False
             if tmp[0] == actionPrefix and len(
                     tmp) > 1 and tmp[1] == self.id:
                 pre, src, key = tmp
                 if key == '_shutdown':
                     continue
                 if not data:
                     data = []
                 try:
                     result = self.runAction(key, data)
                 except Exception as e:
                     self.sendActionError(key, e, correlationId)
                 else:
                     if result is not None or correlationId:
                         self.sendActionResponse(key, result, correlationId)
             elif tmp[0] == heartBeatRequestPrefix:
                 self.sendHeartBeatResponse()
             else:
                 self.onMessage(topic, data, correlationId)
             log.notice('Handeled topic: %s' % topic)
     log.info('Exited listen() loop - self._shutdown = %s' % self._shutdown)
Ejemplo n.º 2
0
 def listen(self):
     log.info('Start listening for messages, topics = %s' % self.getConsumeKeys())
     actionPrefix = self.getActionPrefix()
     heartBeatRequestPrefix = self.getHeartBeatRequestPrefix()
     while not self._shutdown:
         log.notice('Waiting for messages')
         try:
             topic, data, correlationId = self.consumer.get()
         except socket.error as e:
             self.reconnect(e)
             continue
         except amqplib.client_0_8.exceptions.AMQPConnectionException as e:
             self.reconnect(e)
             continue
         except IOError as e:
             self.reconnect(e)
             continue
         except Exception as e:
             log.error('Caught during consumer.get() in listen: %s' % utils.e2str(e))
             raise e
         try:
             self.consumer.ack()
         except Exception as e:
             log.error('Caught during consumer.ack() in listen: %s' % utils.e2str(e))
             raise e
         if self._shutdown:
             log.info('Not handeling topic: %s because self._shutdown = True' % (topic,))
         else:
             log.notice('Handeling topic: %s' % topic)
             tmp = topic.split('.')
             handle = False
             if tmp[0] == actionPrefix and len(tmp) > 1 and tmp[1] == self.id:
                 pre, src, key = tmp
                 if key == '_shutdown':
                     continue
                 if not data:
                     data = []
                 try:
                     result = self.runAction(key, data)
                 except Exception as e:
                     self.sendActionError(key, e, correlationId)
                 else:
                     if result is not None or correlationId:
                         self.sendActionResponse(key, result, correlationId)
             elif tmp[0] == heartBeatRequestPrefix:
                 self.sendHeartBeatResponse()
             else:
                 self.onMessage(topic, data, correlationId)
             log.notice('Handeled topic: %s' % topic)
     log.info('Exited listen() loop - self._shutdown = %s' % self._shutdown)
Ejemplo n.º 3
0
    def poll(self):

        data = {}

        keys = [
            'firmware_version',
            'power_loss',
            'battery_voltage',
            'rpi_voltage',
            'sot23_temperature',
            'to92_temperature',
            'shutdown_time',
            'battery_temperature_on_error',
            'ad1',
            'ad2'
        ]

        for key in keys:
            func = 'get_%s' % key
            try:
                func = getattr(self, func)
                value = func()
            except Exception, e:
                log.warn("Ignoring error on get %s: %s" % (key, utils.e2str(e)))
                continue
            data[key] = value
Ejemplo n.º 4
0
 def run(self):
     try:
         self.func()
     except Exception, e:
         log.error("Thread for %s in service %s died: %s" % (
             self.name,
             self.serviceId,
             utils.e2str(e)
         ))
Ejemplo n.º 5
0
 def sendActionError(self, key, data, correlationId=None):
     if isinstance(data, Exception):
         resp = {'errorMessage': data.message, 'errorDetails': utils.e2str(data), 'remoteError': None}
         if isinstance(data, RemoteException):
             resp['remoteError'] = data.getResponse()
     else:
         resp = data
     resp['error'] = True
     resp['source'] = '%s.%s.%s' % (self.type, self.id, key)
     topic = '%s.%s.%s' % (self.getActionErrorPrefix(), self.id, key)
     log.debug('sendActionError: %s = %s' % (topic, resp))
     self.producer.put(topic, resp, correlationId)
Ejemplo n.º 6
0
def runWithSignalHandler(daemon):
    def signalHandler(signal, frame):
        daemon.shutdown()
        daemon.sendOfflineEvent()
        sys.exit(0)
    signal.signal(signal.SIGTERM, signalHandler)  # $ kill <pid>
    signal.signal(signal.SIGINT, signalHandler)  # Ctrl-C
    try:
        daemon.run()
    except Exception as e:
        log.error('Daemon crashed: %s' % utils.e2str(e))
        raise e
Ejemplo n.º 7
0
def runWithSignalHandler(daemon):
    def signalHandler(signal, frame):
        daemon.shutdown()
        daemon.sendOfflineEvent()
        sys.exit(0)

    signal.signal(signal.SIGTERM, signalHandler)  # $ kill <pid>
    signal.signal(signal.SIGINT, signalHandler)  # Ctrl-C
    try:
        daemon.run()
    except Exception as e:
        log.error('Daemon crashed: %s' % utils.e2str(e))
        raise e
Ejemplo n.º 8
0
 def sendActionError(self, key, data, correlationId=None):
     if isinstance(data, Exception):
         resp = {
             'errorMessage': data.message,
             'errorDetails': utils.e2str(data),
             'remoteError': None
         }
         if isinstance(data, RemoteException):
             resp['remoteError'] = data.getResponse()
     else:
         resp = data
     resp['error'] = True
     resp['source'] = '%s.%s.%s' % (self.type, self.id, key)
     topic = '%s.%s.%s' % (self.getActionErrorPrefix(), self.id, key)
     log.debug('sendActionError: %s = %s' % (topic, resp))
     self.producer.put(topic, resp, correlationId)
Ejemplo n.º 9
0
    def poll(self):

        data = {}

        keys = [
            'firmware_version', 'power_loss', 'battery_voltage', 'rpi_voltage',
            'sot23_temperature', 'to92_temperature', 'shutdown_time',
            'battery_temperature_on_error', 'ad1', 'ad2'
        ]

        for key in keys:
            func = 'get_%s' % key
            try:
                func = getattr(self, func)
                value = func()
            except Exception, e:
                log.warn("Ignoring error on get %s: %s" %
                         (key, utils.e2str(e)))
                continue
            data[key] = value
Ejemplo n.º 10
0
def main(req):
    error = None
    opt = parse(req)
    if opt.debug:
        log.setLevel("debug")
    obj = getCommandObject(opt.section, opt.command)
    if not obj:
        print "No such command: %s %s" % (opt.section, opt.command)
        sys.exit(1)
    try:
        result = obj.main(*opt.args)
        if opt.json:
            fmt = formatter.load("json")
        elif opt.raw:
            fmt = formatter.load("generic")
        else:
            fmt = formatter.load(obj.getFormatter())
        if not opt.json:
            print ""
        result = fmt.main(result)
        if result:
            print result
        if not opt.json:
            print ""
    except amqp.RemoteException, e:
        error = e.message + "\n\n"
        if opt.verbose or opt.debug:
            line = "=" * 60
            error += e.getRemoteTraces()
            error += "\n"
            error += "%s\n" % line
            error += "chains-admin %s %s\n" % (opt.section, opt.command)
            error += "%s\n" % line
            error += utils.e2str(e)
        else:
            error += "Add -v to see error trace\n"
Ejemplo n.º 11
0
 def onMessage(self, topic, data, correlationId):
     self.aggregated['total_messages'] += 1
     if topic.startswith('se.') and not topic.endswith('.online'):
         # aggregation: update the total number of events from this service and class
         cursrv = topic.split('.')[1]
         if cursrv in self.aggregated['service_events']:
             self.aggregated['service_events'][cursrv] += 1
         else:
             self.aggregated['service_events'][cursrv] = 1
         if 'class' in data:
             if data['class'] in self.aggregated['class_events']:
                 self.aggregated['class_events'][data['class']] += 1
             else:
                 self.aggregated['class_events'][data['class']] = 1
         # Ignore is in config ignoreclasses or ignore is set for the service
         if 'class' in data:
             if data['class'] in self.ignoreclasses:
                 return
         if 'ignore' in data:
             if data['ignore'] in [True, 'True', 'true', 'Yes', 'yes', 'y']:
                 return
         # start picking out data to report
         measures = []
         tags = {}
         for tag in data:
             if tag in self.known_tags:
                 if tag == 'key':
                     tags.update({'event': data[tag]})
                 else:
                     tagValue = data[tag]
                     if type(tagValue) == types.UnicodeType:
                         tagValue = tagValue.encode('utf-8')
                     tags.update({tag: tagValue})
         for measure in data['data']:
             if 'value' in data['data'][measure]:
                 curval = data['data'][measure]['value']
                 if isinstance(curval, numbers.Number):
                     measures.append(self.ix.data_template(measure, tags, {'value': curval}))
                     # log.info('field: %s: %s' % (measure, str(curval)))
                     # log.info('tags: %s' % str(tags))
                 elif curval in [True, 'True', 'true', 'On', 'on', 'Yes', 'yes']:
                     measures.append(self.ix.data_template(measure, tags, {'value': True}))
                 elif curval in [False, 'False', 'false', 'Off', 'off', 'No', 'no']:
                     measures.append(self.ix.data_template(measure, tags, {'value': False}))
                 else:
                     # log.info('Skipping because value is not a number:')
                     # log.info("topic: " + str(topic))
                     # log.info("data: " + str(data))
                     pass
         log.debug('insert measures: %s' % measures)
         try:
             self.ix.insert(measures)
         except Exception, e:
             log.error('Failed inserting measures in influxdb: %s\nAttempted insert was: %s' % (utils.e2str(e), measures))
Ejemplo n.º 12
0
 def getConfig(self, path):
     try:
         return config.BaseConfig(path)
     except Exception, e:
         log.error("Error loading config: %s, because: %s" % (path,utils.e2str(e)))
         return None
Ejemplo n.º 13
0
def main(req):
    error = None
    opt = parse(req)
    if opt.debug:
        log.setLevel('debug')
    obj = getCommandObject(opt.section, opt.command)
    if not obj:
        print 'No such command: %s %s' % (opt.section, opt.command)
        sys.exit(1)
    if opt.help:
        print ""
        lines = obj.help()
        first = True
        tail = False
        indent = 0
        for line in lines.split("\n"):
            if first:
                _line = line.strip()
                if not _line:
                    continue
                for c in line:
                    if c != ' ':
                        break
                    indent += 1
            first = False
            line = line[indent:]
            print line
            if not line:
                tail = True
        if not tail:
            print ""
    else:
        try:
            result = obj.main(*opt.args)
            if opt.json:
                fmt = formatter.load('json')
            elif opt.raw:
                fmt = formatter.load('generic')
            else:
                fmt = formatter.load(obj.getFormatter())
            if not opt.json:
                print ''
            result = fmt.main(result)
            if result: print result
            if not opt.json:
                print ''
        except amqp.RemoteException, e:
            error = e.message + '\n\n'
            if opt.verbose or opt.debug:
                line = '=' * 60
                error += e.getRemoteTraces()
                error += '\n'
                error += '%s\n' % line
                error += 'chains-admin %s %s\n' % (opt.section, opt.command)
                error += '%s\n' % line
                error += utils.e2str(e)
            else:
                error += 'Add -v to see error trace\n'
        except Exception, e:
            error = e.message + '\n'
            if opt.verbose or opt.debug:
                error += '\n' + utils.e2str(e)
            else:
                error += 'Add -v to see error trace\n'
Ejemplo n.º 14
0
 def onMessage(self, topic, data, correlationId):
     self.aggregated['total_messages'] += 1
     if topic.startswith('se.') and not topic.endswith('.online'):
         # aggregation: update the total number of events from this service and class
         cursrv = topic.split('.')[1]
         if cursrv in self.aggregated['service_events']:
             self.aggregated['service_events'][cursrv] += 1
         else:
             self.aggregated['service_events'][cursrv] = 1
         if 'class' in data:
             if data['class'] in self.aggregated['class_events']:
                 self.aggregated['class_events'][data['class']] += 1
             else:
                 self.aggregated['class_events'][data['class']] = 1
         # Ignore is in config ignoreclasses or ignore is set for the service
         if 'class' in data:
             if data['class'] in self.ignoreclasses:
                 return
         if 'ignore' in data:
             if data['ignore'] in [True, 'True', 'true', 'Yes', 'yes', 'y']:
                 return
         # start picking out data to report
         measures = []
         tags = {}
         for tag in data:
             if tag in self.known_tags:
                 if tag == 'key':
                     tags.update({'event': data[tag]})
                 else:
                     tagValue = data[tag]
                     if type(tagValue) == types.UnicodeType:
                         tagValue = tagValue.encode('utf-8')
                     tags.update({tag: tagValue})
         for measure in data['data']:
             if 'value' in data['data'][measure]:
                 curval = data['data'][measure]['value']
                 if isinstance(curval, numbers.Number):
                     measures.append(
                         self.ix.data_template(measure, tags,
                                               {'value': curval}))
                     # log.info('field: %s: %s' % (measure, str(curval)))
                     # log.info('tags: %s' % str(tags))
                 elif curval in [
                         True, 'True', 'true', 'On', 'on', 'Yes', 'yes'
                 ]:
                     measures.append(
                         self.ix.data_template(measure, tags,
                                               {'value': True}))
                 elif curval in [
                         False, 'False', 'false', 'Off', 'off', 'No', 'no'
                 ]:
                     measures.append(
                         self.ix.data_template(measure, tags,
                                               {'value': False}))
                 else:
                     # log.info('Skipping because value is not a number:')
                     # log.info("topic: " + str(topic))
                     # log.info("data: " + str(data))
                     pass
         log.debug('insert measures: %s' % measures)
         try:
             self.ix.insert(measures)
         except Exception, e:
             log.error(
                 'Failed inserting measures in influxdb: %s\nAttempted insert was: %s'
                 % (utils.e2str(e), measures))
Ejemplo n.º 15
0
from chains.service import process
from chains.service import config # as serviceConfig
from chains import service
from chains.common import log, utils
#from chains.common import config as globalConfig

from .runservice import signalHandler, parseConfigParam, checkNotRunning

serviceObject = None

serviceConfig = parseConfigParam()
serviceId     = serviceConfig.get('id')

log.setFileName('service-%s-%s' % (serviceConfig.get('name'), serviceId))
if serviceConfig.get('loglevel'):
    log.setLevel(serviceConfig.get('loglevel'))

#checkNotRunning(serviceId)
process.setPid(serviceId, os.getpid())

try:
    serviceObject = service.factory(serviceConfig)

    signal.signal(signal.SIGTERM, signalHandler) # $ kill <pid>
    signal.signal(signal.SIGINT, signalHandler)  # Ctrl-C

    serviceObject.start(block=True)
except Exception, e:
    log.error('Service crashed: %s' % serviceId)
    log.error(utils.e2str(e))
Ejemplo n.º 16
0
def main(req):
    error = None
    opt = parse(req)
    if opt.debug:
        log.setLevel('debug')
    obj = getCommandObject(opt.section, opt.command)
    if not obj:
        print 'No such command: %s %s' % (opt.section,opt.command)
        sys.exit(1)
    if opt.help:
        print ""
        lines = obj.help()
        first = True
        tail = False
        indent = 0
        for line in lines.split("\n"):
            if first:
                _line = line.strip()
                if not _line:
                    continue
                for c in line:
                    if c != ' ':
                        break
                    indent += 1
            first = False
            line = line[indent:]
            print line
            if not line:
                tail = True
        if not tail:
            print ""
    else:
        try:
            result = obj.main(*opt.args)
            if opt.json:
                fmt = formatter.load('json')
            elif opt.raw:
                fmt = formatter.load('generic')
            else:
                fmt = formatter.load(obj.getFormatter())
            if not opt.json:
                print ''
            result = fmt.main(result)
            if result: print result
            if not opt.json:
                print ''
        except amqp.RemoteException, e:
            error = e.message + '\n\n'
            if opt.verbose or opt.debug:
                line  = '='*60
                error += e.getRemoteTraces()
                error += '\n'
                error += '%s\n' % line
                error += 'chains-admin %s %s\n' % (opt.section, opt.command)
                error += '%s\n' % line
                error += utils.e2str(e)
            else:
                error += 'Add -v to see error trace\n'
        except Exception, e:
            error = e.message + '\n'
            if opt.verbose or opt.debug:
                error += '\n' + utils.e2str(e)
            else:
                error += 'Add -v to see error trace\n'
Ejemplo n.º 17
0
    except amqp.RemoteException, e:
        error = e.message + "\n\n"
        if opt.verbose or opt.debug:
            line = "=" * 60
            error += e.getRemoteTraces()
            error += "\n"
            error += "%s\n" % line
            error += "chains-admin %s %s\n" % (opt.section, opt.command)
            error += "%s\n" % line
            error += utils.e2str(e)
        else:
            error += "Add -v to see error trace\n"
    except Exception, e:
        error = e.message + "\n"
        if opt.verbose or opt.debug:
            error += "\n" + utils.e2str(e)
        else:
            error += "Add -v to see error trace\n"
    finally:
        obj.close()

    if error:
        print ""
        print "ERROR: %s" % error


def shell():
    chainsCmd = ChainsCmd()
    chainsCmd.cmdloop()

Ejemplo n.º 18
0
from chains.service import process
from chains.service import config  # as serviceConfig
from chains import service
from chains.common import log, utils
#from chains.common import config as globalConfig

from .runservice import signalHandler, parseConfigParam, checkNotRunning

serviceObject = None

serviceConfig = parseConfigParam()
serviceId = serviceConfig.get('id')

log.setFileName('service-%s-%s' % (serviceConfig.get('name'), serviceId))
if serviceConfig.get('loglevel'):
    log.setLevel(serviceConfig.get('loglevel'))

#checkNotRunning(serviceId)
process.setPid(serviceId, os.getpid())

try:
    serviceObject = service.factory(serviceConfig)

    signal.signal(signal.SIGTERM, signalHandler)  # $ kill <pid>
    signal.signal(signal.SIGINT, signalHandler)  # Ctrl-C

    serviceObject.start(block=True)
except Exception, e:
    log.error('Service crashed: %s' % serviceId)
    log.error(utils.e2str(e))
Ejemplo n.º 19
0
 heartBeatRequestPrefix = self.getHeartBeatRequestPrefix()
 while not self._shutdown:
     log.notice('Waiting for messages')
     try:
         topic, data, correlationId = self.consumer.get()
     except socket.error, e:
         self.reconnect(e)
         continue
     except amqplib.client_0_8.exceptions.AMQPConnectionException, e:
         self.reconnect(e)
         continue
     except IOError, e:
         self.reconnect(e)
         continue
     except Exception, e:
         log.error('Caught during consumer.get() in listen: %s' % utils.e2str(e))
         raise e
     try:
         self.consumer.ack()
     except Exception, e:
         log.error('Caught during consumer.ack() in listen: %s' % utils.e2str(e))
         raise e
     if self._shutdown:
         log.info('Not handeling topic: %s because self._shutdown = True' % (topic,))
     else:
         log.notice('Handeling topic: %s' % topic)
         tmp = topic.split('.')
         handle = False
         if tmp[0] == actionPrefix and len(tmp) > 1:
             pre, src, key = tmp
             if key == '_shutdown':