def get_server_ppp_address(): """Get server (local) endpoint PPP address. This address is constant for all client connections when runner is active. This helper is used by at least web UI for its redirection logic. Calling this function only makes sense when runner is active, as the configuration is otherwise not guaranteed to be correct. """ cfg = helpers.get_config() ppp_cfg = cfg.getS(ns.pppConfig, rdf.Type(ns.PppConfig)) ppp_subnet = ppp_cfg.getS(ns.pppSubnet, rdf.IPv4Subnet) if ppp_subnet.getCidr() > 30: raise Exception('PPP subnet does not contain enough usable addresses') local_ip = ppp_subnet.getLastUsableAddress() return local_ip
def reconcile_system_and_rdf_state(self, first_time=False): """Reconcile system and RDF states. Checks system device list and compares it against RDF device information. Extra PPP devices without RDF book-keeping information are terminated. Devices present in RDF but not in the system are also removed from RDF. After calling this function, the system and RDF should be reasonably synchronized. This function also detects and updates the 'liveness' of each PPP device. Devices which are not live are not taken into account in license computations, thus making their detection important. The function also updates global public/private interface rx/tx counters, rates, etc. This function should be periodically, with an interval of 1-10 minutes or so. """ # # XXX: If status is incomplete for some reason, this will now # spout exceptions. The code should check whether checking and # updating e.g. public interface status is useful at this point. # (pub_if, pub_if_name), (priv_if, priv_if_name) = helpers.get_ifaces(helpers.get_config()) # start timestamp now = datetime.datetime.utcnow() # get device info from system (ip command) _log.debug('reconcile: getting system info') ifaces = interfacehelper.get_interfaces() # build devname->node dict _log.debug('reconcile: build devname->rdf dev dict') rdfdevs = helpers.get_ppp_devices() rdfdevmap = self._build_rdf_devmap(rdfdevs) # reconcile devices, pass 1: system vs rdf [collect sysdevs dict at the same time] _log.debug('reconcile: pass 1, system vs rdf') sysdevs, pub_di, priv_di = self._reconcile_system_vs_rdf(rdfdevmap, ifaces, pub_if_name, priv_if_name) # do sysnukes for devices exceeding threshold for k in self._sysnuke_failures.keys(): if self._sysnuke_failures[k] >= SYSNUKE_FAILURE_LIMIT: try: _log.warning('sysnuke failure count for device %s too high, nuking' % k) self._nuke_system_device(k) del self._sysnuke_failures[k] except: _log.exception('sysnuke failed') # reconcile devices, pass 2: rdf vs system _log.debug('reconcile: pass 2, rdf vs system') self._reconcile_rdf_vs_system(rdfdevs, sysdevs) # do rdfnukes for devices exceeding threshold for k in self._rdfnuke_failures.keys(): # key = rdf node of type PppDevice if self._rdfnuke_failures[k] >= RDFNUKE_FAILURE_LIMIT: try: if k.hasS(ns.deviceName): devname = k.getS(ns.deviceName, rdf.String) else: devname = '<unknown devname>' _log.warning('rdfnuke failure count for device %s (%s) too high, nuking' % (k, devname)) self._nuke_rdf_device(k) del self._rdfnuke_failures[k] except: _log.exception('failed during reconcile, skipping device') # NB: device will be left in _rdfnuke_failures, and be removed next time # update rx/tx counters, transfer rates, etc # (reget devs because we may have nuked something) _log.debug('reconcile: updating rx/tx counters') rdfdevs = helpers.get_ppp_devices() self._update_rxtx_etc(now, rdfdevs, sysdevs) # update device liveness status _log.debug('reconcile: updating liveness status') rdfdevs = helpers.get_ppp_devices() self._update_liveness_status(now, rdfdevs) # update public/private interface status _log.debug('reconcile: updating public and/or private interface status') self._update_public_private_ifaces(now, ifaces, pub_di, priv_di, first_time)
def reconcile_system_and_rdf_state(self, first_time=False): """Reconcile system and RDF states. Checks system device list and compares it against RDF device information. Extra PPP devices without RDF book-keeping information are terminated. Devices present in RDF but not in the system are also removed from RDF. After calling this function, the system and RDF should be reasonably synchronized. This function also detects and updates the 'liveness' of each PPP device. Devices which are not live are not taken into account in license computations, thus making their detection important. The function also updates global public/private interface rx/tx counters, rates, etc. This function should be periodically, with an interval of 1-10 minutes or so. """ # # XXX: If status is incomplete for some reason, this will now # spout exceptions. The code should check whether checking and # updating e.g. public interface status is useful at this point. # (pub_if, pub_if_name), (priv_if, priv_if_name) = helpers.get_ifaces( helpers.get_config()) # start timestamp now = datetime.datetime.utcnow() # get device info from system (ip command) _log.debug('reconcile: getting system info') ifaces = interfacehelper.get_interfaces() # build devname->node dict _log.debug('reconcile: build devname->rdf dev dict') rdfdevs = helpers.get_ppp_devices() rdfdevmap = self._build_rdf_devmap(rdfdevs) # reconcile devices, pass 1: system vs rdf [collect sysdevs dict at the same time] _log.debug('reconcile: pass 1, system vs rdf') sysdevs, pub_di, priv_di = self._reconcile_system_vs_rdf( rdfdevmap, ifaces, pub_if_name, priv_if_name) # do sysnukes for devices exceeding threshold for k in self._sysnuke_failures.keys(): if self._sysnuke_failures[k] >= SYSNUKE_FAILURE_LIMIT: try: _log.warning( 'sysnuke failure count for device %s too high, nuking' % k) self._nuke_system_device(k) del self._sysnuke_failures[k] except: _log.exception('sysnuke failed') # reconcile devices, pass 2: rdf vs system _log.debug('reconcile: pass 2, rdf vs system') self._reconcile_rdf_vs_system(rdfdevs, sysdevs) # do rdfnukes for devices exceeding threshold for k in self._rdfnuke_failures.keys( ): # key = rdf node of type PppDevice if self._rdfnuke_failures[k] >= RDFNUKE_FAILURE_LIMIT: try: if k.hasS(ns.deviceName): devname = k.getS(ns.deviceName, rdf.String) else: devname = '<unknown devname>' _log.warning( 'rdfnuke failure count for device %s (%s) too high, nuking' % (k, devname)) self._nuke_rdf_device(k) del self._rdfnuke_failures[k] except: _log.exception('failed during reconcile, skipping device') # NB: device will be left in _rdfnuke_failures, and be removed next time # update rx/tx counters, transfer rates, etc # (reget devs because we may have nuked something) _log.debug('reconcile: updating rx/tx counters') rdfdevs = helpers.get_ppp_devices() self._update_rxtx_etc(now, rdfdevs, sysdevs) # update device liveness status _log.debug('reconcile: updating liveness status') rdfdevs = helpers.get_ppp_devices() self._update_liveness_status(now, rdfdevs) # update public/private interface status _log.debug( 'reconcile: updating public and/or private interface status') self._update_public_private_ifaces(now, ifaces, pub_di, priv_di, first_time)