Monitor Serial Ports on Windows

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
User avatar
mikew
Posts: 243
Joined: Sun Feb 05, 2012 7:05 pm

Monitor Serial Ports on Windows

Post by mikew »

I need to be able to monitor both serial-to-ethernet and serial ports for a project on a Windows 7 machine. Just looking for ideas on the best approach if performance counters might work or a powershell script to capture serial port status. I do not need to parse data on the serial port only determine if it is working or not. Any ideas would be appreciated.
Mike Weber

Nagios Training/Consulting
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Monitor Serial Ports on Windows

Post by lmiltchev »

Mike, do you want to know if there is a serial port available or if it is usable (or in use)?

If you can put together a powershell script to give you the info you need, you could call it with NSClient++ or NCPA. I tested running the following two commands from within the powershell terminal on a Windows 7 box, and the output looked promising. Maybe you can try something similar in your script.

Code: Select all

Get-WMIObject Win32_SerialPort
Get-WMIObject Win32_SerialPort | Select-Object Name,DeviceID,Description
Be sure to check out our Knowledgebase for helpful articles and solutions!
User avatar
mikew
Posts: 243
Joined: Sun Feb 05, 2012 7:05 pm

Re: Monitor Serial Ports on Windows

Post by mikew »

Thanks for your help, just what I needed. If you leave this open I will post the code when I get it working.
Mike Weber

Nagios Training/Consulting
cmerchant
Posts: 546
Joined: Wed Sep 24, 2014 11:19 am

Re: Monitor Serial Ports on Windows

Post by cmerchant »

Ok. We'll leave this open for now.
User avatar
mikew
Posts: 243
Joined: Sun Feb 05, 2012 7:05 pm

Re: Monitor Serial Ports on Windows

Post by mikew »

Here is what I worked out:

Code: Select all

# Script to check COM port is used.
# Returns 0 if it is being used;
# Returns 2 if it's not used, or error occured.

# Usage:
# PowerShell.exe -ExecutionPolicy unrestricted -command ".\PS_CheckCOMPorts.ps1"
# PowerShell.exe -ExecutionPolicy unrestricted -command ".\PS_CheckCOMPorts.ps1" "COM1"
# PowerShell.exe -ExecutionPolicy unrestricted -command ".\PS_CheckCOMPorts.ps1" "COM1,COM3"

Param(
    [array] $COM_PORTS = @() # Can be set as a command line parameter: COM1, COM2, COM3, COM4. If not set, all available ports will be checked. 
)

$errorsCount = 0 # Used to track errors

if ($COM_PORTS.Count -eq 0) # check all available ports
{
    $COM_PORTS = ([System.IO.Ports.SerialPort]::GetPortNames() | sort)
  
}

if ($COM_PORTS -eq $null)
{
    Write-Error "COM ports are not found!"
    $errorsCount += 1
}
else
{
    foreach ($COM_PORT_NAME in $COM_PORTS)
    {
        Write-Host "Checking $COM_PORT_NAME..."
            
        # Let's try to open COM port.
        Try
        {
            $port = new-Object System.IO.Ports.SerialPort($COM_PORT_NAME)
                    
            # if it's already used the exception will be thrown 
            $port.open()
            $port.close()

            # if we are here - it means port is not used. Exception wasn't thrown
            Write-Host -ForegroundColor Red "$COM_PORT_NAME is not used [FAIL]"
            $errorsCount += 1 # Port is not used
        }
        Catch
        {
            $ErrorMessage = $_.Exception.InnerException

            if($ErrorMessage -match "Access to the port '$COM_PORT_NAME' is denied.")
            {
                # port is used
                Write-Host -ForegroundColor Green "$COM_PORT_NAME is used [OK]"
            }
            else
            {
                Write-Error $ErrorMessage
                $errorsCount += 1
            }

        }
    }
}

if ($errorsCount -gt 0)
{
    exit 2
}
Mike Weber

Nagios Training/Consulting
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Monitor Serial Ports on Windows

Post by lmiltchev »

MIke, I tested the script. I shows if the device is in use or not. It doesn't show if the device is connected (but not in use).

Anyway, I placed the script in the NSClient++ scripts directory, created a command in the nsclient.ini, restarted the NSClient++ service and ran on the Nagios XI server:

Code: Select all

[root@testbox libexec]# ./check_nrpe -H x.x.x.x -c check_serial_port
Checking COM1...
COM1 is not used [FAIL]
Checking COM3...
COM3 is not used [FAIL]|
I connected a device to the serial port and ran the check again - same output. Then I used putty to connect to COM1 and ran the command again. Now I got:

Code: Select all

[root@testbox libexec]# ./check_nrpe -H x.x.x.x -c check_serial_port
Checking COM1...
COM1 is used [OK]
Checking COM3...
COM3 is not used [FAIL]|
Hope this helps.
Be sure to check out our Knowledgebase for helpful articles and solutions!
Locked