Suppress Test::More Output For Use In Nagios

by Thomas Beutel

I needed a quick way to check for XML well-formedness in a Perl Nagios script and I found that Test::XML has a test called is_well_formed_xml($xml) that does the job nicely. The only problem was that as part of the Test::More framework, Test::XML outputs test results (as it should). In my case though, I just wanted to perform the test. So I was able to suppress the output like so:

Test::More->builder->output('/dev/null');

Here is a larger code fragment if you are interested:

use Getopt::Euclid;
use Test::XML;
use Test::More;

Test::More->builder->output('/dev/null');

my $warning = $ARGV{'-w'};
my $critical = $ARGV{'-c'};

my $xml = `curl --silent 'http://example.com/some-xml-source.xml'`;

my $well_formed = is_well_formed_xml($xml);
done_testing();

if (!$well_formed) {
print "CRITICAL - XML not well formed\n";
exit(2);
}

# ... checking XML content for OK and WARNING states not shown here

I’m sure there are other quick ways to check for well-formedness but this is working well for me.