Skip to content

Instantly share code, notes, and snippets.

@kimthostrup
Forked from amitsaha/disks_lshw.py
Created May 17, 2018 12:16
Show Gist options
  • Save kimthostrup/60fa7f16ad803b936a4fe9a118c0e636 to your computer and use it in GitHub Desktop.
Save kimthostrup/60fa7f16ad803b936a4fe9a118c0e636 to your computer and use it in GitHub Desktop.
Use lshw -xml to find the disk details
#!/usr/bin/env python
# Parse disk:* from lshw -xml
"""
Sample Output:
ATA Disk Hitachi HDS72105 /dev/sda
Disk Space: 500107862016
Sector size: 512
ATA Disk Hitachi HDS72105 /dev/sdb
Disk Space: 500107862016
Sector size: 512
Num disks 2
Total disk space 953880
"""
from lxml import etree
from subprocess import Popen,PIPE
inventory = Popen(['lshw', '-xml', '-numeric'], stdout=PIPE).communicate()[0]
inventory = etree.XML(inventory)
find_disks = etree.XPath(".//node[@class='disk']")
numdisks = 0
diskspace = 0
for disk in find_disks(inventory):
# has to be a hard-disk
if disk.find('size') is not None:
numdisks = numdisks + 1
diskspace = diskspace + int(disk.find('size').text)
print disk.find('description').text, disk.find('product').text, disk.find('logicalname').text
print 'Disk Space: ', disk.find('size').text
print 'Sector size: ',disk.find('configuration/setting/[@id="sectorsize"]').get('value')
print
print 'Num disks', numdisks
print 'Total disk space', diskspace/(1024**2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment