plugins/nagios/templates/check_xml-monitoring.pl

filter: Statementstmt 87.5% 63 / 72 Branchbran 57.6% 30 / 52 Conditioncond 75.0% 9 / 12 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 75.6% 115 / 152 CC 2 SCAR 2.4
CC
2
Cov
75%
BEGIN
2.5
BEGIN
1.0
BEGIN
1.0
CRAP 1.3
« plugins/nagios/templates/check_xml.pl ↑ Summary plugins/templates/check_template-WebTransact-XML-Monitoring.pl »
1 #!/usr/bin/env perl
2 # ----------------------------------------------------------------------------------------------------------
3 # © Copyright 2003-2011 by Alex Peeters [alex.peeters@citap.be]
4 # ----------------------------------------------------------------------------------------------------------
5 # 2011/mm/dd, v3.002.003, check_xml-monitoring.pl
6 # ----------------------------------------------------------------------------------------------------------
7  
8 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use warnings; # Must be used in test mode only. This reduces a little process speed
Subroutine BEGIN: called CC=1 SCAR=0.0
10 #use diagnostics; # Must be used in test mode only. This reduces a lot of process speed
11  
12 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
13  
14 0 ▶BEGIN { if ( $ENV{ASNMTAP_PERL5LIB} ) { eval 'use lib ( "$ENV{ASNMTAP_PERL5LIB}" )'; } }
Subroutine BEGIN: called CC=2 SCAR=9.2
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"ASNMTAP_PERL5LIB"}) 0 12 12
15  
16 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
17  
18 12 ▶use Date::Calc qw(check_date check_time);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 12 ▶use Time::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
22  
23 12 ▶use ASNMTAP::Asnmtap::Plugins::Nagios v3.002.003;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 12 ▶use ASNMTAP::Asnmtap::Plugins::Nagios qw(:NAGIOS);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
27  
28 12 my $schema = "1.0";
29  
30 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
31  
32 12 my $objectNagios = ASNMTAP::Asnmtap::Plugins::Nagios->new (
33 _programName => 'check_xml-monitoring.pl',
34 _programDescription => 'Check Nagios by XML Monitoring',
35 _programVersion => '3.002.003',
36 _programUsagePrefix => '-H|--hostname <hostname> -s|--service <service> [-P|--plugin <plugin>] [-p|--parameters <parameters>] [--validation <validation>]',
37 _programHelpPrefix => "-H, --hostname=<Nagios Hostname>
38 -s, --service=<Nagios service name>
39 -P, --plugin=<plugin to execute>
40 -p, --parameters=<parameters for the plugin to execute>
41 --validation=F|T
42 F(alse) : dtd validation off (default)
43 T(true) : dtd validation on",
44 _programGetOptions => ['filename|F=s', 'hostname|H=s', 'service|s=s', 'plugin|P:s', 'parameters|p:s', 'validation:s', 'interval|i=i', 'environment|e=s'],
45 _timeout => 30,
46 _debug => 0);
47  
48 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
49  
50 7 my $filename = $objectNagios->getOptionsArgv ('filename');
51  
52 7 ▶my $hostname = $objectNagios->getOptionsArgv ('hostname') ? $objectNagios->getOptionsArgv ('hostname') : undef;
Branch: 1 branch
BranchTrueFalseTotal
$objectNagios->getOptionsArgv('hostname') ? : 6 1 7
53 7 ▶$objectNagios->printUsage ('Missing command line argument hostname') unless (defined $hostname);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hostname 1 6 7
54  
55 6 ▶my $service = $objectNagios->getOptionsArgv ('service') ? $objectNagios->getOptionsArgv ('service') : undef;
Branch: 1 branch
BranchTrueFalseTotal
$objectNagios->getOptionsArgv('service') ? : 5 1 6
56 6 ▶$objectNagios->printUsage ('Missing command line argument service') unless ( defined $service);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $service 1 5 6
57  
58 5 ▶my $plugin = $objectNagios->getOptionsArgv ('plugin') ? $objectNagios->getOptionsArgv ('plugin') : undef;
Branch: 1 branch
BranchTrueFalseTotal
$objectNagios->getOptionsArgv('plugin') ? : 0 5 5
59 5 ▶my $parameters = $objectNagios->getOptionsArgv ('parameters') ? $objectNagios->getOptionsArgv ('parameters') : '';
Branch: 1 branch
BranchTrueFalseTotal
$objectNagios->getOptionsArgv('parameters') ? : 0 5 5
60 5 ▶my $validateDTD = $objectNagios->getOptionsArgv ('validation') ? $objectNagios->getOptionsArgv ('validation') : 'F';
Branch: 1 branch
BranchTrueFalseTotal
$objectNagios->getOptionsArgv('validation') ? : 0 5 5
61  
62 5 ▶if (defined $validateDTD) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $validateDTD) 5 0 5
63 5 ▶ $objectNagios->printUsage ('Invalid validation option: '. $validateDTD) unless ($validateDTD =~ /^[FT]$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $validateDTD =~ /^[FT]$/ 0 5 5
64 5 ▶ $validateDTD = ($validateDTD eq 'T') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$validateDTD eq 'T' ? : 0 5 5
65 }
66  
67 5 my $resultOutOfDate = $objectNagios->getOptionsArgv ('interval');
68  
69 5 ▶my $environment = $objectNagios->getOptionsArgv ('environment') ? $objectNagios->getOptionsArgv ('environment') : 'P';
Branch: 1 branch
BranchTrueFalseTotal
$objectNagios->getOptionsArgv('environment') ? : 5 0 5
70  
71 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
72  
73 12 ▶use ASNMTAP::Asnmtap::Plugins::XML qw(&extract_XML);
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 12 ▶use constant HEADER => '<?xml version="1.0" encoding="UTF-8"?>';
Subroutine BEGIN: called CC=1 SCAR=0.0
76 12 ▶use constant FOOTER => '</MonitoringXML>';
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
79  
80 5 ▶if ( defined $plugin ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $plugin) 0 5 5
81 0 ▶ if (-s $plugin ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-s $plugin) { } 0 0 0
82 0 ▶ $objectNagios->exit (3) if ( $objectNagios->call_system ( $plugin .' '. $parameters, 1 ) );
Branch: 1 branch
BranchTrueFalseTotal
if $objectNagios->call_system($plugin . ' ' . $parameters, 1) 0 0 0
83 } else {
84 0 $objectNagios->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => "The Plugin '$plugin' doesn't exist" }, $TYPE{APPEND} );
85 0 $objectNagios->exit (3);
86 }
87 }
88  
89 5 my ($returnCode, $xml) = extract_XML ( asnmtapInherited => \$objectNagios, filenameXML => $filename, headerXML => HEADER, footerXML => FOOTER, validateDTD => $validateDTD, filenameDTD => "dtd/Monitoring-$schema.dtd" );
90 5 ▶$objectNagios->exit (3) if ( $returnCode );
Branch: 1 branch
BranchTrueFalseTotal
if $returnCode 1 4 5
91  
92 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
93  
94 4 my $currentTimeslot = timelocal (0, (localtime)[1,2,3,4,5]);
95 4 my %environment = ( P => 'PROD', S => 'SIM', A => 'ACC', T => 'TEST', D => 'DEV', L => 'LOCAL' );
96  
97 4 ▶if ($xml->{Monitoring}{Schema}{Value} eq $schema and $xml->{Monitoring}{Results}{Details}{Host} eq $hostname and $xml->{Monitoring}{Results}{Details}{Service} eq $service and $xml->{Monitoring}{Results}{Details}{Environment} =~ /^$environment{$environment}$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml->{'Monitoring'}{'Schema'}{'Value'} eq $schema and $xml->{'Monitoring'}{'Results'}{'Details'}{'Host'} eq $hostname and $xml->{'Monitoring'}{'Results'}{'Details'}{'Service'} eq $service and $xml->{'Monitoring'}{'Results'}{'Details'}{'Environment'} =~ /^$environment{$environment}$/i) { } 1 3 4
Condition88%8/9
$xml->{"Monitoring"}{"Schema"}{"Value"} eq $schema and $xml->{"Monitoring"}{"Results"}{"Details"}{"Host"} eq $hostname and $xml->{"Monitoring"}{"Results"}{"Details"}{"Service"} eq $service and $xml->{"Monitoring"}{"Results"}{"Details"}{"Environment"} =~ /^$environment{$environment}$/i
!ll&&!rl&&r
211
$xml->{"Monitoring"}{"Schema"}{"Value"} eq $schema and $xml->{"Monitoring"}{"Results"}{"Details"}{"Host"} eq $hostname and $xml->{"Monitoring"}{"Results"}{"Details"}{"Service"} eq $service
!ll&&!rl&&r
112
$xml->{"Monitoring"}{"Schema"}{"Value"} eq $schema and $xml->{"Monitoring"}{"Results"}{"Details"}{"Host"} eq $hostname
!ll&&!rl&&r
013
MC/DC75%3/4
$xml->{"Monitoring"}{"Schema"}{"Value"} eq $schema and $xml->{"Monitoring"}{"Results"}{"Details"}{"Host"} eq $hostname and $xml->{"Monitoring"}{"Results"}{"Details"}{"Service"} eq $service and $xml->{"Monitoring"}{"Results"}{"Details"}{"Environment"} =~ /^$environment{$environment}$/i
$xml->{"Monitoring"}{"Schema"}{"Value"} eq $schema $xml->{"Monitoring"}{"Results"}{"Details"}{"Host"} eq $hostname $xml->{"Monitoring"}{"Results"}{"Details"}{"Service"} eq $service $xml->{"Monitoring"}{"Results"}{"Details"}{"Environment"} =~ /^$environment{$environment}$/i
Truth table80%4/5
A and B and C and D
A= $xml->{"Monitoring"}{"Schema"}{"Value"} eq $schemaB= $xml->{"Monitoring"}{"Results"}{"Details"}{"Host"} eq $hostnameC= $xml->{"Monitoring"}{"Results"}{"Details"}{"Service"} eq $serviceD= $xml->{"Monitoring"}{"Results"}{"Details"}{"Environment"} =~ /^$environment{$environment}$/i
ABCDresult
0XXX0
10XX0
110X0
11100
11111
98 1 my ($checkEpochtime, $checkDate, $checkTime) = ($xml->{Monitoring}{Results}{Details}{Epochtime}, $xml->{Monitoring}{Results}{Details}{Date}, $xml->{Monitoring}{Results}{Details}{Time});
99 1 my ($checkYear, $checkMonth, $checkDay) = split (/[\/-]/, $checkDate);
100 1 my ($checkHour, $checkMin, $checkSec) = split (/:/, $checkTime);
101 1 my $xmlEpochtime = timelocal ( $checkSec, $checkMin, $checkHour, $checkDay, ($checkMonth-1), ($checkYear-1900) );
102 1 ▶ print "$checkEpochtime, $xmlEpochtime ($checkDate, $checkTime), $currentTimeslot - $checkEpochtime = ". ($currentTimeslot - $checkEpochtime) ." > $resultOutOfDate\n" if ( $objectNagios->getOptionsValue ('debug') );
Branch: 1 branch
BranchTrueFalseTotal
if $objectNagios->getOptionsValue("debug") 0 1 1
103  
104 1 ▶ unless ( check_date ( $checkYear, $checkMonth, $checkDay) or check_time($checkHour, $checkMin, $checkSec ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (check_date($checkYear, $checkMonth, $checkDay) or check_time($checkHour, $checkMin, $checkSec)) { } 1 0 1
if (do { $checkEpochtime != $xmlEpochtime }) { } 1 0 1
elsif ($currentTimeslot - $checkEpochtime > $resultOutOfDate) { } 0 0 0
Condition33%1/3
check_date($checkYear, $checkMonth, $checkDay) or check_time($checkHour, $checkMin, $checkSec)
l!l&&r!l&&!r
100
MC/DC0%0/2
check_date($checkYear, $checkMonth, $checkDay) or check_time($checkHour, $checkMin, $checkSec)
check_date($checkYear, $checkMonth, $checkDay) check_time($checkHour, $checkMin, $checkSec)
Truth table33%1/3
A or B
A= check_date($checkYear, $checkMonth, $checkDay)B= check_time($checkHour, $checkMin, $checkSec)
ABresult
1X1
011
000
105 0 $objectNagios->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => "Date or Time into XML file '$filename' are wrong: $checkDate $checkTime", result => undef }, $TYPE{APPEND} );
106 } elsif ( $checkEpochtime != $xmlEpochtime ) {
107 1 $objectNagios->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => "Epochtime difference from Date and Time into XML file '$filename' are wrong: $checkEpochtime != $xmlEpochtime ($checkDate $checkTime)", result => undef }, $TYPE{APPEND} );
108 } elsif ( $currentTimeslot - $checkEpochtime > $resultOutOfDate ) {
109 0 $objectNagios->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => "Result into XML file '$filename' are out of date: $checkDate $checkTime", result => undef }, $TYPE{APPEND} );
110 } else {
111 0 $objectNagios->pluginValues ( { stateError => $STATE{$xml->{Monitoring}{Results}{Details}{Status}}, alert => $xml->{Monitoring}{Results}{Details}{StatusMessage}, result => $xml->{Monitoring}{Results}{Details}{content} }, $TYPE{APPEND} );
112 0 ▶ $objectNagios->appendPerformanceData( $xml->{Monitoring}{Results}{Details}{PerfData} ) if ( $xml->{Monitoring}{Results}{Details}{PerfData} );
Branch: 1 branch
BranchTrueFalseTotal
if $xml->{"Monitoring"}{"Results"}{"Details"}{"PerfData"} 0 0 0
113 }
114 } else {
115 3 my $tError = 'Content Error:';
116 3 ▶ $tError .= ' - Schema: '. $xml->{Monitoring}{Schema}{Value} ." ne $schema" if ($xml->{Monitoring}{Schema}{Value} ne $schema);
Branch: 1 branch
BranchTrueFalseTotal
if $xml->{"Monitoring"}{"Schema"}{"Value"} ne $schema 0 3 3
117 3 ▶ $tError .= ' - Host: '. $xml->{Monitoring}{Results}{Details}{Host} ." ne $hostname" if ($xml->{Monitoring}{Results}{Details}{Host} ne $hostname);
Branch: 1 branch
BranchTrueFalseTotal
if $xml->{"Monitoring"}{"Results"}{"Details"}{"Host"} ne $hostname 1 2 3
118 3 ▶ $tError .= ' - Service: '. $xml->{Monitoring}{Results}{Details}{Service} ." ne $service" if ($xml->{Monitoring}{Results}{Details}{Service} ne $service);
Branch: 1 branch
BranchTrueFalseTotal
if $xml->{"Monitoring"}{"Results"}{"Details"}{"Service"} ne $service 2 1 3
119 3 ▶ $tError .= ' - Environment: ' .$xml->{Monitoring}{Results}{Details}{Environment} ." ne ". $environment{$environment} if ($xml->{Monitoring}{Results}{Details}{Environment} !~ /^$environment{$environment}$/i);
Branch: 1 branch
BranchTrueFalseTotal
unless $xml->{"Monitoring"}{"Results"}{"Details"}{"Environment"} =~ /^$environment{$environment}$/i 3 0 3
120 3 $objectNagios->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => $tError, result => undef }, $TYPE{APPEND} );
121 }
122  
123 4 $objectNagios->exit (3);
124  
125 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
126  
127 __END__
« plugins/nagios/templates/check_xml.pl ↑ Summary plugins/templates/check_template-WebTransact-XML-Monitoring.pl »