Nagios check_mac_address plugin

Last Updated on 17 januari 2022 by Syds

Onlangs overgegaan op het Solcon glasvezel pakket. De meegeleverde Arris settop boxen registreren geen hostname in DNS, dus dat maakte het checken op beschikbaarheid van de settop box in Nagios onmogelijk. Een oplossing zou het toekennen van static ip-addressen aan de settop boxen op mijn DHCP-server  zijn, maar dat is een beetje tegen mijn principes. Googlen naar “nagios check mac address” leverde niets op, alleen een post van een netwerkbeheerder van een hotel die van zijn 150 settop boxen alleen maar de MAC Addressen kende en het advies kreeg om ze allemaal maar een statisch IP address toe te kennen. Dus toen zelf maar een plugin geschreven.

In het kort doet de plugin een lookup in de arp tables of het mac address voorkomt, en zo ja, geeft het ip-address terug. Vervolgens voer ik een reguliere check_ping uit op dit ip-address, en geef het resultaat terug aan Nagios.

Hieronder de plugin:

pi@raspberrypi:/usr/local/nagios/libexec $ cat check_mac_address.sh
#!/bin/bash

while getopts "M:" OPTION;
do
        case $OPTION in
                "M") # Assign MAC_ADDRESS
                        MAC_ADDRESS="$OPTARG"
                ;;
        esac
done

if [ -z $MAC_ADDRESS ]; then
        # we need this parameter to continue
        EXIT_STRING="CRITICAL: MAC_ADDRESS variable has not been set!\n"
        EXIT_CODE=2
else
        IP_ADDRESS=$(arp -a | grep -i $MAC_ADDRESS | awk '{print $2}' | tr -d "()")
        if [ -z $IP_ADDRESS ]; then
                EXIT_STRING="WARNING: MAC Address not found\n"
                EXIT_CODE=1
        else
                EXIT_STRING=$(/usr/local/nagios/libexec/check_ping -H $IP_ADDRESS -w 3000.0,80% -c 5000.0,100% -p 5)
                EXIT_STRING=$(echo $EXIT_STRING | tr '[:lower:]' '[:upper:]')
                if [ $(echo $EXIT_STRING | grep "PING OK" | wc -l) -eq 1 ]; then
                        EXIT_CODE=0
                elif [ $(echo $EXIT_STRING | grep "WARNING" | wc -l) -eq 1 ]; then
                        EXIT_CODE=1
                else
                        EXIT_CODE=2
                fi
        fi
fi

echo "$EXIT_STRING\n"
echo "$EXIT_CODE\n"
exit $EXIT_CODE

Plugin is ook te download van Github: https://github.com/sydspost/check_mac_address

De plugin kent maar 1 optie, namelijk -M <mac address>.

Vervolgens twee commando’s toegevoegd aan /usr/local/nagios/etc/objects/commands.cfg, namelijk check_mac_address, die bovenstaande plugin aanroept en een dummy check, check-dummy-alive.

define command {

    command_name    check_mac_address
    command_line    $USER1$/check_mac_address.sh -M $MAC_ADDRESS$
}

define command {

    command_name    check-dummy-alive
    command_line    $USER1$/check_dummy 0
}

De dummy check zorgt ervoor dat de default check command in je host template altijd een “OK” oplevert. Om dit te bewerkstellingen passen we de parameter check_command in de host-definitie aan. In mijn geval resorteren de settop boxen onder de host definitie “ontvanger”. De check_command parameter passen we aan naar de dummy check “check-dummy-alive”

# TV ontvanger definition template
# This is NOT a real device, just a template!

define host {

    name                            ontvanger               ; The name of this host template
    use                             generic-host            ; This template inherits other values from the generic-host template
    check_period                    24x7                    ; By default, Linux hosts are checked round the clock
    check_interval                  5                       ; Actively check the host every 5 minutes
    retry_interval                  1                       ; Schedule host check retries at 1 minute intervals
    max_check_attempts              10                      ; Check each Linux host 10 times (max)
    check_command                   check-dummy-alive       ; Default command to check Linux hosts
    notification_period             Once_a_day_morning               ; Linux admins hate to be woken up, so we only notify during the day
                                                            ; Note that the notification_period variable is being overridden from
                                                            ; the value that is inherited from the generic-host template!
    notification_interval           30                     ; Resend notifications every 2 hours
    notification_options            d,u,r                   ; Only send notifications for specific host states
    contact_groups                  admins                  ; Notifications get sent to the admins by default
    register                        0                       ; DON'T REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
    check_interval                  1
    retry_interval                  1
    check_period                    24x7
    notification_interval           60
    first_notification_delay        0
}

Tot slot veranderen we het check command in de service definitie van de settop boxen. In mijn geval zijn die gedefinieerd in het configuratiebestand “tv_ontvanger.cfg”. Hieronder een voorbeeld van die service definitie

define service {
    use                     generic-service
    host_name               slaapkamersettop.sydspost.nl
    service_description     Mac Address Ping
    check_command           check_mac_address!-M a4:98:13:6e:ed:de
}

Het resultaat ziet er dan zo uit

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *