async def check_failover_group(self, ifname, groups): """ Check the other members (if any) in failover group for `ifname` """ masters, backups = [], [] # get failover group id for `iface` group_id = [ group for group, names in groups.items() if ifname in names ][0] # get all interfaces in `group_id` ids = [names for group, names in groups.items() if group == group_id][0] # need to remove the passed in `ifname` from the list ids.remove(ifname) # if the user provided VIP(s) is/are missing from the interface # then it's considered "BACKUP" if the interface has the VIP(s) # then it's considered "MASTER" if len(ids): # we can have more than one interface in the failover # group so check the state of the interface for i in ids: iface = netif.get_interface(i) for j in iface.vrrp_config: if j['state'] == 'MASTER': masters.append(i) else: backups.append(i) return masters, backups
def sync(self, iface, internal_ip): try: iface = netif.get_interface(iface) except KeyError: self.logger.error(f'Internal interface:"{iface}" not found.') return configured = False for address in iface.addresses: if address.af != netif.AddressFamily.INET: continue # Internal interface is already configured if str(address.address) == internal_ip: configured = True if not configured: iface.add_address(self.middleware.call_sync('interface.alias_to_addr', { 'address': internal_ip, 'netmask': '24', })) # testing shows that the z-series interface is not brought up automatically # so we need to up the interface after we apply the IP address iface.up()
def sync(self, iface, carp1_skew, carp2_skew, internal_ip): try: iface = netif.get_interface(iface) except KeyError: self.logger.error(f'Internal interface:"{iface}" not found.') return carp1_addr = '169.254.10.20' carp2_addr = '169.254.10.80' found_i = found_1 = found_2 = False for address in iface.addresses: if address.af != netif.AddressFamily.INET: continue if str(address.address) == internal_ip: found_i = True elif str(address.address) == carp1_addr: found_1 = True elif str(address.address) == carp2_addr: found_2 = True else: iface.remove_address(address) # VHID needs to be configured before aliases found = 0 for carp_config in iface.carp_config: if carp_config.vhid == 10 and carp_config.advskew == carp1_skew: found += 1 elif carp_config.vhid == 20 and carp_config.advskew == carp2_skew: found += 1 else: found -= 1 if found != 2: iface.carp_config = [ netif.CarpConfig(10, advskew=carp1_skew), netif.CarpConfig(20, advskew=carp2_skew), ] if not found_i: iface.add_address(self.middleware.call_sync('interface.alias_to_addr', { 'address': internal_ip, 'netmask': '24', })) if not found_1: iface.add_address(self.middleware.call_sync('interface.alias_to_addr', { 'address': carp1_addr, 'netmask': '32', 'vhid': 10, })) if not found_2: iface.add_address(self.middleware.call_sync('interface.alias_to_addr', { 'address': carp2_addr, 'netmask': '32', 'vhid': 20, }))
def setup_nic_attach(self): nic_attach = self.data['attributes'].get('nic_attach') interfaces = netif.list_interfaces() if nic_attach and nic_attach not in interfaces: raise CallError(f'{nic_attach} not found.') else: if not nic_attach: try: nic_attach = netif.RoutingTable().default_route_ipv4.interface nic = netif.get_interface(nic_attach) except Exception as e: raise CallError(f'Unable to retrieve default interface: {e}') else: nic = netif.get_interface(nic_attach) if netif.InterfaceFlags.UP not in nic.flags: nic.up() self.nic_attach = nic.name
def pre_start_vm_freebsd(self, *args, **kwargs): self.setup_nic_attach() interfaces = netif.list_interfaces() bridge = None if self.nic_attach.startswith('bridge'): bridge = interfaces[self.nic_attach] if not bridge: for iface in filter(lambda v: v.startswith('bridge'), interfaces): if self.nic_attach in interfaces[iface].members: bridge = interfaces[iface] break else: bridge = netif.get_interface(netif.create_interface('bridge')) bridge.add_member(self.nic_attach) self.bridge_created = True if netif.InterfaceFlags.UP not in bridge.flags: bridge.up() self.bridge = bridge.name
def sync(self, iface, internal_ip): try: iface = netif.get_interface(iface) except KeyError: self.logger.error(f'Internal interface:"{iface}" not found.') return configured = False for address in iface.addresses: if address.af != netif.AddressFamily.INET: continue # Internal interface is already configured if str(address.address) == internal_ip: configured = True if not configured: iface.add_address( self.middleware.call_sync('interface.alias_to_addr', { 'address': internal_ip, 'netmask': '24', }))
def run_sync(self): interval = self.arg['interval'] cp_time_last = None cp_times_last = None last_interface_stats = {} last_interface_speeds = { 'time': time.monotonic(), 'speeds': self.get_interface_speeds() } last_disk_stats = {} internal_interfaces = tuple( self.middleware.call_sync('interface.internal_interfaces')) while not self._cancel_sync.is_set(): data = {} # ZFS ARC Size (raw value is in Bytes) hits = 0 misses = 0 data['zfs'] = {} with open('/proc/spl/kstat/zfs/arcstats') as f: for line in f.readlines()[2:]: if line.strip(): name, type, value = line.strip().split() if name == 'hits': hits = int(value) if name == 'misses': misses = int(value) if name == 'c_max': data['zfs']['arc_max_size'] = int(value) if name == 'size': data['zfs']['arc_size'] = int(value) total = hits + misses if total > 0: data['zfs']['cache_hit_ratio'] = hits / total else: data['zfs']['cache_hit_ratio'] = 0 # Virtual memory use data['memory'] = self.get_memory_info(data['zfs']['arc_size']) data['virtual_memory'] = psutil.virtual_memory()._asdict() # Get CPU usage % data['cpu'] = {} num_times = 10 with open('/proc/stat') as f: stat = f.read() cp_times = [] cp_time = [] for line in stat.split('\n'): bits = line.split() if bits[0].startswith('cpu'): line_ints = [int(i) for i in bits[1:]] # cpu has a sum of all cpus if bits[0] == 'cpu': cp_time = line_ints # cpuX is for each core else: cp_times += line_ints else: break if cp_time and cp_times and cp_times_last: # Get the difference of times between the last check and the current one # cp_time has a list with user, nice, system, interrupt and idle cp_diff = list( map(lambda x: x[0] - x[1], zip(cp_times, cp_times_last))) cp_nums = int(len(cp_times) / num_times) for i in range(cp_nums): data['cpu'][i] = self.get_cpu_usages( cp_diff[i * num_times:i * num_times + num_times]) cp_diff = list( map(lambda x: x[0] - x[1], zip(cp_time, cp_time_last))) data['cpu']['average'] = self.get_cpu_usages(cp_diff) cp_time_last = cp_time cp_times_last = cp_times # CPU temperature data['cpu']['temperature_celsius'] = self.middleware.call_sync( 'reporting.cpu_temperatures') data['cpu']['temperature'] = { k: 2732 + int(v * 10) for k, v in data['cpu']['temperature_celsius'].items() } # Interface related statistics if last_interface_speeds['time'] < time.monotonic( ) - self.INTERFACE_SPEEDS_CACHE_INTERVAL: last_interface_speeds.update({ 'time': time.monotonic(), 'speeds': self.get_interface_speeds(), }) data['interfaces'] = defaultdict(dict) retrieve_stat = { 'rx_bytes': 'received_bytes', 'tx_bytes': 'sent_bytes' } stats_time = time.time() for i in glob.glob('/sys/class/net/*/statistics'): iface_name = i.replace('/sys/class/net/', '').split('/')[0] if iface_name.startswith(internal_interfaces): continue iface_obj = netif.get_interface(iface_name, True) data['interfaces'][iface_name] = { 'speed': last_interface_speeds['speeds'].get(iface_name), 'link_state': iface_obj.link_state.name if iface_obj else 'LINK_STATE_UNKNOWN', } for stat, name in retrieve_stat.items(): with open(f'{i}/{stat}', 'r') as f: value = int(f.read()) data['interfaces'][iface_name][name] = value traffic_stats = None if (last_interface_stats.get(iface_name) and name in last_interface_stats[iface_name]): traffic_stats = value - last_interface_stats[ iface_name][name] traffic_stats = int( traffic_stats / (stats_time - last_interface_stats[iface_name]['stats_time'])) data['interfaces'][iface_name][ f'{retrieve_stat[stat]}_rate'] = traffic_stats last_interface_stats[iface_name] = { **data['interfaces'][iface_name], 'stats_time': stats_time, } # Disk IO Stats if not last_disk_stats: # means this is the first time disk stats are being gathered so # get the results but don't set anything yet since we need to # calculate the difference between the iterations last_disk_stats, new = DiskStats(interval, last_disk_stats).read() else: last_disk_stats, data['disks'] = DiskStats( interval, last_disk_stats).read() self.send_event('ADDED', fields=data) time.sleep(interval)