File Coverage

blib/lib/Monitoring/GLPlugin/UPNP.pm
Criterion Covered Total %
statement 15 79 18.9
branch 0 28 0.0
condition 0 3 0.0
subroutine 5 8 62.5
pod 0 3 0.0
total 20 121 16.5


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 3     3   1313 use strict;
  3         8  
  3         102  
6 3     3   18 use File::Basename;
  3         8  
  3         238  
7 3     3   21 use Digest::MD5 qw(md5_hex);
  3         7  
  3         126  
8 3     3   18 use AutoLoader;
  3         7  
  3         23  
9             our $AUTOLOAD;
10              
11 3     3   174 use constant { OK => 0, WARNING => 1, CRITICAL => 2, UNKNOWN => 3 };
  3         7  
  3         3233  
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           $self->debug(sprintf "igddesc productname is %s", $self->{productname});
65 0           my @services = ();
66 0           my @servicedescs = $xpc->find('(//n:service)')->get_nodelist;
67 0           foreach my $service (@servicedescs) {
68 0           my $servicetype = undef;
69 0           my $serviceid = undef;
70 0           my $controlurl = undef;
71 0           foreach my $node ($service->nonBlankChildNodes("./*")) {
72 0 0         $serviceid = $node->textContent if ($node->nodeName eq "serviceId");
73 0 0         $servicetype = $node->textContent if ($node->nodeName eq "serviceType");
74 0 0         $controlurl = $node->textContent if ($node->nodeName eq "controlURL");
75             }
76 0 0 0       if ($serviceid && $controlurl) {
77 0           push(@services, {
78             serviceType => $servicetype,
79             serviceId => $serviceid,
80             controlURL => sprintf('http://%s:%s%s',
81             $self->opts->hostname, $self->opts->port, $controlurl),
82             });
83 0           $self->debug(sprintf "found %s service %s",
84             $servicetype, $serviceid);
85             }
86             }
87 0           $self->set_variable('services', \@services);
88             };
89 0 0         if ($@) {
90 0           $self->add_critical($@);
91             }
92             }
93 0 0         if (! $self->check_messages()) {
94 0           eval {
95 0           my $service = (grep { $_->{serviceId} =~ /WANIPConn1/ } @{$self->get_variable('services')})[0];
  0            
  0            
96             my $som = SOAP::Lite
97             -> proxy($service->{controlURL})
98             -> uri($service->{serviceType})
99 0           -> GetStatusInfo();
100 0           $self->{uptime} = $som->valueof("//GetStatusInfoResponse/NewUptime");
101 0           $self->{uptime} /= 1.0;
102 0           $self->debug("WANIPConn1->GetStatusInfo returned uptime");
103             };
104 0 0         if ($@) {
105 0           $self->add_critical("could not get uptime: ".$@);
106             }
107             }
108             }
109              
110             sub create_statefile {
111 0     0 0   my ($self, %params) = @_;
112 0           my $extension = "";
113 0 0         $extension .= $params{name} ? '_'.$params{name} : '';
114 0 0         if ($self->opts->community) {
115 0           $extension .= md5_hex($self->opts->community);
116             }
117 0           $extension =~ s/\//_/g;
118 0           $extension =~ s/\(/_/g;
119 0           $extension =~ s/\)/_/g;
120 0           $extension =~ s/\*/_/g;
121 0           $extension =~ s/\s/_/g;
122 0 0         if ($^O =~ /MSWin/) {
123 0           $extension =~ s/:/_/g;
124             }
125 0           return sprintf "%s/%s_%s%s", $self->statefilesdir(),
126             $self->opts->hostname, $self->opts->mode, lc $extension;
127             }
128              
129             1;
130              
131             __END__