Exemplo n.º 1
0
    def _get_status(self):
        """
        Get status from Opsview Server
        """
        # following XXXX to get ALL services in ALL states except OK
        # because we filter them out later
        # the REST API gets all host and service info in one call
        try:
            result = self.FetchURL(
                self.monitor_url +
                "/rest/status/service?state=1&state=2&state=3",
                giveback="raw")
            data = json.loads(result.result)

            if str(self.conf.debug_mode) == "True":
                self.Debug(server=self.get_name(),
                           debug="Fetched JSON: " + pprint.pformat(data))

            #for host in xmlobj.opsview.findAll("item"):
            for host in data["list"]:
                self.new_hosts[host["name"]] = GenericHost()
                self.new_hosts[host["name"]].name = str(host["name"])
                self.new_hosts[host["name"]].server = self.name
                # states come in lower case from Opsview
                self.new_hosts[host["name"]].status = str(
                    host["state"].upper())
                self.new_hosts[host["name"]].status_type = str(
                    host["state_type"])
                self.new_hosts[
                    host["name"]].last_check = datetime.fromtimestamp(
                        int(host["last_check"])).strftime(
                            "%Y-%m-%d %H:%M:%S %z")
                self.new_hosts[host[
                    "name"]].duration = Actions.HumanReadableDurationFromSeconds(
                        host["state_duration"])
                self.new_hosts[host["name"]].attempt = host[
                    "current_check_attempt"] + "/" + host["max_check_attempts"]
                self.new_hosts[
                    host["name"]].status_information = host["output"].replace(
                        "\n", " ")

                # if host is in downtime add it to known maintained hosts
                if host["downtime"] == "2":
                    self.new_hosts[host["name"]].scheduled_downtime = True
                if host.has_key("acknowledged"):
                    self.new_hosts[host["name"]].acknowledged = True
                if host.has_key("flapping"):
                    self.new_hosts[host["name"]].flapping = True

                #services
                for service in host["services"]:
                    self.new_hosts[host["name"]].services[
                        service["name"]] = OpsviewService()
                    self.new_hosts[host["name"]].services[
                        service["name"]].host = str(host["name"])
                    self.new_hosts[host["name"]].services[
                        service["name"]].name = service["name"]
                    self.new_hosts[host["name"]].services[
                        service["name"]].server = self.name

                    # states come in lower case from Opsview
                    self.new_hosts[host["name"]].services[
                        service["name"]].status = service["state"].upper()
                    self.new_hosts[host["name"]].services[
                        service["name"]].status_type = service["state_type"]
                    self.new_hosts[host["name"]].services[
                        service["name"]].last_check = datetime.fromtimestamp(
                            int(service["last_check"])).strftime(
                                "%Y-%m-%d %H:%M:%S %z")
                    self.new_hosts[host["name"]].services[service[
                        "name"]].duration = Actions.HumanReadableDurationFromSeconds(
                            service["state_duration"])
                    self.new_hosts[host["name"]].services[
                        service["name"]].attempt = service[
                            "current_check_attempt"] + "/" + service[
                                "max_check_attempts"]
                    self.new_hosts[host["name"]].services[
                        service["name"]].status_information = service[
                            "output"].replace("\n", " ")
                    if service["downtime"] == "2":
                        self.new_hosts[host["name"]].services[
                            service["name"]].scheduled_downtime = True
                    if service.has_key("acknowledged"):
                        self.new_hosts[host["name"]].services[
                            service["name"]].acknowledged = True
                    if service.has_key("flapping"):
                        self.new_hosts[host["name"]].services[
                            service["name"]].flapping = True

                    # extra opsview id for service, needed for submitting check results
                    self.new_hosts[host["name"]].services[
                        service["name"]].service_object_id = service[
                            "service_object_id"]

        except:
            # 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()
Exemplo n.º 2
0
    def _get_status(self):
        """
        Get status from Opsview Server
        """
        # following http://docs.opsview.org/doku.php?id=opsview3.4:api to get ALL services in ALL states except OK
        # because we filter them out later
        # the API seems not to let hosts information directly, we hope to get it from service informations
        try:
            result = self.FetchURL(
                self.monitor_url +
                "/api/status/service?state=1&state=2&state=3",
                giveback="xml")
            xmlobj, error = result.result, result.error
            if error != "":
                return Result(result=xmlobj, error=copy.deepcopy(error))

            for host in xmlobj.data.findAll("list"):
                # host
                hostdict = dict(host._getAttrMap())
                self.new_hosts[str(hostdict["name"])] = GenericHost()
                self.new_hosts[str(hostdict["name"])].name = str(
                    hostdict["name"])
                self.new_hosts[str(hostdict["name"])].server = self.name
                # states come in lower case from Opsview
                self.new_hosts[str(hostdict["name"])].status = str(
                    hostdict["state"].upper())
                self.new_hosts[str(hostdict["name"])].status_type = str(
                    hostdict["state_type"])
                self.new_hosts[str(hostdict["name"])].last_check = str(
                    hostdict["last_check"])
                self.new_hosts[str(
                    hostdict["name"]
                )].duration = Actions.HumanReadableDurationFromSeconds(
                    hostdict["state_duration"])
                self.new_hosts[str(hostdict["name"])].attempt = str(
                    hostdict["current_check_attempt"]) + "/" + str(
                        hostdict["max_check_attempts"])
                self.new_hosts[str(hostdict["name"])].status_information = str(
                    hostdict["output"].replace("\n", " "))
                # if host is in downtime add it to known maintained hosts
                if hostdict["downtime"] == "2":
                    self.new_hosts[str(
                        hostdict["name"])].scheduled_downtime = True
                if hostdict.has_key("acknowledged"):
                    self.new_hosts[str(hostdict["name"])].acknowledged = True
                if hostdict.has_key("flapping"):
                    self.new_hosts[str(hostdict["name"])].flapping = True

                #services
                for service in host.findAll("services"):
                    servicedict = dict(service._getAttrMap())
                    self.new_hosts[str(hostdict["name"])].services[str(
                        servicedict["name"])] = OpsviewService()
                    self.new_hosts[str(hostdict["name"])].services[str(
                        servicedict["name"])].host = str(hostdict["name"])
                    self.new_hosts[str(hostdict["name"])].services[str(
                        servicedict["name"])].name = str(servicedict["name"])
                    self.new_hosts[str(hostdict["name"])].services[str(
                        servicedict["name"])].server = self.name
                    # states come in lower case from Opsview
                    self.new_hosts[str(hostdict["name"])].services[str(
                        servicedict["name"])].status = str(
                            servicedict["state"].upper())
                    self.new_hosts[str(hostdict["name"])].services[str(
                        servicedict["name"])].status_type = str(
                            servicedict["state_type"])
                    self.new_hosts[str(hostdict["name"])].services[str(
                        servicedict["name"])].last_check = str(
                            servicedict["last_check"])
                    self.new_hosts[str(hostdict["name"])].services[str(
                        servicedict["name"]
                    )].duration = Actions.HumanReadableDurationFromSeconds(
                        servicedict["state_duration"])
                    self.new_hosts[str(hostdict["name"])].services[str(
                        servicedict["name"])].attempt = str(
                            servicedict["current_check_attempt"]) + "/" + str(
                                servicedict["max_check_attempts"])
                    self.new_hosts[str(hostdict["name"])].services[str(
                        servicedict["name"])].status_information = str(
                            servicedict["output"].replace("\n", " "))
                    if servicedict["downtime"] == "2":
                        self.new_hosts[str(hostdict["name"])].services[str(
                            servicedict["name"])].scheduled_downtime = True
                    if servicedict.has_key("acknowledged"):
                        self.new_hosts[str(hostdict["name"])].services[str(
                            servicedict["name"])].acknowledged = True
                    if servicedict.has_key("flapping"):
                        self.new_hosts[str(hostdict["name"])].services[str(
                            servicedict["name"])].flapping = True

                    # extra opsview id for service, needed for submitting check results
                    self.new_hosts[str(str(hostdict["name"]))].services[str(
                        str(servicedict["name"]))].service_object_id = str(
                            servicedict["service_object_id"])
                del servicedict
                del hostdict

        except:
            # 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()