Monitoring multiple tcp-ports at once in Nagios


Warning: fopen(/home/joustie/domains/joustie.nl/public_html/wp-content/plugins/devformatter/geshi/geshi/perl.php) [function.fopen]: failed to open stream: Permission denied in /home/joustie/domains/joustie.nl/public_html/wp-content/plugins/devformatter/devgeshi.php on line 103

Warning: fopen(/home/joustie/domains/joustie.nl/public_html/wp-content/plugins/devformatter/geshi/geshi/perl.php) [function.fopen]: failed to open stream: Permission denied in /home/joustie/domains/joustie.nl/public_html/wp-content/plugins/devformatter/devgeshi.php on line 103

Warning: fopen(/home/joustie/domains/joustie.nl/public_html/wp-content/plugins/devformatter/geshi/geshi/perl.php) [function.fopen]: failed to open stream: Permission denied in /home/joustie/domains/joustie.nl/public_html/wp-content/plugins/devformatter/devgeshi.php on line 103

During the last two months I have been setting up Nagios as a side project. We have a lot of new customers coming in and we need to have a tight grip on our systems to satisfy service levels. So as I set up a Debian host to run Nagios on and was making progress in adding systems, I encountered several issues.

One of them was the fact that I need to monitor several tcp ports that are utilized in the applications we are running in a JBoss container. The default script in Nagios to monitor a tcp-port works well and I have used it to create a custom plugin to monitor several ports sequentially.

I have defined a service for this in Nagios and you can see this service is applied to a particular host group:

define service {
        hostgroup_name                  dme-servers
        servicegroups                   DME_DIENSTVERLENING
        service_description             check_tcp_ports
        check_command                   check_tcp_multiport!1098,1099,4444,5011,5021
        use                             generic-service
}

Below is the code I have put in
/usr/lib/nagios/plugins/check_tcp_multiport.pl

 perl |  copy code |? 
01
02
#!/usr/bin/perl -w
03
use strict;
04
use Getopt::Long;
05
my ($plugin_home,$hostname,$ports);
06
$plugin_home = "/usr/lib/nagios/plugins/";
07
getoptions();
08
 
09
sub getoptions
10
{
11
      my @ports;
12
      my $error_count = 0;
13
      my $error_ports ="";
14
 
15
      my $result=GetOptions
16
      (
17
              "h|host=s"=>\$hostname,
18
              "p|port=s" => sub
19
              {
20
                      $ports = pop,
21
                      @ports = split(/,/,$ports)
22
              },
23
      );
24
 
25
      foreach my $port (@ports)
26
      {
27
           my $output = `$plugin_home/check_tcp -H $hostname -p $port`;
28
           my $exit_code = $?;
29
           chomp($output);
30
           if  ($exit_code != 0)
31
           {
32
                   $error_count++;
33
                   $error_ports .= "$port,";
34
           }
35
      }
36
 
37
      if ( $error_count == 0)
38
      {
39
              print "All ports are OK on $hostname\n";
40
              exit 0;
41
      }
42
      chop($error_ports);
43
      print "There are $error_count errors on $hostname: $error_ports\n";
44
      exit 2;
45
}
46
 
47
 
48

Hello world!

Welcome to my renewed site. I used to blog in the past, and I’d like to share some things again. This time, most of my ramblings will be work related. I hope I can help/inspire/amuse my readers!