| 1 | package Ocs::Nagios; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 2 | ▶ | use SOAP::Lite; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 2 | ▶ | use XML::Entities; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 2 | ▶ | use XML::Simple; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 0 | ▶ | sub new(){ | ||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 10 | 0 | my $class=shift; | |||||||||||||
| 11 | 0 | ▶ | my %args=ref($_[0])?%{$_[0]}:@_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 12 | 0 | my $self=\%args; | |||||||||||||
| 13 | 0 | bless $self, $class; | |||||||||||||
| 14 | 0 | return $self; | |||||||||||||
| 15 | }; | ||||||||||||||
| 16 | |||||||||||||||
| 17 | |||||||||||||||
| 18 | 0 | ▶ | sub init(){ | ||||||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 19 | 0 | my $self = shift; | |||||||||||||
| 20 | 0 | my $server=$self->{'server'}; | |||||||||||||
| 21 | 0 | my $soap_user=$self->{'soap_user'}; | |||||||||||||
| 22 | 0 | my $soap_pass=$self->{'soap_pass'}; | |||||||||||||
| 23 | 0 | my $soap_port=$self->{'soap_port'}; | |||||||||||||
| 24 | 0 | my %devices_list=$self->_get_devices($server,$soap_user,$soap_pass,$soap_port); | |||||||||||||
| 25 | 0 | return %devices_list; | |||||||||||||
| 26 | } | ||||||||||||||
| 27 | |||||||||||||||
| 28 | 0 | ▶ | sub _get_devices(){ | ||||||||||||
|
Subroutine
_get_devices: not called CC=8 SCAR=42.8
| |||||||||||||||
| 29 | 0 | my $self = shift; | |||||||||||||
| 30 | 0 | my($s,$u,$pw,$port) = @_; | |||||||||||||
| 31 | 0 | my $method="get_computers_V1"; | |||||||||||||
| 32 | 0 | my $proto; | |||||||||||||
| 33 | my $result; | ||||||||||||||
| 34 | 0 | my @result; | |||||||||||||
| 35 | 0 | my @split; | |||||||||||||
| 36 | 0 | my %devices; | |||||||||||||
| 37 | 0 | my $devices; | |||||||||||||
| 38 | 0 | my $hostname; | |||||||||||||
| 39 | 0 | my $hostip; | |||||||||||||
| 40 | 0 | ▶ | if ( $port == 80 ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 41 | 0 | $proto="http"; | |||||||||||||
| 42 | } | ||||||||||||||
| 43 | elsif ( $port == 443 ) { | ||||||||||||||
| 44 | 0 | $proto="https"; | |||||||||||||
| 45 | } | ||||||||||||||
| 46 | |||||||||||||||
| 47 | 0 | my @params="<REQUEST> | |||||||||||||
| 48 | <ENGINE>FIRST</ENGINE> | ||||||||||||||
| 49 | <ASKING_FOR>INVENTORY</ASKING_FOR> | ||||||||||||||
| 50 | <CHECKSUM>1</CHECKSUM> | ||||||||||||||
| 51 | <OFFSET>0</OFFSET> | ||||||||||||||
| 52 | <WANTED>0</WANTED> | ||||||||||||||
| 53 | </REQUEST>"; | ||||||||||||||
| 54 | |||||||||||||||
| 55 | 0 | ▶ | my $lite = SOAP::Lite | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 56 | ->uri("$proto://$s/Apache/Ocsinventory/Interface") | ||||||||||||||
| 57 | ->proxy("$proto://$u".($u?':':'').($u?"$pw\@":'')."$s/ocsinterface\n") | ||||||||||||||
| 58 | ->$method(@params); | ||||||||||||||
| 59 | |||||||||||||||
| 60 | 0 | ▶ | if($lite->fault){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 61 | 0 | print "ERROR:\n\n",XML::Entities::decode( 'all', $lite->fault->{faultstring} ),"\nEND\n\n"; | |||||||||||||
| 62 | } else{ | ||||||||||||||
| 63 | 0 | my $i = 0; | |||||||||||||
| 64 | 0 | for( $lite->paramsall ){ | |||||||||||||
| 65 | 0 | ▶ | if (/^<COMPUTER>/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | 0 | $result=$self->_ocs2nagios(XML::Entities::decode( 'all', $_ )); | |||||||||||||
| 67 | 0 | @split= split(";",$result); | |||||||||||||
| 68 | 0 | $hostname=$split[0]; | |||||||||||||
| 69 | 0 | $hostip=$split[1]; | |||||||||||||
| 70 | 0 | $devices{$hostname} = $hostip; | |||||||||||||
| 71 | 0 | $i++; | |||||||||||||
| 72 | } | ||||||||||||||
| 73 | } | ||||||||||||||
| 74 | } | ||||||||||||||
| 75 | 0 | return %devices; | |||||||||||||
| 76 | } | ||||||||||||||
| 77 | |||||||||||||||
| 78 | 0 | ▶ | sub _ocs2nagios() { | ||||||||||||
|
Subroutine
_ocs2nagios: not called CC=1 SCAR=6.9
| |||||||||||||||
| 79 | 0 | my $self = shift; | |||||||||||||
| 80 | 0 | my $xml = new XML::Simple; | |||||||||||||
| 81 | 0 | my @datas= @_; | |||||||||||||
| 82 | 0 | my $data = $xml->XMLin(@datas); | |||||||||||||
| 83 | 0 | return ($data->{HARDWARE}->{NAME}.";".$data->{HARDWARE}->{IPADDR}); | |||||||||||||
| 84 | } | ||||||||||||||
| 85 | |||||||||||||||
| 86 | 0 | ▶ | sub host(){ | ||||||||||||
|
Subroutine
host: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 87 | 0 | my $self = shift; | |||||||||||||
| 88 | 0 | ▶ | my %opts=ref($_[0])?%{$_[0]}:@_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 89 | 0 | my $self_host=\%opts; | |||||||||||||
| 90 | 0 | $self->{'_HOSTNAME'} = $self_host->{'host'}; | |||||||||||||
| 91 | 0 | $self->{'_IP'} = $self_host->{'ip'}; | |||||||||||||
| 92 | 0 | open(FILE,">> $self->{'directory'}/$self_host->{'host'}\.cfg"); | |||||||||||||
| 93 | 0 | print FILE "define host{ \n"; | |||||||||||||
| 94 | 0 | print FILE "\t use $self_host->{'template'} \n"; | |||||||||||||
| 95 | 0 | print FILE "\t host_name $self->{'_HOSTNAME'} \n"; | |||||||||||||
| 96 | 0 | print FILE "\t address $self->{'_IP'} \n"; | |||||||||||||
| 97 | 0 | print FILE "} \n\n"; | |||||||||||||
| 98 | 0 | close(FILE); | |||||||||||||
| 99 | 0 | return $self_host; | |||||||||||||
| 100 | } | ||||||||||||||
| 101 | |||||||||||||||
| 102 | 0 | ▶ | sub service(){ | ||||||||||||
|
Subroutine
service: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 103 | 0 | my $self = shift; | |||||||||||||
| 104 | 0 | ▶ | my %s_opts=ref($_[0])?%{$_[0]}:@_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 105 | 0 | my $self_service=\%s_opts; | |||||||||||||
| 106 | 0 | open(FILE,">> $self->{'directory'}/$self->{'_HOSTNAME'}\.cfg"); | |||||||||||||
| 107 | 0 | print FILE "\n"; | |||||||||||||
| 108 | 0 | print FILE "define service{ \n"; | |||||||||||||
| 109 | 0 | print FILE "\t use $self_service->{'template'} \n"; | |||||||||||||
| 110 | 0 | print FILE "\t host_name $self->{'_HOSTNAME'} \n"; | |||||||||||||
| 111 | 0 | print FILE "\t service_description $self_service->{'service_description'} \n"; | |||||||||||||
| 112 | 0 | print FILE "\t check_command $self_service->{'check_command'} \n"; | |||||||||||||
| 113 | 0 | print FILE "} \n"; | |||||||||||||
| 114 | 0 | close(FILE); | |||||||||||||
| 115 | } | ||||||||||||||
| 116 | |||||||||||||||
| 117 | |||||||||||||||
| 118 | =head1 NAME | ||||||||||||||
| 119 | |||||||||||||||
| 120 | Ocs::Nagios - Import OCS Inventory devices in Nagios | ||||||||||||||
| 121 | |||||||||||||||
| 122 | =head1 VERSION | ||||||||||||||
| 123 | |||||||||||||||
| 124 | Version 0.02 | ||||||||||||||
| 125 | |||||||||||||||
| 126 | =cut | ||||||||||||||
| 127 | |||||||||||||||
| 128 | 2 | our $VERSION = '0.02'; | |||||||||||||
| 129 | |||||||||||||||
| 130 | |||||||||||||||
| 131 | =head1 SYNOPSIS | ||||||||||||||
| 132 | |||||||||||||||
| 133 | use Ocs::Nagios; | ||||||||||||||
| 134 | |||||||||||||||
| 135 | my $obj = new Ocs::Nagios( server => $server, | ||||||||||||||
| 136 | soap_user => $soap_user, | ||||||||||||||
| 137 | soap_pass => $soap_pass, | ||||||||||||||
| 138 | soap_port => $soap_port, | ||||||||||||||
| 139 | directory => $directory | ||||||||||||||
| 140 | ); | ||||||||||||||
| 141 | ... | ||||||||||||||
| 142 | |||||||||||||||
| 143 | |||||||||||||||
| 144 | =head1 METHODS | ||||||||||||||
| 145 | |||||||||||||||
| 146 | =head2 new() - Create a new OCS::Nagios object | ||||||||||||||
| 147 | |||||||||||||||
| 148 | =head2 init() - Initialize OCS::Nagios object and get devices results | ||||||||||||||
| 149 | |||||||||||||||
| 150 | =head2 host() - Create a Host cfg File | ||||||||||||||
| 151 | |||||||||||||||
| 152 | =head2 service() - Create a Nagios Service in cfg file | ||||||||||||||
| 153 | |||||||||||||||
| 154 | =head1 EXAMPLES | ||||||||||||||
| 155 | |||||||||||||||
| 156 | =head2 1. Connect to SOAP OcsInventory Server and generate files for Nagios | ||||||||||||||
| 157 | |||||||||||||||
| 158 | This example create a .cfg file for the host, add a host definition and a service ping | ||||||||||||||
| 159 | |||||||||||||||
| 160 | use strict; | ||||||||||||||
| 161 | use warnings; | ||||||||||||||
| 162 | use Ocs::Nagios; | ||||||||||||||
| 163 | |||||||||||||||
| 164 | my $server="192.168.0.100"; | ||||||||||||||
| 165 | my $soap_user="soap"; | ||||||||||||||
| 166 | my $soap_pass="pass"; | ||||||||||||||
| 167 | my $soap_port=80; | ||||||||||||||
| 168 | my $directory="/etc/nagios2/conf.d/"; | ||||||||||||||
| 169 | |||||||||||||||
| 170 | my $obj = new Ocs::Nagios( server => $server, | ||||||||||||||
| 171 | soap_user => $soap_user, | ||||||||||||||
| 172 | soap_pass => $soap_pass, | ||||||||||||||
| 173 | soap_port => $soap_port, | ||||||||||||||
| 174 | directory => $directory | ||||||||||||||
| 175 | ); | ||||||||||||||
| 176 | |||||||||||||||
| 177 | my %hash=$obj->init(); | ||||||||||||||
| 178 | |||||||||||||||
| 179 | while ((my $host,my $ip) = each(%hash)) { | ||||||||||||||
| 180 | print "KEY : $host, Value : $ip\n"; | ||||||||||||||
| 181 | # Create a host Object | ||||||||||||||
| 182 | $obj->host( host => $host, | ||||||||||||||
| 183 | ip => $ip, | ||||||||||||||
| 184 | template => "generic-host" | ||||||||||||||
| 185 | ); | ||||||||||||||
| 186 | # Create a SERVICE for this host | ||||||||||||||
| 187 | $obj->service( template => "generic-service", | ||||||||||||||
| 188 | service_description => "PING", | ||||||||||||||
| 189 | check_command => "check_ping!100.0,20%!500.0,60%" | ||||||||||||||
| 190 | ); | ||||||||||||||
| 191 | } | ||||||||||||||
| 192 | |||||||||||||||
| 193 | |||||||||||||||
| 194 | =head1 AUTHOR | ||||||||||||||
| 195 | |||||||||||||||
| 196 | Julien SAFAR, C<< <jsasys at gmail.com> >> | ||||||||||||||
| 197 | |||||||||||||||
| 198 | =head1 BUGS | ||||||||||||||
| 199 | |||||||||||||||
| 200 | Please report any bugs or feature requests to C<bug-ocs-nagios at rt.cpan.org>, or through | ||||||||||||||
| 201 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Ocs-Nagios>. I will be notified, and then you'll | ||||||||||||||
| 202 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||
| 203 | |||||||||||||||
| 204 | |||||||||||||||
| 205 | |||||||||||||||
| 206 | |||||||||||||||
| 207 | =head1 SUPPORT | ||||||||||||||
| 208 | |||||||||||||||
| 209 | You can find documentation for this module with the perldoc command. | ||||||||||||||
| 210 | |||||||||||||||
| 211 | perldoc Ocs::Nagios | ||||||||||||||
| 212 | |||||||||||||||
| 213 | |||||||||||||||
| 214 | You can also look for information at: | ||||||||||||||
| 215 | |||||||||||||||
| 216 | =over 4 | ||||||||||||||
| 217 | |||||||||||||||
| 218 | =item * RT: CPAN's request tracker | ||||||||||||||
| 219 | |||||||||||||||
| 220 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Ocs-Nagios> | ||||||||||||||
| 221 | |||||||||||||||
| 222 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||
| 223 | |||||||||||||||
| 224 | L<http://annocpan.org/dist/Ocs-Nagios> | ||||||||||||||
| 225 | |||||||||||||||
| 226 | =item * CPAN Ratings | ||||||||||||||
| 227 | |||||||||||||||
| 228 | L<http://cpanratings.perl.org/d/Ocs-Nagios> | ||||||||||||||
| 229 | |||||||||||||||
| 230 | =item * Search CPAN | ||||||||||||||
| 231 | |||||||||||||||
| 232 | L<http://search.cpan.org/dist/Ocs-Nagios/> | ||||||||||||||
| 233 | |||||||||||||||
| 234 | =back | ||||||||||||||
| 235 | |||||||||||||||
| 236 | |||||||||||||||
| 237 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||
| 238 | |||||||||||||||
| 239 | |||||||||||||||
| 240 | =head1 COPYRIGHT & LICENSE | ||||||||||||||
| 241 | |||||||||||||||
| 242 | Copyright 2009 Julien SAFAR, all rights reserved. | ||||||||||||||
| 243 | |||||||||||||||
| 244 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||
| 245 | under the same terms as Perl itself. | ||||||||||||||
| 246 | |||||||||||||||
| 247 | |||||||||||||||
| 248 | =cut | ||||||||||||||
| 249 | |||||||||||||||
| 250 | 2 | 1; # End of Ocs::Nagios | |||||||||||||