Example #1
0
    def __init__(self, session_id, host_id, interval=-1):
        """Initialises a new class for a specified host.

        The specified session must be valid and have appropriate access to
        the database.
        """
        
        self.lock = threading.RLock()

        session = getSession(session_id)
        if session is None:
            raise ccs_host_error("Invalid session id")
        self._session_id = session_id
        
        # See if the specified host id makes sense
        self.host = ccs_host(session_id, host_id)
        self._hostname = self.host["host_name"]
        self._ip = self.host["ip_address"]
        
        # Initialise the status information
        self.reachable = False
        self.operatingRevision = -1
        self.operatingRevisionStatus = STATUS_UNKNOWN
        self.operatingRevisionText = "Unknown"
        self.operatingRevisionHint = "Status not yet fetched"
        self.currentLoad = (-1,-1,-1)
        self.uptime = 0
        self.infoUpdatedAt = 0
        self.lastCheckedAt = time.time()
        self.interval = interval
        self.attempt = 0
        # Retrieve locally available information
        self.activeRevision = getActiveRevision(self._hostname)
        self.generatedRevision = getGeneratedRevision(self._hostname)

        # Will this object be updated?
        if interval != -1:
            ccs_host_status.queue(time.time(), self)
        log_info("Initialised host status monitor for %s" % self._hostname)
Example #2
0
    def update(self):
        """Called by the processing thread to trigger an update of the data"""
        ip = self._ip
        hostname = self._hostname
        
        # Grab the lock
        lock = self.lock
        lock.acquire()
        try:
            # Retrieve locally available information
            self.activeRevision = getActiveRevision(hostname)
            self.generatedRevision = getGeneratedRevision(self._hostname)
            # Check that the host is reachable
            if not isHostUp(ip):
                self.reachable = False
                self.lastCheckedAt = time.time()
                self.attempt+=1
                return
            self.reachable = True
            # Retrieve host status information
            pMod = api.protoModules[api.protoVersion1]
            reqPDU =  pMod.GetRequestPDU()
            pMod.apiPDU.setDefaults(reqPDU)
            pMod.apiPDU.setVarBinds(
                    reqPDU, ((CCS_REVISION_OID, pMod.Null()),
                        (LOAD_AVG1_OID, pMod.Null()),
                        (LOAD_AVG5_OID, pMod.Null()),
                        (LOAD_AVG15_OID, pMod.Null()),
                        (UPTIME_OID, pMod.Null())
                        )
                    )
            # Build message
            reqMsg = pMod.Message()
            pMod.apiMessage.setDefaults(reqMsg)
            pMod.apiMessage.setCommunity(reqMsg, 'public')
            pMod.apiMessage.setPDU(reqMsg, reqPDU)
            # Dispatch Request
            transportDispatcher = AsynsockDispatcher()
            transportDispatcher.registerTransport(
                    udp.domainName, udp.UdpSocketTransport().openClientMode()
            )

            startedAt = time.time()
            def cbTimerFun(timeNow):
                if timeNow - startedAt > 3:
                    transportDispatcher.jobFinished(1)
                    transportDispatcher.closeDispatcher()
                    self.attempt+=1
                    log_warn("Timeout fetching status from %s. Attempt #%s" % 
                        (hostname, self.attempt))

            def cbRecvFun(transportDispatcher, transportDomain, \
                    transportAddress, wholeMsg, reqPDU=reqPDU):
                while wholeMsg:
                    rspMsg, wholeMsg = decoder.decode(wholeMsg, \
                            asn1Spec=pMod.Message())
                    rspPDU = pMod.apiMessage.getPDU(rspMsg)
                    # Match response to request
                    if pMod.apiPDU.getRequestID(reqPDU) == \
                            pMod.apiPDU.getRequestID(rspPDU):
                        # Check for SNMP errors reported
                        errorStatus = pMod.apiPDU.getErrorStatus(rspPDU)
                        if errorStatus:
                            transportDispatcher.jobFinished(1)
                            transportDispatcher.closeDispatcher()
                            raise ccs_status_error(errorStatus.prettyPrint())
                        else:
                            la1 = 0
                            la5 = 0
                            la15 = 0
                            for oid, val in pMod.apiPDU.getVarBinds(rspPDU):
                                if oid == CCS_REVISION_OID:
                                    self.operatingRevision = str(val)
                                    self._parseOperatingRevision()
                                elif oid == LOAD_AVG1_OID:
                                    la1 = val
                                elif oid == LOAD_AVG5_OID:
                                    la5 = val
                                elif oid == LOAD_AVG15_OID:
                                    la15 = val
                                elif oid == UPTIME_OID:
                                    self.uptime = val
                            self.currentLoad = (la1, la5, la15)
                        transportDispatcher.jobFinished(1)
                self.attempt = 0
                return wholeMsg

            transportDispatcher.registerRecvCbFun(cbRecvFun)
            transportDispatcher.registerTimerCbFun(cbTimerFun)
            transportDispatcher.sendMessage(
                    encoder.encode(reqMsg), udp.domainName, (ip, 161)
            )
            transportDispatcher.jobStarted(1)
            transportDispatcher.runDispatcher()
            transportDispatcher.closeDispatcher()
            # Record that the details were updated
            self.infoUpdatedAt = time.time()
            self.lastCheckedAt = time.time()
        finally:
            lock.release()