Skip to content

Instantly share code, notes, and snippets.

@joshua-chavanne
Created March 2, 2017 00:51
Show Gist options
  • Save joshua-chavanne/1ade63bb72128c64a7d1e435487d972d to your computer and use it in GitHub Desktop.
Save joshua-chavanne/1ade63bb72128c64a7d1e435487d972d to your computer and use it in GitHub Desktop.
Run and Send NSCA checks
#!/usr/bin/perl
use strict;
my $HOST="subdomain.hostname.com";
my $NSCA_HOST="NSCA HOST IP ADDRESS";
my $NSCA_CONFIG="/etc/send_nsca.cfg";
my $NSCA="/usr/sbin/send_nsca";
my $PLUGINS_PATH="/usr/lib/nagios/plugins/";
my $DISK_ARGS="-w 20% -c 10% -p ";
my $CHECK_ROOT="/usr/lib/nagios/plugins/check_disk ". $DISK_ARGS ."/";
my $CHECK_PROCS="/usr/lib/nagios/plugins/check_procs -w 250 -c 400 -s RSZDT";
my $CHECK_LOAD="/usr/lib/nagios/plugins/check_load -w 5.0,5.0,5.0 -c 10.0,6.0,6.0";
# Register PORTS to check if running NSCA or MySQL or STMP/Postfix register those as args
# For process that doesn't listen on a port '--counts processName' for counts
my $CHECK_PORTS_PROCS=$PLUGINS_PATH ."check_system_pp.pl --ports \"ssh=22,master=25,mysql=3306,apache2=80\"";
my $CHECK_MYSQL=$PLUGINS_PATH ."check_mysql -u debian-sys-maint -p PASSWORD";
# ***START STANZA***
# OPEN NSCA EXECUTABLE/File handle -> /usr/sbin/send_nsca
open(NSCA, "|$NSCA -H $NSCA_HOST -c $NSCA_CONFIG") or
die "could not start nsca: $NSCA -H $NSCA_HOST -c $NSCA_CONFIG";
# RUN CHECK COMMAND (in this case CHECK_ROOT
my $RESULT=`$CHECK_ROOT`;
# Capture System response code (Give me the exit code of process)
my $rc = ($?>>8);
# Send to NSCA server ($NSCA_HOST)
# Make sure the 'Service Check Name' (In between Tabs) maps to Nagios Service Check on Host
print NSCA "$HOST\tRoot Partition\t$rc\t$RESULT";
# Close file handle
close NSCA;
# ***END STANZA***
open(NSCA, "|$NSCA -H $NSCA_HOST -c $NSCA_CONFIG") or
die "could not start nsca: $NSCA -H $NSCA_HOST -c $NSCA_CONFIG";
my $RESULT=`$CHECK_PROCS`;
my $rc = ($?>>8);
print NSCA "$HOST\tProcess count\t$rc\t$RESULT";
close NSCA;
open(NSCA, "|$NSCA -H $NSCA_HOST -c $NSCA_CONFIG") or
die "could not start nsca: $NSCA -H $NSCA_HOST -c $NSCA_CONFIG";
my $RESULT=`$CHECK_LOAD`;
my $rc = ($?>>8);
print NSCA "$HOST\tCPU Load\t$rc\t$RESULT";
close NSCA;
open(NSCA, "|$NSCA -H $NSCA_HOST -c $NSCA_CONFIG") or
die "could not start nsca: $NSCA -H $NSCA_HOST -c $NSCA_CONFIG";
my $RESULT=`$CHECK_PORTS_PROCS`;
my $rc = ($?>>8);
print NSCA "$HOST\tPorts and Procs\t$rc\t$RESULT";
close NSCA;
open(NSCA, "|$NSCA -H $NSCA_HOST -c $NSCA_CONFIG") or
die "could not start nsca: $NSCA -H $NSCA_HOST -c $NSCA_CONFIG";
my $RESULT=`$CHECK_MYSQL`;
my $rc = ($?>>8);
print NSCA "$HOST\tMySQL\t$rc\t$RESULT";
close NSCA;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment