Skip to content

Code snippets

thiell edited this page Oct 13, 2016 · 18 revisions

sasutils code snippets

Get started with the sasutils Python library by using the following code snippets. This is a work in progress, more examples are expected soon.

Listing SAS hosts (controllers)

The following example will list all SAS hosts (controllers) found in sysfs.

    from sasutils.sas import SASHost
    from sasutils.sysfs import sysfs

    # sysfs is a helper to walk through sysfs (/sys)
    for node in sysfs.node('class').node('sas_host'):

        # Instantiate SASHost with the sas_host sysfs device class
        host = SASHost(node.node('device'))

        # To get its sysfs name, use:
        print(host.name)
        # To get attributes from scsi_host, use:
        print('  %s' % host.scsi_host.attrs.host_sas_address)
        print('  %s' % host.scsi_host.attrs.version_fw)

Result (in this example, we have two SAS HBAs):

host21
  0x500123400ab06e40
  12.00.00.00
host22
  0x500123400ab09310
  03.00.08.00

Listing expanders

The following example will list all SAS expanders found in sysfs (one instance for each path).

from sasutils.sas import SASExpander
from sasutils.sysfs import sysfs

# sysfs is a helper to walk through sysfs (/sys)
for node in sysfs.node('class').node('sas_expander'):

    # Instantiate SASExpander with the sas_expander sysfs device class
    expander = SASExpander(node.node('device'))

    # To get its sysfs name, use:
    print(expander.name)
    # To access its attributes, use:
    print('  %s' % expander.attrs.product_id)
    # To get attributes from the sas_device sysfs class, use:
    print('  %s' % expander.sas_device.attrs.sas_address)

Result:

expander-21:0
  Switch184
  0x50012be001234bff
expander-21:1
  JB4602 SIM 0
  0x5001636001234d7f
expander-22:0
  Switch184
  0x50012be001234c7f
expander-22:1
  JB4602 SIM 1
  0x5001636001234e3f

Listing unique expanders

Expanders may be access through multiple paths and sysfs instantiates a sas_expander for each of them. The following example shows how to list unique SAS expanders.

    from operator import attrgetter
    from itertools import groupby

    from sasutils.sas import SASExpander
    from sasutils.sysfs import sysfs

    expanders = (SASExpander(node.node('device'))
                 for node in sysfs.node('class').node('sas_expander'))

    # Find unique expander thanks to their sas_address
    attrname = 'sas_device.attrs.sas_address'
    # Sort the expander list before using groupby()
    expanders = sorted(expanders, key=attrgetter(attrname))
    # Group expanders by SAS address
    for addr, expgroup in groupby(expanders, attrgetter(attrname)):
        print('SAS expander %s (paths=%d)' % (addr, len(list(expgroup))))

Result:

SAS expander 0x50012be012343bff (paths=2)
SAS expander 0x50012be012343c7f (paths=2)
SAS expander 0x5001636012342e3f (paths=2)

SCSI Enclosure

SAS is obviously very close to the SCSI layer of Linux. sasutils has a scsi module that supports SCSI types like Enclosure SCSI devices (type 13). The following example will quickly list all enclosure devices found.

from sasutils.scsi import EnclosureDevice
from sasutils.sysfs import sysfs

# Iterate over sysfs SCSI enclosures
for node in sysfs.node('class').node('enclosure'):
    # Get enclosure device
    enclosure = EnclosureDevice(node.node('device'))
    # Get enclosure SG device
    sg_dev = enclosure.scsi_generic
    print('SCSI Enclosure %s' % sg_dev.name)
    print('  %s' % enclosure.attrs.vendor)
    print('  %s' % enclosure.attrs.sas_address)

Result:

SCSI Enclosure sg124
  QCT
  0x500163600112343d
SCSI Enclosure sg1
  ASTEK
  0x50012be001234bfd
SCSI Enclosure sg63
  ASTEK
  0x50012be00012347d
SCSI Enclosure sg62
  QCT
  0x500163600123457d
Clone this wiki locally