Monitoring individual ports on a network switch

I want to monitor each individual ports on a network switch and I’m able to do so with the following code:

apply Service for (interface => config in host.vars.snmp_interfaces) {
  import "generic-service"

  display_name = "Network traffic on " + config.snmp_interface_label
  check_command = "snmp-interface"

  vars.snmp_interface_ifname = "true"
  vars.snmp_interface_noregexp = "true"
  vars.snmp_warn = "100,100"
  vars.snmp_crit = "100,100"
  vars += config

  assign where host.vars.snmp_address
}

On the host configuration file, I have the following code:

  vars.snmp_interfaces["snmp-int-port1"] = {
    snmp_interface = "1"
    snmp_interface_label = "port 1"
  }

The problem is that since the network switch has 52 ports, I’ll need to create 52 entries for the above code. Is there a simpler and more efficient way of doing this?

I’ve found a better way around this by creating a template and copy and paste the snmp_interfaces[""] code with the updated port number. By doing this, I should be able to reuse this template for other Network switch. Also, it will keep my host configuration file cleaner.

Hi,

can you share the concrete solution for others looking into your topic too?

Thanks,
Michael

Sure!

Basically, I have created a template that contains the interface information named int-ports-template.conf

template Host "int-ports-template" {

  vars.snmp_interfaces["snmp-int-port1"] = {
    snmp_interface = "1"
    snmp_interface_label = "port 01"
    grafana_graph_disable = true
  }

  vars.snmp_interfaces["snmp-int-port2"] = {
    snmp_interface = "2"
    snmp_interface_label = "port 02"
    grafana_graph_disable = true
  }

  vars.snmp_interfaces["snmp-int-port3"] = {
    snmp_interface = "3"
    snmp_interface_label = "port 03"
    grafana_graph_disable = true
  }
}

The Host configuration file will simply import the above template.

object Host "network-switch" {
  import "generic-host"
  import "int-ports-template"

  address = "x.x.x.x"
  groups = [ "network" ]
  vars.hostgroup = "Network"

  vars.snmp_v3 = true
  ...
}

The Service configuration file remains the same.

apply Service for (interface => config in host.vars.snmp_interfaces) {
  import "generic-service"

  display_name = "Network traffic on " + config.snmp_interface_label
  check_command = "snmp-interface"

  vars.snmp_interface_ifname = "true"
  vars.snmp_interface_noregexp = "true"
  vars.snmp_warn = "100,100"
  vars.snmp_crit = "100,100"
  vars += config

  assign where host.vars.snmp_address
}
1 Like