def start_sensors(node_sensor_config): cmd_templ = 'env PYTHONPATH="{0}" python -m ' + \ "sensors.main -d start -u {1} {2}" cmd = cmd_templ.format(remote_path, node_sensor_config.monitor_url, config_remote_path) run_over_ssh(node_sensor_config.conn, cmd, node=node_sensor_config.url)
def stop_sensors(sens_cfg): with sens_cfg.conn.open_sftp() as sftp: try: pid = ssh_utils.read_from_remote(sftp, PID_FILE) pid = int(pid.strip()) ssh_utils.run_over_ssh(sens_cfg.conn, "kill -9 " + str(pid)) sftp.remove(PID_FILE) except: pass
def stop_and_gather_data(node_sensor_config): cmd = 'env PYTHONPATH="{0}" python -m sensors.main -d stop' cmd = cmd.format(remote_path) run_over_ssh(node_sensor_config.conn, cmd, node=node_sensor_config.url) # some magic time.sleep(1) assert node_sensor_config.monitor_url.startswith("csvfile://") res_path = node_sensor_config.monitor_url.split("//", 1)[1] with node_sensor_config.conn.open_sftp() as sftp: res = read_from_remote(sftp, res_path) return res
def test_ssh_executor1(self): id_rsa_path = os.path.expanduser('~/.ssh/id_rsa') ssh_url = "ssh://localhost::" + id_rsa_path expected = sorted(os.listdir('/')) conn = ssh_utils.connect(ssh_url) out = ssh_utils.run_over_ssh(conn, "ls /") ok(sorted(out.split())) == expected
def get_external_interface(conn, ip): data = run_over_ssh(conn, "ip a", node="fuel-master", nolog=True) curr_iface = None for line in data.split("\n"): match1 = re.match(r"\d+:\s+(?P<name>.*?):\s\<", line) if match1 is not None: curr_iface = match1.group("name") match2 = re.match(r"\s+inet\s+(?P<ip>[0-9.]+)/", line) if match2 is not None: if match2.group("ip") == ip: assert curr_iface is not None return curr_iface raise KeyError("Can't found interface for ip {0}".format(ip))
def get_external_interface(conn, ip): data = run_over_ssh(conn, "ip a", node='fuel-master', nolog=True) curr_iface = None for line in data.split("\n"): match1 = re.match(r"\d+:\s+(?P<name>.*?):\s\<", line) if match1 is not None: curr_iface = match1.group('name') match2 = re.match(r"\s+inet\s+(?P<ip>[0-9.]+)/", line) if match2 is not None: if match2.group('ip') == ip: assert curr_iface is not None return curr_iface raise KeyError("Can't found interface for ip {0}".format(ip))
def remove_sensors(node_sensor_config): run_over_ssh(node_sensor_config.conn, "rm -rf {0}".format(remote_path), node=node_sensor_config.url, timeout=10)
def closure(*args, **kwargs): return run_over_ssh(node.connection, *args, node=node.get_conn_id(), **kwargs)
def get_hw_info(conn): res = HWInfo() lshw_out = ssh_utils.run_over_ssh(conn, 'sudo lshw -xml 2>/dev/null', nolog=True) res.raw = lshw_out lshw_et = ET.fromstring(lshw_out) try: res.hostname = lshw_et.find("node").attrib['id'] except: pass try: res.sys_name = (lshw_et.find("node/vendor").text + " " + lshw_et.find("node/product").text) res.sys_name = res.sys_name.replace("(To be filled by O.E.M.)", "") res.sys_name = res.sys_name.replace("(To be Filled by O.E.M.)", "") except: pass core = lshw_et.find("node/node[@id='core']") if core is None: return try: res.mb = " ".join(core.find(node).text for node in ['vendor', 'product', 'version']) except: pass for cpu in core.findall("node[@class='processor']"): try: model = cpu.find('product').text threads_node = cpu.find("configuration/setting[@id='threads']") if threads_node is None: threads = 1 else: threads = int(threads_node.attrib['value']) res.cores.append((model, threads)) except: pass res.ram_size = 0 for mem_node in core.findall(".//node[@class='memory']"): descr = mem_node.find('description') try: if descr is not None and descr.text == 'System Memory': mem_sz = mem_node.find('size') if mem_sz is None: for slot_node in mem_node.find("node[@class='memory']"): slot_sz = slot_node.find('size') if slot_sz is not None: assert slot_sz.attrib['units'] == 'bytes' res.ram_size += int(slot_sz.text) else: assert mem_sz.attrib['units'] == 'bytes' res.ram_size += int(mem_sz.text) except: pass for net in core.findall(".//node[@class='network']"): try: link = net.find("configuration/setting[@id='link']") if link.attrib['value'] == 'yes': name = net.find("logicalname").text speed_node = net.find("configuration/setting[@id='speed']") if speed_node is None: speed = None else: speed = speed_node.attrib['value'] dup_node = net.find("configuration/setting[@id='duplex']") if dup_node is None: dup = None else: dup = dup_node.attrib['value'] res.net_info[name] = (speed, dup, []) except: pass for controller in core.findall(".//node[@class='storage']"): try: description = getattr(controller.find("description"), 'text', "") product = getattr(controller.find("product"), 'text', "") vendor = getattr(controller.find("vendor"), 'text', "") dev = getattr(controller.find("logicalname"), 'text', "") if dev != "": res.storage_controllers.append( "{0}: {1} {2} {3}".format(dev, description, vendor, product)) else: res.storage_controllers.append( "{0} {1} {2}".format(description, vendor, product)) except: pass for disk in core.findall(".//node[@class='disk']"): try: lname_node = disk.find('logicalname') if lname_node is not None: dev = lname_node.text.split('/')[-1] if dev == "" or dev[-1].isdigit(): continue sz_node = disk.find('size') assert sz_node.attrib['units'] == 'bytes' sz = int(sz_node.text) res.disks_info[dev] = ('', sz) else: description = disk.find('description').text product = disk.find('product').text vendor = disk.find('vendor').text version = disk.find('version').text serial = disk.find('serial').text full_descr = "{0} {1} {2} {3} {4}".format( description, product, vendor, version, serial) businfo = disk.find('businfo').text res.disks_raw_info[businfo] = full_descr except: pass return res
def rr(cmd): try: return ssh_utils.run_over_ssh(conn, cmd, nolog=True) except: return None