A useful script for enabling/disabling HD LEDs in a server running TrueNAS

If you happen to have a large storage array and are running TrueNAS, this script might come in handy. It makes it easy to turn on/off the LED lights associated with the drive bays (assuming your drive bays do have them)

#!/usr/bin/env python3

import subprocess
import sys
import re

def get_disk_info(disk_name):
    try:
        cmd = "sesutil show"
        result = subprocess.run(cmd, shell=True, check=True, capture_output=True, text=True)

        current_ses = None
        for line in result.stdout.split('\n'):
            if line.startswith('ses'):
                current_ses = line.split(':')[0]
            elif disk_name in line:
                parts = re.split(r'\s{2,}', line.strip())
                if len(parts) >= 6:
                    return {
                        'ses_device': current_ses,
                        'slot': parts[0],
                        'device': parts[1],
                        'model': parts[2],
                        'ident': parts[3],
                        'size': parts[4].rstrip(','),
                        'status': parts[5] if len(parts) > 5 else ''
                    }
    except subprocess.CalledProcessError as e:
        print(f"Error running sesutil show: {e}")

    return None

def set_disk_led(disk_number, status):
    status = status.lower()

    if status not in ['on', 'off']:
        print("Error: Status must be 'on' or 'off'")
        return

    disk_name = f"da{disk_number}"

    disk_info = get_disk_info(disk_name)

    if disk_info:
        print("Disk Information:")
        print(f"  SES Device: {disk_info['ses_device']}")
        print(f"  Slot  : {disk_info['slot']}")
        print(f"  Device: {disk_info['device']}")
        print(f"  Model : {disk_info['model']}")
        print(f"  Ident : {disk_info['ident']}")
        print(f"  Size  : {disk_info['size']}")
        print(f"  Status: {disk_info['status']}")

        try:
            cmd = f"sesutil locate -u /dev/{disk_info['ses_device']} {disk_name} {status}"
            subprocess.run(cmd, shell=True, check=True, capture_output=True, text=True)
            print(f"\nSuccessfully set LED for {disk_name} to '{status}' on {disk_info['ses_device']}")
        except subprocess.CalledProcessError as e:
            print(f"\nFailed to set LED for {disk_name} on {disk_info['ses_device']}: {e}")
    else:
        print(f"Failed to find information for {disk_name}. Check if the disk exists and is connected to an SES-capable enclosure.")


if __name__ == "__main__":
    if len(sys.argv) < 2 or len(sys.argv) > 3:
        print("Usage: python script_name.py <disk_number> [on|off]")
        sys.exit(1)

    disk_number = sys.argv[1]
    status = sys.argv[2] if len(sys.argv) == 3 else "on"

    set_disk_led(disk_number, status)

To run it, its as simple as this

./set-let-status.py 8 on

Leave a Reply