Personal tools
A custom port scanner plugin for Nagios and Checkmk

doors scanner

Jan 11, 2013

A custom port scanner plugin for Nagios and Checkmk

“I can't believe how dark was the time without a monitoring system...”. This is what my boss told me last time we spoke about reliability of service

Some months ago I posted (Italian only) about Checkmk as a monitoring tool which can be used also to taking care of Plone instances. This time I will post a small custom plugin I wrote for Nagios.

Checkmk scan_port plugincode

 

 

 

I was looking for a way (a plugin) to test some specific ports on my servers within the local network. Surfing the net I found many scripts that do almost what I wanted (especially one written in Perl as nmap wrapper) but despite of all I decide to write my own.

The reason of this choice is because most of them make use of system tools in order to perform port scan action. There is no problem with that, tools like nmap are really commons, but what I want is avoid any kind of dependence.

Moreover I prefer have control on the code since the beginning mainly because I can choose the programming language ;)

The code

Check_port plugin is written in Python and it uses only the standard library thus it should be portable as long as you have Python installed on your system.

The code is quite simple, for each given port the program test if the connection is active.

Note: This test tells you if something is listening but nothing more.

Usage

Plugin accepts 3 arguments:
  • -o <ip>  : remote host to scan
  • -p <ports>   : list of ports to scan
  • -t <timeout>] (optional)

Example from command line:
# Scan for port 22 and 25 on a remote server 192.168.1.100
./check_ports.py -o 192.168.1.100 -p 22,25
Only two return states are allowed:
  • OK: All the given ports are opened
  • CRITICAL: At list one of the given port is closed
State: OK
./check_ports.py -o 188.165.195.56 -p 22
OK; opened 22 ; closed
State CRITICAL
./check_ports.py -o 188.165.195.56 -p 23
CRITICAL; opened  ; closed 23

Checkmk configuration

The easier way to configure the plugin is define a command as legacy check, and
Checkmk will do the rest:
# 'check_ports'
define command{
command_name check_ports
command_line $USER1$/check_ports.py -o $ARG1$ -p $ARG2$
}
Then you can specify a list of remote server to test as show below:
(( "Check_ports!127.0.0.1!22,23", "Check ports", False), ['host'])

Note: be sure you are not blocked by a firewall.

At the moment this plugin accepts only a list of ports, maybe in future will be possible to use also a range.
If you need more information on how to use lagacy plugin in Checkmk you can find some resource here.
comments powered by Disqus