def detect_disks(hw_lst): """Detect disks.""" names = diskinfo.disknames() sizes = diskinfo.disksizes(names) disks = [name for name, size in sizes.items() if size > 0] hw_lst.append(('disk', 'logical', 'count', str(len(disks)))) for name in disks: diskinfo.get_disk_info(name, sizes, hw_lst) # nvme devices do not need standard cache mechanisms if not name.startswith('nvme'): diskinfo.get_disk_cache(name, hw_lst) diskinfo.get_disk_id(name, hw_lst) # smartctl support # run only if smartctl command is there if which("smartctl"): if name.startswith('nvme'): sys.stderr.write('Reading SMART for nvme\n') smart_utils.read_smart_nvme(hw_lst, name) else: smart_utils.read_smart(hw_lst, "/dev/%s" % name) else: sys.stderr.write("Cannot find smartctl, exiting\n")
def read_smart(hwlst, device, optional_flag=""): if not which("smartctl"): sys.stderr.write("Cannot find smartctl, exiting\n") return optional_string = "" if optional_flag: optional_string = " with %s" % optional_flag if os.path.exists(device): sys.stderr.write( "read_smart: Reading S.M.A.R.T information on %s%s\n" % (device, optional_string)) sdparm_cmd = subprocess.Popen("smartctl -a %s %s" % (device, optional_flag), shell=True, stdout=subprocess.PIPE) for line in sdparm_cmd.stdout: line = line.strip() if isinstance(line, six.binary_type): line = line.decode() if (line.startswith("Device does not support SMART") or ("Unavailable - device lacks SMART capability" in line) or line.startswith( "Device supports SMART and is Enabled")): return read_smart_scsi(hwlst, device, optional_flag) if line.startswith("ID#"): return read_smart_ata(hwlst, device, optional_flag) # If no ID# was found, let's retry with "-d ata" if optional_flag == "": return read_smart(hwlst, device, "-d ata") sys.stderr.write("read_smart: no device %s\n" % device) return