#!/usr/bin/perl -w # # This Nagios check plugin will always return OK. # # It also passes along a string of health data about the system: # # iostat, vmstat, and netstat data # # This script needs a lot of time to run. Make sure that Nagios does not # time-out while waiting for the results. Each of the checks will take a # minute, i.e. set a timeout of 210 seconds (3 1/2 minutes total). # # Michael Hocke, NYU, 5/31/2006 use strict; use Getopt::Long; # tools my $vmstat_command = "/usr/bin/vmstat"; my $netstat_command = "/usr/bin/netstat -s"; # result my $result_string = ""; my $exit_code = 0; # parse commandline my $vmstat_delay = 100; my $vmstat_runs = 2; my $run_vmstat = 0; my $run_netstat = 0; GetOptions ("vmstat-delay=i" => \$vmstat_delay, "vmstat-runs=i" => \$vmstat_runs, "vmstat" => \$run_vmstat, "netstat" => \$run_netstat); if (not $run_vmstat and not $run_netstat) { $result_string = "need either --vmstat or --netstat or both"; $exit_code = 1; } ### run vmstat if ($run_vmstat) { my $vmstat_output; if (open VMSTAT, "$vmstat_command $vmstat_delay $vmstat_runs 2>&1 |") { # we are only concerned about the last line while () { chomp; $vmstat_output = $_; } close VMSTAT; # reformat output my ($cpu_r, $cpu_b, $cpu_w, $mem_swap, $mem_free, $mem_re, $mem_mf, $mem_pi, $mem_po, $fr, $de, $sr, $disk1, $disk2, $disk3, $disk4, $cpu_int, $cpu_syc, $cpu_csw, $cpu_usr, $cpu_sys, $cpu_idl) = split " ", $vmstat_output; $result_string .= "cpu_r=$cpu_r cpu_b=$cpu_b cpu_w=$cpu_w mem_swap=$mem_swap mem_free=$mem_free mem_re=$mem_re mem_mf=$mem_mf mem_pi=$mem_pi mem_po=$mem_po sr=$sr cpu_int=$cpu_int cpu_syc=$cpu_syc cpu_csw=$cpu_csw cpu_usr=$cpu_usr cpu_sys=$cpu_sys cpu_idl=$cpu_idl"; } else { $result_string .= "unable to execute $vmstat_command;"; $exit_code = 1; } } ### run netstat sub parse_netstat { my ($output, $keyword) = @_; if ($output =~ /$keyword\s+=\s*(\d+)/) { return $1; } else { return "-"; } } if ($run_netstat) { if (open NETSTAT, "$netstat_command 2>&1 |") { # parse out the exciting pieces local $/; undef $/; my $netstat_out = ; close NETSTAT; $result_string .= " " if ($result_string); $result_string .= "udp_in=" . parse_netstat ($netstat_out, "udpInDatagrams"); $result_string .= " udp_out=" . parse_netstat ($netstat_out, "udpOutDatagrams"); $result_string .= " udp_err=" . parse_netstat ($netstat_out, "udpInErrors"); $result_string .= " tcp_act=" . parse_netstat ($netstat_out, "tcpActiveOpens"); $result_string .= " tcp_pass=" . parse_netstat ($netstat_out, "tcpPassiveOpens"); $result_string .= " tcp_fail=" . parse_netstat ($netstat_out, "tcpAttemptFails"); $result_string .= " tcp_rst=" . parse_netstat ($netstat_out, "tcpEstabResets"); $result_string .= " tcp_est=" . parse_netstat ($netstat_out, "tcpCurrEstab"); $result_string .= " tcp_out=" . parse_netstat ($netstat_out, "tcpOutDataBytes"); $result_string .= " tcp_ino=" . parse_netstat ($netstat_out, "tcpInInorderBytes"); $result_string .= " tcp_inu=" . parse_netstat ($netstat_out, "tcpInUnorderBytes"); $result_string .= " tcp_retr=" . parse_netstat ($netstat_out, "tcpRetransBytes"); } else { $result_string .= "unable to execute $netstat_command;"; $exit_code = 1; } } # done and over if ($exit_code == 0) { print "OK - $result_string\n"; } else { print "WARNING - $result_string\n"; } exit $exit_code;