Created
March 2, 2017 00:30
-
-
Save joshua-chavanne/03aeaf7039929ba1f8c1777ed82fc65f to your computer and use it in GitHub Desktop.
Ports and Procs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
#use strict; | |
#Author: Dennison Williams | |
use Nagios::Plugin; | |
use Data::Dumper; | |
use File::Basename; | |
my $pgrep = '/usr/bin/pgrep '; | |
my $netstat = '/usr/bin/sudo /bin/netstat -tnup --listening'; | |
my $VERSION = '0.1'; | |
my $np = Nagios::Plugin->new( | |
usage => "Usage: %s [ -v|--verbose ] [ -p|--ports ] [ -c|--counts ]", | |
version => $VERSION, | |
plugin => "Ports and Procs", | |
shortname => "Ports and Procs", | |
timeout => 15, | |
); | |
$np->add_arg( | |
spec => 'ports|p=s', | |
help => "--ports=STRING\n Port numbers to check. Format: ". | |
"comma-separated, no spaces e.g. sshd=22,master=25,apache2=80", | |
required => 1 | |
); | |
$np->add_arg( | |
spec => 'counts|c=s', | |
help => "--count=STRING\n Procs that you want a process count for. Format:". | |
" comma-separated, no spaces e.g. apache2,amvisd", | |
required => 0 | |
); | |
$np->getopts; | |
alarm $np->opts->timeout; | |
#load the ports options into a hash | |
my %ports; | |
if (defined($np->opts->ports) && length(($np->opts->ports))) { | |
for $port (split(/,/, $np->opts->ports)) { | |
my ($proc, $port) = split(/=/, $port); | |
#print "Arg $proc = $port\n" if ($np->opts->verbose); | |
$ports{$port} = $proc; | |
} | |
} | |
#print '%ports = '. Dumper(%ports) ."\n" if ($np->opts->verbose); | |
#if we were given any ports to look for lets find them | |
if (scalar(keys %ports)) { | |
open (NETSTAT, $netstat .'|') or | |
$np->nagios_exit(UNKNOWN, 'plugin does not have sufficient privileges to '. | |
"run, or command ($netstat) is incorrect. $!"); | |
while (<NETSTAT>) { | |
#tcp 0 0 127.0.0.1:10024 0.0.0.0:* LISTEN 28699/amavisd (mast | |
#udp 0 0 127.0.0.1:45248 0.0.0.0:* 7864/skype | |
#tcp6 0 0 :::993 :::* LISTEN 2297/couriertcpd | |
next unless /^[^\s]+\s+\d+\s+\d+\s+(?:(?:\d{1,3}\.){3}\d{1,3}|::):(\d+)\s+(?:0\.0\.0\.0|::):\*\s+(?:LISTEN)?\s+\d+\/([^\s]+)/; | |
my $port = $1; | |
my $proc = $2; | |
#check if the port is listening, but for a different proc | |
if (defined($ports{$port}) && $proc !~ /$ports{$port}/) { | |
$np->add_message( CRITICAL, "$port is listening for $proc not ". | |
$ports{$port} ."."); | |
} elsif (defined($ports{$port}) && $ports{$port} eq $proc) { | |
$np->add_message( OK, "$port is listening for $proc."); | |
print "$proc is listening on $port\n" | |
if ($np->opts->verbose); | |
} | |
delete $ports{$port} if defined($ports{$port}); | |
} | |
close NETSTAT; | |
} | |
#check if any of the requested ports were not found | |
for my $port (keys %ports) { | |
$np->add_message( CRITICAL, "$port is not listening for ". $ports{$port} .'.'); | |
} | |
#process performance data if checks are given | |
my %procs; | |
if (defined($np->opts->counts) && length(($np->opts->counts))) { | |
for $proc (split(/,/, $np->opts->counts)) { | |
$procs{$proc} = 0; | |
$run_pgrep = $pgrep . "$proc|"; | |
open (PGREP, $run_pgrep) or | |
$np->nagios_exit(UNKNOWN, 'plugin does not have sufficient privileges to '. | |
"run, or command ($run_pgrep) is incorrect. $!"); | |
while (<PGREP>) { | |
$procs{$proc}++; | |
} | |
close PGREP; | |
$np->add_perfdata( | |
label => "$proc count", | |
value => $procs{$proc}, | |
#uom => 'Count', | |
); | |
} | |
} | |
$np->nagios_exit($np->check_messages()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment