Beispiel #1
0
 def get_result(self, skipCache=0, **keywords):
     if not skipCache:
         dt = now() - self.__cached_result.timestamp
         if dt > self.__cache_ttl:
             skipCache = 1
     if skipCache:
         value = self.__agent.snmp_get(self.__var_name)[1]
         result = Result(value, now(), 0, self.__cached_result.changes+1)
         self.__cached_result = Result(result.value, result.timestamp,
                                      1, result.changes)
         return result
     return self.__cached_result
Beispiel #2
0
 def _set(self, value, asyncOK=0):
     self.__cached_result = Result(None, 0, 0, 0)
     if not isinstance(value, base.Asn1Item):
         asn1_value = self.__smi.syntax.clone(value)
     else:
         asn1_value = value
     self.__agent.snmp_set(self.__var_name, asn1_value)
     return
Beispiel #3
0
 def get_batch(self):
     timestamp = now()
     if timestamp >= self.expires_at:
         results = {}
         for oid, value in self.agent.snmp_get_multiple(*self.oid_list):
             result = Result(value, timestamp, 0)
             sm_key = self.batch_map[oid].sm_key
             results[sm_key] = result
         self.results = results
         self.expires_at = timestamp + self.ttl
     return self.results
Beispiel #4
0
 def update_value(self, value):
     if self._cached_result:
         previous = self._cached_result.value
         if value == previous:
             return False
         changes = self._cached_result.changes + 1
     else:
         changes = 1
         previous = None
     self._cached_result = Result(value, uptime.secs(), 1, changes)
     self._trigger_cov(previous, value, time.time())
     return True
Beispiel #5
0
def as_result(value):
    if isinstance(value, Exception):
        value = error_dictionary(value)
    if isinstance(value, dict):
        if value.has_key('edt__typ'):
            value = edtlib.edt_decode(value)
        elif (value.get('__base__') == 'Result' or 
              all(value.has_key(key) for key in 
                  ["cached", "changes", "timestamp", "value"])):
            value = Result.from_dict(value)
    if not isinstance(value, Result):
        msglog.log("broadway", msglog.types.WARN, 
                   "Unable to convert %r to Result." % (value,))
    return value
Beispiel #6
0
 def update(self, msg):
     self._last_rcvd = uptime.secs()
     if self._cached_result is None:
         change_count = 1
         last_value = None
     else:
         change_count = self._cached_result.changes + 1
         last_value = self._cached_result.value
     if isinstance(msg, Exception):
         value = msg
     else:
         value = getattr(msg, self.prop_name)
     self._cached_result = Result(value, time.time(), changes=change_count)
     self._trigger_cov(last_value)
Beispiel #7
0
 def __init__(self, parent=None, info=None):
     super(ScalarInstance, self).__init__()
     self.__cache_ttl = -1
     self.__agent = None
     self.__smi = None
     self.__var_name = None
     # By using a valid, but expired __cached_result the number of checks
     # can be reduced.
     self.__cached_result = Result(None, 0, 0, 0)
     self.index = REQUIRED
     # Both parent and info must be None, or both must NOT be None
     assert (parent is None) == (info is None)
     if parent is not None:
         self.__cfg_from_info(parent, info)
     return
Beispiel #8
0
 def update(self, value):
     ts = time.time()
     if self._last_result is None:
         last_ts = ts
         changes = 0
     else:
         last_ts = self._last_result.timestamp
         changes = self._last_result.changes
     if not isinstance(value, Exception):
         # we've found that different servers
         # ie. pg&e vs sce, despite sharing wsdl's
         # have different return values.  :(
         value = self._get_value(value)
     self._last_result = Result(value, ts, 0, changes)
     return
Beispiel #9
0
 def update_cache(self):
     self.updated(True)
     fwstatus = self.fw_node.get()
     netstatus = int(bool(self.net_node.get()))
     value = {"net": netstatus, "framework": fwstatus}
     if self._cached_result:
         previous = self._cached_result.value
         if value == previous:
             return
         changes = self._cached_result.changes + 1
     else:
         changes = 1
         previous = None
     self._cached_result = Result(value, self.refreshed(), 1, changes)
     event = ChangeOfValueEvent(self, previous, value, time.time())
     self.event_generate(event)
     self.state_changed(True)
Beispiel #10
0
 def update_cache(self, value):
     now = uptime.secs()
     if isinstance(value, ValueObj):
         value = value.get(self.prop_name)
     if value is None or isinstance(value, Exception):
         value = ETimeout()
     if value != self._cached_value:
         if self.event_has_subscribers():
             self._trigger_cov(self._cached_value, value, time.time())
         self._cached_value = value
     if self._cached_result is None:
         changes = 0
     else:
         changes = self._cached_result.changes + 1
     self._cached_result = Result(self._cached_value, self._last_rcvd,
                                  changes)
     self._last_rcvd = now
     return
Beispiel #11
0
 def update(self, result):
     if not isinstance(result, Result):
         if isinstance(result, dict):
             result = Result.from_dict(result)
         else:
             message = "update() expects Result instance, not: %r"
             raise ValueError(message % result)
     self.synclock.acquire()
     try:
         if self.initialized():
             previous = self.get_value()
         else:
             previous = None
         self.result = result
         value = self.get_value()
         self.updated = uptime.secs()
     finally:
         self.synclock.release()
     if self.support_cov:
         self.event_generate(COVEvent(self, previous, value, time.time()))
     if self.debug:
         msglog.debug("%s updated: %r" % (self, result))
Beispiel #12
0
 def update(self, result):
     if not isinstance(result, Result):
         if isinstance(result, dict):
             result = Result.from_dict(result)
         else:
             message = "update() expects Result instance, not: %r"
             raise ValueError(message % result)
     self.synclock.acquire()
     try:
         if self.initialized():
             previous = self.get_value()
         else:
             previous = None
         self.result = result
         value = self.get_value()
         self.updated = uptime.secs()
     finally:
         self.synclock.release()
     if self.support_cov:
         self.event_generate(COVEvent(self, previous, value, time.time()))
     if self.debug:
         msglog.debug("%s updated: %r" % (self, result))
Beispiel #13
0
 def get_result(self, skipCache=False):
     return Result(self.get(skipCache), time.time(), cached=False)
 def get_result(self, skipCache=0, **keywords):
     return Result(self.get(), time.time())
Beispiel #15
0
 def get_result(self, skipCache=0):
     return Result(self.get(), time.time(), 0, 0)
Beispiel #16
0
 def as_result(self):
     answer = self.value
     if not isinstance(answer, Result):
         answer = Result(answer, self.timestamp)
     return answer
Beispiel #17
0
 def _update_cached_value(self, value):
     self.__cached_result = Result(value, now(), 1,
                                   self.__cached_result.changes+1)
     return
Beispiel #18
0
 def _reset_result(self):
     result = Result({"net": None, "framework": None}, 0, 1, 0)
     self.last_update = 0
     self.last_refresh = 0
     self._cached_result = result