#! /usr/bin/perl # # check_wireless_ssid [warn] [critical] [port] # # Nagios host script to check if a wireless SSID exists. Relies on iwlist # to scan for SSIDs. iwlist must be accessible to the nagios user. # # Changes and Modifications # ========================= # 15=Jul-2011 - David Shivak # require 5.004; use POSIX; use strict; use warnings; use FindBin qw($Bin); use lib "$Bin/../perl/lib", "/usr/local/nagios/libexec"; use utils qw($TIMEOUT %ERRORS &print_revision &support &usage); use Nagios::Plugin; use Nagios::Plugin::Getopt; my $checkname = "check_wireless_ssid"; my ($np) = Nagios::Plugin->new( shortname => $checkname, timeout => $TIMEOUT, ); my ($ng) = Nagios::Plugin::Getopt->new( usage => "Usage: %s -d -s [--help] [--version]\n", version => '0.1', blurb => 'Perl Check Wireless SSID plugin for Nagios', extra => 'Requires that nagios user has access to iwlist', license => 'Copyright (c) 2000 Michael Anthon/Karl DeBisschop, (c) 2010 David Shivak', ); $ng->arg( spec => 'device|d=s', help => "Wireless device name without /dev prefix", required => 1, ); $ng->arg( spec => 'ssid|s=s', help => "SSID to look for", required => 1, ); $ng->getopts(); my ($device) = $ng->device; my ($ssid) = $ng->ssid; my ( $verbose, $warn, $crit ); $ENV{'PATH'} = ''; $ENV{'BASH_ENV'} = ''; $ENV{'ENV'} = ''; my $iwlist = "/usr/local/bin/iwlist"; if ( !$iwlist || !-x $iwlist ) { $np->nagios_die("$iwlist is not findable/executable by the Nagios user"); } # sanitize options that will be passed to shell $device = quotemeta($device); $ssid = quotemeta($ssid); # Does this device exist on the system? # Iwconfig takes forever to return a nonexistent device, use proc instead my $deviceok = 0; open DEVLIST, "; close DEVLIST; my $devname = ""; my $devline = ""; foreach my $devline (@devices) { ($devname) = $devline =~ /(\w+):/; $devname = "" unless defined($devname); if ($devname eq $device) { $deviceok = 1; } } unless ($deviceok) { warn "Device $device does not exist"; print "SSID CRITICAL: No such device $device on system"; exit; } open (IWLIST, "-|") or exec ($iwlist, $device, "scan"); my @output = ; close IWLIST; my (@ssid_list) = grep {/SSID:/} @output; my (@ssid) = grep {/$ssid/i} @ssid_list; my $output; my $state = "CRITICAL"; if (scalar(@ssid) > 0) { $state = "OK"; } if ($state eq "OK") { $output = "SSID $state: Device $device found ssid:'$ssid' "; } else { $output = "SSID $state: Device $device could not find ssid:'$ssid' "; } print $output; # Just in case of problems, let's not hang Nagios $SIG{'ALRM'} = sub { $np->nagios_exit( $ERRORS{'CRITICAL'}, "Wireless scan not completed" ); }; alarm($TIMEOUT); #Turn off alarm alarm(0); #Split $res into an array of lines # did we get a number back or a string? my ($EXIT) = ( $state =~ /^\d$/ ) ? $state : $ERRORS{$state}; my $answer = 0; # CHECK THIS FOR CORRECTNESS $np->nagios_exit( $EXIT, $answer );