/[nagios-plugins-perl]/trunk/plugins/NSD.pm
ViewVC logotype

Contents of /trunk/plugins/NSD.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 121 - (show annotations) (download)
Thu Nov 12 13:29:04 2009 UTC (11 years ago) by racvision
File size: 4171 byte(s)
convert source structure from autotools to rpm-build.sh
1 #!/usr/bin/perl -w
2 #
3 # Copyright (c) 2002-2004 Stéphane Urbanovski <stephane.urbanovski@ac-nancy-metz.fr>
4 #
5 # Copyright (c) 1999 Charlie Cook & Nick Reinking
6 #
7 #
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty
15 # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # you should have received a copy of the GNU General Public License
19 # along with this program (or with Netsaint); if not, write to the
20 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 # Boston, MA 02111-1307, USA
22 #
23 #
24 package NSD ;
25
26 use strict; # should never be differently :-)
27 use warnings;
28
29 use Carp;
30
31 use Socket; # Needed to connect to remote Nagiosstatd.
32
33 our $VERSION = '1.00';
34
35 # Nagiosstatd connection port:
36 our $NSD_DEFAULT_PORT = 1970;
37
38 # Nagiosstatd response timout :
39 our $NSD_TIMEOUT = 15;
40
41
42 sub new ($) {
43 my $pkg=shift;
44 my %param = @_;
45
46 my %conf = (
47 NSD_VERSION => $VERSION
48 );
49
50 $conf{DEBUG} = 0;
51 if ( exists($param{debug}) ) {
52 $conf{DEBUG} = $param{debug};
53 }
54
55 if ( ! defined($param{address}) ) {
56 croak ("<address> not defined !");
57 } else {
58 $conf{NSD_ADDRESS} = $param{address};
59 }
60
61 if ( ! defined($param{port}) ) {
62 $conf{NSD_PORT} = $NSD_DEFAULT_PORT;
63 } else {
64 $conf{NSD_PORT} = $param{port};
65 }
66
67 if ( ! defined($param{timeout}) ) {
68 $conf{NSD_TIMEOUT} = $NSD_TIMEOUT;
69 } else {
70 $conf{NSD_TIMEOUT} = $param{timeout};
71 }
72
73
74 $conf{NSD_ERROR} = _("Error message not set.");
75 bless \%conf,$pkg ;
76 return \%conf;
77 }
78
79 sub connect {
80 # Connect to remote Nagiosstatd:
81 my $o = shift;
82
83 my $remoteaddr ;
84 my $paddr;
85
86 if ( ! ($remoteaddr = inet_aton($o->{NSD_ADDRESS})) ) {
87 $o->{NSD_ERROR} = sprintf (_("Unkown host: %s %s"),$o->{NSD_ADDRESS},$!);
88 return 1;
89 } elsif ( ! ($paddr = sockaddr_in($o->{NSD_PORT}, $remoteaddr)) ) {
90 $o->{NSD_ERROR} = sprintf (_("Can't create info for connection: %s"),$!);
91 return 1;
92 } elsif ( !socket(SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp')) ) {
93 $o->{NSD_ERROR} = sprintf (_("Can't create socket: %s"),$!);
94 return 1;
95 }
96
97 setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1);
98
99 if ( ! connect(SERVER, $paddr) ) {
100 $o->{NSD_ERROR} = sprintf (_("Can't connect to NagiosStatd at \'%s:%s\' : %s"),$o->{NSD_ADDRESS},$o->{NSD_PORT},$!);
101 return 1;
102 }
103
104 # remove buffers:
105 select(SERVER);
106 $| = 1;
107 select(STDOUT);
108
109 # save filehandler:
110 $o->{TCP_SOCKET} = \*SERVER;
111
112 return 0;
113 }
114
115
116 sub close {
117 # Close connection
118 my $o = shift;
119 close($o->{TCP_SOCKET});
120 return 0;
121 }
122
123 sub ask {
124 # Ask:
125 my $o = shift;
126 my ($service) = @_ ;
127 my $SERVER = $o->{TCP_SOCKET};
128
129 # Just in case of problems, let's not hang the plugin
130 $SIG{'ALRM'} = sub {
131 $o->close();
132 select(STDOUT);
133 $o->{NSD_ERROR} = _("No Answer from NagiosStatd !");
134 return 1;
135 };
136
137 if ( $o->{DEBUG} ) { print "Send [ ".$service."]\n"; }
138
139 alarm($o->{NSD_TIMEOUT});
140 print $SERVER "$service\n";
141 my ($servanswer) = <$SERVER>;
142 alarm(0);
143 $o->close();
144
145 if ( !$servanswer ) {
146 $servanswer = "";
147 }
148
149 chomp($servanswer) ;
150 $o->{NSD_RETURN} = $servanswer;
151
152 if ( $o->{DEBUG} ) { print "Received [ ".$servanswer."]\n"; }
153
154 return 0;
155 }
156
157
158 sub getResponse {
159 # Return nagiosstatd response:
160 my $o = shift;
161 return $o->{NSD_RETURN} ;
162 }
163
164 sub getErrorMsg {
165 # Return last error msg:
166 my $o = shift;
167 return $o->{NSD_ERROR} ;
168 }
169
170 return 1;
171
172 =head1 NAME
173
174 This module deal with the TCP connection to a remote Nagiosstatd.
175
176 =head1 EXAMPLE
177
178 use libplugins;
179 use NSD;
180
181 # Connect to remote Nagiosstatd:
182 my $o_nsd = new NSD ( address => '127.0.0.1' , port => 1970, debug => 0);
183
184 if ( $o_nsd->connect() ) {
185 print $o_nsd->getErrorMsg()."\n";
186 exit $EXIT_CODES{'UNKNOWN'};
187 }
188 if ( $o_nsd->ask($service) ) {
189 $o_nsd->close();
190 print $o_nsd->getErrorMsg()."\n";
191 exit $EXIT_CODES{'UNKNOWN'};
192 }
193 $o_nsd->close();
194 my $servanswer = $o_nsd->getResponse();
195
196 =cut

  ViewVC Help
Powered by ViewVC 1.1.8