File Coverage

blib/lib/Monitoring/GLPlugin/UPNP.pm
Criterion Covered Total %
statement 15 76 19.7
branch 0 28 0.0
condition 0 3 0.0
subroutine 5 8 62.5
pod 0 3 0.0
total 20 118 16.9


line stmt bran cond sub pod time code
1             package Monitoring::GLPlugin::UPNP;
2             our @ISA = qw(Monitoring::GLPlugin);
3             # ABSTRACT: helper functions to build a upnp-based monitoring plugin
4              
5 2     2   1232 use strict;
  2         3  
  2         49  
6 2     2   8 use File::Basename;
  2         2  
  2         119  
7 2     2   8 use Digest::MD5 qw(md5_hex);
  2         3  
  2         62  
8 2     2   7 use AutoLoader;
  2         3  
  2         21  
9             our $AUTOLOAD;
10              
11 2     2   85 use constant { OK => 0, WARNING => 1, CRITICAL => 2, UNKNOWN => 3 };
  2         2  
  2         1364  
12              
13             {
14             our $mode = undef;
15             our $plugin = undef;
16             our $blacklist = undef;
17             our $session = undef;
18             our $rawdata = {};
19             our $info = [];
20             our $extendedinfo = [];
21             our $summary = [];
22             our $oidtrace = [];
23             our $uptime = 0;
24             }
25              
26             sub init {
27 0     0 0   my ($self) = @_;
28 0 0         if ($self->mode =~ /device::walk/) {
    0          
29             } elsif ($self->mode =~ /device::uptime/) {
30             my $info = sprintf 'device is up since %s',
31 0           $self->human_timeticks($self->{uptime});
32 0           $self->add_info($info);
33 0           $self->set_thresholds(warning => '15:', critical => '5:');
34 0           $self->add_message($self->check_thresholds($self->{uptime}), $info);
35             $self->add_perfdata(
36             label => 'uptime',
37             value => $self->{uptime} / 60,
38             warning => $self->{warning},
39             critical => $self->{critical},
40 0           );
41 0           my ($code, $message) = $self->check_messages(join => ', ', join_all => ', ');
42 0           $Monitoring::GLPlugin::plugin->nagios_exit($code, $message);
43             }
44             }
45              
46             sub check_upnp_and_model {
47 0     0 0   my ($self) = @_;
48 0 0         if (eval "require SOAP::Lite") {
49 0           require XML::LibXML;
50             } else {
51 0           $self->add_critical('could not find SOAP::Lite module');
52             }
53 0           $self->{services} = {};
54 0 0         if (! $self->check_messages()) {
55 0           eval {
56 0           my $igddesc = sprintf "http://%s:%s/igddesc.xml",
57             $self->opts->hostname, $self->opts->port;
58 0           my $parser = XML::LibXML->new();
59 0           my $doc = $parser->parse_file($igddesc);
60 0           my $root = $doc->documentElement();
61 0           my $xpc = XML::LibXML::XPathContext->new( $root );
62 0           $xpc->registerNs('n', 'urn:schemas-upnp-org:device-1-0');
63 0           $self->{productname} = $xpc->findvalue('(//n:device)[position()=1]/n:modelName' );
64 0           my @services = ();
65 0           my @servicedescs = $xpc->find('(//n:service)')->get_nodelist;
66 0           foreach my $service (@servicedescs) {
67 0           my $servicetype = undef;
68 0           my $serviceid = undef;
69 0           my $controlurl = undef;
70 0           foreach my $node ($service->nonBlankChildNodes("./*")) {
71 0 0         $serviceid = $node->textContent if ($node->nodeName eq "serviceId");
72 0 0         $servicetype = $node->textContent if ($node->nodeName eq "serviceType");
73 0 0         $controlurl = $node->textContent if ($node->nodeName eq "controlURL");
74             }
75 0 0 0       if ($serviceid && $controlurl) {
76 0           push(@services, {
77             serviceType => $servicetype,
78             serviceId => $serviceid,
79             controlURL => sprintf('http://%s:%s%s',
80             $self->opts->hostname, $self->opts->port, $controlurl),
81             });
82             }
83             }
84 0           $self->set_variable('services', \@services);
85             };
86 0 0         if ($@) {
87 0           $self->add_critical($@);
88             }
89             }
90 0 0         if (! $self->check_messages()) {
91 0           eval {
92 0           my $service = (grep { $_->{serviceId} =~ /WANIPConn1/ } @{$self->get_variable('services')})[0];
  0            
  0            
93             my $som = SOAP::Lite
94             -> proxy($service->{controlURL})
95             -> uri($service->{serviceType})
96 0           -> GetStatusInfo();
97 0           $self->{uptime} = $som->valueof("//GetStatusInfoResponse/NewUptime");
98 0           $self->{uptime} /= 1.0;
99             };
100 0 0         if ($@) {
101 0           $self->add_critical("could not get uptime: ".$@);
102             }
103             }
104             }
105              
106             sub create_statefile {
107 0     0 0   my ($self, %params) = @_;
108 0           my $extension = "";
109 0 0         $extension .= $params{name} ? '_'.$params{name} : '';
110 0 0         if ($self->opts->community) {
111 0           $extension .= md5_hex($self->opts->community);
112             }
113 0           $extension =~ s/\//_/g;
114 0           $extension =~ s/\(/_/g;
115 0           $extension =~ s/\)/_/g;
116 0           $extension =~ s/\*/_/g;
117 0           $extension =~ s/\s/_/g;
118 0 0         if ($^O =~ /MSWin/) {
119 0           $extension =~ s/:/_/g;
120             }
121 0           return sprintf "%s/%s_%s%s", $self->statefilesdir(),
122             $self->opts->hostname, $self->opts->mode, lc $extension;
123             }
124              
125             1;
126              
127             __END__