Create Sensor with Powershell for Adaptec RAID 6805

Dear Community,

I wanted to monitor the Status and Temperature of my Adaptec RAID 6805 card. The card is installed in a Windows Server 2016 and have Adaptec maxView Storage Manager installed. Because Adaptec stopped support for SNMP monitoring in the newer versions I had to find a different way. I started searching reddit and this community and couldn’t find anyone who created a sensor with powershell.

So I’ve made a powershell script to create/update a sensor with the Home Assistant API. I tried to make the script/function as reusable as possible for any future sensors.

Two sensors are created in Home-Assistant:

  • The RaidStatus sensor state is “Optimal”.
  • The RaidTemperature sensor state is “71”. This sensor is also using the optional parameter “unit_of_measurement” for the degrees symbol.

Script:

# Function Update-Sensor 
# Parameter examples:
#     Url   'https://homeassistantbaseurl' (Mandatory)
#     Name  'Raid Temperature'             (Mandatory)
#     State  $raid_temp                    (Mandatory)
#     Unit   '°C'                          (Optional)


Function Update-Sensor {
    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$Url,
        [Parameter(Mandatory=$true)]
        [string]$Name,
        [Parameter(Mandatory=$true)]
        [string]$State,
        [Parameter(Mandatory=$false)]
        [string]$Unit
        )

    # Create uri and convert friendly_name to "sensor.friendly_name"
    $InvokeUri = "$Url/api/states/" + "sensor." + $Name.ToLower() | foreach-object {$_.replace(' ','_')}
    
    # Setting body with state and attributes
    $InvokeBody = @{
        "state"= $State
        "attributes"= @{
            "friendly_name"= "$Name"
            "unit_of_measurement"= "$Unit"
        }
    } | ConvertTo-Json

    # Setting Security protocol, prevents SSL error
    # Powershell error: The underlying connection was closed: An unexpected error occurred on a send.
    # Home-assistant.log: Error doing job: SSL error errno:1 reason: WRONG_VERSION_NUMBER
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

    # Post to API Url with attributes in body
    Invoke-Webrequest -Method POST -Uri $InvokeUri -Body $InvokeBody
}

Push-Location $PSScriptRoot
# Setting Adaptec Maxview Storage Manager CLI
$Arcconf = "c:\program files\adaptec\maxview storage manager\arcconf.exe"

# Get arcconf config report
$Report = & $Arcconf getconfig 1 AL

# Extracting variables from report
$RaidStatus = $Report | where-object {$_ -match "Controller Status"} | ForEach-Object { ($_ -split "\s+",(5+1 ))[(5-1)] }
$RaidTemp = $Report | where-object {$_ -match "Temperature"} | where-object {$_ -notmatch "Supported" } | ForEach-Object { ($_ -split "\s+",(4+1 ))[(4-1)] }

# Update or Create Home-Assistant Sensor
Update-Sensor -Url 'https://example.com' -Name 'Raid Status' -State $RaidStatus
Update-Sensor -Url 'https://example.com' -Name 'Raid Temperature' -State $RaidTemp -Unit '°C'

Enjoy and happy new year!!

2 Likes

Updated powerhell function to include the long-lived token.
And added a check for logical array status and failed stripes (disks) in arrays.

Function Update-Sensor {
    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$Token,
        [Parameter(Mandatory=$true)]
        [string]$Url,
        [Parameter(Mandatory=$true)]
        [string]$Name,
        [Parameter(Mandatory=$true)]
        [string]$State,
        [Parameter(Mandatory=$false)]
        [string]$Unit
        )

    # Header with home assistant long-lived token
    $InvokeHeaders = @{
        "Authorization"= "Bearer $Token"
    } 

    # Create uri and convert friendly_name to "sensor.friendly_name"
    $InvokeUri = "$Url/api/states/" + "sensor." + $Name.ToLower() | foreach-object {$_.replace(' ','_')}
    
    # Setting body with state and attributes
    $InvokeBody = @{
        "state"= $State
        "attributes"= @{
            "friendly_name"= "$Name"
            "unit_of_measurement"= "$Unit"
        }
    } | ConvertTo-Json

    # Setting Security protocol, prevents SSL error
    # Powershell error: The underlying connection was closed: An unexpected error occurred on a send.
    # Home-assistant.log: Error doing job: SSL error errno:1 reason: WRONG_VERSION_NUMBER
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    
    # Post to API Url with attributes in body
    Invoke-Webrequest -Method POST -Headers $InvokeHeaders -Uri $InvokeUri -Body $InvokeBody
}

$Logfile = "$PSScriptRoot\log-checkraid.txt"
Push-Location $PSScriptRoot

# Setting Adaptec Maxview Storage Manager CLI
$Arcconf = "c:\program files\adaptec\maxview storage manager\arcconf.exe"

# Get arcconf config report
$ReportAL = & $Arcconf getconfig 1 AL
$ReportLD0 = & $Arcconf getconfig 1 LD 0
$ReportLD1 = & $Arcconf getconfig 1 LD 1
$ReportSmart = & $Arcconf getsmartstats 1 #TABULAR
$ReportAL | Out-File -FilePath $Logfile

# Extracting variables from reports
$RaidStatus = $ReportAL | where-object {$_ -match "Controller Status"} | ForEach-Object { ($_ -split "\s+",(5+1))[(5-1)] }
$RaidTemp = $ReportAL | where-object {$_ -match "Temperature"} | where-object {$_ -notmatch "Supported" } | ForEach-Object { ($_ -split "\s+",(4+1))[(4-1)] }
$RaidStatusArray01 = $ReportLD0 | where-object {$_ -match "Status of Logical Device"} | ForEach-Object { ($_ -split "\s+",(7+1))[(7-1)] }
$RaidFailedArray01 = $ReportLD0 | where-object {$_ -match "Failed stripes"} | ForEach-Object { ($_ -split "\s+",(5+1))[(5-1)] }
$RaidStatusArray02 = $ReportLD1 | where-object {$_ -match "Status of Logical Device"} | ForEach-Object { ($_ -split "\s+",(7+1))[(7-1)] }
$RaidFailedArray02 = $ReportLD1 | where-object {$_ -match "Failed stripes"} | ForEach-Object { ($_ -split "\s+",(5+1))[(5-1)] }


# Update or Create Home-Assistant Sensor
Update-Sensor -Url 'https://example.com' -Name 'Raid Status' -State $RaidStatus -Token 'insert long-lived token here'
Update-Sensor -Url 'https://example.com' -Name 'Raid Temperature' -State $RaidTemp -Unit '°C' -Token  'insert long-lived token here'
Update-Sensor -Url 'https://example.com' -Name 'Raid Status Array01' -State $RaidStatusArray01 -Token  'insert long-lived token here'
Update-Sensor -Url 'https://example.com' -Name 'Raid Failed Array01' -State $RaidFailedArray01 -Token  'insert long-lived token here'
Update-Sensor -Url 'https://example.com' -Name 'Raid Status Array02' -State $RaidStatusArray02 -Token  'insert long-lived token here'
Update-Sensor -Url 'https://example.com' -Name 'Raid Failed Array02' -State $RaidFailedArray02 -Token  'insert long-lived token here'