Using PHP preg_replace to read NIC information from check_nwc_health

Written by - 0 comments

Published on - Listed in PHP


For an automatic monitoring discovery script, I decided to use the "check_nwc_health" plugin in the background to list all interfaces of a target switch:

/usr/lib/nagios/plugins/check_nwc_health --hostname target --community public --mode list-interfaces-detail
000001 GigabitEthernet0/0 ________ unknown unknown
000002 Null0 ________ unknown unknown
000003 GigabitEthernet1/0/1 Server1 switchport notTrunking
000004 GigabitEthernet1/0/2 ________ switchport notTrunking
000005 GigabitEthernet1/0/3 ________ switchport notTrunking
000006 GigabitEthernet1/0/4 DNS Appliance switchport notTrunking
000007 GigabitEthernet1/0/5 Server-2 switchport notTrunking
000008 GigabitEthernet1/0/6 ________ switchport notTrunking
000009 GigabitEthernet1/0/7 Link to Switch 35 switchport notTrunking
000010 GigabitEthernet1/0/8 Printer switchport notTrunking
000011 GigabitEthernet1/0/9 ________ switchport notTrunking
000012 GigabitEthernet1/0/10 ________ switchport notTrunking
[...]

The output shows the following information for each interface:

INDEX INTERFACENAME INTERFACEALIAS INTERFACETYPE TRUNKINGINFO

Now I needed to find a way to go through each found line and show the interface name as well as the alias name.
As I programmed the script in PHP, I first attempted to write the output of check_nwc_health into a file, and "awk" or "sed" the relevant information out by using the exec() function. But when I needed to do this with the alias, I quickly came to limits. For example sed doesn't support all PCRE regular expressions. Therefore I decided to use PHP's preg_replace() function - which should have been the first choice anyway.

Especially the alias name was a bit tricky to figure out, because it can contain spaces. This is how I got there:

exec("/usr/lib/nagios/plugins/check_nwc_health --hostname $vAddress --community $vSnmpCommunity --mode list-interfaces-detail | grep -v '^OK'", $SnmpNetworkInterfaces, $returnNetworkInterfaces);

foreach ($SnmpNetworkInterfaces as $nic) {
  $nicname=preg_replace("/\d+\s(\S+)\s.*/i", "$1", $nic);
  $nicalias=preg_replace("/\d+\s\S+\s(.*)\s(switchport|unknown)\s(nottrunking|trunking|unknown)/i", "$1", $nic);
  echo "$nicname: $nicalias";
}

Which results in the following output:

GigabitEthernet0/0: ________
Null0: ________
GigabitEthernet1/0/1: Server1
GigabitEthernet1/0/2: ________
GigabitEthernet1/0/3: ________
GigabitEthernet1/0/4: DNS Appliance
GigabitEthernet1/0/5: Server-2
GigabitEthernet1/0/6: ________
GigabitEthernet1/0/7: Link to Switch 35
GigabitEthernet1/0/8: Printer
GigabitEthernet1/0/9: ________
GigabitEthernet1/0/10: ________

The empty lines are added from the check_nwc_health plugin if no ifAlias was defined in the switch's config.

To live-test the regex, I came across http://www.phpliveregex.com/, which helped me a lot. Check it out!


Add a comment

Show form to leave a comment

Comments (newest first)

No comments yet.