| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Ocs::Nagios; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
39142
|
use strict; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
46
|
|
|
4
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
41
|
|
|
5
|
1
|
|
|
1
|
|
2671
|
use SOAP::Lite; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use XML::Entities; |
|
7
|
|
|
|
|
|
|
use XML::Simple; |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new(){ |
|
10
|
|
|
|
|
|
|
my $class=shift; |
|
11
|
|
|
|
|
|
|
my %args=ref($_[0])?%{$_[0]}:@_; |
|
12
|
|
|
|
|
|
|
my $self=\%args; |
|
13
|
|
|
|
|
|
|
bless $self, $class; |
|
14
|
|
|
|
|
|
|
return $self; |
|
15
|
|
|
|
|
|
|
}; |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub init(){ |
|
19
|
|
|
|
|
|
|
my $self = shift; |
|
20
|
|
|
|
|
|
|
my $server=$self->{'server'}; |
|
21
|
|
|
|
|
|
|
my $soap_user=$self->{'soap_user'}; |
|
22
|
|
|
|
|
|
|
my $soap_pass=$self->{'soap_pass'}; |
|
23
|
|
|
|
|
|
|
my $soap_port=$self->{'soap_port'}; |
|
24
|
|
|
|
|
|
|
my %devices_list=$self->_get_devices($server,$soap_user,$soap_pass,$soap_port); |
|
25
|
|
|
|
|
|
|
return %devices_list; |
|
26
|
|
|
|
|
|
|
} |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub _get_devices(){ |
|
29
|
|
|
|
|
|
|
my $self = shift; |
|
30
|
|
|
|
|
|
|
my($s,$u,$pw,$port) = @_; |
|
31
|
|
|
|
|
|
|
my $method="get_computers_V1"; |
|
32
|
|
|
|
|
|
|
my $proto; |
|
33
|
|
|
|
|
|
|
my $result; |
|
34
|
|
|
|
|
|
|
my @result; |
|
35
|
|
|
|
|
|
|
my @split; |
|
36
|
|
|
|
|
|
|
my %devices; |
|
37
|
|
|
|
|
|
|
my $devices; |
|
38
|
|
|
|
|
|
|
my $hostname; |
|
39
|
|
|
|
|
|
|
my $hostip; |
|
40
|
|
|
|
|
|
|
if ( $port == 80 ) { |
|
41
|
|
|
|
|
|
|
$proto="http"; |
|
42
|
|
|
|
|
|
|
} |
|
43
|
|
|
|
|
|
|
elsif ( $port == 443 ) { |
|
44
|
|
|
|
|
|
|
$proto="https"; |
|
45
|
|
|
|
|
|
|
} |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
my @params=" |
|
48
|
|
|
|
|
|
|
FIRST |
|
49
|
|
|
|
|
|
|
INVENTORY |
|
50
|
|
|
|
|
|
|
1 |
|
51
|
|
|
|
|
|
|
0 |
|
52
|
|
|
|
|
|
|
0 |
|
53
|
|
|
|
|
|
|
"; |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
my $lite = SOAP::Lite |
|
56
|
|
|
|
|
|
|
->uri("$proto://$s/Apache/Ocsinventory/Interface") |
|
57
|
|
|
|
|
|
|
->proxy("$proto://$u".($u?':':'').($u?"$pw\@":'')."$s/ocsinterface\n") |
|
58
|
|
|
|
|
|
|
->$method(@params); |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
if($lite->fault){ |
|
61
|
|
|
|
|
|
|
print "ERROR:\n\n",XML::Entities::decode( 'all', $lite->fault->{faultstring} ),"\nEND\n\n"; |
|
62
|
|
|
|
|
|
|
} else{ |
|
63
|
|
|
|
|
|
|
my $i = 0; |
|
64
|
|
|
|
|
|
|
for( $lite->paramsall ){ |
|
65
|
|
|
|
|
|
|
if (/^/) { |
|
66
|
|
|
|
|
|
|
$result=$self->_ocs2nagios(XML::Entities::decode( 'all', $_ )); |
|
67
|
|
|
|
|
|
|
@split= split(";",$result); |
|
68
|
|
|
|
|
|
|
$hostname=$split[0]; |
|
69
|
|
|
|
|
|
|
$hostip=$split[1]; |
|
70
|
|
|
|
|
|
|
$devices{$hostname} = $hostip; |
|
71
|
|
|
|
|
|
|
$i++; |
|
72
|
|
|
|
|
|
|
} |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
} |
|
75
|
|
|
|
|
|
|
return %devices; |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub _ocs2nagios() { |
|
79
|
|
|
|
|
|
|
my $self = shift; |
|
80
|
|
|
|
|
|
|
my $xml = new XML::Simple; |
|
81
|
|
|
|
|
|
|
my @datas= @_; |
|
82
|
|
|
|
|
|
|
my $data = $xml->XMLin(@datas); |
|
83
|
|
|
|
|
|
|
return ($data->{HARDWARE}->{NAME}.";".$data->{HARDWARE}->{IPADDR}); |
|
84
|
|
|
|
|
|
|
} |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub host(){ |
|
87
|
|
|
|
|
|
|
my $self = shift; |
|
88
|
|
|
|
|
|
|
my %opts=ref($_[0])?%{$_[0]}:@_; |
|
89
|
|
|
|
|
|
|
my $self_host=\%opts; |
|
90
|
|
|
|
|
|
|
$self->{'_HOSTNAME'} = $self_host->{'host'}; |
|
91
|
|
|
|
|
|
|
$self->{'_IP'} = $self_host->{'ip'}; |
|
92
|
|
|
|
|
|
|
open(FILE,">> $self->{'directory'}/$self_host->{'host'}\.cfg"); |
|
93
|
|
|
|
|
|
|
print FILE "define host{ \n"; |
|
94
|
|
|
|
|
|
|
print FILE "\t use $self_host->{'template'} \n"; |
|
95
|
|
|
|
|
|
|
print FILE "\t host_name $self->{'_HOSTNAME'} \n"; |
|
96
|
|
|
|
|
|
|
print FILE "\t address $self->{'_IP'} \n"; |
|
97
|
|
|
|
|
|
|
print FILE "} \n\n"; |
|
98
|
|
|
|
|
|
|
close(FILE); |
|
99
|
|
|
|
|
|
|
return $self_host; |
|
100
|
|
|
|
|
|
|
} |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub service(){ |
|
103
|
|
|
|
|
|
|
my $self = shift; |
|
104
|
|
|
|
|
|
|
my %s_opts=ref($_[0])?%{$_[0]}:@_; |
|
105
|
|
|
|
|
|
|
my $self_service=\%s_opts; |
|
106
|
|
|
|
|
|
|
open(FILE,">> $self->{'directory'}/$self->{'_HOSTNAME'}\.cfg"); |
|
107
|
|
|
|
|
|
|
print FILE "\n"; |
|
108
|
|
|
|
|
|
|
print FILE "define service{ \n"; |
|
109
|
|
|
|
|
|
|
print FILE "\t use $self_service->{'template'} \n"; |
|
110
|
|
|
|
|
|
|
print FILE "\t host_name $self->{'_HOSTNAME'} \n"; |
|
111
|
|
|
|
|
|
|
print FILE "\t service_description $self_service->{'service_description'} \n"; |
|
112
|
|
|
|
|
|
|
print FILE "\t check_command $self_service->{'check_command'} \n"; |
|
113
|
|
|
|
|
|
|
print FILE "} \n"; |
|
114
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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<< >> |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=head1 BUGS |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
|
201
|
|
|
|
|
|
|
the web interface at L. 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 |
|
221
|
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
|
223
|
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
L |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=item * CPAN Ratings |
|
227
|
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
L |
|
229
|
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=item * Search CPAN |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
L |
|
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
|
|
|
|
|
|
|
1; # End of Ocs::Nagios |