Exemple #1
0
    def on_receive(self, msg):
        logger.debug("Received admin command: %s" % msg)

        cmd = msg.get('command', '')
        if cmd.count('.') == 1:
            subject, action = cmd.split('.')

            # hosts
            if subject == 'hosts':

                # .list
                if action == 'list':
                    return {'hosts': [n.to_json() for n in Session.query(Host).filter(Host.acknowledged==True)]}

                # .details
                elif action == 'details':
                    host = Session.query(Host).filter(Host.id==msg['args'][0]).first()
                    return {'host': host.to_json()}

            # autohosts
            elif subject == 'autohosts':

                # .list
                if action == 'list':
                    return {'hosts': [n.to_json() for n in Session.query(Host).filter(Host.acknowledged==False)]}

                # .add
                elif action == 'add':
                    host = Session.query(Host).filter(Host.id==msg['args'][0]).first()
                    host.acknowledged = True
                    host.key = uuid4()
                    Session.add(host)
                    Session.commit()
                    return {'key': host.key}

                # .decline
                elif action == 'decline':
                    host = Session.query(Host).filter(Host.id==msg['args'][0]).first()
                    Session.delete(host)
                    Session.commit()
                    return {}

            # values
            elif subject == 'values':

                # .latest
                if action == 'latest':
                    host = Session.query(Host).filter(Host.id==msg['args'][0]).first()
                    return {'values': [v.to_json() for v in host.reports[-1].values.all()]}

            # triggers
            elif subject == 'triggers':

                # .list
                if action == 'list':
                    return {'triggers': [t.to_json() for t in Session.query(Trigger).all()]}

                # .add
                if action == 'add':
                    trigger = Trigger(
                        host_id     = msg['args'][0]['host'],
                        name        = msg['args'][0]['name'],
                        description = msg['args'][0]['description'],
                        expression  = msg['args'][0]['expression'],
                        action      = msg['args'][0]['action']
                    )
                    Session.add(trigger)
                    Session.commit()
                    return {'id': trigger.id}

            # actions
            elif subject == 'actions':

                # .list
                if action == 'list':
                    return {'actions': Action.get_actions()}

        else:
            raise PackageError('Missing or invalid command')