def verify(c): # Check if QAT service installed if not os.path.exists('/etc/init.d/vyos-qat-utilities'): raise ConfigError("Warning: QAT init file not found") if c['qat_conf'] == None: return # Check if QAT device exist output, err = popen('sudo lspci -nn', decode='utf-8') if not err: data = re.findall('(8086:19e2)|(8086:37c8)|(8086:0435)|(8086:6f54)', output) #If QAT devices found if not data: print("\t No QAT acceleration device found") sys.exit(1)
def get_qat_proc_path(qat_dev): q_type = "" q_bsf = "" output, err = popen('sudo /etc/init.d/qat_service status', decode='utf-8') if not err: # Parse QAT service output data_st = output.split("\n") for elm_str in range(len(data_st)): if re.search(qat_dev, data_st[elm_str]): elm_list = data_st[elm_str].split(", ") for elm in range(len(elm_list)): if re.search('type', elm_list[elm]): q_list = elm_list[elm].split(": ") q_type = q_list[1] elif re.search('bsf', elm_list[elm]): q_list = elm_list[elm].split(": ") q_bsf = q_list[1] return "/sys/kernel/debug/qat_" + q_type + "_" + q_bsf + "/"
def show_ifdescr(i): ven_id = '' dev_id = '' try: with open(r'/sys/class/net/' + i + '/device/vendor', 'r') as f: ven_id = f.read().replace('\n', '') except FileNotFoundError: pass try: with open(r'/sys/class/net/' + i + '/device/device', 'r') as f: dev_id = f.read().replace('\n', '') except FileNotFoundError: pass if ven_id == '' and dev_id == '': ret = 'ifDescr = {0}'.format(i) return ret device = str(ven_id) + ':' + str(dev_id) out, err = popen(f'/usr/bin/lspci -mm -d {device}', decode='utf-8') vendor = "" device = "" # convert output to string string = out.split('"') if len(string) > 3: vendor = string[3] if len(string) > 5: device = string[5] ret = 'ifDescr = {0} {1}'.format(vendor, device) return ret.replace('\n', '')
def show_clients(intf): # XXX: This ignores errors tmp, _ = popen(f'/sbin/iw dev {intf} station dump') clients = [] data = { 'mac': '', 'signal': '', 'rx_bytes': '', 'rx_packets': '', 'tx_bytes': '', 'tx_packets': '' } re_mac = re.compile(r'([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})') for line in tmp.splitlines(): if line.startswith('Station'): client = deepcopy(data) client['mac'] = re.search(re_mac, line).group() elif line.lstrip().startswith('signal avg:'): client['signal'] = line.lstrip().split(':')[-1].lstrip().split()[0] elif line.lstrip().startswith('rx bytes:'): client['rx_bytes'] = line.lstrip().split(':')[-1].lstrip() elif line.lstrip().startswith('rx packets:'): client['rx_packets'] = line.lstrip().split(':')[-1].lstrip() elif line.lstrip().startswith('tx bytes:'): client['tx_bytes'] = line.lstrip().split(':')[-1].lstrip() elif line.lstrip().startswith('tx packets:'): client['tx_packets'] = line.lstrip().split(':')[-1].lstrip() clients.append(client) continue return clients
def get_qat_devices(): data_st, err = popen('sudo /etc/init.d/qat_service status', decode='utf-8') if not err: elm_lst = re.findall('qat_dev\d', data_st) print('\n'.join(elm_lst))
def show_ifalias(intf): out, err = popen(f'/bin/ip link show {intf}', decode='utf-8') alias = out.split('alias')[1].lstrip() if 'alias' in out else intf return 'ifAlias = ' + alias.replace('\n', '')
def show_ifindex(intf): out, err = popen(f'/bin/ip link show {intf}', decode='utf-8') index = 'ifIndex = ' + out.split(':')[0] return index.replace('\n', '')
def _popen(self, command): return popen(command, self.debug)
def _get_neighbors(): command = '/usr/sbin/lldpcli -f xml show neighbors' out, _ = popen(command) return out
OUT_TMPL_SRC = """ {%- if cpu -%} {% if 'vendor' in cpu %}CPU Vendor: {{cpu.vendor}}{%- endif %} {% if 'model' in cpu %}Model: {{cpu.model}}{%- endif %} {% if 'cpus' in cpu %}Total CPUs: {{cpu.cpus}}{%- endif %} {% if 'sockets' in cpu %}Sockets: {{cpu.sockets}}{%- endif %} {% if 'cores' in cpu %}Cores: {{cpu.cores}}{%- endif %} {% if 'threads' in cpu %}Threads: {{cpu.threads}}{%- endif %} {% if 'mhz' in cpu %}Current MHz: {{cpu.mhz}}{%- endif %} {% if 'mhz_min' in cpu %}Minimum MHz: {{cpu.mhz_min}}{%- endif %} {% if 'mhz_max' in cpu %}Maximum MHz: {{cpu.mhz_max}}{%- endif %} {% endif %} """ cpu = {} cpu_json, code = popen('lscpu -J', stderr=DEVNULL) if code == 0: cpu_info = json.loads(cpu_json) if len(cpu_info) > 0 and 'lscpu' in cpu_info: for prop in cpu_info['lscpu']: if (prop['field'].find('Thread(s)') > -1): cpu['threads'] = prop['data'] if (prop['field'].find('Core(s)')) > -1: cpu['cores'] = prop['data'] if (prop['field'].find('Socket(s)')) > -1: cpu['sockets'] = prop['data'] if (prop['field'].find('CPU(s):')) > -1: cpu['cpus'] = prop['data'] if (prop['field'].find('CPU MHz')) > -1: cpu['mhz'] = prop['data'] if (prop['field'].find('CPU min MHz')) > -1: cpu['mhz_min'] = prop['data']
#!/usr/bin/env python3 import argparse from vyos.util import popen if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--selector', help='Selector: username|ifname|sid', required=True) args = parser.parse_args() output, err = popen("accel-cmd -p 2002 show sessions {0}".format( args.selector)) if not err: res = output.split("\r\n") # Delete header from list del res[:2] print(' '.join(res))
def run(command): xml, code = popen(command, stderr=DEVNULL) if code: sys.exit('conntrack failed') return xml