Configure Nagios SNMP monitoring for network devices with automated discovery and templates

Intermediate 45 min Apr 10, 2026 29 views
Ubuntu 24.04 Debian 12 AlmaLinux 9 Rocky Linux 9

Set up comprehensive SNMP monitoring in Nagios Core with automated network device discovery, custom templates for switches and routers, and real-time alerting for network infrastructure monitoring.

Prerequisites

  • Nagios Core 4.5 installed and configured
  • Network devices with SNMP enabled
  • Basic understanding of SNMP and network protocols
  • Root or sudo access to the monitoring server

What this solves

Network administrators need comprehensive monitoring of switches, routers, and other SNMP-enabled devices to maintain network performance and availability. This tutorial configures Nagios Core with SNMP monitoring capabilities, automated device discovery, and customizable templates for different network equipment types.

Prerequisites

Before starting, ensure you have a working Nagios Core installation. If you haven't installed Nagios yet, follow our Nagios Core installation guide. You'll also need network devices with SNMP enabled and proper community strings configured.

Step-by-step configuration

Install SNMP utilities and MIB libraries

Install the necessary SNMP tools and Management Information Base (MIB) libraries for querying network devices.

sudo apt update
sudo apt install -y snmp snmp-mibs-downloader libsnmp-perl
sudo dnf install -y net-snmp net-snmp-utils net-snmp-perl

Download and configure MIB files

Download standard MIB files and configure SNMP to use them for better device identification and monitoring.

sudo download-mibs
sudo sed -i 's/^mibs :/# mibs :/' /etc/snmp/snmp.conf
sudo mkdir -p /usr/share/snmp/mibs
sudo wget -O /usr/share/snmp/mibs/RFC1213-MIB http://www.iana.org/assignments/ianaiftype-mib/ianaiftype-mib

Test SNMP connectivity to network devices

Verify SNMP communication with your network devices before configuring Nagios monitoring.

snmpwalk -v2c -c public 203.0.113.10 1.3.6.1.2.1.1.1.0
snmpget -v2c -c public 203.0.113.10 1.3.6.1.2.1.1.5.0
Note: Replace "public" with your actual SNMP community string and 203.0.113.10 with your device IP address.

Create SNMP command definitions

Define reusable SNMP commands in Nagios for common network monitoring tasks.

# SNMP System Information
define command{
    command_name    check_snmp_system
    command_line    $USER1$/check_snmp -H $HOSTADDRESS$ -C $ARG1$ -o 1.3.6.1.2.1.1.3.0 -w $ARG2$ -c $ARG3$
}

SNMP Interface Status

define command{ command_name check_snmp_interface command_line $USER1$/check_snmp -H $HOSTADDRESS$ -C $ARG1$ -o 1.3.6.1.2.1.2.2.1.8.$ARG2$ -r 1 -w 1 -c 1 }

SNMP Interface Utilization

define command{ command_name check_snmp_interface_util command_line $USER1$/check_snmp -H $HOSTADDRESS$ -C $ARG1$ -o 1.3.6.1.2.1.2.2.1.10.$ARG2$,1.3.6.1.2.1.2.2.1.16.$ARG2$ -w $ARG3$ -c $ARG4$ }

SNMP CPU Utilization

define command{ command_name check_snmp_cpu command_line $USER1$/check_snmp -H $HOSTADDRESS$ -C $ARG1$ -o 1.3.6.1.4.1.9.9.109.1.1.1.1.7.1 -w $ARG2$ -c $ARG3$ }

SNMP Memory Utilization

define command{ command_name check_snmp_memory command_line $USER1$/check_snmp -H $HOSTADDRESS$ -C $ARG1$ -o 1.3.6.1.4.1.9.9.48.1.1.1.5.1,1.3.6.1.4.1.9.9.48.1.1.1.6.1 -w $ARG2$ -c $ARG3$ }

Create network device host template

Define a reusable host template for network devices with common SNMP monitoring settings.

# Generic Network Device Template
define host{
    name                    generic-network-device
    use                     generic-host
    check_period            24x7
    check_interval          5
    retry_interval          1
    max_check_attempts      10
    check_command           check-host-alive
    notification_period     24x7
    notification_interval   30
    notification_options    d,u,r
    contact_groups          admins
    register                0
}

Switch Template

define host{ name switch-template use generic-network-device hostgroups switches icon_image switch.png vrml_image switch.png statusmap_image switch.gd2 register 0 }

Router Template

define host{ name router-template use generic-network-device hostgroups routers icon_image router.png vrml_image router.png statusmap_image router.gd2 register 0 }

Create service templates for SNMP monitoring

Define service templates for common network device monitoring tasks.

# SNMP Service Template
define service{
    name                    generic-snmp-service
    use                     generic-service
    check_interval          5
    retry_interval          1
    max_check_attempts      3
    notification_interval   30
    register                0
}

Network Interface Service Template

define service{ name network-interface-service use generic-snmp-service normal_check_interval 2 retry_check_interval 1 register 0 }

Create host groups for network devices

Organize network devices into logical groups for easier management and monitoring.

# Network Device Host Groups
define hostgroup{
    hostgroup_name  switches
    alias           Network Switches
    members
}

define hostgroup{
    hostgroup_name  routers
    alias           Network Routers
    members
}

define hostgroup{
    hostgroup_name  firewalls
    alias           Network Firewalls
    members
}

define hostgroup{
    hostgroup_name  wireless-aps
    alias           Wireless Access Points
    members
}

Configure network devices for monitoring

Create individual host definitions for your network devices using the templates.

# Core Switch
define host{
    use                 switch-template
    host_name           core-switch-01
    alias               Core Switch 01
    address             203.0.113.10
    hostgroups          switches
    parents
}

Access Switch

define host{ use switch-template host_name access-switch-01 alias Access Switch 01 address 203.0.113.11 hostgroups switches parents core-switch-01 }

Border Router

define host{ use router-template host_name border-router-01 alias Border Router 01 address 203.0.113.1 hostgroups routers parents }

Configure SNMP services for network devices

Define specific SNMP monitoring services for each type of network device.

# System Uptime Monitoring
define service{
    use                     generic-snmp-service
    host_name               core-switch-01,access-switch-01,border-router-01
    service_description     System Uptime
    check_command           check_snmp_system!public!100000!50000
}

Interface Status Monitoring (GigabitEthernet1/0/1)

define service{ use network-interface-service host_name core-switch-01 service_description Interface Gi1/0/1 Status check_command check_snmp_interface!public!1 }

Interface Status Monitoring (GigabitEthernet1/0/2)

define service{ use network-interface-service host_name core-switch-01 service_description Interface Gi1/0/2 Status check_command check_snmp_interface!public!2 }

CPU Utilization (Cisco devices)

define service{ use generic-snmp-service host_name border-router-01 service_description CPU Utilization check_command check_snmp_cpu!public!80!95 }

Memory Utilization (Cisco devices)

define service{ use generic-snmp-service host_name border-router-01 service_description Memory Utilization check_command check_snmp_memory!public!80!95 }

Create automated device discovery script

Implement a script for automated discovery of SNMP-enabled devices on your network.

#!/bin/bash

SNMP Network Device Discovery Script

Usage: ./snmp_discovery.sh network_range community_string

NETWORK_RANGE=${1:-"203.0.113.0/24"} COMMUNITY=${2:-"public"} OUTPUT_FILE="/tmp/discovered_devices.txt" CONFIG_FILE="/usr/local/nagios/etc/objects/discovered-devices.cfg" echo "Starting SNMP device discovery on network: $NETWORK_RANGE" echo "Using community string: $COMMUNITY"

Clear previous results

> $OUTPUT_FILE > $CONFIG_FILE

Scan network range for SNMP devices

nmap -sU -p 161 --open $NETWORK_RANGE | grep -E "^Nmap scan report" | awk '{print $5}' > /tmp/potential_devices.txt while read -r ip; do # Test SNMP connectivity sysname=$(snmpget -v2c -c $COMMUNITY -t 2 -r 1 $ip 1.3.6.1.2.1.1.5.0 2>/dev/null | awk -F'STRING: ' '{print $2}' | tr -d '"') sysdesc=$(snmpget -v2c -c $COMMUNITY -t 2 -r 1 $ip 1.3.6.1.2.1.1.1.0 2>/dev/null | awk -F'STRING: ' '{print $2}' | tr -d '"') if [ ! -z "$sysname" ]; then echo "$ip|$sysname|$sysdesc" >> $OUTPUT_FILE echo "Found device: $ip - $sysname" # Generate basic Nagios configuration hostname=$(echo $sysname | tr ' ' '-' | tr '[:upper:]' '[:lower:]') echo "# Auto-discovered device: $sysname" >> $CONFIG_FILE echo "define host{" >> $CONFIG_FILE echo " use generic-network-device" >> $CONFIG_FILE echo " host_name $hostname" >> $CONFIG_FILE echo " alias $sysname" >> $CONFIG_FILE echo " address $ip" >> $CONFIG_FILE echo " hostgroups discovered-devices" >> $CONFIG_FILE echo "}" >> $CONFIG_FILE echo "" >> $CONFIG_FILE # Add basic SNMP service echo "define service{" >> $CONFIG_FILE echo " use generic-snmp-service" >> $CONFIG_FILE echo " host_name $hostname" >> $CONFIG_FILE echo " service_description System Uptime" >> $CONFIG_FILE echo " check_command check_snmp_system!$COMMUNITY!100000!50000" >> $CONFIG_FILE echo "}" >> $CONFIG_FILE echo "" >> $CONFIG_FILE fi done < /tmp/potential_devices.txt echo "Discovery complete. Found $(wc -l < $OUTPUT_FILE) SNMP devices." echo "Configuration written to: $CONFIG_FILE" echo "Please review and restart Nagios to apply changes."

Make discovery script executable and create hostgroup

Set proper permissions for the discovery script and create a hostgroup for discovered devices.

sudo chmod 755 /usr/local/nagios/libexec/snmp_discovery.sh
sudo chown nagios:nagios /usr/local/nagios/libexec/snmp_discovery.sh
# Add this to your existing hostgroups configuration
define hostgroup{
    hostgroup_name  discovered-devices
    alias           Auto-discovered Network Devices
    members
}

Create interface monitoring script

Implement automated interface discovery and monitoring for network devices.

#!/bin/bash

Network Interface Discovery Script

Usage: ./interface_discovery.sh device_ip community_string

DEVICE_IP=$1 COMMUNITY=$2 OUTPUT_FILE="/tmp/interfaces_${DEVICE_IP}.cfg" if [ -z "$DEVICE_IP" ] || [ -z "$COMMUNITY" ]; then echo "Usage: $0 " exit 1 fi echo "Discovering interfaces on device: $DEVICE_IP"

Clear previous results

> $OUTPUT_FILE

Get interface information

snmpwalk -v2c -c $COMMUNITY $DEVICE_IP 1.3.6.1.2.1.2.2.1.2 2>/dev/null | while read line; do interface_index=$(echo $line | awk -F'.' '{print $NF}' | awk '{print $1}') interface_name=$(echo $line | awk -F'STRING: ' '{print $2}' | tr -d '"') # Skip loopback and null interfaces if [[ $interface_name == "Loopback" ]] || [[ $interface_name == "Null" ]]; then continue fi # Get interface operational status oper_status=$(snmpget -v2c -c $COMMUNITY $DEVICE_IP 1.3.6.1.2.1.2.2.1.8.$interface_index 2>/dev/null | awk '{print $NF}') admin_status=$(snmpget -v2c -c $COMMUNITY $DEVICE_IP 1.3.6.1.2.1.2.2.1.7.$interface_index 2>/dev/null | awk '{print $NF}') if [ ! -z "$interface_name" ] && [ "$admin_status" == "1" ]; then service_name=$(echo "Interface $interface_name Status" | sed 's/[^a-zA-Z0-9 ]/_/g') hostname=$(snmpget -v2c -c $COMMUNITY $DEVICE_IP 1.3.6.1.2.1.1.5.0 2>/dev/null | awk -F'STRING: ' '{print $2}' | tr -d '"' | tr ' ' '-' | tr '[:upper:]' '[:lower:]') echo "# Interface: $interface_name (Index: $interface_index)" >> $OUTPUT_FILE echo "define service{" >> $OUTPUT_FILE echo " use network-interface-service" >> $OUTPUT_FILE echo " host_name $hostname" >> $OUTPUT_FILE echo " service_description $service_name" >> $OUTPUT_FILE echo " check_command check_snmp_interface!$COMMUNITY!$interface_index" >> $OUTPUT_FILE echo "}" >> $OUTPUT_FILE echo "" >> $OUTPUT_FILE echo "Found interface: $interface_name (Index: $interface_index)" fi done echo "Interface discovery complete for $DEVICE_IP" echo "Configuration written to: $OUTPUT_FILE"

Make interface discovery script executable

Set proper permissions for the interface discovery script.

sudo chmod 755 /usr/local/nagios/libexec/interface_discovery.sh
sudo chown nagios:nagios /usr/local/nagios/libexec/interface_discovery.sh

Update main Nagios configuration

Include the new configuration files in the main Nagios configuration.

# Add these lines to include SNMP monitoring configurations
cfg_file=/usr/local/nagios/etc/objects/network-devices.cfg
cfg_file=/usr/local/nagios/etc/objects/network-services.cfg
cfg_file=/usr/local/nagios/etc/objects/discovered-devices.cfg
cfg_file=/usr/local/nagios/etc/objects/hostgroups.cfg

Validate configuration and restart Nagios

Check the Nagios configuration for syntax errors and restart the service.

sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
sudo systemctl restart nagios
sudo systemctl status nagios

Run automated discovery

Execute the discovery scripts to automatically find and configure network devices for monitoring.

# Discover SNMP devices on your network
sudo /usr/local/nagios/libexec/snmp_discovery.sh 203.0.113.0/24 public

Discover interfaces on a specific device

sudo /usr/local/nagios/libexec/interface_discovery.sh 203.0.113.10 public

Validate and reload configuration

sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg sudo systemctl reload nagios

Configure custom monitoring templates

Create specialized monitoring templates for different types of network equipment with vendor-specific OIDs and thresholds.

# Cisco Switch Template
define host{
    name                    cisco-switch-template
    use                     switch-template
    hostgroups              cisco-switches
    register                0
}

Cisco Router Template

define host{ name cisco-router-template use router-template hostgroups cisco-routers register 0 }

HP/Aruba Switch Template

define host{ name hp-switch-template use switch-template hostgroups hp-switches register 0 }

Juniper Router Template

define host{ name juniper-router-template use router-template hostgroups juniper-routers register 0 }

Verify your setup

Test SNMP connectivity and verify that Nagios is properly monitoring your network devices.

# Test SNMP connectivity
snmpwalk -v2c -c public 203.0.113.10 1.3.6.1.2.1.1

Check Nagios service status

sudo systemctl status nagios

Verify configuration syntax

sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

Check active services in Nagios log

sudo tail -f /usr/local/nagios/var/nagios.log

View discovered devices

cat /tmp/discovered_devices.txt

Access the Nagios web interface at http://your-server-ip/nagios to view the network device monitoring status and configure additional alerting as needed. For enhanced visualization, consider integrating with Grafana dashboards.

Common issues

Symptom Cause Fix
SNMP timeout errors Incorrect community string or device IP Verify community string and test with snmpget
No data returned from SNMP queries SNMP service disabled on device Enable SNMP on network device with proper community
Discovery script finds no devices Network range incorrect or SNMP blocked Check network range and firewall rules for UDP port 161
Interface monitoring shows incorrect status Wrong interface index or OID Use snmpwalk to verify interface indices and OIDs
Nagios configuration validation fails Syntax errors in configuration files Check file syntax and ensure proper template inheritance
Services show "UNKNOWN" status Check command not found or permissions Verify check_snmp plugin exists and is executable

Next steps

Automated install script

Run this to automate the entire setup

Need help?

Don't want to manage this yourself?

We handle managed devops services for businesses that depend on uptime. From initial setup to ongoing operations.