def test_sys_tools(self): SysTools.touch('/tmp/test_utils') SysTools.REBOOT_SKIP_FILES = ('/tmp/test_utils') self.assertFalse(SysTools.is_system_openwrt()) self.assertEqual(SysTools.get_mac_address('if_test'), "00:00:00:00:00:00") self.assertEqual(SysTools.get_sys_mac_address(), "00:00:00:00:00:00") with open('/tmp/test_mac', 'w') as f: f.write('01:02:03:04:05:06') f.close() SysTools.SYS_MAC_FILE = '/tmp/test_mac' self.assertEqual(SysTools.get_sys_mac_address(), "01:02:03:04:05:06") SysTools.SYS_MAC_FILE = '/etc/gshadow' self.assertEqual(SysTools.get_sys_mac_address(), "00:00:00:00:00:00") print(SysTools.get_host_name()) print(SysTools.get_ip_address('if_test')) if not SysTools.reboot(): SysTools.sys_failure_reboot('skip files exist') SysTools.external_reboot(('test', 'test')) if os.path.exists('/tmp/test_utils'): os.remove('/tmp/test_utils') # d = Dispatcher() # timestamp = time.time() + 1000 # SysTools.set_system_time(d, timestamp) # SysTools.touch('/etc/openwrt_release') # SysTools.set_system_time(d, timestamp) # time.sleep(2) # self.assertTrue(timestamp > time.time()) #should no permission to change system time # del d # if os.path.exists('/etc/openwrt_release'): # os.remove('/etc/openwrt_release') self.assertIsNotNone(SysTools.sys_up_time()) ret = SysTools.if_indextoname(1) print "ifname of index 1:", ret self.assertIsNotNone(ret) ret = SysTools.if_indextoname("test") self.assertIsNone(ret) ret = SysTools.if_indextoname(600) self.assertIsNone(ret) ret = SysTools.if_nametoindex("lo") print "ifindex of lo:", ret self.assertIsNotNone(ret) ret = SysTools.if_nametoindex(5) self.assertIsNone(ret) ret = SysTools.if_nametoindex("dafsd") self.assertIsNone(ret) ret = SysTools.is_if_oper_up('eth0') self.assertTrue(ret)
def config_rsyslog_remote(self, address): # write to remote server, check whether it's been configured already. conf = open('/etc/rsyslog.conf', 'r') server_configed = conf.read() address_configed = server_configed.find(address) conf.close() if address_configed > 0: self.logger.info( "Log Server IP address provided for remote logging already configed" ) return conf = open('/etc/rsyslog.conf', 'a+') # write to remote logserver, TCP to logserver conf.write("\n") remote_channel = "*." + self.rsyslog_loglevel + " @" + address conf.write(remote_channel) conf.write("\n") conf.close() if SysTools.is_vrpd(): hostmac = SysTools.get_mac_address("eth0") else: hostmac = SysTools.get_sys_mac_address() hostname = 'RPD' + hostmac.replace(':', '') set_host_cmd = 'echo ' + hostname + '>/proc/sys/kernel/hostname' os.system(set_host_cmd) new_hostline = hostname + " localhost" # write to /etc/hosts new_host = open('/tmp/new_hosts', 'w') old_host = open('/etc/hosts', 'r') line = old_host.read() found = line.find('localhost') configed = line.find('RPD') if found > 0 and configed < 0: new_host.write(line.replace('localhost', str(new_hostline))) else: new_host.write(line) old_host.close() new_host.flush() new_host.close() os.remove('/etc/hosts') shutil.move('/tmp/new_hosts', '/etc/hosts') return
class ProvisionStateMachineRecord(object): """provision statistics""" __metaclass__ = AddLoggerToClass RECORD_CCAP_CORE_CNT_MAX = 20 RECORD_CORE_ID_PER_CCAP_CORE_MAX = 5 RECORD_EVENT_PER_CORE_ID_MAX = 100 RPD_MAC = SysTools.get_sys_mac_address() def __init__(self): self.statistics = dict() def cleanup(self): self.statistics = dict() @staticmethod def fmt_timestamp(timestamp): return datetime.utcfromtimestamp(timestamp).strftime( '%Y %b %d %H:%M:%S:%f') def update(self, core_instance, event): """update the statistics info. :param core_instance: CCAP core instance :param event: fsm event instance :return: """ self.RPD_MAC = SysTools.get_mac_address(core_instance.interface) ccap_core_id = core_instance.ccap_core_id added_by = core_instance.added_by item = "{}/{}".format(core_instance.interface, core_instance.ccap_core_network_address) # limit the core ip count to 20 each node if len(self.statistics) >= self.RECORD_CCAP_CORE_CNT_MAX: self.logger.warning( "Ccap core already exceed the max count %d, ignore %s ", self.RECORD_CCAP_CORE_CNT_MAX, item) return if item not in self.statistics: self.statistics[item] = OrderedDict() if ccap_core_id not in self.statistics[item]: self.statistics[item][ccap_core_id] = OrderedDict() # limit the core id count to 5 under each core ip while len(self.statistics[item] ) >= self.RECORD_CORE_ID_PER_CCAP_CORE_MAX: self.statistics[item].popitem(last=False) # limit the event count to 100 each core id while len(self.statistics[item] [ccap_core_id]) >= self.RECORD_EVENT_PER_CORE_ID_MAX: self.statistics[item][ccap_core_id].popitem(last=False) current = time() self.statistics[item][ccap_core_id].update({ self.fmt_timestamp(current): { "interface": core_instance.interface, "core-ip": core_instance.ccap_core_network_address, "mac": self.RPD_MAC, "src": event.src, "dst": event.dst, "event": event.event, "Added-By": added_by } })