Пример #1
0
    def _get_status(self):
        self.new_hosts = dict()

        try:
            events = self._get_all_events()
            for event in events:
                event_check = event['check']
                event_client = event['client']

                new_service = GenericService()
                new_service.event_id = event['id']
                new_service.host = event_client['name']
                new_service.name = event_check['name']
                # Uchiwa needs the 'dc' for re_check; Sensu does not
                if 'dc' in event:
                    new_service.site = event['dc']
                else:
                    new_service.site = None
                new_service.status = None
                try:
                    new_service.status = self.SEVERITY_CODE_TEXT_MAP.get(
                        event_check['status'])
                except KeyError:
                    new_service.status = 'UNKNOWN'
                last_check_time = datetime.utcfromtimestamp(
                    int(event['timestamp']))
                new_service.last_check = self._aslocaltimestr(last_check_time)
                new_service.duration = HumanReadableDurationFromTimestamp(
                    int(event['last_state_change']))
                new_service.status_information = event_check['output']
                # needs a / with a number on either side to work
                new_service.attempt = str(event['occurrences']) + '/1'
                new_service.passiveonly = False
                new_service.notifications_disabled = event['silenced']
                new_service.flapping = False
                new_service.acknowledged = event['silenced']
                new_service.scheduled_downtime = False

                self._insert_service_to_hosts(new_service)
        except:
            self.isChecking = False
            result, error = self.Error(sys.exc_info())
            print(traceback.format_exc())
            return Result(result=result, error=error)

        return Result(error="")
Пример #2
0
 def _parse_event_to_service(self, event):
     service = GenericService()
     namespace_host = event['entity']['metadata'][
         'namespace'] + NAMESPACE_SEPARATOR + event['entity']['metadata'][
             'name']
     service.hostid = namespace_host
     service.host = namespace_host
     service.name = event['check']['metadata']['name']
     service.status = SensuGoAPI.parse_check_status(
         event['check']['status'])
     service.last_check = datetime.fromtimestamp(int(
         event['timestamp'])).strftime('%Y-%m-%d %H:%M:%S')
     service.duration = self._duration_since(event['check']['last_ok'])
     service.status_information = event['check']['output']
     service.acknowledged = event['check']['is_silenced']
     service.notifications_disabled = event['check']['is_silenced']
     service.attempt = str(event['check']['occurrences']) + '/1'
     service.passiveonly = event['check']['publish']
     service.flapping = False
     service.scheduled_downtime = False
     return service
Пример #3
0
    def _get_status(self):
        """
            Get status from Icinga Server and translate it into Nagstamon magic
            generic array
        """
        # new_hosts dictionary
        self.new_hosts = dict()

        # hosts - the down ones
        try:
            # We ask icinga for hosts which are not doing well
            hosts = self._get_host_events()
            for host in hosts:
                host_name = host['attrs']['name']
                if host_name not in self.new_hosts:
                    self.new_hosts[host_name] = GenericHost()
                    self.new_hosts[host_name].name = host_name
                    self.new_hosts[host_name].site = self.name
                    try:
                        self.new_hosts[
                            host_name].status = self.HOST_SEVERITY_CODE_TEXT_MAP.get(
                                host['attrs']['state'])
                    except KeyError:
                        self.new_hosts[host_name].status = 'UNKNOWN'
                    if int(
                            host['attrs']['state_type']
                    ) > 0:  # if state is not SOFT, icinga does not report attempts properly
                        self.new_hosts[host_name].attempt = "{}/{}".format(
                            int(host['attrs']['max_check_attempts']),
                            int(host['attrs']['max_check_attempts']))
                    else:
                        self.new_hosts[host_name].attempt = "{}/{}".format(
                            int(host['attrs']['check_attempt']),
                            int(host['attrs']['max_check_attempts']))
                    self.new_hosts[host_name].last_check = arrow.get(
                        host['attrs']['last_check']).humanize()
                    self.new_hosts[host_name].duration = arrow.get(
                        host['attrs']['previous_state_change']).humanize()
                    self.new_hosts[host_name].status_information = host[
                        'attrs']['last_check_result']['output']
                    self.new_hosts[host_name].passiveonly = not (
                        host['attrs']['enable_active_checks'])
                    self.new_hosts[host_name].notifications_disabled = not (
                        host['attrs']['enable_notifications'])
                    self.new_hosts[host_name].flapping = host['attrs'][
                        'flapping']
                    self.new_hosts[host_name].acknowledged = host['attrs'][
                        'acknowledgement']
                    self.new_hosts[host_name].status_type = {
                        0: "soft",
                        1: "hard"
                    }[host['attrs']['state_type']]
                del host_name
            del hosts

        except Exception as e:
            # set checking flag back to False
            self.isChecking = False
            result, error = self.Error(sys.exc_info())
            log.exception(e)
            return Result(result=result, error=error)

        # services
        try:
            services = self._get_service_events()
            for service in services:
                new_service = GenericService()
                new_service.host = service['attrs']['host_name']
                new_service.name = service['attrs']['name']
                try:
                    new_service.status = self.SERVICE_SEVERITY_CODE_TEXT_MAP.get(
                        service['attrs']['state'])
                except KeyError:
                    new_service.status = 'UNKNOWN'
                if int(
                        service['attrs']['state_type']
                ) > 0:  # if state is not SOFT, icinga does not report attempts properly
                    new_service.attempt = "{}/{}".format(
                        int(service['attrs']['max_check_attempts']),
                        int(service['attrs']['max_check_attempts']))
                else:
                    new_service.attempt = "{}/{}".format(
                        int(service['attrs']['check_attempt']),
                        int(service['attrs']['max_check_attempts']))
                new_service.last_check = arrow.get(
                    service['attrs']['last_check']).humanize()
                new_service.duration = arrow.get(
                    service['attrs']['previous_state_change']).humanize()
                new_service.status_information = service['attrs'][
                    'last_check_result']['output']
                new_service.passiveonly = not (
                    service['attrs']['enable_active_checks'])
                new_service.notifications_disabled = not (
                    service['attrs']['enable_notifications'])
                new_service.flapping = service['attrs']['flapping']
                new_service.acknowledged = service['attrs']['acknowledgement']
                new_service.status_type = {
                    0: "soft",
                    1: "hard"
                }[service['attrs']['state_type']]
                self._insert_service_to_hosts(new_service)
            del services

        except Exception as e:
            log.exception(e)
            # set checking flag back to False
            self.isChecking = False
            result, error = self.Error(sys.exc_info())
            return Result(result=result, error=error)

        # dummy return in case all is OK
        return Result()