Check multiple ports on single machine using check_tcp



This being my first post kindly excuse if any errors!
I would like to know if its possible to check multiple ports on a machine using check_tcp plugin.
I have a critical server which needs to be monitored with 5 different tcp ports
Something like (under services.cfg)

define service {
service_description TCP_CHECK
use tcp-service
contact_groups tcpadmins
check_command check_tcp!26585,4333,5555,6666,7777
host_name alpha2

where tcp-service looks like;

define service{
name tcp-service
max_check_attempts 5
normal_check_interval 5
retry_check_interval 2
active_checks_enabled 1
passive_checks_enabled 1
parallelize_check 1
obsess_over_service 1
check_freshness 0
notifications_enabled 1
event_handler_enabled 1
flap_detection_enabled 1
process_perf_data 0
retain_status_information 1
retain_nonstatus_information 1
is_volatile 0
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r,f
register 0

Would the ‘comma’ separated ports numbers in check_command actually work?
Is there a alternative work around?

Any quick help would be greatly appreciated.

Thank you!!


I’m not really familiar with check_tcp, but it seems to only accept 1 port. Maybe a good solution could be to define 1 service per port to monitor, and a service group to gather this services: … rvicegroup


Thanks a lot Jean for a quick response.
I already do that… defining 1 service per port.
But i have too many hosts to be monitored, each having ‘n’ number of ports to be checked. This way it makes services.cfg a bulky file with too much repetition.


well, you could write a small script that would take your list of ports as argument and call check_tcp for each port in this list.
it should be easy enough and will spare you from writing a lot of cfg files :slight_smile:


Thats a nice idea as well… Here’s what i came up with:

#!/usr/bin/perl -w
use strict;
use Getopt::Long;
my ($hostname,$ports);

sub getoptions{
        my $result=GetOptions(
                              "p|port=s" => sub {
                                                          $ports = pop,
                                                          my $output = `/usr/local/bin/nagios/libexec/check_tcp -H $hostname -p $ports`;
                                                          print "$output : $ports\n";
        if (!($hostname && $ports)) {
                print "Enter hostname & port\n";

Usage : -h -p -p -p
Excuse the indentation… i tried to make it look better…but!

Any comments?


well, if it works, there’s nothing more to say :slight_smile:
BUT, as it will be used as a nagios plug in, there are 2 things you have to do:

  1. print the output on 1 line
  2. use an exit code depending on the results

btw: I would have done a bit simpler by passing the ports as a comma separated list and used split. Something like that:

[code]#!/usr/bin/perl -w
use strict;

my $hostname = $ARGV[0];
my $ports = $ARGV[1];

my $error = 0;
my $error_ports = “”;
my @ports = split(/,/,$ports);
foreach my $port (@ports) {
my $output = /usr/local/bin/nagios/libexec/check_tcp -H $hostname -p $port;
my $exit_code = $?;
if ( $exit_code != 0 ) {
$error_ports .= “$port,”;
if ( $error == 0 ) {
print “All ports are OK on $host\n”;
exit 0;
print “There are $error errors on $hostname: $error_ports\n”;
exit 2;

ps: I just wrote it directly on this post, so don’t expect this code to work if you copy/paste it :slight_smile: