diff options
Diffstat (limited to 'plugins/t')
40 files changed, 3271 insertions, 0 deletions
diff --git a/plugins/t/check_apt.t b/plugins/t/check_apt.t new file mode 100644 index 0000000..430eb53 --- /dev/null +++ b/plugins/t/check_apt.t @@ -0,0 +1,106 @@ +#!/usr/bin/perl -w -I .. +# +# Test check_apt using input files. +# Contributed by Alex Bradley, October 2012 +# + +use strict; +use Test::More; +use NPTest; + +sub make_result_regexp { + my ($warning, $critical) = @_; + my $status; + if ($warning == 0 && $critical == 0) { + $status = "OK"; + } elsif ($critical == 0) { + $status = "WARNING"; + } else { + $status = "CRITICAL"; + } + return sprintf('/^APT %s: %d packages available for upgrade \(%d critical updates\)\. |available_upgrades=%d;;;0 critical_updates=%d;;;0$/', + $status, $warning, $critical, $warning, $critical); +} + +if (-x "./check_apt") { + plan tests => 36; +} else { + plan skip_all => "No check_apt compiled"; +} + +my $result; + +my $testfile_command = "./check_apt %s --input-file=t/check_apt_input/%s"; + +$result = NPTest->testCmd( sprintf($testfile_command, "", "debian1") ); +is( $result->return_code, 0, "No upgrades" ); +like( $result->output, make_result_regexp(0, 0), "Output correct" ); + +$result = NPTest->testCmd( sprintf($testfile_command, "", "debian2") ); +is( $result->return_code, 1, "Debian apt output, warning" ); +like( $result->output, make_result_regexp(13, 0), "Output correct" ); + +$result = NPTest->testCmd( sprintf($testfile_command, "-o", "debian2") ); +is( $result->return_code, 0, "Debian apt output, no critical" ); +like( $result->output, make_result_regexp(13, 0), "Output correct" ); + +$result = NPTest->testCmd( sprintf($testfile_command, "", "debian3") ); +is( $result->return_code, 2, "Debian apt output, some critical" ); +like( $result->output, make_result_regexp(19, 4), "Output correct" ); + +$result = NPTest->testCmd( sprintf($testfile_command, "-o", "debian3") ); +is( $result->return_code, 2, "Debian apt output, some critical" ); +like( $result->output, make_result_regexp(19, 4), "Output correct" ); + +$result = NPTest->testCmd( sprintf($testfile_command, "-c '^[^\\(]*\\(.* (Debian-Security:|Ubuntu:[^/]*/[^-]*-security)'", "debian3") ); +is( $result->return_code, 2, "Debian apt output - should have same result when default security regexp specified via -c" ); +like( $result->output, make_result_regexp(19, 4), "Output correct" ); + +$result = NPTest->testCmd( sprintf($testfile_command, "-i libc6", "debian3") ); +is( $result->return_code, 1, "Debian apt output, filter for libc6" ); +like( $result->output, make_result_regexp(3, 0), "Output correct" ); + +$result = NPTest->testCmd( sprintf($testfile_command, "-i libc6", "debian3") ); +is( $result->return_code, 1, "Debian apt output, filter for libc6, not critical" ); +like( $result->output, make_result_regexp(3, 0), "Output correct" ); + +$result = NPTest->testCmd( sprintf($testfile_command, "-i libc6 -i xen", "debian3") ); +is( $result->return_code, 2, "Debian apt output, filter for libc6 and xen" ); +like( $result->output, make_result_regexp(9, 4), "Output correct" ); + +$result = NPTest->testCmd( sprintf($testfile_command, "-i libc6 -i xen -i linux", "debian3") ); +is( $result->return_code, 2, "Debian apt output, filter for libc6, xen, linux" ); +like( $result->output, make_result_regexp(12, 4), "Output correct" ); + +$result = NPTest->testCmd( sprintf($testfile_command, "-e libc6", "debian3") ); +is( $result->return_code, 2, "Debian apt output, filter out libc6" ); +like( $result->output, make_result_regexp(16, 4), "Output correct" ); + +$result = NPTest->testCmd( sprintf($testfile_command, "-e libc6 -o", "debian3") ); +is( $result->return_code, 2, "Debian apt output, filter out libc6, critical" ); +like( $result->output, make_result_regexp(16, 4), "Output correct" ); + +$result = NPTest->testCmd( sprintf($testfile_command, "-e libc6 -e xen", "debian3") ); +is( $result->return_code, 1, "Debian apt output, filter out libc6 and xen" ); +like( $result->output, make_result_regexp(10, 0), "Output correct" ); + +$result = NPTest->testCmd( sprintf($testfile_command, "-e libc6 -e xen -e linux", "debian3") ); +is( $result->return_code, 1, "Debian apt output, filter out libc6, xen, linux" ); +like( $result->output, make_result_regexp(7, 0), "Output correct" ); + +$result = NPTest->testCmd( sprintf($testfile_command, "-c Debian-Security -c linux", "debian3") ); +is( $result->return_code, 2, "Debian apt output, critical on Debian-Security or linux" ); +like( $result->output, make_result_regexp(19, 9), "Output correct" ); + +$result = NPTest->testCmd( sprintf($testfile_command, "-i lib -i linux -e gc1c -c linux-image", "debian3") ); +is( $result->return_code, 2, "Debian apt output, include lib and linux, exclude gc1c, critical on linux-image" ); +like( $result->output, make_result_regexp(10, 2), "Output correct" ); + +$result = NPTest->testCmd( sprintf($testfile_command, "", "ubuntu1") ); +is( $result->return_code, 1, "Ubuntu apt output, warning" ); +like( $result->output, make_result_regexp(5, 0), "Output correct" ); + +$result = NPTest->testCmd( sprintf($testfile_command, "", "ubuntu2") ); +is( $result->return_code, 2, "Ubuntu apt output, some critical" ); +like( $result->output, make_result_regexp(25, 14), "Output correct" ); + diff --git a/plugins/t/check_apt_input/debian1 b/plugins/t/check_apt_input/debian1 new file mode 100644 index 0000000..317e7ea --- /dev/null +++ b/plugins/t/check_apt_input/debian1 @@ -0,0 +1,4 @@ +NOTE: This is only a simulation! + apt-get needs root privileges for real execution. + Keep also in mind that locking is deactivated, + so don't depend on the relevance to the real current situation! diff --git a/plugins/t/check_apt_input/debian2 b/plugins/t/check_apt_input/debian2 new file mode 100644 index 0000000..effd155 --- /dev/null +++ b/plugins/t/check_apt_input/debian2 @@ -0,0 +1,37 @@ +NOTE: This is only a simulation! + apt-get needs root privileges for real execution. + Keep also in mind that locking is deactivated, + so don't depend on the relevance to the real current situation! +Reading package lists... Done +Building dependency tree +Reading state information... Done +The following packages will be upgraded: + base-files debian-archive-keyring dpkg firmware-linux-free libc-bin libc-dev-bin libc6 libc6-dev linux-base + linux-image-2.6.32-5-xen-amd64 linux-libc-dev locales lockfile-progs +13 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. +Inst base-files [6.0squeeze5] (6.0squeeze6 Debian:6.0.6/stable [amd64]) +Conf base-files (6.0squeeze6 Debian:6.0.6/stable [amd64]) +Inst dpkg [1.15.8.12] (1.15.8.13 Debian:6.0.6/stable [amd64]) +Conf dpkg (1.15.8.13 Debian:6.0.6/stable [amd64]) +Inst linux-base [2.6.32-45] (2.6.32-46 Debian:6.0.6/stable [all]) +Inst linux-image-2.6.32-5-xen-amd64 [2.6.32-45] (2.6.32-46 Debian:6.0.6/stable [amd64]) +Inst debian-archive-keyring [2010.08.28] (2010.08.28+squeeze1 Debian:6.0.6/stable [all]) +Conf debian-archive-keyring (2010.08.28+squeeze1 Debian:6.0.6/stable [all]) +Inst libc6-dev [2.11.3-3] (2.11.3-4 Debian:6.0.6/stable [amd64]) [] +Inst libc-dev-bin [2.11.3-3] (2.11.3-4 Debian:6.0.6/stable [amd64]) [] +Inst linux-libc-dev [2.6.32-45] (2.6.32-46 Debian:6.0.6/stable [amd64]) [] +Inst libc-bin [2.11.3-3] (2.11.3-4 Debian:6.0.6/stable [amd64]) [libc6:amd64 ] +Conf libc-bin (2.11.3-4 Debian:6.0.6/stable [amd64]) [libc6:amd64 ] +Inst libc6 [2.11.3-3] (2.11.3-4 Debian:6.0.6/stable [amd64]) +Conf libc6 (2.11.3-4 Debian:6.0.6/stable [amd64]) +Inst locales [2.11.3-3] (2.11.3-4 Debian:6.0.6/stable [all]) +Inst firmware-linux-free [2.6.32-45] (2.6.32-46 Debian:6.0.6/stable [all]) +Inst lockfile-progs [0.1.15] (0.1.15+squeeze1 Debian:6.0.6/stable [amd64]) +Conf linux-base (2.6.32-46 Debian:6.0.6/stable [all]) +Conf linux-image-2.6.32-5-xen-amd64 (2.6.32-46 Debian:6.0.6/stable [amd64]) +Conf libc-dev-bin (2.11.3-4 Debian:6.0.6/stable [amd64]) +Conf linux-libc-dev (2.6.32-46 Debian:6.0.6/stable [amd64]) +Conf libc6-dev (2.11.3-4 Debian:6.0.6/stable [amd64]) +Conf locales (2.11.3-4 Debian:6.0.6/stable [all]) +Conf firmware-linux-free (2.6.32-46 Debian:6.0.6/stable [all]) +Conf lockfile-progs (0.1.15+squeeze1 Debian:6.0.6/stable [amd64]) diff --git a/plugins/t/check_apt_input/debian3 b/plugins/t/check_apt_input/debian3 new file mode 100644 index 0000000..719dce9 --- /dev/null +++ b/plugins/t/check_apt_input/debian3 @@ -0,0 +1,42 @@ +NOTE: This is only a simulation! + apt-get needs root privileges for real execution. + Keep also in mind that locking is deactivated, + so don't depend on the relevance to the real current situation! +Inst base-files [6.0squeeze5] (6.0squeeze6 Debian:6.0.6/stable [amd64]) +Conf base-files (6.0squeeze6 Debian:6.0.6/stable [amd64]) +Inst dpkg [1.15.8.12] (1.15.8.13 Debian:6.0.6/stable [amd64]) +Conf dpkg (1.15.8.13 Debian:6.0.6/stable [amd64]) +Inst linux-base [2.6.32-45] (2.6.32-46 Debian:6.0.6/stable [all]) +Inst linux-image-2.6.32-5-amd64 [2.6.32-45] (2.6.32-46 Debian:6.0.6/stable [amd64]) +Inst xen-hypervisor-4.0-amd64 [4.0.1-5.3] (4.0.1-5.4 Debian:6.0.6/stable, Debian-Security:6.0/stable [amd64]) +Inst xen-linux-system-2.6.32-5-xen-amd64 [2.6.32-45] (2.6.32-46 Debian:6.0.6/stable [amd64]) [] +Inst linux-image-2.6.32-5-xen-amd64 [2.6.32-45] (2.6.32-46 Debian:6.0.6/stable [amd64]) +Inst debian-archive-keyring [2010.08.28] (2010.08.28+squeeze1 Debian:6.0.6/stable [all]) +Conf debian-archive-keyring (2010.08.28+squeeze1 Debian:6.0.6/stable [all]) +Inst libc6-i386 [2.11.3-3] (2.11.3-4 Debian:6.0.6/stable [amd64]) [] +Inst libc-bin [2.11.3-3] (2.11.3-4 Debian:6.0.6/stable [amd64]) [libc6:amd64 ] +Conf libc-bin (2.11.3-4 Debian:6.0.6/stable [amd64]) [libc6:amd64 ] +Inst libc6 [2.11.3-3] (2.11.3-4 Debian:6.0.6/stable [amd64]) +Conf libc6 (2.11.3-4 Debian:6.0.6/stable [amd64]) +Inst libgc1c2 [1:6.8-1.2] (1:6.8-2 Debian:6.0.6/stable [amd64]) +Inst locales [2.11.3-3] (2.11.3-4 Debian:6.0.6/stable [all]) +Inst firmware-linux-free [2.6.32-45] (2.6.32-46 Debian:6.0.6/stable [all]) +Inst libxenstore3.0 [4.0.1-5.3] (4.0.1-5.4 Debian:6.0.6/stable, Debian-Security:6.0/stable [amd64]) +Inst lockfile-progs [0.1.15] (0.1.15+squeeze1 Debian:6.0.6/stable [amd64]) +Inst xen-utils-4.0 [4.0.1-5.3] (4.0.1-5.4 Debian:6.0.6/stable, Debian-Security:6.0/stable [amd64]) +Inst xenstore-utils [4.0.1-5.3] (4.0.1-5.4 Debian:6.0.6/stable, Debian-Security:6.0/stable [amd64]) +Inst libconfig-inifiles-perl [2.52-1] (2.52-1+squeeze1 Debian:6.0.6/stable [all]) +Conf linux-base (2.6.32-46 Debian:6.0.6/stable [all]) +Conf linux-image-2.6.32-5-amd64 (2.6.32-46 Debian:6.0.6/stable [amd64]) +Conf xen-hypervisor-4.0-amd64 (4.0.1-5.4 Debian:6.0.6/stable, Debian-Security:6.0/stable [amd64]) +Conf linux-image-2.6.32-5-xen-amd64 (2.6.32-46 Debian:6.0.6/stable [amd64]) +Conf xen-linux-system-2.6.32-5-xen-amd64 (2.6.32-46 Debian:6.0.6/stable [amd64]) +Conf libc6-i386 (2.11.3-4 Debian:6.0.6/stable [amd64]) +Conf libgc1c2 (1:6.8-2 Debian:6.0.6/stable [amd64]) +Conf locales (2.11.3-4 Debian:6.0.6/stable [all]) +Conf firmware-linux-free (2.6.32-46 Debian:6.0.6/stable [all]) +Conf libxenstore3.0 (4.0.1-5.4 Debian:6.0.6/stable, Debian-Security:6.0/stable [amd64]) +Conf lockfile-progs (0.1.15+squeeze1 Debian:6.0.6/stable [amd64]) +Conf xen-utils-4.0 (4.0.1-5.4 Debian:6.0.6/stable, Debian-Security:6.0/stable [amd64]) +Conf xenstore-utils (4.0.1-5.4 Debian:6.0.6/stable, Debian-Security:6.0/stable [amd64]) +Conf libconfig-inifiles-perl (2.52-1+squeeze1 Debian:6.0.6/stable [all]) diff --git a/plugins/t/check_apt_input/ubuntu1 b/plugins/t/check_apt_input/ubuntu1 new file mode 100644 index 0000000..2f61c30 --- /dev/null +++ b/plugins/t/check_apt_input/ubuntu1 @@ -0,0 +1,14 @@ +NOTE: This is only a simulation! + apt-get needs root privileges for real execution. + Also keep in mind that locking is deactivated, + so don't depend on the relevance to the real current situation! +Inst grub-pc [1.99-21ubuntu3.1] (1.99-21ubuntu3.4 Ubuntu:12.04/precise-updates [amd64]) [] +Inst grub-pc-bin [1.99-21ubuntu3.1] (1.99-21ubuntu3.4 Ubuntu:12.04/precise-updates [amd64]) [] +Inst grub2-common [1.99-21ubuntu3.1] (1.99-21ubuntu3.4 Ubuntu:12.04/precise-updates [amd64]) [] +Inst grub-efi-amd64-bin [1.99-21ubuntu3.1] (1.99-21ubuntu3.4 Ubuntu:12.04/precise-updates [amd64]) [] +Inst grub-common [1.99-21ubuntu3.1] (1.99-21ubuntu3.4 Ubuntu:12.04/precise-updates [amd64]) +Conf grub-common (1.99-21ubuntu3.4 Ubuntu:12.04/precise-updates [amd64]) +Conf grub2-common (1.99-21ubuntu3.4 Ubuntu:12.04/precise-updates [amd64]) +Conf grub-pc-bin (1.99-21ubuntu3.4 Ubuntu:12.04/precise-updates [amd64]) +Conf grub-pc (1.99-21ubuntu3.4 Ubuntu:12.04/precise-updates [amd64]) +Conf grub-efi-amd64-bin (1.99-21ubuntu3.4 Ubuntu:12.04/precise-updates [amd64]) diff --git a/plugins/t/check_apt_input/ubuntu2 b/plugins/t/check_apt_input/ubuntu2 new file mode 100644 index 0000000..29a14a0 --- /dev/null +++ b/plugins/t/check_apt_input/ubuntu2 @@ -0,0 +1,54 @@ +NOTE: This is only a simulation! + apt-get needs root privileges for real execution. + Also keep in mind that locking is deactivated, + so don't depend on the relevance to the real current situation! +Inst libc6-dev [2.15-0ubuntu10] (2.15-0ubuntu10.2 Ubuntu:12.04/precise-security [amd64]) [] +Inst libc-dev-bin [2.15-0ubuntu10] (2.15-0ubuntu10.2 Ubuntu:12.04/precise-security [amd64]) [] +Inst linux-libc-dev [3.2.0-29.46] (3.2.0-31.50 Ubuntu:12.04/precise-security [amd64]) [] +Inst tzdata [2012e-0ubuntu0.12.04] (2012e-0ubuntu0.12.04.1 Ubuntu:12.04/precise-security [all]) [] +Conf tzdata (2012e-0ubuntu0.12.04.1 Ubuntu:12.04/precise-security [all]) [] +Inst libc-bin [2.15-0ubuntu10] (2.15-0ubuntu10.2 Ubuntu:12.04/precise-security [amd64]) [libc6:amd64 ] +Conf libc-bin (2.15-0ubuntu10.2 Ubuntu:12.04/precise-security [amd64]) [libc6:amd64 ] +Inst libc6 [2.15-0ubuntu10] (2.15-0ubuntu10.2 Ubuntu:12.04/precise-security [amd64]) +Conf libc6 (2.15-0ubuntu10.2 Ubuntu:12.04/precise-security [amd64]) +Inst libapt-pkg4.12 [0.8.16~exp12ubuntu10.2] (0.8.16~exp12ubuntu10.3 Ubuntu:12.04/precise-updates [amd64]) +Conf libapt-pkg4.12 (0.8.16~exp12ubuntu10.3 Ubuntu:12.04/precise-updates [amd64]) +Inst ubuntu-keyring [2011.11.21] (2011.11.21.1 Ubuntu:12.04/precise-updates [all]) +Conf ubuntu-keyring (2011.11.21.1 Ubuntu:12.04/precise-updates [all]) +Inst gpgv [1.4.11-3ubuntu2] (1.4.11-3ubuntu2.1 Ubuntu:12.04/precise-security [amd64]) +Conf gpgv (1.4.11-3ubuntu2.1 Ubuntu:12.04/precise-security [amd64]) +Inst gnupg [1.4.11-3ubuntu2] (1.4.11-3ubuntu2.1 Ubuntu:12.04/precise-security [amd64]) +Conf gnupg (1.4.11-3ubuntu2.1 Ubuntu:12.04/precise-security [amd64]) +Inst apt [0.8.16~exp12ubuntu10.2] (0.8.16~exp12ubuntu10.3 Ubuntu:12.04/precise-updates [amd64]) +Conf apt (0.8.16~exp12ubuntu10.3 Ubuntu:12.04/precise-updates [amd64]) +Inst libssl1.0.0 [1.0.1-4ubuntu5.3] (1.0.1-4ubuntu5.5 Ubuntu:12.04/precise-updates [amd64]) +Conf libssl1.0.0 (1.0.1-4ubuntu5.5 Ubuntu:12.04/precise-updates [amd64]) +Inst libapt-inst1.4 [0.8.16~exp12ubuntu10.2] (0.8.16~exp12ubuntu10.3 Ubuntu:12.04/precise-updates [amd64]) +Inst resolvconf [1.63ubuntu15] (1.63ubuntu16 Ubuntu:12.04/precise-updates [all]) +Inst libdbus-1-3 [1.4.18-1ubuntu1] (1.4.18-1ubuntu1.1 Ubuntu:12.04/precise-security [amd64]) +Inst libxml2 [2.7.8.dfsg-5.1ubuntu4.1] (2.7.8.dfsg-5.1ubuntu4.2 Ubuntu:12.04/precise-security [amd64]) +Inst multiarch-support [2.15-0ubuntu10] (2.15-0ubuntu10.2 Ubuntu:12.04/precise-security [amd64]) +Conf multiarch-support (2.15-0ubuntu10.2 Ubuntu:12.04/precise-security [amd64]) +Inst apt-utils [0.8.16~exp12ubuntu10.2] (0.8.16~exp12ubuntu10.3 Ubuntu:12.04/precise-updates [amd64]) +Inst isc-dhcp-client [4.1.ESV-R4-0ubuntu5.2] (4.1.ESV-R4-0ubuntu5.5 Ubuntu:12.04/precise-security [amd64]) [] +Inst isc-dhcp-common [4.1.ESV-R4-0ubuntu5.2] (4.1.ESV-R4-0ubuntu5.5 Ubuntu:12.04/precise-security [amd64]) +Inst dbus [1.4.18-1ubuntu1] (1.4.18-1ubuntu1.1 Ubuntu:12.04/precise-security [amd64]) +Inst linux-firmware [1.79] (1.79.1 Ubuntu:12.04/precise-updates [all]) +Inst xserver-common [2:1.11.4-0ubuntu10.7] (2:1.11.4-0ubuntu10.8 Ubuntu:12.04/precise-updates [all]) +Inst xserver-xorg-core [2:1.11.4-0ubuntu10.7] (2:1.11.4-0ubuntu10.8 Ubuntu:12.04/precise-updates [amd64]) +Inst xserver-xorg-input-synaptics [1.6.2-1ubuntu1~precise1] (1.6.2-1ubuntu1~precise2 Ubuntu:12.04/precise-updates [amd64]) +Conf libc-dev-bin (2.15-0ubuntu10.2 Ubuntu:12.04/precise-security [amd64]) +Conf linux-libc-dev (3.2.0-31.50 Ubuntu:12.04/precise-security [amd64]) +Conf libc6-dev (2.15-0ubuntu10.2 Ubuntu:12.04/precise-security [amd64]) +Conf libapt-inst1.4 (0.8.16~exp12ubuntu10.3 Ubuntu:12.04/precise-updates [amd64]) +Conf resolvconf (1.63ubuntu16 Ubuntu:12.04/precise-updates [all]) +Conf libdbus-1-3 (1.4.18-1ubuntu1.1 Ubuntu:12.04/precise-security [amd64]) +Conf libxml2 (2.7.8.dfsg-5.1ubuntu4.2 Ubuntu:12.04/precise-security [amd64]) +Conf apt-utils (0.8.16~exp12ubuntu10.3 Ubuntu:12.04/precise-updates [amd64]) +Conf isc-dhcp-common (4.1.ESV-R4-0ubuntu5.5 Ubuntu:12.04/precise-security [amd64]) +Conf isc-dhcp-client (4.1.ESV-R4-0ubuntu5.5 Ubuntu:12.04/precise-security [amd64]) +Conf dbus (1.4.18-1ubuntu1.1 Ubuntu:12.04/precise-security [amd64]) +Conf linux-firmware (1.79.1 Ubuntu:12.04/precise-updates [all]) +Conf xserver-common (2:1.11.4-0ubuntu10.8 Ubuntu:12.04/precise-updates [all]) +Conf xserver-xorg-core (2:1.11.4-0ubuntu10.8 Ubuntu:12.04/precise-updates [amd64]) +Conf xserver-xorg-input-synaptics (1.6.2-1ubuntu1~precise2 Ubuntu:12.04/precise-updates [amd64]) diff --git a/plugins/t/check_by_ssh.t b/plugins/t/check_by_ssh.t new file mode 100644 index 0000000..b6479f1 --- /dev/null +++ b/plugins/t/check_by_ssh.t @@ -0,0 +1,153 @@ +#! /usr/bin/perl -w -I .. +# +# check_by_ssh tests +# +# + +use strict; +use Test::More; +use NPTest; + +# Required parameters +my $ssh_service = getTestParameter("NP_SSH_HOST", "A host providing SSH service", "localhost"); +my $ssh_key = getTestParameter("NP_SSH_IDENTITY", "A key allowing access to NP_SSH_HOST", "~/.ssh/id_dsa"); +my $ssh_conf = getTestParameter( "NP_SSH_CONFIGFILE", "A config file with ssh settings", "~/.ssh/config"); + + +plan skip_all => "SSH_HOST and SSH_IDENTITY must be defined" unless ($ssh_service && $ssh_key); + +plan tests => 42; + +# Some random check strings/response +my @response = ('OK: Everything is fine', + 'WARNING: Hey, pick me, pick me', + 'CRITICAL: Shit happens', + 'UNKNOWN: What can I do for ya', + 'WOOPS: What did I smoke', +); +my @response_re; +my @check; +for (@response) { + push(@check, "echo $_"); + my $re_str = $_; + $re_str =~ s{(.)} { "\Q$1" }ge; + push(@response_re, $re_str); +} + +my $result; + +# expand paths +$ssh_key = glob($ssh_key) if $ssh_key; +$ssh_conf = glob($ssh_conf) if $ssh_conf; + +## Single active checks + +for (my $i=0; $i<4; $i++) { + $result = NPTest->testCmd( + "./check_by_ssh -i $ssh_key -H $ssh_service -C '$check[$i]; exit $i'" + ); + cmp_ok($result->return_code, '==', $i, "Exit with return code $i"); + is($result->output, $response[$i], "Status text is correct for check $i"); +} + +$result = NPTest->testCmd( + "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 0'" + ); +cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); +is($result->output, 'OK - check_by_ssh: Remote command \'exit 0\' returned status 0', "Status text if command returned none (OK)"); + +$result = NPTest->testCmd( + "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 1'" + ); +cmp_ok($result->return_code, '==', 1, "Exit with return code 1 (WARNING)"); +is($result->output, 'WARNING - check_by_ssh: Remote command \'exit 1\' returned status 1', "Status text if command returned none (WARNING)"); + +$result = NPTest->testCmd( + "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 2'" + ); +cmp_ok($result->return_code, '==', 2, "Exit with return code 2 (CRITICAL)"); +is($result->output, 'CRITICAL - check_by_ssh: Remote command \'exit 2\' returned status 2', "Status text if command returned none (CRITICAL)"); + +$result = NPTest->testCmd( + "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 3'" + ); +cmp_ok($result->return_code, '==', 3, "Exit with return code 3 (UNKNOWN)"); +is($result->output, 'UNKNOWN - check_by_ssh: Remote command \'exit 3\' returned status 3', "Status text if command returned none (UNKNOWN)"); + +$result = NPTest->testCmd( + "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 7'" + ); +cmp_ok($result->return_code, '==', 7, "Exit with return code 7 (out of bounds)"); +is($result->output, 'UNKNOWN - check_by_ssh: Remote command \'exit 7\' returned status 7', "Status text if command returned none (out of bounds)"); + +$result = NPTest->testCmd( + "./check_by_ssh -i $ssh_key -H $ssh_service -C '$check[4]; exit 8'" + ); +cmp_ok($result->return_code, '==', 8, "Exit with return code 8 (out of bounds)"); +is($result->output, $response[4], "Return proper status text even with unknown status codes"); + +$result = NPTest->testCmd( + "./check_by_ssh -i $ssh_key -H $ssh_service -F $ssh_conf -C 'exit 0'" + ); +cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); +is($result->output, 'OK - check_by_ssh: Remote command \'exit 0\' returned status 0', "Status text if command returned none (OK)"); + +# Multiple active checks +$result = NPTest->testCmd( + "./check_by_ssh -i $ssh_key -H $ssh_service -C '$check[1]; sh -c exit\\ 1' -C '$check[0]; sh -c exit\\ 0' -C '$check[3]; sh -c exit\\ 3' -C '$check[2]; sh -c exit\\ 2'" + ); +cmp_ok($result->return_code, '==', 0, "Multiple checks always return OK"); +my @lines = split(/\n/, $result->output); +cmp_ok(scalar(@lines), '==', 8, "Correct number of output lines for multiple checks"); +my %linemap = ( + '0' => '1', + '2' => '0', + '4' => '3', + '6' => '2', +); +foreach my $line (0, 2, 4, 6) { + my $code = $linemap{$line}; + my $statline = $line+1; + is($lines[$line], "$response[$code]", "multiple checks status text is correct for line $line"); + is($lines[$statline], "STATUS CODE: $code", "multiple check status code is correct for line $line"); +} + +# Passive checks +unlink("/tmp/check_by_ssh.$$"); +$result = NPTest->testCmd( + "./check_by_ssh -i $ssh_key -H $ssh_service -n flint -s serv -C '$check[2]; sh -c exit\\ 2' -O /tmp/check_by_ssh.$$" + ); +cmp_ok($result->return_code, '==', 0, "Exit always ok on passive checks"); +open(PASV, "/tmp/check_by_ssh.$$") or die("Unable to open '/tmp/check_by_ssh.$$': $!"); +my @pasv = <PASV>; +close(PASV) or die("Unable to close '/tmp/check_by_ssh.$$': $!"); +cmp_ok(scalar(@pasv), '==', 1, 'One passive result for one check performed'); +for (0) { + if ($pasv[$_]) { + like($pasv[$_], '/^\[\d+\] PROCESS_SERVICE_CHECK_RESULT;flint;serv;2;' . $response_re[2] . '$/', 'proper result for passive check'); + } else { + fail('proper result for passive check'); + } +} +unlink("/tmp/check_by_ssh.$$") or die("Unable to unlink '/tmp/check_by_ssh.$$': $!"); +undef @pasv; + +$result = NPTest->testCmd( + "./check_by_ssh -i $ssh_key -H $ssh_service -n flint -s c0:c1:c2:c3:c4 -C '$check[0];sh -c exit\\ 0' -C '$check[1];sh -c exit\\ 1' -C '$check[2];sh -c exit\\ 2' -C '$check[3];sh -c exit\\ 3' -C '$check[4];sh -c exit\\ 9' -O /tmp/check_by_ssh.$$" + ); +cmp_ok($result->return_code, '==', 0, "Exit always ok on passive checks"); +open(PASV, "/tmp/check_by_ssh.$$") or die("Unable to open '/tmp/check_by_ssh.$$': $!"); +@pasv = <PASV>; +close(PASV) or die("Unable to close '/tmp/check_by_ssh.$$': $!"); +cmp_ok(scalar(@pasv), '==', 5, 'Five passive result for five checks performed'); +for (0, 1, 2, 3, 4) { + if ($pasv[$_]) { + my $ret = $_; + $ret = 9 if ($_ == 4); + like($pasv[$_], '/^\[\d+\] PROCESS_SERVICE_CHECK_RESULT;flint;c' . $_ . ';' . $ret . ';' . $response_re[$_] . '$/', "proper result for passive check $_"); + } else { + fail("proper result for passive check $_"); + } +} +unlink("/tmp/check_by_ssh.$$") or die("Unable to unlink '/tmp/check_by_ssh.$$': $!"); + diff --git a/plugins/t/check_cluster.t b/plugins/t/check_cluster.t new file mode 100644 index 0000000..4736f06 --- /dev/null +++ b/plugins/t/check_cluster.t @@ -0,0 +1,82 @@ +#! /usr/bin/perl -w -I .. +# +# check_cluster tests +# +# + +use strict; +use Test::More tests => 15; +use NPTest; + +my $result; + +$result = NPTest->testCmd( + "./check_cluster -s -w 0:0 -c 0:0 -d 0,0,0,0" + ); +cmp_ok( $result->return_code, '==', 0, "Exit OK if non-ok services are inside critical and warning ranges" ); +like( $result->output, qr/service/i, "Output contains the word 'service' (case insensitive)"); + +$result = NPTest->testCmd( + "./check_cluster -l LABEL -s -w 0:0 -c 0:0 -d 0,0,0,0" + ); +like( $result->output, qr/LABEL/, "Output contains the defined label 'LABEL' (case sensitive)"); + +$result = NPTest->testCmd( + "./check_cluster -s -w 0:0 -c 0:1 -d 0,0,0,1" + ); +cmp_ok( $result->return_code, '==', 1, "Exit WARNING if non-ok services are inside critical and outside warning ranges" ); + +$result = NPTest->testCmd( + "./check_cluster -s -w 0:0 -c 0:1 -d 0,0,1,1" + ); +cmp_ok( $result->return_code, '==', 2, "Exit CRITICAL if non-ok services are inside critical and outside warning ranges" ); + +$result = NPTest->testCmd( + "./check_cluster -s -w 0 -c 0 -d 0,0,0,0" + ); +cmp_ok( $result->return_code, '==', 0, "Exit OK if non-ok services are inside critical and warning (no ranges)" ); + +$result = NPTest->testCmd( + "./check_cluster -s -w 0 -c 1 -d 0,0,1,0" + ); +cmp_ok( $result->return_code, '==', 1, "Exit WARNING if number of non-ok services exceed warning (no ranges)" ); + +$result = NPTest->testCmd( + "./check_cluster -s -w 0 -c 1 -d 0,0,1,1" + ); +cmp_ok( $result->return_code, '==', 2, "Exit Critical if non-ok services exceed critical warning (no ranges)" ); + + +# +# And for hosts.. +# +$result = NPTest->testCmd( + "./check_cluster -h -w 0:0 -c 0:0 -d 0,0,0,0" + ); +cmp_ok( $result->return_code, '==', 0, "Exit OK if non-ok hosts are inside critical and warning ranges" ); +like( $result->output, qr/host/i, "Output contains the word 'host' (case insensitive)"); + +$result = NPTest->testCmd( + "./check_cluster -h -w 0:0 -c 0:1 -d 0,0,0,1" + ); +cmp_ok( $result->return_code, '==', 1, "Exit WARNING if non-ok hosts are inside critical and outside warning ranges" ); + +$result = NPTest->testCmd( + "./check_cluster -h -w 0:0 -c 0:1 -d 0,0,1,1" + ); +cmp_ok( $result->return_code, '==', 2, "Exit CRITICAL if non-ok hosts are inside critical and outside warning ranges" ); + +$result = NPTest->testCmd( + "./check_cluster -h -w 0 -c 0 -d 0,0,0,0" + ); +cmp_ok( $result->return_code, '==', 0, "Exit OK if non-ok hosts are inside critical and warning (no ranges)" ); + +$result = NPTest->testCmd( + "./check_cluster -h -w 0 -c 1 -d 0,0,1,0" + ); +cmp_ok( $result->return_code, '==', 1, "Exit WARNING if number of non-ok hosts exceed warning (no ranges)" ); + +$result = NPTest->testCmd( + "./check_cluster -h -w 0 -c 1 -d 0,0,1,1" + ); +cmp_ok( $result->return_code, '==', 2, "Exit Critical if non-ok hosts exceed critical warning (no ranges)" ); diff --git a/plugins/t/check_curl.t b/plugins/t/check_curl.t new file mode 100644 index 0000000..eae98cc --- /dev/null +++ b/plugins/t/check_curl.t @@ -0,0 +1,213 @@ +#! /usr/bin/perl -w -I .. +# +# HyperText Transfer Protocol (HTTP) Test via check_curl +# +# + +use strict; +use Test::More; +use POSIX qw/mktime strftime/; + +use vars qw($tests $has_ipv6); + +BEGIN { + use NPTest; + $has_ipv6 = NPTest::has_ipv6(); + $tests = $has_ipv6 ? 59 : 57; + plan tests => $tests; +} + + +my $successOutput = '/OK.*HTTP.*second/'; + +my $res; +my $plugin = 'check_http'; +$plugin = 'check_curl' if $0 =~ m/check_curl/mx; + +my $host_tcp_http = getTestParameter("NP_HOST_TCP_HTTP", "A host providing the HTTP Service (a web server)", "localhost"); +my $host_tcp_http_ipv6 = getTestParameter("NP_HOST_TCP_HTTP_IPV6", "An IPv6 address providing a HTTP Service (a web server)", "::1"); +my $host_tls_http = getTestParameter("NP_HOST_TLS_HTTP", "A host providing the HTTPS Service (a tls web server)", "localhost"); +my $host_tls_cert = getTestParameter("NP_HOST_TLS_CERT", "the common name of the certificate.", "localhost"); +my $host_nonresponsive = getTestParameter("NP_HOST_NONRESPONSIVE", "The hostname of system not responsive to network requests", "10.0.0.1"); +my $hostname_invalid = getTestParameter("NP_HOSTNAME_INVALID", "An invalid (not known to DNS) hostname", "nosuchhost"); +my $internet_access = getTestParameter("NP_INTERNET_ACCESS", "Is this system directly connected to the internet?", "yes"); +my $host_tcp_http2 = getTestParameter("NP_HOST_TCP_HTTP2", "A host providing an index page containing the string 'monitoring'", "test.monitoring-plugins.org"); +my $host_tcp_proxy = getTestParameter("NP_HOST_TCP_PROXY", "A host providing a HTTP proxy with CONNECT support", "localhost"); +my $port_tcp_proxy = getTestParameter("NP_PORT_TCP_PROXY", "Port of the proxy with HTTP and CONNECT support", "3128"); + +my $faketime = -x '/usr/bin/faketime' ? 1 : 0; + + +$res = NPTest->testCmd( + "./$plugin $host_tcp_http -wt 300 -ct 600" + ); +cmp_ok( $res->return_code, '==', 0, "Webserver $host_tcp_http responded" ); +like( $res->output, $successOutput, "Output OK" ); + +if ($has_ipv6) { + # Test for IPv6 formatting + $res = NPTest->testCmd( + "./$plugin -I $host_tcp_http_ipv6 -wt 300 -ct 600" + ); + cmp_ok( $res->return_code, '==', 0, "IPv6 URL formatting is working" ); + like( $res->output, $successOutput, "Output OK" ); +} + +$res = NPTest->testCmd( + "./$plugin $host_tcp_http -wt 300 -ct 600 -v -v -v -k 'bob:there' -k 'carl:frown'" + ); +like( $res->output, '/bob:there\r\ncarl:frown\r\n/', "Got headers with multiple -k options" ); + +$res = NPTest->testCmd( + "./$plugin $host_nonresponsive -wt 1 -ct 2 -t 3" + ); +cmp_ok( $res->return_code, '==', 2, "Webserver $host_nonresponsive not responding" ); +# was CRITICAL only, but both check_curl and check_http print HTTP CRITICAL (puzzle?!) +like( $res->output, "/HTTP CRITICAL - Invalid HTTP response received from host on port 80: cURL returned 28 - Connection timed out after/", "Output OK"); + +$res = NPTest->testCmd( + "./$plugin $hostname_invalid -wt 1 -ct 2" + ); +cmp_ok( $res->return_code, '==', 2, "Webserver $hostname_invalid not valid" ); +# The first part of the message comes from the OS catalogue, so cannot check this. +# On Debian, it is Name or service not known, on Darwin, it is No address associated with nodename +# Is also possible to get a socket timeout if DNS is not responding fast enough +# cURL gives us consistent strings from it's own 'lib/strerror.c' +like( $res->output, "/cURL returned 6 - Could not resolve host:/", "Output OK"); + +# host header checks +$res = NPTest->testCmd("./$plugin -v -H $host_tcp_http"); +like( $res->output, '/^Host: '.$host_tcp_http.'\s*$/ms', "Host Header OK" ); +like( $res->output, '/CURLOPT_URL: http:\/\/'.$host_tcp_http.':80\//ms', "Url OK" ); + +$res = NPTest->testCmd("./$plugin -v -H $host_tcp_http -p 80"); +like( $res->output, '/^Host: '.$host_tcp_http.'\s*$/ms', "Host Header OK" ); +like( $res->output, '/CURLOPT_URL: http:\/\/'.$host_tcp_http.':80\//ms', "Url OK" ); + +$res = NPTest->testCmd("./$plugin -v -H $host_tcp_http:8080 -p 80"); +like( $res->output, '/^Host: '.$host_tcp_http.':8080\s*$/ms', "Host Header OK" ); +like( $res->output, '/CURLOPT_URL: http:\/\/'.$host_tcp_http.':80\//ms', "Url OK" ); + +$res = NPTest->testCmd("./$plugin -v -H $host_tcp_http:8080 -p 80"); +like( $res->output, '/^Host: '.$host_tcp_http.':8080\s*$/ms', "Host Header OK" ); +like( $res->output, '/CURLOPT_URL: http:\/\/'.$host_tcp_http.':80\//ms', "Url OK" ); + +$res = NPTest->testCmd("./$plugin -v -H $host_tcp_http:8080 -p 80 -k 'Host: testhost:8001'"); +like( $res->output, '/^Host: testhost:8001\s*$/ms', "Host Header OK" ); +like( $res->output, '/CURLOPT_URL: http:\/\/'.$host_tcp_http.':80\//ms', "Url OK" ); + +$res = NPTest->testCmd("./$plugin -v -I $host_tcp_http -p 80 -k 'Host: testhost:8001'"); +like( $res->output, '/^Host: testhost:8001\s*$/ms', "Host Header OK" ); +like( $res->output, '/CURLOPT_URL: http:\/\/'.$host_tcp_http.':80\//ms', "Url OK" ); + +SKIP: { + skip "No internet access", 4 if $internet_access eq "no"; + + $res = NPTest->testCmd("./$plugin -v -H $host_tls_http -S"); + like( $res->output, '/^Host: '.$host_tls_http.'\s*$/ms', "Host Header OK" ); + + $res = NPTest->testCmd("./$plugin -v -H $host_tls_http:8080 -S -p 443"); + like( $res->output, '/^Host: '.$host_tls_http.':8080\s*$/ms', "Host Header OK" ); + + $res = NPTest->testCmd("./$plugin -v -H $host_tls_http:443 -S -p 443"); + like( $res->output, '/^Host: '.$host_tls_http.'\s*$/ms', "Host Header OK" ); + + $res = NPTest->testCmd("./$plugin -v -H $host_tls_http -D -S -p 443"); + like( $res->output, '/(^Host: '.$host_tls_http.'\s*$)|(cURL returned 60)/ms', "Host Header OK" ); +}; + +SKIP: { + skip "No host serving monitoring in index file", 7 unless $host_tcp_http2; + + $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'monitoring'" ); + cmp_ok( $res->return_code, "==", 0, "Got a reference to 'monitoring'"); + + $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'mONiTORing'" ); + cmp_ok( $res->return_code, "==", 2, "Not got 'mONiTORing'"); + like ( $res->output, "/pattern not found/", "Error message says 'pattern not found'"); + + $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -R 'mONiTORing'" ); + cmp_ok( $res->return_code, "==", 0, "But case insensitive doesn't mind 'mONiTORing'"); + + $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'monitoring' --invert-regex" ); + cmp_ok( $res->return_code, "==", 2, "Invert results work when found"); + like ( $res->output, "/pattern found/", "Error message says 'pattern found'"); + + $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'mONiTORing' --invert-regex" ); + cmp_ok( $res->return_code, "==", 0, "And also when not found"); +} +SKIP: { + skip "No internet access", 28 if $internet_access eq "no"; + + $res = NPTest->testCmd( + "./$plugin --ssl $host_tls_http" + ); + cmp_ok( $res->return_code, '==', 0, "Can read https for $host_tls_http" ); + + $res = NPTest->testCmd( "./$plugin -C 1 --ssl $host_tls_http" ); + cmp_ok( $res->return_code, '==', 0, "Checking certificate for $host_tls_http"); + like ( $res->output, "/Certificate '$host_tls_cert' will expire on/", "Output OK" ); + my $saved_cert_output = $res->output; + + $res = NPTest->testCmd( "./$plugin -C 8000,1 --ssl $host_tls_http" ); + cmp_ok( $res->return_code, '==', 1, "Checking certificate for $host_tls_http"); + like ( $res->output, qr/WARNING - Certificate '$host_tls_cert' expires in \d+ day/, "Output Warning" ); + + $res = NPTest->testCmd( "./$plugin $host_tls_http -C 1" ); + is( $res->return_code, 0, "Old syntax for cert checking okay" ); + is( $res->output, $saved_cert_output, "Same output as new syntax" ); + + $res = NPTest->testCmd( "./$plugin -H $host_tls_http -C 1" ); + is( $res->return_code, 0, "Updated syntax for cert checking okay" ); + is( $res->output, $saved_cert_output, "Same output as new syntax" ); + + $res = NPTest->testCmd( "./$plugin -C 1 $host_tls_http" ); + cmp_ok( $res->output, 'eq', $saved_cert_output, "--ssl option automatically added"); + + $res = NPTest->testCmd( "./$plugin $host_tls_http -C 1" ); + cmp_ok( $res->output, 'eq', $saved_cert_output, "Old syntax for cert checking still works"); + + # run some certificate checks with faketime + SKIP: { + skip "No faketime binary found", 12 if !$faketime; + $res = NPTest->testCmd("LC_TIME=C TZ=UTC ./$plugin -C 1 $host_tls_http"); + like($res->output, qr/OK - Certificate '$host_tls_cert' will expire on/, "Catch cert output"); + is( $res->return_code, 0, "Catch cert output exit code" ); + my($mon,$day,$hour,$min,$sec,$year) = ($res->output =~ /(\w+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\d+)/); + if(!defined $year) { + die("parsing date failed from: ".$res->output); + } + my $months = {'Jan' => 0, 'Feb' => 1, 'Mar' => 2, 'Apr' => 3, 'May' => 4, 'Jun' => 5, 'Jul' => 6, 'Aug' => 7, 'Sep' => 8, 'Oct' => 9, 'Nov' => 10, 'Dec' => 11}; + my $ts = mktime($sec, $min, $hour, $day, $months->{$mon}, $year-1900); + my $time = strftime("%Y-%m-%d %H:%M:%S", localtime($ts)); + $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts))."' ./$plugin -C 1 $host_tls_http"); + like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' just expired/, "Output on expire date"); + is( $res->return_code, 2, "Output on expire date" ); + + $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts-1))."' ./$plugin -C 1 $host_tls_http"); + like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' expires in 0 minutes/, "cert expires in 1 second output"); + is( $res->return_code, 2, "cert expires in 1 second exit code" ); + + $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts-120))."' ./$plugin -C 1 $host_tls_http"); + like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' expires in 2 minutes/, "cert expires in 2 minutes output"); + is( $res->return_code, 2, "cert expires in 2 minutes exit code" ); + + $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts-7200))."' ./$plugin -C 1 $host_tls_http"); + like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' expires in 2 hours/, "cert expires in 2 hours output"); + is( $res->return_code, 2, "cert expires in 2 hours exit code" ); + + $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts+1))."' ./$plugin -C 1 $host_tls_http"); + like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' expired on/, "Certificate expired output"); + is( $res->return_code, 2, "Certificate expired exit code" ); + }; + + $res = NPTest->testCmd( "./$plugin --ssl $host_tls_http -E" ); + like ( $res->output, '/time_connect=[\d\.]+/', 'Extended Performance Data Output OK' ); + like ( $res->output, '/time_ssl=[\d\.]+/', 'Extended Performance Data SSL Output OK' ); + + $res = NPTest->testCmd( "./$plugin -H www.mozilla.com -u /firefox -f curl" ); + is( $res->return_code, 0, "Redirection based on location is okay"); + + $res = NPTest->testCmd( "./$plugin -H www.mozilla.com --extended-perfdata" ); + like ( $res->output, '/time_connect=[\d\.]+/', 'Extended Performance Data Output OK' ); +} diff --git a/plugins/t/check_dbi.t b/plugins/t/check_dbi.t new file mode 100644 index 0000000..c24b5a8 --- /dev/null +++ b/plugins/t/check_dbi.t @@ -0,0 +1,103 @@ +#! /usr/bin/perl -w -I .. +# +# Database Server Tests via check_dbi +# +# +# Uses the 'sqlite3' DBD driver and command line utility. + +use strict; +use Test::More; +use NPTest; + +use File::Temp; + +use vars qw($tests); + +plan skip_all => "check_dbi not compiled" unless (-x "check_dbi"); + +$tests = 20; +plan tests => $tests; + +my $missing_driver_output = "failed to open DBI driver 'sqlite3'"; + +my $bad_driver_output = "/failed to open DBI driver 'nodriver'/"; +my $conn_time_output = "/OK - connection time: [0-9\.]+s \|/"; +my $missing_query_output = "/Must specify a query to execute/"; +my $no_rows_output = "/WARNING - no rows returned/"; +my $not_numeric_output = "/CRITICAL - result value is not a numeric:/"; +my $query_time_output = "/OK - connection time: [0-9\.]+s, 'SELECT 1' returned 1.000000 in [0-9\.]+s \|/"; +my $syntax_error_output = "/CRITICAL - failed to execute query 'GET ALL FROM test': 1: near \"GET\": syntax error/"; + +my $result; + +SKIP: { + my $sqlite3 = qx(which sqlite3 2> /dev/null); + chomp($sqlite3); + + skip "No Sqlite3 found", $tests unless $sqlite3; + + my $sqlite3_check = qx(./check_dbi -d sqlite3 -q ''); + if ($sqlite3_check =~ m/$missing_driver_output/) { + skip "No 'sqlite3' DBD driver found", $tests; + } + + my $fh = File::Temp->new( + TEMPLATE => "/tmp/check_dbi_sqlite3.XXXXXXX", + UNLINK => 1, + ); + my $filename = $fh->filename; + $filename =~ s/^\/tmp\///; + + system("$sqlite3 /tmp/$filename 'CREATE TABLE test(a INT, b TEXT)'"); + system("$sqlite3 /tmp/$filename 'INSERT INTO test VALUES (1, \"text1\")'"); + system("$sqlite3 /tmp/$filename 'INSERT INTO test VALUES (2, \"text2\")'"); + + my $check_cmd = "./check_dbi -d sqlite3 -o sqlite3_dbdir=/tmp -o dbname=$filename"; + + $result = NPTest->testCmd("$check_cmd -q 'SELECT 1'"); + cmp_ok($result->return_code, '==', 0, "Sqlite3 login okay and can run query"); + + $result = NPTest->testCmd("$check_cmd"); + cmp_ok($result->return_code, '==', 3, "Missing query parameter"); + like($result->output, $missing_query_output, "Missing query parameter error message"); + + $result = NPTest->testCmd("$check_cmd -q 'GET ALL FROM test'"); + cmp_ok($result->return_code, '==', 2, "Invalid query"); + like($result->output, $syntax_error_output, "Syntax error message"); + + $result = NPTest->testCmd("$check_cmd -q 'SELECT 2.71828' -w 2 -c 3"); + cmp_ok($result->return_code, '==', 1, "Got warning"); + + $result = NPTest->testCmd("$check_cmd -q 'SELECT 3.1415' -w 2 -c 3"); + cmp_ok($result->return_code, '==', 2, "Got critical"); + + $result = NPTest->testCmd("$check_cmd -q ''"); + cmp_ok($result->return_code, '==', 1, "No rows returned"); + like($result->output, $no_rows_output, "Now rows returned warning message"); + + $result = NPTest->testCmd("$check_cmd -q 'SELECT b FROM test'"); + cmp_ok($result->return_code, '==', 2, "Value is not a numeric"); + like($result->output, $not_numeric_output, "Value is not a numeric error message"); + + $result = NPTest->testCmd("$check_cmd -m QUERY_RESULT -q 'SELECT b FROM test' -e text1"); + cmp_ok($result->return_code, '==', 0, "Query result string comparison okay"); + + $result = NPTest->testCmd("$check_cmd -q 'SELECT b FROM test' -r 'eXt[0-9]'"); + cmp_ok($result->return_code, '==', 2, "Query result case-insensitive regex failure"); + + $result = NPTest->testCmd("$check_cmd -q 'SELECT b FROM test' -R 'eXt[0-9]'"); + cmp_ok($result->return_code, '==', 0, "Query result case-sensitive regex okay"); + + $result = NPTest->testCmd("$check_cmd -m CONN_TIME -w 0.5 -c 0.7"); + cmp_ok($result->return_code, '==', 0, "CONN_TIME metric okay"); + like($result->output, $conn_time_output, "CONN_TIME metric output okay"); + + $result = NPTest->testCmd("$check_cmd -m QUERY_TIME -q 'SELECT 1'"); + cmp_ok($result->return_code, '==', 0, "QUERY_TIME metric okay"); + like($result->output, $query_time_output, "QUERY_TIME metric output okay"); + + $result = NPTest->testCmd("./check_dbi -d nodriver -q ''"); + cmp_ok($result->return_code, '==', 3, "Unknown DBI driver"); + like($result->output, $bad_driver_output, "Correct error message"); +} + diff --git a/plugins/t/check_dig.t b/plugins/t/check_dig.t new file mode 100644 index 0000000..3ed6cc8 --- /dev/null +++ b/plugins/t/check_dig.t @@ -0,0 +1,98 @@ +#! /usr/bin/perl -w -I .. +# +# Domain Name Server (DNS) Tests via check_dig +# +# + +use strict; +use Test::More; + + +use vars qw($tests $has_ipv6); +BEGIN { + plan skip_all => "check_dig not compiled" unless (-x "check_dig"); + use NPTest; + $has_ipv6 = NPTest::has_ipv6(); + $tests = $has_ipv6 ? 16 : 14; + plan tests => $tests; +} + +my $successOutput = '/DNS OK - [\.0-9]+ seconds? response time/'; + +my $hostname_valid = getTestParameter( + "NP_HOSTNAME_VALID", + "A valid (known to DNS) hostname", + "orwell.monitoring-plugins.org" + ); + +my $hostname_valid_ip = getTestParameter( + "NP_HOSTNAME_VALID_IP", + "The IP address of the valid hostname $hostname_valid", + "130.133.8.40", + ); + +my $hostname_valid_reverse = getTestParameter( + "NP_HOSTNAME_VALID_REVERSE", + "The hostname of $hostname_valid_ip", + "orwell.monitoring-plugins.org.", + ); + +my $hostname_invalid = getTestParameter( + "NP_HOSTNAME_INVALID", + "An invalid (not known to DNS) hostname", + "nosuchhost.monitoring-plugins.org", + ); + +my $dns_server = getTestParameter( + "NP_DNS_SERVER", + "A non default (remote) DNS server", + ); + +my $res; + +SKIP: { + skip "check_dig.t: not enough parameters given", + 12 unless ($hostname_valid && $hostname_valid_ip && $hostname_valid_reverse && $hostname_invalid && $dns_server); + + $res = NPTest->testCmd("./check_dig -H $dns_server -l $hostname_valid -t 5"); + cmp_ok( $res->return_code, '==', 0, "Found $hostname_valid"); + like ( $res->output, $successOutput, "Output OK" ); + + $res = NPTest->testCmd("./check_dig -H $dns_server -l $hostname_valid -t 5 -w 0.000001 -c 0.00001"); + cmp_ok( $res->return_code, '==', 2, "Critical threshold passed"); + + $res = NPTest->testCmd("./check_dig -H $dns_server -l $hostname_valid -t 5 -w 0.000001 -c 5"); + cmp_ok( $res->return_code, '==', 1, "Warning threshold passed"); + + $res = NPTest->testCmd("./check_dig -H $dns_server -t 1"); + cmp_ok( $res->return_code, '==', 3, "Invalid command line -l missing"); + + $res = NPTest->testCmd("./check_dig -H $dns_server -l $hostname_invalid -t 1"); + cmp_ok( $res->return_code, '==', 2, "Invalid $hostname_invalid"); + + $res = NPTest->testCmd("./check_dig -H $dns_server -l $hostname_valid -t 5"); + cmp_ok( $res->return_code, '==', 0, "Found $hostname_valid on $dns_server"); + like ( $res->output, $successOutput, "Output OK" ); + + $res = NPTest->testCmd("./check_dig -H $dns_server -l $hostname_valid -t 5 -4"); + cmp_ok( $res->return_code, '==', 0, "Found $hostname_valid on $dns_server"); + like ( $res->output, $successOutput, "Output OK for IPv4" ); + + $res = NPTest->testCmd("./check_dig -H $dns_server -l $hostname_valid -a $hostname_valid_ip -t 5"); + cmp_ok( $res->return_code, '==', 0, "Got expected address"); + + $res = NPTest->testCmd("./check_dig -H $dns_server -l $hostname_valid -a 10.10.10.10 -t 5"); + cmp_ok( $res->return_code, '==', 1, "Got wrong address"); + + my $ip_reverse = $hostname_valid_ip; + $ip_reverse =~ s/(\d+)\.(\d+)\.(\d+)\.(\d+)/$4.$3.$2.$1.in-addr.arpa/; + $res = NPTest->testCmd("./check_dig -H $dns_server -l $ip_reverse -a $hostname_valid_reverse -T PTR -t 5"); + cmp_ok( $res->return_code, '==', 0, "Got expected fqdn"); + like ( $res->output, $successOutput, "Output OK"); + + if($has_ipv6) { + $res = NPTest->testCmd("./check_dig -H $dns_server -l $hostname_valid -t 5 -6"); + cmp_ok( $res->return_code, '==', 0, "Found $hostname_valid on $dns_server"); + like ( $res->output, $successOutput, "Output OK for IPv6" ); + } +} diff --git a/plugins/t/check_disk.t b/plugins/t/check_disk.t new file mode 100644 index 0000000..ca035ce --- /dev/null +++ b/plugins/t/check_disk.t @@ -0,0 +1,378 @@ +#! /usr/bin/perl -w -I .. +# +# Disk Space Tests via check_disk +# +# + +# TODO: Add in tests for perf data. Need to beef up Monitoring::Plugin::Performance to cater for max, min, etc + +use strict; +use Test::More; +use NPTest; +use POSIX qw(ceil floor); + +my $successOutput = '/^DISK OK/'; +my $failureOutput = '/^DISK CRITICAL/'; +my $warningOutput = '/^DISK WARNING/'; + +my $result; + +my $mountpoint_valid = getTestParameter( "NP_MOUNTPOINT_VALID", "Path to valid mountpoint", "/"); +my $mountpoint2_valid = getTestParameter( "NP_MOUNTPOINT2_VALID", "Path to another valid mountpoint. Must be different from 1st one", "/var"); + +if ($mountpoint_valid eq "" or $mountpoint2_valid eq "") { + plan skip_all => "Need 2 mountpoints to test"; +} else { + plan tests => 88; +} + +$result = NPTest->testCmd( + "./check_disk -w 1% -c 1% -p $mountpoint_valid -w 1% -c 1% -p $mountpoint2_valid" + ); +cmp_ok( $result->return_code, "==", 0, "Checking two mountpoints (must have at least 1% free in space and inodes)"); +my $c = 0; +$_ = $result->output; +$c++ while /\(/g; # counts number of "(" - should be two +cmp_ok( $c, '==', 2, "Got two mountpoints in output"); + + +# Get perf data +# Should use Monitoring::Plugin +my @perf_data = sort(split(/ /, $result->perf_output)); + + +# Calculate avg_free free on mountpoint1 and mountpoint2 +# because if you check in the middle, you should get different errors +$_ = $result->output; +my ($free_on_mp1, $free_on_mp2) = (m/\((\d+)%.*\((\d+)%/); +die "Cannot parse output: $_" unless ($free_on_mp1 && $free_on_mp2); +my $avg_free = ceil(($free_on_mp1+$free_on_mp2)/2); +my ($more_free, $less_free); +if ($free_on_mp1 > $free_on_mp2) { + $more_free = $mountpoint_valid; + $less_free = $mountpoint2_valid; +} elsif ($free_on_mp1 < $free_on_mp2) { + $more_free = $mountpoint2_valid; + $less_free = $mountpoint_valid; +} else { + die "Two mountpoints are the same - cannot do rest of test"; +} +if($free_on_mp1 == $avg_free || $free_on_mp2 == $avg_free) { + die "One mountpoints has average space free - cannot do rest of test"; +} + + +# Do same for inodes +$_ = $result->output; +my ($free_inode_on_mp1, $free_inode_on_mp2) = (m/inode=(\d+)%.*inode=(\d+)%/); +die "Cannot parse free inodes: $_" unless ($free_inode_on_mp1 && $free_inode_on_mp2); +my $avg_inode_free = ceil(($free_inode_on_mp1 + $free_inode_on_mp2)/2); +my ($more_inode_free, $less_inode_free); +if ($free_inode_on_mp1 > $free_inode_on_mp2) { + $more_inode_free = $mountpoint_valid; + $less_inode_free = $mountpoint2_valid; +} elsif ($free_inode_on_mp1 < $free_inode_on_mp2) { + $more_inode_free = $mountpoint2_valid; + $less_inode_free = $mountpoint_valid; +} else { + die "Two mountpoints with same inodes free - cannot do rest of test"; +} +if($free_inode_on_mp1 == $avg_inode_free || $free_inode_on_mp2 == $avg_inode_free) { + die "One mountpoints has average inodes free - cannot do rest of test"; +} + +# Verify performance data +# First check absolute thresholds... +$result = NPTest->testCmd( + "./check_disk -w 20 -c 10 -p $mountpoint_valid" + ); +$_ = $result->perf_output; +my ($warn_absth_data, $crit_absth_data, $total_absth_data) = (m/=.[^;]*;(\d+);(\d+);\d+;(\d+)/); +# default unit is MiB, but perfdata is always bytes +is ($warn_absth_data, $total_absth_data - (20 * (2 ** 20)), "Wrong warning in perf data using absolute thresholds"); +is ($crit_absth_data, $total_absth_data - (10 * (2 ** 20)), "Wrong critical in perf data using absolute thresholds"); + +# Then check percent thresholds. +$result = NPTest->testCmd( + "./check_disk -w 20% -c 10% -p $mountpoint_valid" + ); +$_ = $result->perf_output; +my ($warn_percth_data, $crit_percth_data, $total_percth_data) = (m/=.[^;]*;(\d+);(\d+);\d+;(\d+)/); +is ($warn_percth_data, int((1-20/100)*$total_percth_data), "Wrong warning in perf data using percent thresholds"); +is ($crit_percth_data, int((1-10/100)*$total_percth_data), "Wrong critical in perf data using percent thresholds"); + + +# Check when order of mount points are reversed, that perf data remains same +$result = NPTest->testCmd( + "./check_disk -w 1% -c 1% -p $mountpoint2_valid -w 1% -c 1% -p $mountpoint_valid" + ); +@_ = sort(split(/ /, $result->perf_output)); +is_deeply( \@perf_data, \@_, "perf data for both filesystems same when reversed"); + + +# Basic filesystem checks for sizes +$result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free" ); +cmp_ok( $result->return_code, '==', 0, "At least 1 MB available on $more_free"); +like ( $result->output, $successOutput, "OK output" ); +like ( $result->only_output, qr/free space/, "Have free space text"); +like ( $result->only_output, qr/$more_free/, "Have disk name in text"); + +$result = NPTest->testCmd( "./check_disk -w 1 -c 1 -p $more_free -p $less_free" ); +cmp_ok( $result->return_code, '==', 0, "At least 1 MB available on $more_free and $less_free"); +$_ = $result->output; +my ($free_mb_on_mp1, $free_mb_on_mp2) = (m/(\d+)MiB .* (\d+)MiB /g); +my $free_mb_on_all = $free_mb_on_mp1 + $free_mb_on_mp2; + + + +$result = NPTest->testCmd( "./check_disk -e -w 1 -c 1 -p $more_free" ); +is( $result->only_output, "DISK OK", "No print out of disks with -e for OKs"); + +$result = NPTest->testCmd( "./check_disk 100 100 $more_free" ); +cmp_ok( $result->return_code, '==', 0, "Old syntax okay" ); + +$result = NPTest->testCmd( "./check_disk -w 1% -c 1% -p $more_free" ); +cmp_ok( $result->return_code, "==", 0, "At least 1% free" ); + +$result = NPTest->testCmd( + "./check_disk -w 1% -c 1% -p $more_free -w 100% -c 100% -p $less_free" + ); +cmp_ok( $result->return_code, "==", 2, "Get critical on less_free mountpoint $less_free" ); +like( $result->output, $failureOutput, "Right output" ); + + +$result = NPTest->testCmd( + "./check_disk -w $avg_free% -c 0% -p $less_free" + ); +cmp_ok( $result->return_code, '==', 1, "Get warning on less_free mountpoint, when checking avg_free"); + +$result = NPTest->testCmd( + "./check_disk -w $avg_free% -c $avg_free% -p $more_free" + ); +cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, when checking avg_free"); + +$result = NPTest->testCmd( + "./check_disk -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free" + ); +cmp_ok( $result->return_code, "==", 1, "Combining above two tests, get warning"); +my $all_disks = $result->output; + +$result = NPTest->testCmd( + "./check_disk -e -w $avg_free% -c 0% -p $less_free -w $avg_free% -c $avg_free% -p $more_free" + ); +isnt( $result->output, $all_disks, "-e gives different output"); + +# Need spaces around filesystem name in case less_free and more_free are nested +like( $result->output, qr/ $less_free /, "Found problem $less_free"); +unlike( $result->only_output, qr/ $more_free /, "Has ignored $more_free as not a problem"); +like( $result->perf_output, qr/ $more_free=/, "But $more_free is still in perf data"); + +$result = NPTest->testCmd( + "./check_disk -w $avg_free% -c 0% -p $more_free" + ); +cmp_ok( $result->return_code, '==', 0, "Get ok on more_free mountpoint, checking avg_free"); + +$result = NPTest->testCmd( + "./check_disk -w $avg_free% -c $avg_free% -p $less_free" + ); +cmp_ok( $result->return_code, '==', 2, "Get critical on less_free, checking avg_free"); +$result = NPTest->testCmd( + "./check_disk -w $avg_free% -c 0% -p $more_free -w $avg_free% -c $avg_free% -p $less_free" + ); +cmp_ok( $result->return_code, '==', 2, "Combining above two tests, get critical"); + +$result = NPTest->testCmd( + "./check_disk -w $avg_free% -c $avg_free% -p $less_free -w $avg_free% -c 0% -p $more_free" + ); +cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference"); + + + +# Basic inode checks for sizes + +$result = NPTest->testCmd( "./check_disk --icritical 1% --iwarning 1% -p $more_inode_free" ); +is( $result->return_code, 0, "At least 1% free on inodes for both mountpoints"); + +$result = NPTest->testCmd( "./check_disk -K 100% -W 100% -p $less_inode_free" ); +is( $result->return_code, 2, "Critical requesting 100% free inodes for both mountpoints"); + +$result = NPTest->testCmd( "./check_disk --iwarning 1% --icritical 1% -p $more_inode_free -K 100% -W 100% -p $less_inode_free" ); +is( $result->return_code, 2, "Get critical on less_inode_free mountpoint $less_inode_free"); + +$result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $less_inode_free" ); +is( $result->return_code, 1, "Get warning on less_inode_free, when checking average"); + +$result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $more_inode_free "); +is( $result->return_code, 0, "Get ok on more_inode_free when checking average"); + +$result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $less_inode_free -W $avg_inode_free% -K $avg_inode_free% -p $more_inode_free" ); +is ($result->return_code, 1, "Combine above two tests, get warning"); +$all_disks = $result->output; + +$result = NPTest->testCmd( "./check_disk -e -W $avg_inode_free% -K 0% -p $less_inode_free -W $avg_inode_free% -K $avg_inode_free% -p $more_inode_free" ); +isnt( $result->output, $all_disks, "-e gives different output"); +like( $result->output, qr/$less_inode_free/, "Found problem $less_inode_free"); +unlike( $result->only_output, qr/$more_inode_free\s/, "Has ignored $more_inode_free as not a problem"); +like( $result->perf_output, qr/$more_inode_free/, "But $more_inode_free is still in perf data"); + +$result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $more_inode_free" ); +is( $result->return_code, 0, "Get ok on more_inode_free mountpoint, checking average"); + +$result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $less_inode_free" ); +is( $result->return_code, 2, "Get critical on less_inode_free, checking average"); + +$result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K 0% -p $more_inode_free -W $avg_inode_free% -K $avg_inode_free% -p $less_inode_free" ); +is( $result->return_code, 2, "Combining above two tests, get critical"); + +$result = NPTest->testCmd( "./check_disk -W $avg_inode_free% -K $avg_inode_free% -p $less_inode_free -W $avg_inode_free% -K 0% -p $more_inode_free" ); +cmp_ok( $result->return_code, '==', 2, "And reversing arguments should not make a difference"); + + + + + + +TODO: { + local $TODO = "Invalid percent figures"; + $result = NPTest->testCmd( + "./check_disk -w 10% -c 15% -p $mountpoint_valid" + ); + cmp_ok( $result->return_code, '==', 3, "Invalid command line options" ); +} + +$result = NPTest->testCmd( + "./check_disk -p $mountpoint_valid -w 10% -c 15%" + ); +cmp_ok( $result->return_code, "==", 3, "Invalid options: -p must come after thresholds" ); + +$result = NPTest->testCmd( "./check_disk -w 100% -c 100% ".${mountpoint_valid} ); # 100% empty +cmp_ok( $result->return_code, "==", 2, "100% empty" ); +like( $result->output, $failureOutput, "Right output" ); + +$result = NPTest->testCmd( "./check_disk -w 100000000 -c 100000000 $mountpoint_valid" ); +cmp_ok( $result->return_code, '==', 2, "Check for 100TB free" ); + +$result = NPTest->testCmd( "./check_disk -w 100 -c 100 -u TB ".${mountpoint_valid} ); # 100 TB empty +cmp_ok( $result->return_code, "==", 2, "100 TB empty" ); + + +# Checking old syntax of check_disk warn crit [fs], with warn/crit at USED% thresholds +$result = NPTest->testCmd( "./check_disk 0 0 ".${mountpoint_valid} ); +cmp_ok( $result->return_code, "==", 2, "Old syntax: 0% used"); +like ( $result->only_output, qr(^[^;]*;[^;]*$), "Select only one path with positional arguments"); + +$result = NPTest->testCmd( "./check_disk 100 100 $mountpoint_valid" ); +cmp_ok( $result->return_code, '==', 0, "Old syntax: 100% used" ); + +$result = NPTest->testCmd( "./check_disk 0 100 $mountpoint_valid" ); +cmp_ok( $result->return_code, '==', 1, "Old syntax: warn 0% used" ); + +TODO: { + local $TODO = "Invalid values"; + $result = NPTest->testCmd( "./check_disk 0 200 $mountpoint_valid" ); + cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" ); + + $result = NPTest->testCmd( "./check_disk 200 200 $mountpoint_valid" ); + cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" ); + + $result = NPTest->testCmd( "./check_disk 200 0 $mountpoint_valid" ); + cmp_ok( $result->return_code, '==', 3, "Old syntax: Error with values outside percent range" ); +} + +$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /bob" ); +cmp_ok( $result->return_code, '==', 2, "Checking /bob - return error because /bob does not exist" ); +like( $result->output, '/^DISK CRITICAL - /bob is not accessible:.*$/', 'Output OK'); + +$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /" ); +my $root_output = $result->output; + +$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc" ); +cmp_ok( $result->return_code, '==', 0, "Checking /etc - should return info for /" ); +cmp_ok( $result->output, 'eq', $root_output, "check_disk /etc gives same as check_disk /"); + +$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -E -p /etc " ); +cmp_ok( $result->return_code, '==', 2, "... unless -E/--exact-match is specified"); + +$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p /etc -E " ); +cmp_ok( $result->return_code, '==', 3, "-E/--exact-match must be specified before -p"); + +$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -r /etc -E " ); +cmp_ok( $result->return_code, '==', 3, "-E/--exact-match must be specified before -r"); + +$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /bob" ); +cmp_ok( $result->return_code, '==', 2, "Checking / and /bob gives critical"); +unlike( $result->perf_output, '/\/bob/', "perf data does not have /bob in it"); + +$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p / -p /" ); +unlike( $result->output, '/ \/ .* \/ /', "Should not show same filesystem twice"); + +# are partitions added if -C is given without path selection -p ? +$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -C -w 0% -c 0% -p $mountpoint_valid" ); +like( $result->output, '/;.*;\|/', "-C selects partitions if -p is not given"); + +# grouping: exit crit if the sum of free megs on mp1+mp2 is less than warn/crit +$result = NPTest->testCmd( "./check_disk -w ". ($free_mb_on_all + 1) ." -c ". ($free_mb_on_all + 1) ."-g group -p $mountpoint_valid -p $mountpoint2_valid" ); +cmp_ok( $result->return_code, '==', 2, "grouping: exit crit if the sum of free megs on mp1+mp2 is less than warn/crit"); + +# grouping: exit warning if the sum of free megs on mp1+mp2 is between -w and -c +$result = NPTest->testCmd( "./check_disk -w ". ($free_mb_on_all + 1) ." -c ". ($free_mb_on_all - 1) ." -g group -p $mountpoint_valid -p $mountpoint2_valid" ); +cmp_ok( $result->return_code, '==', 1, "grouping: exit warning if the sum of free megs on mp1+mp2 is between -w and -c "); + +# grouping: exit ok if the sum of free megs on mp1+mp2 is more than warn/crit +$result = NPTest->testCmd( "./check_disk -w ". ($free_mb_on_all - 1) ." -c ". ($free_mb_on_all - 1) ." -g group -p $mountpoint_valid -p $mountpoint2_valid" ); +cmp_ok( $result->return_code, '==', 0, "grouping: exit ok if the sum of free megs on mp1+mp2 is more than warn/crit"); + +# grouping: exit unknown if group name is given after -p +$result = NPTest->testCmd( "./check_disk -w ". ($free_mb_on_all - 1) ." -c ". ($free_mb_on_all - 1) ." -p $mountpoint_valid -g group -p $mountpoint2_valid" ); +cmp_ok( $result->return_code, '==', 3, "Invalid options: -p must come after groupname"); + +# regex: exit unknown if given regex is not compilable +$result = NPTest->testCmd( "./check_disk -w 1 -c 1 -r '('" ); +cmp_ok( $result->return_code, '==', 3, "Exit UNKNOWN if regex is not compilable"); + +# ignore: exit unknown, if all paths are deselected using -i +$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '$mountpoint_valid' -i '$mountpoint2_valid'" ); +cmp_ok( $result->return_code, '==', 3, "ignore-ereg: Unknown if all fs are ignored (case sensitive)"); + +# ignore: exit unknown, if all paths are deselected using -I +$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -I '".uc($mountpoint_valid)."' -I '".uc($mountpoint2_valid)."'" ); +cmp_ok( $result->return_code, '==', 3, "ignore-ereg: Unknown if all fs are ignored (case insensitive)"); + +# ignore: exit unknown, if all paths are deselected using -i +$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '.*'" ); +cmp_ok( $result->return_code, '==', 3, "ignore-ereg: Unknown if all fs are ignored using -i '.*'"); + +# ignore: test if ignored path is actually ignored +$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '^$mountpoint2_valid\$'"); +like( $result->output, qr/$mountpoint_valid/, "output data does have $mountpoint_valid in it"); +unlike( $result->output, qr/$mountpoint2_valid/, "output data does not have $mountpoint2_valid in it"); + +# ignore: test if all paths are listed when ignore regex doesn't match +$result = NPTest->testCmd( "./check_disk -w 0% -c 0% -p $mountpoint_valid -p $mountpoint2_valid -i '^barbazJodsf\$'"); +like( $result->output, qr/$mountpoint_valid/, "ignore: output data does have $mountpoint_valid when regex doesn't match"); +like( $result->output, qr/$mountpoint2_valid/,"ignore: output data does have $mountpoint2_valid when regex doesn't match"); + +# ignore-missing: exit okay, when fs is not accessible +$result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -p /bob"); +cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for not existing filesystem /bob"); +like( $result->output, '/^DISK OK - No disks were found for provided parameters; - ignored paths: /bob;.*$/', 'Output OK'); + +# ignore-missing: exit okay, when regex does not match +$result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -r /bob"); +cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for regular expression not matching"); +like( $result->output, '/^DISK OK - No disks were found for provided parameters;.*$/', 'Output OK'); + +# ignore-missing: exit okay, when fs with exact match (-E) is not found +$result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -E -p /etc"); +cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay when exact match does not find fs"); +like( $result->output, '/^DISK OK - No disks were found for provided parameters; - ignored paths: /etc;.*$/', 'Output OK'); + +# ignore-missing: exit okay, when checking one existing fs and one non-existing fs (regex) +$result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -r '/bob' -r '^/\$'"); +cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for regular expression not matching"); +like( $result->output, '/^DISK OK - free space: \/ .*$/', 'Output OK'); + +# ignore-missing: exit okay, when checking one existing fs and one non-existing fs (path) +$result = NPTest->testCmd( "./check_disk --ignore-missing -w 0% -c 0% -p '/bob' -p '/'"); +cmp_ok( $result->return_code, '==', 0, "ignore-missing: return okay for regular expression not matching"); +like( $result->output, '/^DISK OK - free space: / .*; - ignored paths: /bob;.*$/', 'Output OK');
\ No newline at end of file diff --git a/plugins/t/check_dns.t b/plugins/t/check_dns.t new file mode 100644 index 0000000..afb2062 --- /dev/null +++ b/plugins/t/check_dns.t @@ -0,0 +1,115 @@ +#! /usr/bin/perl -w -I .. +# +# Domain Name Server (DNS) Tests via check_dns +# +# + +use strict; +use Test::More; +use NPTest; + +plan skip_all => "check_dns not compiled" unless (-x "check_dns"); + +plan tests => 23; + +my $successOutput = '/DNS OK: [\.0-9]+ seconds? response time/'; + +my $hostname_valid = getTestParameter( + "NP_HOSTNAME_VALID", + "A valid (known to DNS) hostname", + "monitoring-plugins.org", + ); + +my $hostname_valid_ip = getTestParameter( + "NP_HOSTNAME_VALID_IP", + "The IP address of the valid hostname $hostname_valid", + "130.133.8.40", + ); + +my $hostname_valid_cidr = getTestParameter( + "NP_HOSTNAME_VALID_CIDR", + "An valid CIDR range containing $hostname_valid_ip", + "130.133.8.41/30", + ); + +my $hostname_invalid_cidr = getTestParameter( + "NP_HOSTNAME_INVALID_CIDR", + "An (valid) CIDR range NOT containing $hostname_valid_ip", + "130.133.8.39/30", + ); + +my $hostname_valid_reverse = getTestParameter( + "NP_HOSTNAME_VALID_REVERSE", + "The hostname of $hostname_valid_ip", + "orwell.monitoring-plugins.org.", + ); + +my $hostname_invalid = getTestParameter( + "NP_HOSTNAME_INVALID", + "An invalid (not known to DNS) hostname", + "nosuchhost.altinity.com", + ); + +my $dns_server = getTestParameter( + "NP_DNS_SERVER", + "A non default (remote) DNS server", + ); + +my $host_nonresponsive = getTestParameter( + "NP_HOST_NONRESPONSIVE", + "The hostname of system not responsive to network requests", + "10.0.0.1", + ); + +my $res; + +$res = NPTest->testCmd("./check_dns -H $hostname_valid -t 5"); +cmp_ok( $res->return_code, '==', 0, "Found $hostname_valid"); +like ( $res->output, $successOutput, "Output OK" ); + +$res = NPTest->testCmd("./check_dns -H $hostname_valid -t 5 -w 0 -c 0"); +cmp_ok( $res->return_code, '==', 2, "Critical threshold passed"); + +$res = NPTest->testCmd("./check_dns -H $hostname_valid -t 5 -w 0 -c 5"); +cmp_ok( $res->return_code, '==', 1, "Warning threshold passed"); +like( $res->output, '/\|time=[\d\.]+s;0.0*;5\.0*;0\.0*/', "Output performance data OK" ); + +$res = NPTest->testCmd("./check_dns -H $hostname_invalid -t 1"); +cmp_ok( $res->return_code, '==', 2, "Invalid $hostname_invalid"); + +$res = NPTest->testCmd("./check_dns -H $hostname_valid -s $dns_server -t 5"); +cmp_ok( $res->return_code, '==', 0, "Found $hostname_valid on $dns_server"); +like ( $res->output, $successOutput, "Output OK" ); + +$res = NPTest->testCmd("./check_dns -H $hostname_invalid -s $dns_server -t 1"); +cmp_ok( $res->return_code, '==', 2, "Invalid $hostname_invalid on $dns_server"); + +$res = NPTest->testCmd("./check_dns -H $hostname_valid -a $hostname_valid_ip -s $host_nonresponsive -t 2"); +cmp_ok( $res->return_code, '==', 2, "Got no answer from unresponsive server"); +like ( $res->output, "/CRITICAL - /", "Output OK"); + +$res = NPTest->testCmd("./check_dns -H $hostname_valid -a $hostname_valid_ip -t 5"); +cmp_ok( $res->return_code, '==', 0, "Got expected address"); + +$res = NPTest->testCmd("./check_dns -H $hostname_valid -a 10.10.10.10 -t 5"); +cmp_ok( $res->return_code, '==', 2, "Got wrong address"); +like ( $res->output, "/^DNS CRITICAL.*expected '10.10.10.10' but got '$hostname_valid_ip'".'$/', "Output OK"); + +$res = NPTest->testCmd("./check_dns -H $hostname_valid_ip -a $hostname_valid_reverse -t 5"); +cmp_ok( $res->return_code, '==', 0, "Got expected fqdn"); +like ( $res->output, $successOutput, "Output OK"); + +$res = NPTest->testCmd("./check_dns -H $hostname_valid -a $hostname_valid_cidr -t 5"); +cmp_ok( $res->return_code, '==', 0, "Got expected address"); + +$res = NPTest->testCmd("./check_dns -H $hostname_valid -a $hostname_invalid_cidr -t 5"); +cmp_ok( $res->return_code, '==', 2, "Got wrong address"); +like ( $res->output, "/^DNS CRITICAL.*expected '$hostname_invalid_cidr' but got '$hostname_valid_ip'".'$/', "Output OK"); + +$res = NPTest->testCmd("./check_dns -H $hostname_valid -n"); +cmp_ok( $res->return_code, '==', 2, "Found $hostname_valid"); +like ( $res->output, "/^DNS CRITICAL.*Domain '$hostname_valid' was found by the server:/", "Output OK"); + +$res = NPTest->testCmd("./check_dns -H $hostname_invalid -n"); +cmp_ok( $res->return_code, '==', 0, "Did not find $hostname_invalid"); +like ( $res->output, $successOutput, "Output OK" ); diff --git a/plugins/t/check_dummy.t b/plugins/t/check_dummy.t new file mode 100644 index 0000000..15fc157 --- /dev/null +++ b/plugins/t/check_dummy.t @@ -0,0 +1,54 @@ +#! /usr/bin/perl -w -I .. +# +# check_dummy tests +# +# + +use strict; +use Test::More; +use NPTest; + +plan tests => 20; + +my $res; + +$res = NPTest->testCmd("./check_dummy"); +is( $res->return_code, 3, "No args" ); +like( $res->output, "/Could not parse arguments/", "Correct usage message"); + +$res = NPTest->testCmd("./check_dummy 0"); +is( $res->return_code, 0, "OK state returned"); +is( $res->output, "OK", "Says 'OK'"); + +$res = NPTest->testCmd("./check_dummy 0 'some random data'"); +is( $res->return_code, 0, "Still OK"); +is( $res->output, "OK: some random data", "Sample text okay"); + +$res = NPTest->testCmd("./check_dummy 1"); +is( $res->return_code, 1, "Warning okay"); +is( $res->output, "WARNING", "Says 'WARNING'"); + +$res = NPTest->testCmd("./check_dummy 1 'more stuff'"); +is( $res->return_code, 1, "Still warning"); +is( $res->output, "WARNING: more stuff", "optional text okay" ); + +$res = NPTest->testCmd("./check_dummy 2"); +is( $res->return_code, 2, "Critical ok" ); +is( $res->output, "CRITICAL", "Says 'CRITICAL'"); + +$res = NPTest->testCmd("./check_dummy 2 'roughly drafted'"); +is( $res->return_code, 2, "Still critical"); +is( $res->output, "CRITICAL: roughly drafted", "optional text okay" ); + +$res = NPTest->testCmd("./check_dummy 3"); +is( $res->return_code, 3, "Unknown ok" ); +is( $res->output, "UNKNOWN", "Says 'UNKNOWN'"); + +$res = NPTest->testCmd("./check_dummy 3 'daringfireball'"); +is( $res->return_code, 3, "Still unknown"); +is( $res->output, "UNKNOWN: daringfireball", "optional text okay" ); + +$res = NPTest->testCmd("./check_dummy 4"); +is( $res->return_code, 3, "Invalid error code" ); +is( $res->output, "UNKNOWN: Status 4 is not a supported error state", "With appropriate error message"); + diff --git a/plugins/t/check_fping.t b/plugins/t/check_fping.t new file mode 100644 index 0000000..67b357b --- /dev/null +++ b/plugins/t/check_fping.t @@ -0,0 +1,34 @@ +#! /usr/bin/perl -w -I .. +# +# FPing Tests via check_fping +# +# + +use strict; +use Test::More; +use NPTest; + +my $host_responsive = getTestParameter("NP_HOST_RESPONSIVE", "The hostname of system responsive to network requests", "localhost"); +my $host_nonresponsive = getTestParameter("NP_HOST_NONRESPONSIVE", "The hostname of system not responsive to network requests", "10.0.0.1"); +my $hostname_invalid = getTestParameter("NP_HOSTNAME_INVALID", "An invalid (not known to DNS) hostname", "nosuchhost"); + +my $res; + +my $fping = qx(which fping 2> /dev/null); +chomp($fping); +if( ! -x "./check_fping") { + plan skip_all => "check_fping not found, skipping tests"; +} +elsif ( !$fping || !-x $fping ) { + plan skip_all => "fping not found or cannot be executed, skipping tests"; +} else { + plan tests => 3; + $res = NPTest->testCmd( "./check_fping $host_responsive" ); + cmp_ok( $res->return_code, '==', 0, "Responsive host returns OK"); + + $res = NPTest->testCmd( "./check_fping $host_nonresponsive" ); + cmp_ok( $res->return_code, '==', 2, "Non-Responsive host returns Critical"); + + $res = NPTest->testCmd( "./check_fping $hostname_invalid" ); + cmp_ok( $res->return_code, '==', 3, "Invalid host returns Unknown"); +} diff --git a/plugins/t/check_ftp.t b/plugins/t/check_ftp.t new file mode 100644 index 0000000..93a7d7c --- /dev/null +++ b/plugins/t/check_ftp.t @@ -0,0 +1,28 @@ +#! /usr/bin/perl -w -I .. +# +# File Transfer Protocol (FTP) Test via check_ftp +# +# + +use strict; +use Test; +use NPTest; + +use vars qw($tests); +BEGIN {$tests = 4; plan tests => $tests} + +my $host_tcp_ftp = getTestParameter("NP_HOST_TCP_FTP", "A host providing the FTP Service (an FTP server)", "localhost"); +my $host_nonresponsive = getTestParameter("NP_HOST_NONRESPONSIVE", "The hostname of system not responsive to network requests", "10.0.0.1"); +my $hostname_invalid = getTestParameter("NP_HOSTNAME_INVALID", "An invalid (not known to DNS) hostname", "nosuchhost"); + +my $successOutput = '/FTP OK -\s+[0-9]?\.?[0-9]+ second response time/'; + +my $t; + +$t += checkCmd( "./check_ftp $host_tcp_ftp -wt 300 -ct 600", 0, $successOutput ); +$t += checkCmd( "./check_ftp $host_nonresponsive -wt 0 -ct 0 -to 1", 2 ); +$t += checkCmd( "./check_ftp $hostname_invalid -wt 0 -ct 0", 2 ); + +exit(0) if defined($Test::Harness::VERSION); +exit($tests - $t); + diff --git a/plugins/t/check_hpjd.t b/plugins/t/check_hpjd.t new file mode 100644 index 0000000..4d64852 --- /dev/null +++ b/plugins/t/check_hpjd.t @@ -0,0 +1,71 @@ +#! /usr/bin/perl -w -I .. +# +# HP JetDirect Test via check_hpjd +# +# + +use strict; +use Test::More; +use NPTest; + +plan skip_all => "check_hpjd not compiled" unless (-x "check_hpjd"); + + +my $successOutput = '/^Printer ok - /'; +my $failureOutput = '/Timeout: No [Rr]esponse from /'; + +my $host_tcp_hpjd = getTestParameter( + "NP_HOST_TCP_HPJD", + "A host (usually a printer) providing the HP-JetDirect Services" + ); + +my $host_hpjd_port_invalid = getTestParameter( + "NP_HOST_HPJD_PORT_INVALID", + "A port that HP-JetDirect Services is not listening on", + "162" + ); + +my $host_hpjd_port_valid = getTestParameter( + "NP_HOST_HPJD_PORT_VALID", + "The port that HP-JetDirect Services is currently listening on", + "161" + ); + +my $host_nonresponsive = getTestParameter( + "NP_HOST_NONRESPONSIVE", + "The hostname of system not responsive to network requests", + "10.0.0.1" + ); + +my $hostname_invalid = getTestParameter( + "NP_HOSTNAME_INVALID", + "An invalid (not known to DNS) hostname", + "nosuchhost" + ); + +my $tests = $host_tcp_hpjd ? 9 : 5; +plan tests => $tests; +my $res; + +SKIP: { + skip "No HP JetDirect defined", 2 unless $host_tcp_hpjd; + $res = NPTest->testCmd("./check_hpjd -H $host_tcp_hpjd"); + cmp_ok( $res->return_code, 'eq', 0, "Jetdirect responding" ); + like ( $res->output, $successOutput, "Output correct" ); + + $res = NPTest->testCmd("./check_hpjd -H $host_tcp_hpjd -p $host_hpjd_port_valid"); + cmp_ok( $res->return_code, 'eq', 0, "Jetdirect responding on port $host_hpjd_port_valid" ); + like ( $res->output, $successOutput, "Output correct" ); + + $res = NPTest->testCmd("./check_hpjd -H $host_tcp_hpjd -p $host_hpjd_port_invalid"); + cmp_ok( $res->return_code, 'eq', 2, "Jetdirect not responding on port $host_hpjd_port_invalid" ); + like ( $res->output, $failureOutput, "Output correct" ); +} + +$res = NPTest->testCmd("./check_hpjd -H $host_nonresponsive"); +cmp_ok( $res->return_code, 'eq', 2, "Host not responding"); +like ( $res->output, $failureOutput, "Output OK" ); + +$res = NPTest->testCmd("./check_hpjd -H $hostname_invalid"); +cmp_ok( $res->return_code, 'eq', 3, "Hostname invalid"); + diff --git a/plugins/t/check_http.t b/plugins/t/check_http.t new file mode 100644 index 0000000..1f2fbdf --- /dev/null +++ b/plugins/t/check_http.t @@ -0,0 +1,190 @@ +#! /usr/bin/perl -w -I .. +# +# HyperText Transfer Protocol (HTTP) Test via check_http +# +# + +use strict; +use Test::More; +use POSIX qw/mktime strftime/; +use NPTest; + +plan tests => 49; + +my $successOutput = '/OK.*HTTP.*second/'; + +my $res; +my $plugin = 'check_http'; +$plugin = 'check_curl' if $0 =~ m/check_curl/mx; + +my $host_tcp_http = getTestParameter("NP_HOST_TCP_HTTP", "A host providing the HTTP Service (a web server)", "localhost"); +my $host_tls_http = getTestParameter("NP_HOST_TLS_HTTP", "A host providing the HTTPS Service (a tls web server)", "localhost"); +my $host_tls_cert = getTestParameter("NP_HOST_TLS_CERT", "the common name of the certificate.", "localhost"); +my $host_nonresponsive = getTestParameter("NP_HOST_NONRESPONSIVE", "The hostname of system not responsive to network requests", "10.0.0.1"); +my $hostname_invalid = getTestParameter("NP_HOSTNAME_INVALID", "An invalid (not known to DNS) hostname", "nosuchhost"); +my $internet_access = getTestParameter("NP_INTERNET_ACCESS", "Is this system directly connected to the internet?", "yes"); +my $host_tcp_http2 = getTestParameter("NP_HOST_TCP_HTTP2", "A host providing an index page containing the string 'monitoring'", "test.monitoring-plugins.org"); +my $host_tcp_proxy = getTestParameter("NP_HOST_TCP_PROXY", "A host providing a HTTP proxy with CONNECT support", "localhost"); +my $port_tcp_proxy = getTestParameter("NP_PORT_TCP_PROXY", "Port of the proxy with HTTP and CONNECT support", "3128"); + +my $faketime = -x '/usr/bin/faketime' ? 1 : 0; + + +$res = NPTest->testCmd( + "./$plugin $host_tcp_http -wt 300 -ct 600" + ); +cmp_ok( $res->return_code, '==', 0, "Webserver $host_tcp_http responded" ); +like( $res->output, $successOutput, "Output OK" ); + +$res = NPTest->testCmd( + "./$plugin $host_tcp_http -wt 300 -ct 600 -v -v -v -k 'bob:there' -k 'carl:frown'" + ); +like( $res->output, '/bob:there\r\ncarl:frown\r\n/', "Got headers with multiple -k options" ); + +$res = NPTest->testCmd( + "./$plugin $host_nonresponsive -wt 1 -ct 2 -t 3" + ); +cmp_ok( $res->return_code, '==', 2, "Webserver $host_nonresponsive not responding" ); +cmp_ok( $res->output, 'eq', "CRITICAL - Socket timeout after 3 seconds", "Output OK"); + +$res = NPTest->testCmd( + "./$plugin $hostname_invalid -wt 1 -ct 2" + ); +cmp_ok( $res->return_code, '==', 2, "Webserver $hostname_invalid not valid" ); +# The first part of the message comes from the OS catalogue, so cannot check this. +# On Debian, it is Name or service not known, on Darwin, it is No address associated with nodename +# Is also possible to get a socket timeout if DNS is not responding fast enough +like( $res->output, "/Unable to open TCP socket|Socket timeout after/", "Output OK"); + +# host header checks +$res = NPTest->testCmd("./$plugin -v -H $host_tcp_http"); +like( $res->output, '/^Host: '.$host_tcp_http.'\s*$/ms', "Host Header OK" ); + +$res = NPTest->testCmd("./$plugin -v -H $host_tcp_http -p 80"); +like( $res->output, '/^Host: '.$host_tcp_http.'\s*$/ms', "Host Header OK" ); + +$res = NPTest->testCmd("./$plugin -v -H $host_tcp_http:8080 -p 80"); +like( $res->output, '/^Host: '.$host_tcp_http.':8080\s*$/ms', "Host Header OK" ); + +$res = NPTest->testCmd("./$plugin -v -H $host_tcp_http:8080 -p 80"); +like( $res->output, '/^Host: '.$host_tcp_http.':8080\s*$/ms', "Host Header OK" ); + +SKIP: { + skip "No internet access", 3 if $internet_access eq "no"; + + $res = NPTest->testCmd("./$plugin -v -H $host_tls_http -S"); + like( $res->output, '/^Host: '.$host_tls_http.'\s*$/ms', "Host Header OK" ); + + $res = NPTest->testCmd("./$plugin -v -H $host_tls_http:8080 -S -p 443"); + like( $res->output, '/^Host: '.$host_tls_http.':8080\s*$/ms', "Host Header OK" ); + + $res = NPTest->testCmd("./$plugin -v -H $host_tls_http:443 -S -p 443"); + like( $res->output, '/^Host: '.$host_tls_http.'\s*$/ms', "Host Header OK" ); +}; + +SKIP: { + skip "No host serving monitoring in index file", 7 unless $host_tcp_http2; + + $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'monitoring'" ); + cmp_ok( $res->return_code, "==", 0, "Got a reference to 'monitoring'"); + + $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'mONiTORing'" ); + cmp_ok( $res->return_code, "==", 2, "Not got 'mONiTORing'"); + like ( $res->output, "/pattern not found/", "Error message says 'pattern not found'"); + + $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -R 'mONiTORing'" ); + cmp_ok( $res->return_code, "==", 0, "But case insensitive doesn't mind 'mONiTORing'"); + + $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'monitoring' --invert-regex" ); + cmp_ok( $res->return_code, "==", 2, "Invert results work when found"); + like ( $res->output, "/pattern found/", "Error message says 'pattern found'"); + + $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'mONiTORing' --invert-regex" ); + cmp_ok( $res->return_code, "==", 0, "And also when not found"); +} +SKIP: { + skip "No internet access", 22 if $internet_access eq "no"; + + $res = NPTest->testCmd( + "./$plugin --ssl $host_tls_http" + ); + cmp_ok( $res->return_code, '==', 0, "Can read https for $host_tls_http" ); + + $res = NPTest->testCmd( "./$plugin -C 1 --ssl $host_tls_http" ); + cmp_ok( $res->return_code, '==', 0, "Checking certificate for $host_tls_http"); + like ( $res->output, "/Certificate '$host_tls_cert' will expire on/", "Output OK" ); + my $saved_cert_output = $res->output; + + $res = NPTest->testCmd( "./$plugin -C 8000,1 --ssl $host_tls_http" ); + cmp_ok( $res->return_code, '==', 1, "Checking certificate for $host_tls_http"); + like ( $res->output, qr/WARNING - Certificate '$host_tls_cert' expires in \d+ day/, "Output Warning" ); + + $res = NPTest->testCmd( "./$plugin $host_tls_http -C 1" ); + is( $res->return_code, 0, "Old syntax for cert checking okay" ); + is( $res->output, $saved_cert_output, "Same output as new syntax" ); + + $res = NPTest->testCmd( "./$plugin -H $host_tls_http -C 1" ); + is( $res->return_code, 0, "Updated syntax for cert checking okay" ); + is( $res->output, $saved_cert_output, "Same output as new syntax" ); + + $res = NPTest->testCmd( "./$plugin -C 1 $host_tls_http" ); + cmp_ok( $res->output, 'eq', $saved_cert_output, "--ssl option automatically added"); + + $res = NPTest->testCmd( "./$plugin $host_tls_http -C 1" ); + cmp_ok( $res->output, 'eq', $saved_cert_output, "Old syntax for cert checking still works"); + + # run some certificate checks with faketime + SKIP: { + skip "No faketime binary found", 7 if !$faketime; + $res = NPTest->testCmd("LC_TIME=C TZ=UTC ./$plugin -C 1 $host_tls_http"); + like($res->output, qr/OK - Certificate '$host_tls_cert' will expire on/, "Catch cert output"); + is( $res->return_code, 0, "Catch cert output exit code" ); + my($mon,$day,$hour,$min,$sec,$year) = ($res->output =~ /(\w+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\d+)/); + if(!defined $year) { + die("parsing date failed from: ".$res->output); + } + my $months = {'Jan' => 0, 'Feb' => 1, 'Mar' => 2, 'Apr' => 3, 'May' => 4, 'Jun' => 5, 'Jul' => 6, 'Aug' => 7, 'Sep' => 8, 'Oct' => 9, 'Nov' => 10, 'Dec' => 11}; + my $ts = mktime($sec, $min, $hour, $day, $months->{$mon}, $year-1900); + my $time = strftime("%Y-%m-%d %H:%M:%S", localtime($ts)); + $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts))."' ./$plugin -C 1 $host_tls_http"); + like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' just expired/, "Output on expire date"); + + $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts-1))."' ./$plugin -C 1 $host_tls_http"); + like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' expires in 0 minutes/, "cert expires in 1 second output"); + + $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts-120))."' ./$plugin -C 1 $host_tls_http"); + like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' expires in 2 minutes/, "cert expires in 2 minutes output"); + + $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts-7200))."' ./$plugin -C 1 $host_tls_http"); + like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' expires in 2 hours/, "cert expires in 2 hours output"); + + $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts+1))."' ./$plugin -C 1 $host_tls_http"); + like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' expired on/, "Certificate expired output"); + }; + + $res = NPTest->testCmd( "./$plugin --ssl $host_tls_http -E" ); + like ( $res->output, '/time_connect=[\d\.]+/', 'Extended Performance Data Output OK' ); + like ( $res->output, '/time_ssl=[\d\.]+/', 'Extended Performance Data SSL Output OK' ); + + $res = NPTest->testCmd( "./$plugin -H www.mozilla.com -u /firefox -f follow" ); + is( $res->return_code, 0, "Redirection based on location is okay"); + + $res = NPTest->testCmd( "./$plugin -H www.mozilla.com --extended-perfdata" ); + like ( $res->output, '/time_connect=[\d\.]+/', 'Extended Performance Data Output OK' ); +} + +SKIP: { + skip "No internet access or proxy configured", 6 if $internet_access eq "no" or ! $host_tcp_proxy; + + $res = NPTest->testCmd( "./$plugin -I $host_tcp_proxy -p $port_tcp_proxy -u http://$host_tcp_http -e 200,301,302"); + is( $res->return_code, 0, "Proxy HTTP works"); + like($res->output, qr/OK: Status line output matched/, "Proxy HTTP Output is sufficient"); + + $res = NPTest->testCmd( "./$plugin -I $host_tcp_proxy -p $port_tcp_proxy -H $host_tls_http -S -j CONNECT"); + is( $res->return_code, 0, "Proxy HTTP CONNECT works"); + like($res->output, qr/HTTP OK:/, "Proxy HTTP CONNECT output sufficient"); + + $res = NPTest->testCmd( "./$plugin -I $host_tcp_proxy -p $port_tcp_proxy -H $host_tls_http -S -j CONNECT:HEAD"); + is( $res->return_code, 0, "Proxy HTTP CONNECT works with override method"); + like($res->output, qr/HTTP OK:/, "Proxy HTTP CONNECT output sufficient"); +} diff --git a/plugins/t/check_imap.t b/plugins/t/check_imap.t new file mode 100644 index 0000000..cf2f81c --- /dev/null +++ b/plugins/t/check_imap.t @@ -0,0 +1,38 @@ +#! /usr/bin/perl -w -I .. +# +# Internet Mail Access Protocol (IMAP) Server Tests via check_imap +# +# + +use strict; +use Test::More tests => 7; +use NPTest; + +my $host_tcp_smtp = getTestParameter("NP_HOST_TCP_SMTP", "A host providing an STMP Service (a mail server)", "mailhost"); +my $host_tcp_imap = getTestParameter("NP_HOST_TCP_IMAP", "A host providing an IMAP Service (a mail server)", $host_tcp_smtp); +my $host_nonresponsive = getTestParameter("NP_HOST_NONRESPONSIVE", "The hostname of system not responsive to network requests", "10.0.0.1"); +my $hostname_invalid = getTestParameter("NP_HOSTNAME_INVALID", "An invalid (not known to DNS) hostname", "nosuchhost"); + +my $t; + +$t = NPTest->testCmd( "./check_imap $host_tcp_imap" ); +cmp_ok( $t->return_code, '==', 0, "Contacted imap" ); + +$t = NPTest->testCmd( "./check_imap -H $host_tcp_imap -p 143 -w 9 -c 9 -to 10 -e '* OK'" ); +cmp_ok( $t->return_code, '==', 0, "Got right response" ); + +$t = NPTest->testCmd( "./check_imap $host_tcp_imap -p 143 -wt 9 -ct 9 -to 10 -e '* OK'" ); +cmp_ok( $t->return_code, '==', 0, "Check old parameter options" ); + +$t = NPTest->testCmd( "./check_imap $host_nonresponsive" ); +cmp_ok( $t->return_code, '==', 2, "Get error with non responsive host" ); + +$t = NPTest->testCmd( "./check_imap $hostname_invalid" ); +cmp_ok( $t->return_code, '==', 2, "Invalid hostname" ); + +$t = NPTest->testCmd( "./check_imap -H $host_tcp_imap -e unlikely_string"); +cmp_ok( $t->return_code, '==', 1, "Got warning with bad response" ); + +$t = NPTest->testCmd( "./check_imap -H $host_tcp_imap -e unlikely_string -M crit"); +cmp_ok( $t->return_code, '==', 2, "Got critical error with bad response" ); + diff --git a/plugins/t/check_jabber.t b/plugins/t/check_jabber.t new file mode 100644 index 0000000..fcdae17 --- /dev/null +++ b/plugins/t/check_jabber.t @@ -0,0 +1,50 @@ +#! /usr/bin/perl -w -I .. +# +# Jabber Server Tests via check_jabber +# +# + +use strict; +use Test::More; +use NPTest; + +plan tests => 10; + +my $host_tcp_jabber = getTestParameter("NP_HOST_TCP_JABBER", "A host providing the Jabber Service", "jabber.de"); +my $host_nonresponsive = getTestParameter("NP_HOST_NONRESPONSIVE", "The hostname of system not responsive to network requests", "10.0.0.1"); +my $hostname_invalid = getTestParameter("NP_HOSTNAME_INVALID", "An invalid (not known to DNS) hostname", "nosuchhost"); + + +my $jabberOK = '/JABBER OK\s-\s\d+\.\d+\ssecond response time on '.$host_tcp_jabber.' port 5222/'; + +my $jabberUnresponsive = '/CRITICAL\s-\sSocket timeout after\s\d+\sseconds/'; + +my $jabberInvalid = '/JABBER CRITICAL - Invalid hostname, address or socket:\s.+/'; + +my $r; + +SKIP: { + skip "No jabber server defined", 6 unless $host_tcp_jabber; + + $r = NPTest->testCmd( "./check_jabber -H $host_tcp_jabber" ); + is( $r->return_code, 0, "Connected okay"); + like( $r->output, $jabberOK, "Output as expected" ); + + $r = NPTest->testCmd( "./check_jabber -H $host_tcp_jabber -w 9 -c 9 -t 10" ); + is( $r->return_code, 0, "Connected okay, within limits" ); + like( $r->output, $jabberOK, "Output as expected" ); + + $r = NPTest->testCmd( "./check_jabber -H $host_tcp_jabber -wt 9 -ct 9 -to 10" ); + is( $r->return_code, 0, "Old syntax okay" ); + like( $r->output, $jabberOK, "Output as expected" ); + +} + +$r = NPTest->testCmd( "./check_jabber $host_nonresponsive" ); +is( $r->return_code, 2, "Unresponsive host gives critical" ); +like( $r->output, $jabberUnresponsive ); + +$r = NPTest->testCmd( "./check_jabber $hostname_invalid" ); +is( $r->return_code, 2, "Invalid hostname gives critical" ); +like( $r->output, $jabberInvalid ); + diff --git a/plugins/t/check_ldap.t b/plugins/t/check_ldap.t new file mode 100644 index 0000000..b8a4a76 --- /dev/null +++ b/plugins/t/check_ldap.t @@ -0,0 +1,71 @@ +#!/usr/bin/env perl -I .. +# +# Lightweight Directory Access Protocol (LDAP) Test via check_ldap +# +# + +use strict; +use warnings; +use Test::More; +use NPTest; + +my $host_tcp_ldap = getTestParameter("NP_HOST_TCP_LDAP", "A host providing the LDAP Service", "localhost"); +my $ldap_base_dn = getTestParameter("NP_LDAP_BASE_DN", "A base dn for the LDAP Service", "cn=admin"); +my $host_nonresponsive = getTestParameter("NP_HOST_NONRESPONSIVE", "The hostname of system not responsive to network requests", "10.0.0.1"); +my $hostname_invalid = getTestParameter("NP_HOSTNAME_INVALID", "An invalid (not known to DNS) hostname", "nosuchhost"); + +my($result, $cmd); +my $command = './check_ldap'; + +plan tests => 16; + +SKIP: { + skip "NP_HOST_NONRESPONSIVE not set", 2 if ! $host_nonresponsive; + + $result = NPTest->testCmd("$command -H $host_nonresponsive -b ou=blah -t 2 -w 1 -c 1"); + is( $result->return_code, 2, "$command -H $host_nonresponsive -b ou=blah -t 5 -w 2 -c 3" ); + is( $result->output, 'CRITICAL - Socket timeout after 2 seconds', "output ok" ); +}; + +SKIP: { + skip "NP_HOSTNAME_INVALID not set", 2 if ! $hostname_invalid; + + $result = NPTest->testCmd("$command -H $hostname_invalid -b ou=blah -t 5"); + is( $result->return_code, 2, "$command -H $hostname_invalid -b ou=blah -t 5" ); + is( $result->output, 'Could not bind to the LDAP server', "output ok" ); +}; + +SKIP: { + skip "NP_HOST_TCP_LDAP not set", 12 if ! $host_tcp_ldap; + skip "NP_LDAP_BASE_DN not set", 12 if ! $ldap_base_dn; + + $cmd = "$command -H $host_tcp_ldap -b $ldap_base_dn -t 5 -w 2 -c 3 -3"; + $result = NPTest->testCmd($cmd); + is( $result->return_code, 0, $cmd ); + like( $result->output, '/^LDAP OK - \d+.\d+ seconds response time\|time=\d+\.\d+s;2\.0+;3\.0+;0\.0+$/', "output ok" ); + + $cmd = "$command -H $host_tcp_ldap -b $ldap_base_dn -t 5 -w 2 -c 3 -3 -W 10000000 -C 10000001"; + $result = NPTest->testCmd($cmd); + is( $result->return_code, 0, $cmd ); + like( $result->output, '/^LDAP OK - found \d+ entries in \d+\.\d+ seconds\|time=\d\.\d+s;2\.0+;3\.0+;0\.0+ entries=\d+\.0+;10000000;10000001;0\.0+$/', "output ok" ); + + $cmd = "$command -H $host_tcp_ldap -b $ldap_base_dn -t 5 -w 2 -c 3 -3 -W 10000000: -C 10000001:"; + $result = NPTest->testCmd($cmd); + is( $result->return_code, 2, $cmd ); + like( $result->output, '/^LDAP CRITICAL - found \d+ entries in \d+\.\d+ seconds\|time=\d\.\d+s;2\.0+;3\.0+;0\.0+ entries=\d+\.0+;10000000:;10000001:;0\.0+$/', "output ok" ); + + $cmd = "$command -H $host_tcp_ldap -b $ldap_base_dn -t 5 -w 2 -c 3 -3 -W 0 -C 0"; + $result = NPTest->testCmd($cmd); + is( $result->return_code, 2, $cmd ); + like( $result->output, '/^LDAP CRITICAL - found \d+ entries in \d+\.\d+ seconds\|time=\d\.\d+s;2\.0+;3\.0+;0\.0+ entries=\d+\.0+;0;0;0\.0+$/', "output ok" ); + + $cmd = "$command -H $host_tcp_ldap -b $ldap_base_dn -t 5 -w 2 -c 3 -3 -W 10000000: -C 10000001"; + $result = NPTest->testCmd($cmd); + is( $result->return_code, 1, $cmd ); + like( $result->output, '/^LDAP WARNING - found \d+ entries in \d+\.\d+ seconds\|time=\d\.\d+s;2\.0+;3\.0+;0\.0+ entries=\d+\.0+;10000000:;10000001;0\.0+$/', "output ok" ); + + $cmd = "$command -H $host_tcp_ldap -b $ldap_base_dn -t 5 -w 2 -c 3 -3 -C 10000001"; + $result = NPTest->testCmd($cmd); + is( $result->return_code, 0, $cmd ); + like( $result->output, '/^LDAP OK - found \d+ entries in \d+\.\d+ seconds\|time=\d\.\d+s;2\.0+;3\.0+;0\.0+ entries=\d+\.0+;;10000001;0\.0+$/', "output ok" ); +}; diff --git a/plugins/t/check_load.t b/plugins/t/check_load.t new file mode 100644 index 0000000..bba8947 --- /dev/null +++ b/plugins/t/check_load.t @@ -0,0 +1,43 @@ +#! /usr/bin/perl -w -I .. +# +# Load Average Tests via check_load +# +# + +use strict; +use Test::More; +use NPTest; + +my $res; + +my $loadValue = "[0-9]+\.?[0-9]+"; +my $successOutput = "/^LOAD OK - total load average: $loadValue, $loadValue, $loadValue/"; +my $successScaledOutput = "/^LOAD OK - scaled load average: $loadValue, $loadValue, $loadValue - total load average: $loadValue, $loadValue, $loadValue/"; +my $failureOutput = "/^LOAD CRITICAL - total load average: $loadValue, $loadValue, $loadValue/"; +my $failurScaledOutput = "/^LOAD CRITICAL - scaled load average: $loadValue, $loadValue, $loadValue - total load average: $loadValue, $loadValue, $loadValue/"; + +plan tests => 13; + +$res = NPTest->testCmd( "./check_load -w 100,100,100 -c 100,100,100" ); +cmp_ok( $res->return_code, 'eq', 0, "load not over 100"); +like( $res->output, $successOutput, "Output OK"); + +$res = NPTest->testCmd( "./check_load -w 0,0,0 -c 0,0,0" ); +cmp_ok( $res->return_code, 'eq', 2, "Load over 0"); +like( $res->output, $failureOutput, "Output OK"); + +$res = NPTest->testCmd( "./check_load -r -w 0,0,0 -c 0,0,0" ); +cmp_ok( $res->return_code, 'eq', 2, "Load over 0 with per cpu division"); +like( $res->output, $failurScaledOutput, "Output OK"); + +$res = NPTest->testCmd( "./check_load -w 100 -c 100,110" ); +cmp_ok( $res->return_code, 'eq', 0, "Plugin can handle non-triplet-arguments"); +like( $res->output, $successOutput, "Output OK"); +like( $res->perf_output, "/load1=$loadValue;100.000;100.000/", "Test handling of non triplet thresholds (load1)"); +like( $res->perf_output, "/load5=$loadValue;100.000;110.000/", "Test handling of non triplet thresholds (load5)"); +like( $res->perf_output, "/load15=$loadValue;100.000;110.000/", "Test handling of non triplet thresholds (load15)"); + + +$res = NPTest->testCmd( "./check_load -w 100,100,100 -c 100,100,100 -r" ); +cmp_ok( $res->return_code, 'eq', 0, "load not over 100"); +like( $res->output, $successScaledOutput, "Output OK"); diff --git a/plugins/t/check_mysql.t b/plugins/t/check_mysql.t new file mode 100644 index 0000000..baf3acc --- /dev/null +++ b/plugins/t/check_mysql.t @@ -0,0 +1,74 @@ +#! /usr/bin/perl -w -I .. +# +# MySQL Database Server Tests via check_mysql +# +# +# +# These are the database permissions required for this test: +# GRANT SELECT ON $db.* TO $user@$host IDENTIFIED BY '$password'; +# GRANT SUPER, REPLICATION CLIENT ON *.* TO $user@$host; +# Check with: +# mysql -u$user -p$password -h$host $db + +use strict; +use Test::More; +use NPTest; + +use vars qw($tests); + +plan skip_all => "check_mysql not compiled" unless (-x "check_mysql"); + +plan tests => 15; + +my $bad_login_output = '/Access denied for user /'; +my $mysqlserver = getTestParameter("NP_MYSQL_SERVER", "A MySQL Server hostname or IP with no slaves setup"); +my $mysqlsocket = getTestParameter("NP_MYSQL_SOCKET", "Full path to a MySQL Server socket with no slaves setup"); +my $mysql_login_details = getTestParameter("NP_MYSQL_LOGIN_DETAILS", "Command line parameters to specify login access (requires REPLICATION CLIENT privileges)", "-u test -ptest"); +my $with_slave = getTestParameter("NP_MYSQL_WITH_SLAVE", "MySQL server with slaves setup"); +my $with_slave_login = getTestParameter("NP_MYSQL_WITH_SLAVE_LOGIN", "Login details for server with slave (requires REPLICATION CLIENT privileges)", $mysql_login_details || "-u test -ptest"); + +my $result; + +SKIP: { + skip "No mysql server defined", 5 unless $mysqlserver; + $result = NPTest->testCmd("./check_mysql -H $mysqlserver $mysql_login_details"); + cmp_ok( $result->return_code, '==', 0, "Login okay"); + + $result = NPTest->testCmd("./check_mysql -H $mysqlserver -u dummy -pdummy"); + cmp_ok( $result->return_code, '==', 2, "Login failure"); + like( $result->output, $bad_login_output, "Expected login failure message"); + + $result = NPTest->testCmd("./check_mysql -S -H $mysqlserver $mysql_login_details"); + cmp_ok( $result->return_code, "==", 1, "No slaves defined" ); + like( $result->output, "/No slaves defined/", "Correct error message"); +} + +SKIP: { + skip "No mysql socket defined", 5 unless $mysqlsocket; + $result = NPTest->testCmd("./check_mysql -s $mysqlsocket $mysql_login_details"); + cmp_ok( $result->return_code, '==', 0, "Login okay"); + + $result = NPTest->testCmd("./check_mysql -s $mysqlsocket -u dummy -pdummy"); + cmp_ok( $result->return_code, '==', 2, "Login failure"); + like( $result->output, $bad_login_output, "Expected login failure message"); + + $result = NPTest->testCmd("./check_mysql -S -s $mysqlsocket $mysql_login_details"); + cmp_ok( $result->return_code, "==", 1, "No slaves defined" ); + like( $result->output, "/No slaves defined/", "Correct error message"); +} + +SKIP: { + skip "No mysql server with slaves defined", 5 unless $with_slave; + $result = NPTest->testCmd("./check_mysql -H $with_slave $with_slave_login"); + cmp_ok( $result->return_code, '==', 0, "Login okay"); + + $result = NPTest->testCmd("./check_mysql -S -H $with_slave $with_slave_login"); + cmp_ok( $result->return_code, "==", 0, "Slaves okay" ); + + $result = NPTest->testCmd("./check_mysql -S -H $with_slave $with_slave_login -w 60"); + cmp_ok( $result->return_code, '==', 0, 'Slaves are not > 60 seconds behind'); + + $result = NPTest->testCmd("./check_mysql -S -H $with_slave $with_slave_login -w 60:"); + cmp_ok( $result->return_code, '==', 1, 'Alert warning if < 60 seconds behind'); + like( $result->output, "/^SLOW_SLAVE WARNING:/", "Output okay"); +} diff --git a/plugins/t/check_mysql_query.t b/plugins/t/check_mysql_query.t new file mode 100644 index 0000000..c30245b --- /dev/null +++ b/plugins/t/check_mysql_query.t @@ -0,0 +1,58 @@ +#! /usr/bin/perl -w -I .. +# +# MySQL Database Server Tests via check_mysql +# +# +# +# These are the database permissions required for this test: +# GRANT SELECT ON $db.* TO $user@$host IDENTIFIED BY '$password'; +# Check with: +# mysql -u$user -p$password -h$host $db + +use strict; +use Test::More; +use NPTest; + +use vars qw($tests); + +plan skip_all => "check_mysql_query not compiled" unless (-x "check_mysql_query"); + +my $mysqlserver = getTestParameter("NP_MYSQL_SERVER", "A MySQL Server with no slaves setup"); +my $mysql_login_details = getTestParameter("NP_MYSQL_LOGIN_DETAILS", "Command line parameters to specify login access", "-u user -ppw -d db"); +my $result; + +if (! $mysqlserver) { + plan skip_all => "No mysql server defined"; +} else { + plan tests => 13; +} + +$result = NPTest->testCmd("./check_mysql_query -q 'SELECT 1+1' -H $mysqlserver $mysql_login_details"); +cmp_ok( $result->return_code, '==', 0, "Can run query"); + +$result = NPTest->testCmd("./check_mysql_query -H $mysqlserver $mysql_login_details"); +cmp_ok( $result->return_code, '==', 3, "Missing query parameter"); +like( $result->output, "/Must specify a SQL query to run/", "Missing query error message"); + +$result = NPTest->testCmd("./check_mysql_query -q 'SELECT 1+1' -H $mysqlserver -u dummy -d mysql"); +cmp_ok( $result->return_code, '==', 2, "Login failure"); +like( $result->output, "/Access denied for user /", "Expected login failure message"); + +$result = NPTest->testCmd("./check_mysql_query -q 'SELECT PI()' -w 3 -c 4 -H $mysqlserver $mysql_login_details"); +cmp_ok( $result->return_code, '==', 1, "Got warning"); + +$result = NPTest->testCmd("./check_mysql_query -q 'SELECT PI()*2' -w 3 -c 4 -H $mysqlserver $mysql_login_details"); +cmp_ok( $result->return_code, '==', 2, "Got critical"); + +$result = NPTest->testCmd("./check_mysql_query -q 'SELECT * FROM adsf' -H $mysqlserver $mysql_login_details"); +cmp_ok( $result->return_code, '==', 2, "Bad query"); +like( $result->output, "/Error with query/", "Bad query error message"); + +$result = NPTest->testCmd("./check_mysql_query -q 'SHOW VARIABLES LIKE \"bob\"' -H $mysqlserver $mysql_login_details"); +cmp_ok( $result->return_code, '==', 1, "No rows"); +like( $result->output, "/No rows returned/", "No rows error message"); + +$result = NPTest->testCmd("./check_mysql_query -q 'SHOW VARIABLES' -H $mysqlserver $mysql_login_details"); +cmp_ok( $result->return_code, '==', 2, "Data not numeric"); +like( $result->output, "/Is not a numeric/", "Data not numeric error message"); + diff --git a/plugins/t/check_nagios.nagios1.status.log b/plugins/t/check_nagios.nagios1.status.log new file mode 100644 index 0000000..64d9ce7 --- /dev/null +++ b/plugins/t/check_nagios.nagios1.status.log @@ -0,0 +1,5 @@ +# Nagios 1.2 Status File +[1133537544] PROGRAM;1133537484;21980;1;1133537534;0;1;1;1;1;0;1;1;1 +[1133537544] HOST;ADSL;PENDING;0;0;0;0;0;0;0;0;1;1;1;1;0;0.0;0;1;1;(Not enough data to determine host status yet) +[1133537544] HOST;Internet;UP;1133537486;1132135282;0;1402203;0;0;0;0;1;1;1;1;0;0.00;0;1;1;(Host assumed to be up) +[1133537544] SERVICE;Internet;TCP/IP;OK;1/3;HARD;1133537486;1133537786;ACTIVE;1;1;1;1132135282;0;OK;1402203;0;0;0;0;0;1;0;4;1;0;0.00;0;1;1;0;PING OK - Packet loss = 0%, RTA = 0.09 ms diff --git a/plugins/t/check_nagios.nagios2.status.dat b/plugins/t/check_nagios.nagios2.status.dat new file mode 100644 index 0000000..6f62bac --- /dev/null +++ b/plugins/t/check_nagios.nagios2.status.dat @@ -0,0 +1,127 @@ +######################################## +# NAGIOS STATUS FILE +# +# THIS FILE IS AUTOMATICALLY GENERATED +# BY NAGIOS. DO NOT MODIFY THIS FILE! +######################################## + +info { + created=1133537302 + version=2.0b5 + } + +program { + modified_host_attributes=0 + modified_service_attributes=0 + nagios_pid=2750 + daemon_mode=1 + program_start=1133537167 + last_command_check=1133537297 + last_log_rotation=0 + enable_notifications=1 + active_service_checks_enabled=1 + passive_service_checks_enabled=1 + active_host_checks_enabled=1 + passive_host_checks_enabled=1 + enable_event_handlers=1 + obsess_over_services=0 + obsess_over_hosts=0 + check_service_freshness=1 + check_host_freshness=0 + enable_flap_detection=1 + enable_failure_prediction=1 + process_performance_data=0 + global_host_event_handler= + global_service_event_handler= + } + +host { + host_name=ADSL-derby-office + modified_attributes=0 + check_command=check_host_alive_ping + event_handler= + has_been_checked=0 + should_be_scheduled=0 + check_execution_time=0.000 + check_latency=0.000 + check_type=0 + current_state=0 + last_hard_state=0 + plugin_output= + performance_data= + last_check=0 + next_check=0 + current_attempt=1 + max_attempts=3 + state_type=1 + last_state_change=0 + last_hard_state_change=0 + last_time_up=0 + last_time_down=0 + last_time_unreachable=0 + last_notification=0 + next_notification=0 + no_more_notifications=0 + current_notification_number=0 + notifications_enabled=1 + problem_has_been_acknowledged=0 + acknowledgement_type=0 + active_checks_enabled=1 + passive_checks_enabled=1 + event_handler_enabled=1 + flap_detection_enabled=1 + failure_prediction_enabled=1 + process_performance_data=1 + obsess_over_host=1 + last_update=1133537302 + is_flapping=0 + percent_state_change=0.00 + scheduled_downtime_depth=0 + } + +service { + host_name=ADSL-derby-office + service_description=TCP/IP + modified_attributes=0 + check_command=host5_service23_check_ping + event_handler= + has_been_checked=0 + should_be_scheduled=1 + check_execution_time=0.000 + check_latency=0.000 + check_type=0 + current_state=0 + last_hard_state=0 + current_attempt=1 + max_attempts=3 + state_type=1 + last_state_change=0 + last_hard_state_change=0 + last_time_ok=0 + last_time_warning=0 + last_time_unknown=0 + last_time_critical=0 + plugin_output=(Service assumed to be ok) + performance_data= + last_check=0 + next_check=1133537317 + current_notification_number=0 + last_notification=0 + next_notification=0 + no_more_notifications=0 + notifications_enabled=0 + active_checks_enabled=1 + passive_checks_enabled=1 + event_handler_enabled=1 + problem_has_been_acknowledged=0 + acknowledgement_type=0 + flap_detection_enabled=1 + failure_prediction_enabled=1 + process_performance_data=1 + obsess_over_service=0 + last_update=1133537302 + is_flapping=0 + percent_state_change=0.00 + scheduled_downtime_depth=0 + } + diff --git a/plugins/t/check_nagios.t b/plugins/t/check_nagios.t new file mode 100644 index 0000000..f38f5e9 --- /dev/null +++ b/plugins/t/check_nagios.t @@ -0,0 +1,92 @@ +#! /usr/bin/perl -w -I .. +# +# check_nagios tests +# +# + +use strict; +use Test::More; +use NPTest; + +if (`uname -s` eq "SunOS\n") { + plan skip_all => "Ignoring tests on solaris because of pst3"; +} else { + plan tests => 13; +} + +my $successOutput = '/^NAGIOS OK: /'; +my $warningOutput = '/^NAGIOS WARNING: /'; +my $failureOutput = '/^NAGIOS CRITICAL: /'; + +my $nagios1 = "t/check_nagios.nagios1.status.log"; +my $nagios2 = "t/check_nagios.nagios2.status.dat"; + +my $result; + +# Did use init, but MacOSX 10.4 replaces init with launchd +# Alternative is to insist that nagios is running to run this test +# Reasonable to expect cron because build servers will +# invoke cron to run a build +my $procname = "cron"; + +$result = NPTest->testCmd( + "./check_nagios -F $nagios1 -e 5 -C $procname" + ); +cmp_ok( $result->return_code, '==', 1, "Log over 5 minutes old" ); +like ( $result->output, $warningOutput, "Output for warning correct" ); + +my $now = time; +# This substitution is dependent on the testcase +system( "perl -pe 's/1133537544/$now/' $nagios1 > $nagios1.tmp" ) == 0 or die "Problem with munging $nagios1"; + +$result = NPTest->testCmd( + "./check_nagios -F $nagios1.tmp -e 1 -C $procname" + ); +cmp_ok( $result->return_code, "==", 0, "Log up to date" ); +like ( $result->output, $successOutput, "Output for success correct" ); + +my $later = $now - 61; +system( "perl -pe 's/1133537544/$later/' $nagios1 > $nagios1.tmp" ) == 0 or die "Problem with munging $nagios1"; + +$result = NPTest->testCmd( + "./check_nagios -F $nagios1.tmp -e 1 -C $procname" + ); +cmp_ok( $result->return_code, "==", 1, "Log correctly seen as over 1 minute old" ); +my ($age) = ($_ = $result->output) =~ /status log updated (\d+) seconds ago/; +like( $age, '/^6[0-9]$/', "Log correctly seen as between 60-69 seconds old" ); + +$result = NPTest->testCmd( + "./check_nagios -F $nagios1.tmp -e 5 -C unlikely_command_string" + ); +cmp_ok( $result->return_code, "==", 2, "Nagios command not found" ); +like ( $result->output, $failureOutput, "Output for failure correct" ); + +$result = NPTest->testCmd( + "./check_nagios -F $nagios2 -e 5 -C $procname" + ); +cmp_ok( $result->return_code, "==", 1, "Nagios2 for logfile over 5 mins old" ); + +$now = time; +system( "perl -pe 's/1133537302/$now/' $nagios2 > $nagios2.tmp" ) == 0 or die "Problem with munging $nagios2"; + +$result = NPTest->testCmd( + "./check_nagios -F $nagios2.tmp -e 1 -C $procname" + ); +cmp_ok( $result->return_code, "==", 0, "Nagios2 log up to date" ); + +$later = $now - 61; +system( "perl -pe 's/1133537302/$later/' $nagios2 > $nagios2.tmp" ) == 0 or die "Problem with munging $nagios2"; + +$result = NPTest->testCmd( + "./check_nagios -F $nagios2.tmp -e 1 -C $procname" + ); +cmp_ok( $result->return_code, "==", 1, "Nagios2 log correctly seen as over 1 minute old" ); +($age) = ($_ = $result->output) =~ /status log updated (\d+) seconds ago/; +like( $age, '/^6[0-9]$/', "Log correctly seen as between 60-69 seconds old" ); + +$result = NPTest->testCmd( + "./check_nagios -F t/check_nagios.t -e 1 -C $procname" + ); +cmp_ok( $result->return_code, "==", 2, "Invalid log file" ); + + diff --git a/plugins/t/check_ntp.t b/plugins/t/check_ntp.t new file mode 100644 index 0000000..b8fc8fd --- /dev/null +++ b/plugins/t/check_ntp.t @@ -0,0 +1,110 @@ +#! /usr/bin/perl -w -I .. +# +# Testing NTP +# +# + +use strict; +use Test::More; +use NPTest; + +my @PLUGINS1 = ('check_ntp', 'check_ntp_peer', 'check_ntp_time'); +my @PLUGINS2 = ('check_ntp_peer'); + +plan tests => (12 * scalar(@PLUGINS1)) + (6 * scalar(@PLUGINS2)); + +my $res; + +my $ntp_service = getTestParameter( "NP_GOOD_NTP_SERVICE", + "A host providing NTP service", + "pool.ntp.org"); + +my $no_ntp_service = getTestParameter( "NP_NO_NTP_SERVICE", + "A host NOT providing the NTP service", + "localhost" ); + +my $host_nonresponsive = getTestParameter( "NP_HOST_NONRESPONSIVE", + "The hostname of system not responsive to network requests", + "10.0.0.1" ); + +my $hostname_invalid = getTestParameter( "NP_HOSTNAME_INVALID", + "An invalid (not known to DNS) hostname", + "nosuchhost"); + +my $ntp_okmatch1 = '/^NTP\sOK:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs/'; +my $ntp_warnmatch1 = '/^NTP\sWARNING:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs/'; +my $ntp_critmatch1 = '/^NTP\sCRITICAL:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs/'; +my $ntp_okmatch2 = '/^NTP\sOK:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2},\struechimers=[0-9]+/'; +my $ntp_warnmatch2 = '/^NTP\sWARNING:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2}\s\(WARNING\),\struechimers=[0-9]+/'; +my $ntp_critmatch2 = '/^NTP\sCRITICAL:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+\s\(CRITICAL\),\sstratum=[0-9]{1,2},\struechimers=[0-9]+/'; +my $ntp_noresponse = '/^(CRITICAL - Socket timeout after 3 seconds)|(NTP CRITICAL: No response from NTP server)$/'; +my $ntp_nosuchhost = '/^check_ntp.*: Invalid hostname/address - ' . $hostname_invalid . '/'; + + +foreach my $plugin (@PLUGINS1) { + SKIP: { + skip "No NTP server defined", 6 unless $ntp_service; + $res = NPTest->testCmd( + "./$plugin -H $ntp_service -w 1000 -c 2000" + ); + cmp_ok( $res->return_code, '==', 0, "$plugin: Good NTP result (simple check)" ); + like( $res->output, $ntp_okmatch1, "$plugin: Output match OK (simple check)" ); + + $res = NPTest->testCmd( + "./$plugin -H $ntp_service -w 1000: -c 2000" + ); + cmp_ok( $res->return_code, '==', 1, "$plugin: Warning NTP result (simple check)" ); + like( $res->output, $ntp_warnmatch1, "$plugin: Output match WARNING (simple check)" ); + + $res = NPTest->testCmd( + "./$plugin -H $ntp_service -w 1000 -c 2000:" + ); + cmp_ok( $res->return_code, '==', 2, "$plugin: Critical NTP result (simple check)" ); + like( $res->output, $ntp_critmatch1, "$plugin: Output match CRITICAL (simple check)" ); + } + + SKIP: { + skip "No bad NTP server defined", 1 unless $no_ntp_service; + $res = NPTest->testCmd( + "./$plugin -H $no_ntp_service -t 3" + ); + cmp_ok( $res->return_code, '==', 2, "$plugin: No NTP service" ); + like( $res->output, $ntp_noresponse, "$plugin: Output match no NTP service" ); + } + + $res = NPTest->testCmd( + "./$plugin -H $host_nonresponsive -t 3" + ); + cmp_ok( $res->return_code, '==', 2, "$plugin: Server not responding" ); + like( $res->output, $ntp_noresponse, "$plugin: Output match non-responsive" ); + + $res = NPTest->testCmd( + "./$plugin -H $hostname_invalid" + ); + cmp_ok( $res->return_code, '==', 3, "$plugin: Invalid hostname/address" ); + like( $res->output, $ntp_nosuchhost, "$plugin: Output match invalid hostname/address" ); + +} + +foreach my $plugin (@PLUGINS2) { + SKIP: { + skip "No NTP server defined", 6 unless $ntp_service; + $res = NPTest->testCmd( + "./$plugin -H $ntp_service -w 1000 -c 2000 -W 20 -C 21 -j 100000 -k 200000 -m 1: -n 0:" + ); + cmp_ok( $res->return_code, '==', 0, "$plugin: Good NTP result with jitter, stratum, and truechimers check" ); + like( $res->output, $ntp_okmatch2, "$plugin: Output match OK with jitter, stratum, and truechimers" ); + + $res = NPTest->testCmd( + "./$plugin -H $ntp_service -w 1000 -c 2000 -W \\~:-1 -C 21 -j 100000 -k 200000 -m 1: -n 0:" + ); + cmp_ok( $res->return_code, '==', 1, "$plugin: Warning NTP result with jitter, stratum, and truechimers check" ); + like( $res->output, $ntp_warnmatch2, "$plugin: Output match WARNING with jitter, stratum, and truechimers" ); + + $res = NPTest->testCmd( + "./$plugin -H $ntp_service -w 1000 -c 2000 -W 20 -C 21 -j 100000 -k \\~:-1 -m 1: -n 0:" + ); + cmp_ok( $res->return_code, '==', 2, "$plugin: Critical NTP result with jitter, stratum, and truechimers check" ); + like( $res->output, $ntp_critmatch2, "$plugin: Output match CRITICAL with jitter, stratum, and truechimers" ); + } +} diff --git a/plugins/t/check_ping.t b/plugins/t/check_ping.t new file mode 100644 index 0000000..1604f8f --- /dev/null +++ b/plugins/t/check_ping.t @@ -0,0 +1,109 @@ +#! /usr/bin/perl -w -I .. +# +# Ping Response Tests via check_ping +# +# + +use strict; +use Test::More; +use NPTest; + +plan tests => 20; + +my $successOutput = '/PING (ok|OK) - Packet loss = +[0-9]{1,2}\%, +RTA = [\.0-9]+ ms/'; +my $failureOutput = '/Packet loss = +[0-9]{1,2}\%, +RTA = [\.0-9]+ ms/'; + +my $host_responsive = getTestParameter( "NP_HOST_RESPONSIVE", + "The hostname of system responsive to network requests", + "localhost" ); + +my $host_nonresponsive = getTestParameter( "NP_HOST_NONRESPONSIVE", + "The hostname of system not responsive to network requests", + "10.0.0.1" ); + +my $hostname_invalid = getTestParameter( "NP_HOSTNAME_INVALID", + "An invalid (not known to DNS) hostname", + "nosuchhost" ); + +my $res; + +$res = NPTest->testCmd( + "./check_ping -H $host_responsive -w 10,100% -c 10,100% -p 1" + ); +is( $res->return_code, 0, "Syntax ok" ); +like( $res->output, $successOutput, "Output OK" ); + +$res = NPTest->testCmd( + "./check_ping -H $host_responsive -w 0,0% -c 10,100% -p 1" + ); +is( $res->return_code, 1, "Syntax ok, with forced warning" ); +like( $res->output, $failureOutput, "Output OK" ); + +$res = NPTest->testCmd( + "./check_ping -H $host_responsive -w 0,0% -c 0,0% -p 1" + ); +is( $res->return_code, 2, "Syntax ok, with forced critical" ); +like( $res->output, $failureOutput, "Output OK" ); + +$res = NPTest->testCmd( + "./check_ping $host_responsive 100 100 1000 1000 -p 1" + ); +is( $res->return_code, 0, "Old syntax ok" ); +like( $res->output, $successOutput, "Output OK" ); + +$res = NPTest->testCmd( + "./check_ping $host_responsive 0 0 0 0 -p 1" + ); +is( $res->return_code, 2, "Old syntax, with forced critical" ); +like( $res->output, $failureOutput, "Output OK" ); + + +# check_ping results will depend on whether the ping command discovered by +# ./configure has a timeout option. If it does, then the timeout will +# be set, so check_ping will always get a response. If it doesn't +# then check_ping will timeout. We do 2 tests for check_ping's timeout +# - 1 second +# - 15 seconds +# The latter should be higher than normal ping timeouts, so should always give a packet loss result +open(F, "../config.h") or die "Cannot open ../config.h"; +@_ = grep /define PING_HAS_TIMEOUT 1|define PING_PACKETS_FIRST 1/, <F>; +my $has_timeout; +$has_timeout = 1 if (scalar @_ == 2); # Need both defined +close F; +$res = NPTest->testCmd( + "./check_ping -H $host_nonresponsive -w 10,100% -c 10,100% -p 1 -t 1" + ); +is( $res->return_code, 2, "Timeout 1 second - host nonresponsive" ); +if ($has_timeout) { + like( $res->output, '/100%/', "Error contains '100%' string (for 100% packet loss)" ); +} else { + like( $res->output, '/timed out/', "Error contains 'timed out' string" ); +} + +$res = NPTest->testCmd( + "./check_ping -H $host_nonresponsive -w 10,100% -c 10,100% -p 1 -t 15" + ); +is( $res->return_code, 2, "Timeout 15 seconds - host nonresponsive" ); +like( $res->output, '/100%/', "Error contains '100%' string (for 100% packet loss)" ); + + + + +$res = NPTest->testCmd( + "./check_ping $host_nonresponsive -p 1 -t 15 100 100 1000 10000" + ); +is( $res->return_code, 2, "Old syntax: Timeout - host nonresponsive" ); +like( $res->output, '/100%/', "Error contains '100%' string (for 100% packet loss)" ); + +$res = NPTest->testCmd( + "./check_ping $hostname_invalid 0 0 0 0 -p 1 -t 1" + ); +is( $res->return_code, 3, "Invalid hostname" ); +like( $res->output, '/invalid hostname/i', "Error contains 'invalid hostname' string"); + +$res = NPTest->testCmd( + "./check_ping -w 100,10% -c 200,20%" + ); +is( $res->return_code, 3, "No hostname" ); +like( $res->output, '/You must specify a server address or host name/', "Output with appropriate error message"); + diff --git a/plugins/t/check_pop.t b/plugins/t/check_pop.t new file mode 100644 index 0000000..62352fd --- /dev/null +++ b/plugins/t/check_pop.t @@ -0,0 +1,55 @@ +#! /usr/bin/perl -w -I .. +# +# Post Office Protocol (POP) Server Tests via check_pop +# +# + +use strict; +use Test::More; +use NPTest; + +plan tests => 5; + +my $host_tcp_smtp = getTestParameter( + "NP_HOST_TCP_SMTP", + "A host providing an STMP Service (a mail server)", + "mailhost" + ); + +my $host_tcp_pop = getTestParameter( + "NP_HOST_TCP_POP", + "A host providing a POP Service (a mail server)", + $host_tcp_smtp + ); + +my $host_nonresponsive = getTestParameter( + "NP_HOST_NONRESPONSIVE", + "The hostname of system not responsive to network requests", + "10.0.0.1", + ); + +my $hostname_invalid = getTestParameter( + "NP_HOSTNAME_INVALID", + "An invalid (not known to DNS) hostname", + "nosuchhost", + ); + +my %exceptions = ( 2 => "No POP Server present?" ); + +my $t; +my $res; + +$res = NPTest->testCmd( "./check_pop $host_tcp_pop" ); +cmp_ok( $res->return_code, '==', 0, "POP server ok"); + +$res = NPTest->testCmd( "./check_pop -H $host_tcp_pop -p 110 -w 9 -c 9 -t 10 -e '+OK'"); +cmp_ok( $res->return_code, '==', 0, "POP server returned +OK"); + +$res = NPTest->testCmd( "./check_pop $host_tcp_pop -p 110 -wt 9 -ct 9 -to 10 -e '+OK'"); +cmp_ok( $res->return_code, '==', 0, "Old syntax"); + +$res = NPTest->testCmd( "./check_pop $host_nonresponsive" ); +cmp_ok( $res->return_code, '==', 2, "Non responsive host"); + +$res = NPTest->testCmd( "./check_pop $hostname_invalid" ); +cmp_ok( $res->return_code, '==', 2, "Invalid host"); diff --git a/plugins/t/check_procs.t b/plugins/t/check_procs.t new file mode 100644 index 0000000..abe7284 --- /dev/null +++ b/plugins/t/check_procs.t @@ -0,0 +1,49 @@ +#! /usr/bin/perl -w -I .. +# +# Process Tests via check_procs +# +# + +use strict; +use Test::More; +use NPTest; + +my $t; + +if (`uname -s` eq "SunOS\n" && ! -x "/usr/local/nagios/libexec/pst3") { + plan skip_all => "Ignoring tests on solaris because of pst3"; +} else { + plan tests => 14; +} + +my $result; + +$result = NPTest->testCmd( "./check_procs -w 100000 -c 100000" ); +is( $result->return_code, 0, "Checking less than 10000 processes" ); +like( $result->output, '/^PROCS OK: [0-9]+ process(es)? | procs=[0-9]+;100000;100000;0;$/', "Output correct" ); + +$result = NPTest->testCmd( "./check_procs -w 100000 -c 100000 -s Z" ); +is( $result->return_code, 0, "Checking less than 100000 zombie processes" ); +like( $result->output, '/^PROCS OK: [0-9]+ process(es)? with /', "Output correct" ); + +if(fork() == 0) { exec("sleep 7"); } else { sleep(1) } # fork a test process in child and give child time to fork in parent +$result = NPTest->testCmd( "./check_procs -a 'sleep 7'" ); +is( $result->return_code, 0, "Parent process is ignored" ); +like( $result->output, '/^PROCS OK: 1 process?/', "Output correct" ); + +$result = NPTest->testCmd( "./check_procs -w 0 -c 100000" ); +is( $result->return_code, 1, "Checking warning if processes > 0" ); +like( $result->output, '/^PROCS WARNING: [0-9]+ process(es)? | procs=[0-9]+;0;100000;0;$/', "Output correct" ); + +$result = NPTest->testCmd( "./check_procs -w 0 -c 0" ); +is( $result->return_code, 2, "Checking critical if processes > 0" ); +like( $result->output, '/^PROCS CRITICAL: [0-9]+ process(es)? | procs=[0-9]+;0;0;0;$/', "Output correct" ); + +$result = NPTest->testCmd( "./check_procs -w 0 -c 0 -s Ss" ); +is( $result->return_code, 2, "Checking critical if sleeping processes" ); +like( $result->output, '/^PROCS CRITICAL: [0-9]+ process(es)? with /', "Output correct" ); + +$result = NPTest->testCmd( "./check_procs -w 0 -c 100000 -p 1" ); +is( $result->return_code, 1, "Checking warning for processes by parentid = 1" ); +like( $result->output, '/^PROCS WARNING: [0-9]+ process(es)? with PPID = 1/', "Output correct" ); + diff --git a/plugins/t/check_smtp.t b/plugins/t/check_smtp.t new file mode 100644 index 0000000..1a1ebe3 --- /dev/null +++ b/plugins/t/check_smtp.t @@ -0,0 +1,84 @@ +#! /usr/bin/perl -w -I .. +# +# Simple Mail Transfer Protocol (SMTP) Test via check_smtp +# +# + +use strict; +use Test::More; +use NPTest; + +my $host_tcp_smtp = getTestParameter( "NP_HOST_TCP_SMTP", + "A host providing an SMTP Service (a mail server)", "mailhost"); +my $host_tcp_smtp_starttls = getTestParameter( "NP_HOST_TCP_SMTP_STARTTLS", + "A host providing SMTP with STARTTLS", $host_tcp_smtp); +my $host_tcp_smtp_nostarttls = getTestParameter( "NP_HOST_TCP_SMTP_NOSTARTTLS", + "A host providing SMTP without STARTTLS", ""); +my $host_tcp_smtp_tls = getTestParameter( "NP_HOST_TCP_SMTP_TLS", + "A host providing SMTP with TLS", $host_tcp_smtp); + +my $host_nonresponsive = getTestParameter( "NP_HOST_NONRESPONSIVE", + "The hostname of system not responsive to network requests", "10.0.0.1" ); + +my $hostname_invalid = getTestParameter( "NP_HOSTNAME_INVALID", + "An invalid (not known to DNS) hostname", "nosuchhost" ); +my $res; + +plan tests => 16; + +SKIP: { + skip "No SMTP server defined", 4 unless $host_tcp_smtp; + $res = NPTest->testCmd( "./check_smtp $host_tcp_smtp" ); + is ($res->return_code, 0, "OK"); + + $res = NPTest->testCmd( "./check_smtp -H $host_tcp_smtp -p 25 -w 9 -c 9 -t 10 -e 220" ); + is ($res->return_code, 0, "OK, within 9 second response"); + + $res = NPTest->testCmd( "./check_smtp -H $host_tcp_smtp -p 25 -wt 9 -ct 9 -to 10 -e 220" ); + is ($res->return_code, 0, "OK, old syntax"); + + $res = NPTest->testCmd( "./check_smtp -H $host_tcp_smtp -e 221" ); + is ($res->return_code, 1, "WARNING - got correct error when expecting 221 instead of 220" ); + + TODO: { + local $TODO = "Output is over two lines"; + like ( $res->output, qr/^SMTP WARNING/, "Correct error message" ); + } + + $res = NPTest->testCmd( "./check_smtp -H $host_tcp_smtp --ssl -p 25" ); + is ($res->return_code, 2, "Check rc of connecting to $host_tcp_smtp with TLS on standard SMTP port" ); + like ($res->output, qr/^CRITICAL - Cannot make SSL connection\./, "Check output of connecting to $host_tcp_smtp with TLS on standard SMTP port"); +} + +SKIP: { + skip "No SMTP server with STARTTLS defined", 1 unless $host_tcp_smtp_starttls; + # SSL connection for STARTTLS + $res = NPTest->testCmd( "./check_smtp -H $host_tcp_smtp_starttls -p 25 -S" ); + is ($res->return_code, 0, "OK, with STARTTLS" ); +} + +SKIP: { + skip "No SMTP server without STARTTLS defined", 2 unless $host_tcp_smtp_nostarttls; + $res = NPTest->testCmd( "./check_smtp -H $host_tcp_smtp_nostarttls -p 25 -S" ); + is ($res->return_code, 1, "OK, got warning from server without STARTTLS"); + is ($res->output, "WARNING - TLS not supported by server", "Right error message" ); +} + +SKIP: { + skip "No SMTP server with TLS defined", 1 unless $host_tcp_smtp_tls; + $res = NPTest->testCmd( "./check_smtp -H $host_tcp_smtp_tls --ssl" ); + is ($res->return_code, 0, "Check rc of connecting to $host_tcp_smtp_tls with TLS" ); + like ($res->output, qr/^SMTP OK - /, "Check output of connecting to $host_tcp_smtp_tls with TLS" ); + + my $unused_port = 4465; + $res = NPTest->testCmd( "./check_smtp -H $host_tcp_smtp_tls -p $unused_port --ssl" ); + is ($res->return_code, 2, "Check rc of connecting to $host_tcp_smtp_tls with TLS on unused port $unused_port" ); + like ($res->output, qr/^connect to address $host_tcp_smtp_tls and port $unused_port: Connection refused/, "Check output of connecting to $host_tcp_smtp_tls with TLS on unused port $unused_port"); +} + +$res = NPTest->testCmd( "./check_smtp $host_nonresponsive" ); +is ($res->return_code, 2, "CRITICAL - host non responding" ); + +$res = NPTest->testCmd( "./check_smtp $hostname_invalid" ); +is ($res->return_code, 3, "UNKNOWN - hostname invalid" ); + diff --git a/plugins/t/check_snmp.t b/plugins/t/check_snmp.t new file mode 100644 index 0000000..576cc50 --- /dev/null +++ b/plugins/t/check_snmp.t @@ -0,0 +1,176 @@ +#! /usr/bin/perl -w -I .. +# +# Simple Network Management Protocol (SNMP) Test via check_snmp +# +# + +use strict; +use Test::More; +use NPTest; + +BEGIN { + plan skip_all => 'check_snmp is not compiled' unless -x "./check_snmp"; + plan tests => 63; +} + +my $res; + +my $host_snmp = getTestParameter("NP_HOST_SNMP", "A host providing an SNMP Service", "localhost"); +my $snmp_community = getTestParameter("NP_SNMP_COMMUNITY", "The SNMP Community string for SNMP Testing (assumes snmp v1)", "public"); +my $host_nonresponsive = getTestParameter("NP_HOST_NONRESPONSIVE", "The hostname of system not responsive to network requests", "10.0.0.1"); +my $hostname_invalid = getTestParameter("NP_HOSTNAME_INVALID", "An invalid (not known to DNS) hostname", "nosuchhost"); +my $user_snmp = getTestParameter("NP_SNMP_USER", "An SNMP user", "auth_md5"); + + +$res = NPTest->testCmd( "./check_snmp -t 1" ); +is( $res->return_code, 3, "No host name" ); +is( $res->output, "No host specified" ); + +$res = NPTest->testCmd( "./check_snmp -H fakehostname --ignore-mib-parsing-errors" ); +is( $res->return_code, 3, "No OIDs specified" ); +is( $res->output, "No OIDs specified" ); + +$res = NPTest->testCmd( "./check_snmp -H fakehost --ignore-mib-parsing-errors -o oids -P 3 -U not_a_user --seclevel=rubbish" ); +is( $res->return_code, 3, "Invalid seclevel" ); +like( $res->output, "/check_snmp: Invalid seclevel - rubbish/" ); + +$res = NPTest->testCmd( "./check_snmp -H fakehost --ignore-mib-parsing-errors -o oids -P 3c" ); +is( $res->return_code, 3, "Invalid protocol" ); +like( $res->output, "/check_snmp: Invalid SNMP version - 3c/" ); + +SKIP: { + skip "no snmp host defined", 50 if ( ! $host_snmp ); + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0 -w 1: -c 1:"); + cmp_ok( $res->return_code, '==', 0, "Exit OK when querying uptime" ); + like($res->output, '/^SNMP OK - (\d+)/', "String contains SNMP OK"); + $res->output =~ /^SNMP OK - (\d+)/; + my $value = $1; + cmp_ok( $value, ">", 0, "Got a time value" ); + like($res->perf_output, "/sysUpTime.*$1/", "Got perfdata with value '$1' in it"); + + + # some more threshold tests + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0 -c 1"); + cmp_ok( $res->return_code, '==', 2, "Threshold test -c 1" ); + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0 -c 1:"); + cmp_ok( $res->return_code, '==', 0, "Threshold test -c 1:" ); + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0 -c ~:1"); + cmp_ok( $res->return_code, '==', 2, "Threshold test -c ~:1" ); + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0 -c 1:10"); + cmp_ok( $res->return_code, '==', 2, "Threshold test -c 1:10" ); + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0 -c \@1:10"); + cmp_ok( $res->return_code, '==', 0, "Threshold test -c \@1:10" ); + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0 -c 10:1"); + cmp_ok( $res->return_code, '==', 0, "Threshold test -c 10:1" ); + + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o .1.3.6.1.2.1.1.3.0 -w 1: -c 1:"); + cmp_ok( $res->return_code, '==', 0, "Test with numeric OID (no mibs loaded)" ); + like($res->output, '/^SNMP OK - \d+/', "String contains SNMP OK"); + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysDescr.0"); + cmp_ok( $res->return_code, '==', 0, "Exit OK when querying sysDescr" ); + unlike($res->perf_output, '/sysDescr/', "Perfdata doesn't contain string values"); + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysDescr.0,system.sysDescr.0"); + cmp_ok( $res->return_code, '==', 0, "Exit OK when querying two string OIDs, comma-separated" ); + like($res->output, '/^SNMP OK - /', "String contains SNMP OK"); + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysDescr.0 -o system.sysDescr.0"); + cmp_ok( $res->return_code, '==', 0, "Exit OK when querying two string OIDs, repeated option" ); + like($res->output, '/^SNMP OK - /', "String contains SNMP OK"); + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w 1:1 -c 1:1"); + cmp_ok( $res->return_code, '==', 0, "Exit OK when querying hrSWRunIndex.1" ); + like($res->output, '/^SNMP OK - 1\s.*$/', "String fits SNMP OK and output format"); + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w 0 -c 1:"); + cmp_ok( $res->return_code, '==', 1, "Exit WARNING when querying hrSWRunIndex.1 and warn-th doesn't apply " ); + like($res->output, '/^SNMP WARNING - \*1\*\s.*$/', "String matches SNMP WARNING and output format"); + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w :0 -c 0"); + cmp_ok( $res->return_code, '==', 2, "Exit CRITICAL when querying hrSWRunIndex.1 and crit-th doesn't apply" ); + like($res->output, '/^SNMP CRITICAL - \*1\*\s.*$/', "String matches SNMP CRITICAL and output format"); + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o ifIndex.2,ifIndex.1 -w 1:2 -c 1:2"); + cmp_ok( $res->return_code, '==', 0, "Checking two OIDs at once" ); + like($res->output, "/^SNMP OK - 2 1/", "Got two values back" ); + like( $res->perf_output, "/ifIndex.2=2/", "Got 1st perf data" ); + like( $res->perf_output, "/ifIndex.1=1/", "Got 2nd perf data" ); + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o ifIndex.2,ifIndex.1 -w 1:2,1:2 -c 2:2,2:2"); + cmp_ok( $res->return_code, '==', 2, "Checking critical threshold is passed if any one value crosses" ); + like($res->output, "/^SNMP CRITICAL - 2 *1*/", "Got two values back" ); + like( $res->perf_output, "/ifIndex.2=2/", "Got 1st perf data" ); + like( $res->perf_output, "/ifIndex.1=1/", "Got 2nd perf data" ); + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o host.hrStorage.hrMemorySize.0,host.hrSystem.hrSystemProcesses.0 -w 1:,1: -c 1:,1:"); + cmp_ok( $res->return_code, '==', 0, "Exit OK when querying hrMemorySize and hrSystemProcesses"); + like($res->output, '/^SNMP OK - \d+ \d+/', "String contains hrMemorySize and hrSystemProcesses"); + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w \@:0 -c \@0"); + cmp_ok( $res->return_code, '==', 0, "Exit OK with inside-range thresholds"); + like($res->output, '/^SNMP OK - 1\s.*$/', "String matches SNMP OK and output format"); + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o enterprises.ucdavis.laTable.laEntry.laLoad.3"); + $res->output =~ m/^SNMP OK - (\d+\.\d{2})\s.*$/; + my $lower = $1 - 0.05; + my $higher = $1 + 0.05; + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o enterprises.ucdavis.laTable.laEntry.laLoad.3 -w $lower -c $higher"); + cmp_ok( $res->return_code, '==', 1, "Exit WARNING with fractionnal arguments"); + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0,host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w ,:0 -c ,:2"); + cmp_ok( $res->return_code, '==', 1, "Exit WARNING on 2nd threshold"); + like($res->output, '/^SNMP WARNING - Timeticks:\s\(\d+\)\s+(?:\d+ days?,\s+)?\d+:\d+:\d+\.\d+\s+\*1\*\s.*$/', "First OID returned as string, 2nd checked for thresholds"); + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w '' -c ''"); + cmp_ok( $res->return_code, '==', 0, "Empty thresholds doesn't crash"); + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o host.hrStorage.hrMemorySize.0,host.hrSystem.hrSystemProcesses.0 -w ,,1 -c ,,2"); + cmp_ok( $res->return_code, '==', 0, "Skipping first two thresholds on 2 OID check"); + like($res->output, '/^SNMP OK - \d+ \w+ \d+\s.*$/', "Skipping first two thresholds, result printed rather than parsed"); + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o host.hrStorage.hrMemorySize.0,host.hrSystem.hrSystemProcesses.0 -w ,, -c ,,"); + cmp_ok( $res->return_code, '==', 0, "Skipping all thresholds"); + like($res->output, '/^SNMP OK - \d+ \w+ \d+\s.*$/', "Skipping all thresholds, result printed rather than parsed"); + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0 -c 1000000000000: -u '1/100 sec'"); + cmp_ok( $res->return_code, '==', 2, "Timetick used as a threshold"); + like($res->output, '/^SNMP CRITICAL - \*\d+\* 1\/100 sec.*$/', "Timetick used as a threshold, parsed as numeric"); + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o system.sysUpTime.0"); + cmp_ok( $res->return_code, '==', 0, "Timetick used as a string"); + like($res->output, '/^SNMP OK - Timeticks:\s\(\d+\)\s+(?:\d+ days?,\s+)?\d+:\d+:\d+\.\d+\s.*$/', "Timetick used as a string, result printed rather than parsed"); + + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -C $snmp_community -o HOST-RESOURCES-MIB::hrSWRunName.1"); + cmp_ok( $res->return_code, '==', 0, "snmp response without datatype"); + like( $res->output, '/^SNMP OK - "(systemd|init)" \| $/', "snmp response without datatype" ); +} + +SKIP: { + skip "no SNMP user defined", 1 if ( ! $user_snmp ); + $res = NPTest->testCmd( "./check_snmp -H $host_snmp --ignore-mib-parsing-errors -o HOST-RESOURCES-MIB::hrSystemUptime.0 -P 3 -U $user_snmp -L noAuthNoPriv"); + like( $res->output, '/^SNMP OK - Timeticks:\s\(\d+\)\s+(?:\d+ days?,\s+)?\d+:\d+:\d+\.\d+\s.*$/', "noAuthNoPriv security level works properly" ); +} + +# These checks need a complete command line. An invalid community is used so +# the tests can run on hosts w/o snmp host/community in NPTest.cache. Execution will fail anyway +SKIP: { + skip "no non responsive host defined", 2 if ( ! $host_nonresponsive ); + $res = NPTest->testCmd( "./check_snmp -H $host_nonresponsive --ignore-mib-parsing-errors -C np_foobar -o system.sysUpTime.0 -w 1: -c 1:"); + cmp_ok( $res->return_code, '==', 2, "Exit CRITICAL with non responsive host" ); + like($res->output, '/Plugin timed out while executing system call/', "String matches timeout problem"); +} + +SKIP: { + skip "no non invalid host defined", 2 if ( ! $hostname_invalid ); + $res = NPTest->testCmd( "./check_snmp -H $hostname_invalid --ignore-mib-parsing-errors -C np_foobar -o system.sysUpTime.0 -w 1: -c 1:"); + cmp_ok( $res->return_code, '==', 3, "Exit UNKNOWN with non responsive host" ); + like($res->output, '/External command error: .*(nosuchhost|Name or service not known|Unknown host).*/s', "String matches invalid host"); +} diff --git a/plugins/t/check_ssh.t b/plugins/t/check_ssh.t new file mode 100644 index 0000000..a5cd23c --- /dev/null +++ b/plugins/t/check_ssh.t @@ -0,0 +1,41 @@ +#! /usr/bin/perl -w -I .. +# +# check_ssh tests +# +# + +use strict; +use Test::More; +use NPTest; + +# Required parameters +my $ssh_host = getTestParameter("NP_SSH_HOST", "A host providing SSH service", "localhost"); +my $host_nonresponsive = getTestParameter("NP_HOST_NONRESPONSIVE", "The hostname of system not responsive to network requests", "10.0.0.1" ); +my $hostname_invalid = getTestParameter("NP_HOSTNAME_INVALID", "An invalid (not known to DNS) hostname", "nosuchhost" ); + + +plan skip_all => "SSH_HOST must be defined" unless $ssh_host; +plan tests => 6; + + +my $result = NPTest->testCmd( + "./check_ssh -H $ssh_host" + ); +cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); +like($result->output, '/^SSH OK - /', "Status text if command returned none (OK)"); + + +$result = NPTest->testCmd( + "./check_ssh -H $host_nonresponsive -t 2" + ); +cmp_ok($result->return_code, '==', 2, "Exit with return code 0 (OK)"); +like($result->output, '/^CRITICAL - Socket timeout after 2 seconds/', "Status text if command returned none (OK)"); + + + +$result = NPTest->testCmd( + "./check_ssh -H $hostname_invalid -t 2" + ); +cmp_ok($result->return_code, '==', 3, "Exit with return code 0 (OK)"); +like($result->output, '/^check_ssh: Invalid hostname/', "Status text if command returned none (OK)"); + diff --git a/plugins/t/check_swap.t b/plugins/t/check_swap.t new file mode 100644 index 0000000..de9e0f0 --- /dev/null +++ b/plugins/t/check_swap.t @@ -0,0 +1,31 @@ +#! /usr/bin/perl -w -I .. +# +# Swap Space Tests via check_swap +# +# + +use strict; +use Test::More tests => 8; +use NPTest; + +my $successOutput = '/^SWAP OK - [0-9]+\% free \([0-9]+MB out of [0-9]+MB\)/'; +my $failureOutput = '/^SWAP CRITICAL - [0-9]+\% free \([0-9]+MB out of [0-9]+MB\)/'; +my $warnOutput = '/^SWAP WARNING - [0-9]+\% free \([0-9]+MB out of [0-9]+MB\)/'; + +my $result; + +$result = NPTest->testCmd( "./check_swap -w 1048576 -c 1048576" ); # 1 MB free +cmp_ok( $result->return_code, "==", 0, "At least 1MB free" ); +like( $result->output, $successOutput, "Right output" ); + +$result = NPTest->testCmd( "./check_swap -w 1% -c 1%" ); # 1% free +cmp_ok( $result->return_code, "==", 0, 'At least 1% free' ); +like( $result->output, $successOutput, "Right output" ); + +$result = NPTest->testCmd( "./check_swap -w 100% -c 100%" ); # 100% (always critical) +cmp_ok( $result->return_code, "==", 2, 'Get critical because not 100% free' ); +like( $result->output, $failureOutput, "Right output" ); + +$result = NPTest->testCmd( "./check_swap -w 100% -c 1%" ); # 100% (always warn) +cmp_ok( $result->return_code, "==", 1, 'Get warning because not 100% free' ); +like( $result->output, $warnOutput, "Right output" ); diff --git a/plugins/t/check_tcp.t b/plugins/t/check_tcp.t new file mode 100644 index 0000000..cb4de53 --- /dev/null +++ b/plugins/t/check_tcp.t @@ -0,0 +1,55 @@ +#! /usr/bin/perl -w -I .. +# +# TCP Connection Based Tests via check_tcp +# +# + +use strict; +use Test; + +use vars qw($tests $has_ipv6); +BEGIN { + use NPTest; + $has_ipv6 = NPTest::has_ipv6(); + $tests = $has_ipv6 ? 14 : 11; +} + + +my $host_tcp_http = getTestParameter("NP_HOST_TCP_HTTP", "A host providing the HTTP Service (a web server)", "localhost"); +my $host_tls_http = getTestParameter("NP_HOST_TLS_HTTP", "A host providing the HTTPS Service (a tls web server)", "localhost"); +my $host_nonresponsive = getTestParameter("NP_HOST_NONRESPONSIVE", "The hostname of system not responsive to network requests", "10.0.0.1"); +my $hostname_invalid = getTestParameter("NP_HOSTNAME_INVALID", "An invalid (not known to DNS) hostname", "nosuchhost"); +my $internet_access = getTestParameter("NP_INTERNET_ACCESS", "Is this system directly connected to the internet?", "yes"); + +my $successOutput = '/^TCP OK\s-\s+[0-9]?\.?[0-9]+ second response time on port [0-9]+/'; + +my $failedExpect = '/^TCP WARNING\s-\sUnexpected response from host/socket on port [0-9]+/'; + +my $t; + +$tests = $tests - 4 if $internet_access eq "no"; +plan tests => $tests; + +$t += checkCmd( "./check_tcp $host_tcp_http -p 80 -wt 300 -ct 600", 0, $successOutput ); +$t += checkCmd( "./check_tcp $host_tcp_http -p 81 -wt 0 -ct 0 -to 1", 2 ); # use invalid port for this test +$t += checkCmd( "./check_tcp $host_nonresponsive -p 80 -wt 0 -ct 0 -to 1", 2 ); +$t += checkCmd( "./check_tcp $hostname_invalid -p 80 -wt 0 -ct 0 -to 1", 2 ); +if($internet_access ne "no") { + $t += checkCmd( "./check_tcp -S -D 1 -H $host_tls_http -p 443", 0 ); + $t += checkCmd( "./check_tcp -S -D 9000,1 -H $host_tls_http -p 443", 1 ); + $t += checkCmd( "./check_tcp -S -D 9000 -H $host_tls_http -p 443", 1 ); + $t += checkCmd( "./check_tcp -S -D 9000,8999 -H $host_tls_http -p 443", 2 ); +} + +# Need the \r\n to make it more standards compliant with web servers. Need the various quotes +# so that perl doesn't interpret the \r\n and is passed onto command line correctly +$t += checkCmd( "./check_tcp $host_tcp_http -p 80 -E -s ".'"GET / HTTP/1.1\r\n\r\n"'." -e 'ThisShouldntMatch' -j", 1, $failedExpect ); + +# IPv6 checks +if($has_ipv6) { + $t += checkCmd( "./check_tcp $host_tcp_http -p 80 -wt 300 -ct 600 -6 ", 0, $successOutput ); + $t += checkCmd( "./check_tcp -6 -p 80 www.heise.de", 0 ); +} + +exit(0) if defined($Test::Harness::VERSION); +exit($tests - $t); diff --git a/plugins/t/check_time.t b/plugins/t/check_time.t new file mode 100644 index 0000000..92c2f89 --- /dev/null +++ b/plugins/t/check_time.t @@ -0,0 +1,36 @@ +#! /usr/bin/perl -w -I .. +# +# System Time Tests via check_time +# +# + +use strict; +use Test; +use NPTest; + +use vars qw($tests); +BEGIN {$tests = 8; plan tests => $tests} + +my $host_udp_time = getTestParameter("NP_HOST_UDP_TIME", "A host providing the UDP Time Service", "localhost"); +my $host_nonresponsive = getTestParameter("NP_HOST_NONRESPONSIVE", "The hostname of system not responsive to network requests", "10.0.0.1"); +my $hostname_invalid = getTestParameter("NP_HOSTNAME_INVALID", "An invalid (not known to DNS) hostname", "nosuchhost"); + +my $successOutput = '/^TIME OK - [0-9]+ second time difference/'; + +my %exceptions = ( 3 => "No time server present?"); + +my $t; + +# standard mode +$t += checkCmd( "./check_time -H $host_udp_time -w 999999,59 -c 999999,59 -t 60", 0, $successOutput, %exceptions ); +$t += checkCmd( "./check_time -H $host_udp_time -w 999999 -W 59 -c 999999 -C 59 -t 60", 0, $successOutput, %exceptions ); + +# reverse compatibility mode +$t += checkCmd( "./check_time $host_udp_time -wt 59 -ct 59 -cd 999999 -wd 999999 -to 60", 0, $successOutput, %exceptions ); + +# failure mode +$t += checkCmd( "./check_time -H $host_nonresponsive -t 1", 2 ); +$t += checkCmd( "./check_time -H $hostname_invalid -t 1", 3 ); + +exit(0) if defined($Test::Harness::VERSION); +exit($tests - $t); diff --git a/plugins/t/check_udp.t b/plugins/t/check_udp.t new file mode 100644 index 0000000..6c47d09 --- /dev/null +++ b/plugins/t/check_udp.t @@ -0,0 +1,71 @@ +#! /usr/bin/perl -w -I .. +# +# UDP Connection Based Tests via check_udp +# +# + +use strict; +use Test::More; +use NPTest; + +my $res; + +alarm(120); # make sure tests don't hang + +plan tests => 14; + +$res = NPTest->testCmd( "./check_udp -H localhost -p 3333" ); +cmp_ok( $res->return_code, '==', 3, "Need send/expect string"); +like ( $res->output, '/With UDP checks, a send/expect string must be specified./', "Output OK"); + +$res = NPTest->testCmd( "./check_udp -H localhost -p 3333 -s send" ); +cmp_ok( $res->return_code, '==', 3, "Need expect string"); +like ( $res->output, '/With UDP checks, a send/expect string must be specified./', "Output OK"); + +$res = NPTest->testCmd( "./check_udp -H localhost -p 3333 -e expect" ); +cmp_ok( $res->return_code, '==', 3, "Need send string"); +like ( $res->output, '/With UDP checks, a send/expect string must be specified./', "Output OK"); + +$res = NPTest->testCmd( "./check_udp -H localhost -p 3333 -s foo -e bar" ); +cmp_ok( $res->return_code, '==', 2, "Errors correctly because no udp service running" ); +like ( $res->output, '/No data received from host/', "Output OK"); + +my $nc; +if(system("which nc.traditional >/dev/null 2>&1") == 0) { + $nc = 'nc.traditional -w 3 -l -u -p 3333'; +} +elsif(system("which nc >/dev/null 2>&1") == 0) { + $nc = 'nc -w 3 -l -u -4 localhost 3333'; +} +elsif(system("which netcat >/dev/null 2>&1") == 0) { + $nc = 'netcat -w 3 -l -u -p 3333'; +} + +SKIP: { + skip "solaris netcat does not listen to udp", 6 if $^O eq 'solaris'; + skip "No netcat available", 6 unless $nc; + open (NC, "echo 'barbar' | $nc |"); + sleep 1; + $res = NPTest->testCmd( "./check_udp -H localhost -p 3333 -s '' -e barbar -4" ); + cmp_ok( $res->return_code, '==', 0, "Got barbar response back" ); + like ( $res->output, '/\[barbar\]/', "Output OK"); + close NC; + + # Start up a udp server listening on port 3333, quit after 3 seconds + # Otherwise will hang at close + my $pid = open(NC, "$nc </dev/null |"); + sleep 1; # Allow nc to startup + + my $start = time; + $res = NPTest->testCmd( "./check_udp -H localhost -p 3333 -s foofoo -e barbar -t 5 -4" ); + my $duration = time - $start; + cmp_ok( $res->return_code, '==', '2', "Hung waiting for response"); + like ( $res->output, '/Socket timeout after 5 seconds/', "Timeout message"); + like ( $duration, '/^[56]$/', "Timeout after 5 (possibly 6) seconds"); + my $read_nc = <NC> || ''; + close NC; + cmp_ok( $read_nc, 'eq', "foofoo", "Data received correctly" ); +} + + +alarm(0); # disable alarm diff --git a/plugins/t/check_users.t b/plugins/t/check_users.t new file mode 100644 index 0000000..9ebc2fc --- /dev/null +++ b/plugins/t/check_users.t @@ -0,0 +1,30 @@ +#! /usr/bin/perl -w -I .. +# +# Logged in Users Tests via check_users +# +# Trick: This check requires at least 1 user logged in. These commands should +# leave a session open forever in the background: +# +# $ ssh -tt localhost </dev/null >/dev/null 2>/dev/null & +# $ disown %1 + +use strict; +use Test; +use NPTest; + +use vars qw($tests); +BEGIN {$tests = 8; plan tests => $tests} + +my $successOutput = '/^USERS OK - [0-9]+ users currently logged in/'; +my $failureOutput = '/^USERS CRITICAL - [0-9]+ users currently logged in/'; + +my $t; + +$t += checkCmd( "./check_users 1000 1000", 0, $successOutput ); +$t += checkCmd( "./check_users 0 0", 2, $failureOutput ); +$t += checkCmd( "./check_users -w 0:1000 -c 0:1000", 0, $successOutput ); +$t += checkCmd( "./check_users -w 0:0 -c 0:0", 2, $failureOutput ); + +exit(0) if defined($Test::Harness::VERSION); +exit($tests - $t); + diff --git a/plugins/t/negate.t b/plugins/t/negate.t new file mode 100644 index 0000000..5ec1c84 --- /dev/null +++ b/plugins/t/negate.t @@ -0,0 +1,90 @@ +#! /usr/bin/perl -w -I .. +# +# negate checks +# Need check_dummy to work for testing +# + +use strict; +use Test::More; +use Cwd; +use NPTest; + +# 15 tests in the first part, 9 in timeout tests and 2 * 32 in the last loops +plan tests => 88; + +my $res; + +my $PWD = getcwd(); + +$res = NPTest->testCmd( "./negate" ); +is( $res->return_code, 3, "Not enough parameters"); +like( $res->output, "/Could not parse arguments/", "Could not parse arguments"); + +$res = NPTest->testCmd( "./negate bobthebuilder" ); +is( $res->return_code, 3, "Require full path" ); +like( $res->output, "/Require path to command/", "Appropriate error message"); + +$res = NPTest->testCmd( "./negate $PWD/check_dummy 0 'a dummy okay'" ); +is( $res->return_code, 2, "OK changed to CRITICAL" ); +is( $res->output, "OK: a dummy okay", "Output as expected" ); + +$res = NPTest->testCmd( "./negate '$PWD/check_dummy 0 redsweaterblog'"); +is( $res->return_code, 2, "OK => CRIT with a single quote for command to run" ); +is( $res->output, "OK: redsweaterblog", "Output as expected" ); + +$res = NPTest->testCmd( "./negate $PWD/check_dummy 1 'a warn a day keeps the managers at bay'" ); +is( $res->return_code, 1, "WARN stays same" ); + +$res = NPTest->testCmd( "./negate $PWD/check_dummy 3 mysterious"); +is( $res->return_code, 3, "UNKNOWN stays same" ); + +$res = NPTest->testCmd( "./negate \"$PWD/check_dummy 0 'a dummy okay'\"" ); +is( $res->output, "OK: a dummy okay", "Checking slashed quotes - the single quotes are re-evaluated at shell" ); + +# Output is "OK: a" because check_dummy only returns the first arg +$res = NPTest->testCmd( "./negate $PWD/check_dummy 0 a dummy okay" ); +is( $res->output, "OK: a", "Multiple args passed as arrays" ); + +$res = NPTest->testCmd( "./negate $PWD/check_dummy 0 'a dummy okay'" ); +is( $res->output, "OK: a dummy okay", "The quoted string is passed through to subcommand correctly" ); + +$res = NPTest->testCmd( "./negate '$PWD/check_dummy 0' 'a dummy okay'" ); +is( $res->output, "No data returned from command", "Bad command, as expected (trying to execute './check_dummy 0')"); + +$res = NPTest->testCmd( './negate '.$PWD.'/check_dummy 0 \'$$ a dummy okay\'' ); +is( $res->output, 'OK: $$ a dummy okay', 'Proves that $$ is not being expanded again' ); + +my %state = ( + ok => 0, + warning => 1, + critical => 2, + unknown => 3, + ); + +# Timeout tests +$res = NPTest->testCmd( "./negate -t 2 /bin/sh -c 'sleep 5'" ); +is( $res->output, 'CRITICAL - Plugin timed out after 2 seconds' ); + +foreach my $state (keys(%state)) { + $res = NPTest->testCmd( "./negate -t 2 -T $state /bin/sh -c 'sleep 5'" ); + is( $res->return_code, $state{$state}, "Got timeout state $state" ); + is( $res->output, uc($state)." - Plugin timed out after 2 seconds", "Timeout state $state output"); +} + +foreach my $current_state (keys(%state)) { + foreach my $new_state (keys(%state)) { + $res = NPTest->testCmd( "./negate --$current_state=$new_state ./check_dummy ".$state{$current_state}." 'Fake $new_state'" ); + is( $res->return_code, $state{$new_state}, "Got fake $new_state" ); + is( $res->output, uc($current_state).": Fake $new_state", "Fake $new_state output"); + } +} + +# Same as above with substitute +foreach my $current_state (keys(%state)) { + foreach my $new_state (keys(%state)) { + $res = NPTest->testCmd( "./negate -s --$current_state=$new_state ./check_dummy ".$state{$current_state}." 'Fake $new_state'" ); + is( $res->return_code, $state{$new_state}, "Got fake $new_state (with substitute)" ); + is( $res->output, uc($new_state).": Fake $new_state", "Substituted fake $new_state output"); + } +} + |