NPTest.pm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. package NPTest;
  2. #
  3. # Helper Functions for testing Nagios Plugins
  4. #
  5. require Exporter;
  6. @ISA = qw(Exporter);
  7. @EXPORT = qw(getTestParameter checkCmd skipMissingCmd);
  8. @EXPORT_OK = qw(DetermineTestHarnessDirectory TestsFrom SetCacheFilename);
  9. use strict;
  10. use warnings;
  11. use Cwd;
  12. use File::Basename;
  13. use IO::File;
  14. use Data::Dumper;
  15. use Test;
  16. use vars qw($VERSION);
  17. $VERSION = "1556."; # must be all one line, for MakeMaker
  18. =head1 NAME
  19. NPTest - Simplify the testing of Nagios Plugins
  20. =head1 DESCRIPTION
  21. This modules provides convenience functions to assist in the testing
  22. of Nagios Plugins, making the testing code easier to read and write;
  23. hopefully encouraging the development of a more complete test suite for
  24. the Nagios Plugins. It is based on the patterns of testing seen in the
  25. 1.4.0 release, and continues to use the L<Test> module as the basis of
  26. testing.
  27. =head1 FUNCTIONS
  28. This module defines three public functions, C<getTestParameter(...)>,
  29. C<checkCmd(...)> and C<skipMissingCmd(...)>. These are exported by
  30. default via the C<use NPTest;> statement.
  31. =over
  32. =item getTestParameter( "ENV_VARIABLE", $brief_description, $default )
  33. $default is optional.
  34. This function allows the test harness
  35. developer to interactively request test parameter information from the
  36. user. The user can accept the developer's default value or reply "none"
  37. which will then be returned as "" for the test to skip if appropriate.
  38. If a parameter needs to be entered and the test is run without a tty
  39. attached (such as a cronjob), the parameter will be assigned as if it
  40. was "none". Tests can check for the parameter and skip if not set.
  41. Responses are stored in an external, file-based cache so subsequent test
  42. runs will use these values. The user is able to change the values by
  43. amending the values in the file /var/tmp/NPTest.cache, or by setting
  44. the appropriate environment variable before running the test.
  45. The option exists to store parameters in a scoped means, allowing a
  46. test harness to a localise a parameter should the need arise. This
  47. allows a parameter of the same name to exist in a test harness
  48. specific scope, while not affecting the globally scoped parameter. The
  49. scoping identifier is the name of the test harness sans the trailing
  50. ".t". All cache searches first look to a scoped parameter before
  51. looking for the parameter at global scope. Thus for a test harness
  52. called "check_disk.t" requesting the parameter "mountpoint_valid", the
  53. cache is first searched for "check_disk"/"mountpoint_valid", if this
  54. fails, then a search is conducted for "mountpoint_valid".
  55. To facilitate quick testing setup, it is possible to accept all the
  56. developer provided defaults by setting the environment variable
  57. "NPTEST_ACCEPTDEFAULT" to "1" (or any other perl truth value). Note
  58. that, such defaults are not stored in the cache, as there is currently
  59. no mechanism to edit existing cache entries, save the use of text
  60. editor or removing the cache file completely.
  61. =item C<testCmd($command)>
  62. Call with NPTest->testCmd("./check_disk ...."). This returns a NPTest object
  63. which you can then run $object->return_code or $object->output against.
  64. Testing of results would be done in your test script, not in this module.
  65. =item C<checkCmd(...)>
  66. This function is obsolete. Use C<testCmd()> instead.
  67. This function attempts to encompass the majority of test styles used
  68. in testing Nagios Plugins. As each plug-in is a separate command, the
  69. typical tests we wish to perform are against the exit status of the
  70. command and the output (if any) it generated. Simplifying these tests
  71. into a single function call, makes the test harness easier to read and
  72. maintain and allows additional functionality (such as debugging) to be
  73. provided without additional effort on the part of the test harness
  74. developer.
  75. It is possible to enable debugging via the environment variable
  76. C<NPTEST_DEBUG>. If this environment variable exists and its value in PERL's
  77. boolean context evaluates to true, debugging is enabled.
  78. The function prototype can be expressed as follows:
  79. Parameter 1 : command => DEFINED SCALAR(string)
  80. Parameter 2 : desiredExitStatus => ONE OF
  81. SCALAR(integer)
  82. ARRAYREF(integer)
  83. HASHREF(integer,string)
  84. UNDEFINED
  85. Parameter 3 : desiredOutput => SCALAR(string) OR UNDEFINED
  86. Parameter 4 : exceptions => HASH(integer,string) OR UNDEFINED
  87. Returns : SCALAR(integer) as defined by Test::ok(...)
  88. The function treats the first parameter C<$command> as a command line
  89. to execute as part of the test, it is executed only once and its exit
  90. status (C<$?E<gt>E<gt>8>) and output are captured.
  91. At this point if debugging is enabled the command, its exit status and
  92. output are displayed to the tester.
  93. C<checkCmd(...)> allows the testing of either the exit status or the
  94. generated output or both, not testing either will result in neither
  95. the C<Test::ok(...)> or C<Test::skip(...)> functions being called,
  96. something you probably don't want. Note that each defined test
  97. (C<$desiredExitStatus> and C<$desiredOutput>) results in a invocation
  98. of either C<Test::ok(...)> or C<Test::skip(...)>, so remember this
  99. when counting the number of tests to place in the C<Test::plan(...)>
  100. call.
  101. Many Nagios Plugins test network services, some of which may not be
  102. present on all systems. To cater for this, C<checkCmd(...)> allows the
  103. tester to define exceptions based on the command's exit status. These
  104. exceptions are provided to skip tests if the test case developer
  105. believes the service is not being provided. For example, if a site
  106. does not have a POP3 server, the test harness could map the
  107. appropriate exit status to a useful message the person running the
  108. tests, telling the reason the test is being skipped.
  109. Example:
  110. my %exceptions = ( 2 =E<gt> "No POP Server present?" );
  111. $t += checkCmd( "./check_pop I<some args>", 0, undef, %exceptions );
  112. Thus, in the above example, an exit status of 2 does not result in a
  113. failed test case (as the exit status is not the desired value of 0),
  114. but a skipped test case with the message "No POP Server present?"
  115. given as the reason.
  116. Sometimes the exit status of a command should be tested against a set
  117. of possible values, rather than a single value, this could especially
  118. be the case in failure testing. C<checkCmd(...)> support two methods
  119. of testing against a set of desired exit status values.
  120. =over
  121. =item *
  122. Firstly, if C<$desiredExitStatus> is a reference to an array of exit
  123. stati, if the actual exit status of the command is present in the
  124. array, it is used in the call to C<Test::ok(...)> when testing the
  125. exit status.
  126. =item *
  127. Alternatively, if C<$desiredExitStatus> is a reference to a hash of
  128. exit stati (mapped to the strings "continue" or "skip"), similar
  129. processing to the above occurs with the side affect of determining if
  130. any generated output testing should proceed. Note: only the string
  131. "skip" will result in generated output testing being skipped.
  132. =back
  133. =item C<skipMissingCmd(...)>
  134. If a command is missing and the test harness must C<Test::skip()> some
  135. or all of the tests in a given test harness this function provides a
  136. simple iterator to issue an appropriate message the requested number
  137. of times.
  138. =back
  139. =head1 SEE ALSO
  140. L<Test>
  141. The rest of the code, as I have only commented on the major public
  142. functions that test harness writers will use, not all the code present
  143. in this helper module.
  144. =head1 AUTHOR
  145. Copyright (c) 2005 Peter Bray. All rights reserved.
  146. This package is free software and is provided "as is" without express
  147. or implied warranty. It may be used, redistributed and/or modified
  148. under the same terms as the Nagios Plugins release.
  149. =cut
  150. #
  151. # Package Scope Variables
  152. #
  153. my( %CACHE ) = ();
  154. # I'm not really sure whether to house a site-specific cache inside
  155. # or outside of the extracted source / build tree - lets default to outside
  156. my( $CACHEFILENAME ) = ( exists( $ENV{'NPTEST_CACHE'} ) && $ENV{'NPTEST_CACHE'} )
  157. ? $ENV{'NPTEST_CACHE'} : "/var/tmp/NPTest.cache"; # "../Cache.pdd";
  158. #
  159. # Testing Functions
  160. #
  161. sub checkCmd
  162. {
  163. my( $command, $desiredExitStatus, $desiredOutput, %exceptions ) = @_;
  164. my $result = NPTest->testCmd($command);
  165. my $output = $result->output;
  166. my $exitStatus = $result->return_code;
  167. $output = "" unless defined( $output );
  168. chomp( $output );
  169. my $testStatus;
  170. my $testOutput = "continue";
  171. if ( defined( $desiredExitStatus ) )
  172. {
  173. if ( ref $desiredExitStatus eq "ARRAY" )
  174. {
  175. if ( scalar( grep { $_ == $exitStatus } @{$desiredExitStatus} ) )
  176. {
  177. $desiredExitStatus = $exitStatus;
  178. }
  179. else
  180. {
  181. $desiredExitStatus = -1;
  182. }
  183. }
  184. elsif ( ref $desiredExitStatus eq "HASH" )
  185. {
  186. if ( exists( ${$desiredExitStatus}{$exitStatus} ) )
  187. {
  188. if ( defined( ${$desiredExitStatus}{$exitStatus} ) )
  189. {
  190. $testOutput = ${$desiredExitStatus}{$exitStatus};
  191. }
  192. $desiredExitStatus = $exitStatus;
  193. }
  194. else
  195. {
  196. $desiredExitStatus = -1;
  197. }
  198. }
  199. if ( %exceptions && exists( $exceptions{$exitStatus} ) )
  200. {
  201. $testStatus += skip( $exceptions{$exitStatus}, $exitStatus, $desiredExitStatus );
  202. $testOutput = "skip";
  203. }
  204. else
  205. {
  206. $testStatus += ok( $exitStatus, $desiredExitStatus );
  207. }
  208. }
  209. if ( defined( $desiredOutput ) )
  210. {
  211. if ( $testOutput ne "skip" )
  212. {
  213. $testStatus += ok( $output, $desiredOutput );
  214. }
  215. else
  216. {
  217. $testStatus += skip( "Skipping output test as requested", $output, $desiredOutput );
  218. }
  219. }
  220. return $testStatus;
  221. }
  222. sub skipMissingCmd
  223. {
  224. my( $command, $count ) = @_;
  225. my $testStatus;
  226. for ( 1 .. $count )
  227. {
  228. $testStatus += skip( "Missing ${command} - tests skipped", 1 );
  229. }
  230. return $testStatus;
  231. }
  232. sub getTestParameter
  233. {
  234. my( $param, $envvar, $default, $brief, $scoped );
  235. my $new_style;
  236. if (scalar @_ <= 3) {
  237. ($param, $brief, $default) = @_;
  238. $envvar = $param;
  239. $new_style = 1;
  240. } else {
  241. ( $param, $envvar, $default, $brief, $scoped ) = @_;
  242. $new_style = 0;
  243. }
  244. # Apply default values for optional arguments
  245. $scoped = ( defined( $scoped ) && $scoped );
  246. my $testharness = basename( (caller(0))[1], ".t" ); # used for scoping
  247. if ( defined( $envvar ) && exists( $ENV{$envvar} ) && $ENV{$envvar} )
  248. {
  249. return $ENV{$envvar};
  250. }
  251. my $cachedValue = SearchCache( $param, $testharness );
  252. if ( defined( $cachedValue ) )
  253. {
  254. # This save required to convert to new style because the key required is
  255. # changing to the environment variable
  256. if ($new_style == 0) {
  257. SetCacheParameter( $envvar, undef, $cachedValue );
  258. }
  259. return $cachedValue;
  260. }
  261. my $defaultValid = ( defined( $default ) && $default );
  262. my $autoAcceptDefault = ( exists( $ENV{'NPTEST_ACCEPTDEFAULT'} ) && $ENV{'NPTEST_ACCEPTDEFAULT'} );
  263. if ( $autoAcceptDefault && $defaultValid )
  264. {
  265. return $default;
  266. }
  267. # Set "none" if no terminal attached (eg, tinderbox build servers when new variables set)
  268. return "" unless (-t STDIN);
  269. my $userResponse = "";
  270. while ( $userResponse eq "" )
  271. {
  272. print STDERR "\n";
  273. print STDERR "Test Harness : $testharness\n";
  274. print STDERR "Test Parameter : $param\n";
  275. print STDERR "Environment Variable : $envvar\n" if ($param ne $envvar);
  276. print STDERR "Brief Description : $brief\n";
  277. print STDERR "Enter value (or 'none') ", ($defaultValid ? "[${default}]" : "[]"), " => ";
  278. $userResponse = <STDIN>;
  279. $userResponse = "" if ! defined( $userResponse ); # Handle EOF
  280. chomp( $userResponse );
  281. if ( $defaultValid && $userResponse eq "" )
  282. {
  283. $userResponse = $default;
  284. }
  285. }
  286. print STDERR "\n";
  287. if ($userResponse =~ /^(na|none)$/) {
  288. $userResponse = "";
  289. }
  290. # define all user responses at global scope
  291. SetCacheParameter( $param, ( $scoped ? $testharness : undef ), $userResponse );
  292. return $userResponse;
  293. }
  294. #
  295. # Internal Cache Management Functions
  296. #
  297. sub SearchCache
  298. {
  299. my( $param, $scope ) = @_;
  300. LoadCache();
  301. if ( exists( $CACHE{$scope} ) && exists( $CACHE{$scope}{$param} ) )
  302. {
  303. return $CACHE{$scope}{$param};
  304. }
  305. if ( exists( $CACHE{$param} ) )
  306. {
  307. return $CACHE{$param};
  308. }
  309. return undef; # Need this to say "nothing found"
  310. }
  311. sub SetCacheParameter
  312. {
  313. my( $param, $scope, $value ) = @_;
  314. if ( defined( $scope ) )
  315. {
  316. $CACHE{$scope}{$param} = $value;
  317. }
  318. else
  319. {
  320. $CACHE{$param} = $value;
  321. }
  322. SaveCache();
  323. }
  324. sub LoadCache
  325. {
  326. return if exists( $CACHE{'_cache_loaded_'} );
  327. my $fileContents = "";
  328. if ( -f $CACHEFILENAME )
  329. {
  330. my( $fileHandle ) = new IO::File;
  331. if ( ! $fileHandle->open( "< ${CACHEFILENAME}" ) )
  332. {
  333. print STDERR "NPTest::LoadCache() : Problem opening ${CACHEFILENAME} : $!\n";
  334. return;
  335. }
  336. $fileContents = join("", <$fileHandle>);
  337. $fileHandle->close();
  338. chomp($fileContents);
  339. my( $contentsRef ) = eval $fileContents;
  340. %CACHE = %{$contentsRef} if (defined($contentsRef));
  341. }
  342. $CACHE{'_cache_loaded_'} = 1;
  343. $CACHE{'_original_cache'} = $fileContents;
  344. }
  345. sub SaveCache
  346. {
  347. delete $CACHE{'_cache_loaded_'};
  348. my $oldFileContents = delete $CACHE{'_original_cache'};
  349. my($dataDumper) = new Data::Dumper([\%CACHE]);
  350. $dataDumper->Terse(1);
  351. $dataDumper->Sortkeys(1);
  352. my $data = $dataDumper->Dump();
  353. $data =~ s/^\s+/ /gmx; # make sure all systems use same amount of whitespace
  354. $data =~ s/^\s+}/}/gmx;
  355. chomp($data);
  356. if($oldFileContents ne $data) {
  357. my($fileHandle) = new IO::File;
  358. if (!$fileHandle->open( "> ${CACHEFILENAME}")) {
  359. print STDERR "NPTest::LoadCache() : Problem saving ${CACHEFILENAME} : $!\n";
  360. return;
  361. }
  362. print $fileHandle $data;
  363. $fileHandle->close();
  364. }
  365. $CACHE{'_cache_loaded_'} = 1;
  366. $CACHE{'_original_cache'} = $data;
  367. }
  368. #
  369. # (Questionable) Public Cache Management Functions
  370. #
  371. sub SetCacheFilename
  372. {
  373. my( $filename ) = @_;
  374. # Unfortunately we can not validate the filename
  375. # in any meaningful way, as it may not yet exist
  376. $CACHEFILENAME = $filename;
  377. }
  378. #
  379. # Test Harness Wrapper Functions
  380. #
  381. sub DetermineTestHarnessDirectory
  382. {
  383. my( @userSupplied ) = @_;
  384. my @dirs;
  385. # User Supplied
  386. if ( @userSupplied > 0 )
  387. {
  388. for my $u ( @userSupplied )
  389. {
  390. if ( -d $u )
  391. {
  392. push ( @dirs, $u );
  393. }
  394. }
  395. }
  396. # Simple Cases: "t" and tests are subdirectories of the current directory
  397. if ( -d "./t" )
  398. {
  399. push ( @dirs, "./t");
  400. }
  401. if ( -d "./tests" )
  402. {
  403. push ( @dirs, "./tests");
  404. }
  405. if ( @dirs > 0 )
  406. {
  407. return @dirs;
  408. }
  409. # To be honest I don't understand which case satisfies the
  410. # original code in test.pl : when $tstdir == `pwd` w.r.t.
  411. # $tstdir =~ s|^(.*)/([^/]+)/?$|$1/$2|; and if (-d "../../$2/t")
  412. # Assuming pwd is "/a/b/c/d/e" then we are testing for "/a/b/c/e/t"
  413. # if I understand the code correctly (a big assumption)
  414. # Simple Case : the current directory is "t"
  415. my $pwd = cwd();
  416. if ( $pwd =~ m|/t$| )
  417. {
  418. push ( @dirs, $pwd );
  419. # The alternate that might work better is
  420. # chdir( ".." );
  421. # return "./t";
  422. # As the current test harnesses assume the application
  423. # to be tested is in the current directory (ie "./check_disk ....")
  424. }
  425. return @dirs;
  426. }
  427. sub TestsFrom
  428. {
  429. my( $directory, $excludeIfAppMissing ) = @_;
  430. $excludeIfAppMissing = 0 unless defined( $excludeIfAppMissing );
  431. if ( ! opendir( DIR, $directory ) )
  432. {
  433. print STDERR "NPTest::TestsFrom() - Failed to open ${directory} : $!\n";
  434. return ();
  435. }
  436. my( @tests ) = ();
  437. my $filename;
  438. my $application;
  439. while ( $filename = readdir( DIR ) )
  440. {
  441. if ( $filename =~ m/\.t$/ )
  442. {
  443. if ( $excludeIfAppMissing )
  444. {
  445. $application = basename( $filename, ".t" );
  446. if ( ! -e $application and ! -e $application.'.pm' )
  447. {
  448. print STDERR "No application (${application}) found for test harness (${filename})\n";
  449. next;
  450. }
  451. }
  452. push @tests, "${directory}/${filename}";
  453. }
  454. }
  455. closedir( DIR );
  456. return sort @tests;
  457. }
  458. # All the new object oriented stuff below
  459. sub new {
  460. my $type = shift;
  461. my $self = {};
  462. return bless $self, $type;
  463. }
  464. # Accessors
  465. sub return_code {
  466. my $self = shift;
  467. if (@_) {
  468. return $self->{return_code} = shift;
  469. } else {
  470. return $self->{return_code};
  471. }
  472. }
  473. sub output {
  474. my $self = shift;
  475. if (@_) {
  476. return $self->{output} = shift;
  477. } else {
  478. return $self->{output};
  479. }
  480. }
  481. sub perf_output {
  482. my $self = shift;
  483. $_ = $self->{output};
  484. /\|(.*)$/;
  485. return $1 || "";
  486. }
  487. sub only_output {
  488. my $self = shift;
  489. $_ = $self->{output};
  490. /(.*?)\|/;
  491. return $1 || "";
  492. }
  493. sub testCmd {
  494. my $class = shift;
  495. my $command = shift or die "No command passed to testCmd";
  496. my $object = $class->new;
  497. local $SIG{'ALRM'} = sub { die("timeout in command: $command"); };
  498. alarm(120); # no test should take longer than 120 seconds
  499. my $output = `$command`;
  500. $object->return_code($? >> 8);
  501. $_ = $? & 127;
  502. if ($_) {
  503. die "Got signal $_ for command $command";
  504. }
  505. chomp $output;
  506. $object->output($output);
  507. alarm(0);
  508. my ($pkg, $file, $line) = caller(0);
  509. print "Testing: $command", $/;
  510. if ($ENV{'NPTEST_DEBUG'}) {
  511. print "testCmd: Called from line $line in $file", $/;
  512. print "Output: ", $object->output, $/;
  513. print "Return code: ", $object->return_code, $/;
  514. }
  515. return $object;
  516. }
  517. # do we have ipv6
  518. sub has_ipv6 {
  519. # assume ipv6 if a ping6 to labs.consol.de works
  520. `ping6 -c 1 2a03:3680:0:2::21 2>&1`;
  521. if($? == 0) {
  522. return 1;
  523. }
  524. return;
  525. }
  526. 1;
  527. #
  528. # End of File
  529. #