/[rrd-simple-monitoring]/bin/rrd-client-nagios-perfdata.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /bin/rrd-client-nagios-perfdata.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations)
Thu Jul 16 18:48:19 2009 UTC (14 years, 8 months ago) by dpavlin
File MIME type: text/plain
File size: 4180 byte(s)
import upstream http://rrd.me.uk/rrd-simple-monitoring.tar.gz

without prerequisities

1 #!/usr/bin/perl -w
2 ############################################################
3 #
4 # $Id: rrd-client.pl 775 2006-10-08 18:47:33Z nicolaw $
5 # rrd-client-nagios-perfdata.pl - Send Nagios performance data to rrd-server.cgi
6 #
7 # Copyright 2007 Nicola Worthington
8 #
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file except in compliance with the License.
11 # You may obtain a copy of the License at
12 #
13 # http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20 #
21 ############################################################
22 # vim:ts=4:sw=4:tw=78
23
24 use 5.6.1;
25 use strict;
26 use warnings;
27 no warnings qw(redefine);
28 use Getopt::Std qw();
29 use vars qw($VERSION);
30
31 $VERSION = '1.42' || sprintf('%d', q$Revision: 775 $ =~ /(\d+)/g);
32 $ENV{PATH} = '/bin:/usr/bin';
33 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
34
35 # Get command line options
36 my %opt = ();
37 $Getopt::Std::STANDARD_HELP_VERSION = 1;
38 $Getopt::Std::STANDARD_HELP_VERSION = 1;
39 Getopt::Std::getopts('l:e:T:H:A:s:o:e:p:vh',\%opt);
40 (HELP_MESSAGE() && exit) if defined $opt{h} || defined $opt{'?'};
41 (VERSION_MESSAGE() && exit) if defined $opt{v};
42 (HELP_MESSAGE() && exit) unless defined $opt{l} && defined $opt{e} && defined $opt{s} && defined $opt{p};
43
44 my $time = $opt{T}; $time ||= time;
45 (my $svc = $opt{s}) =~ s/[^a-zA-Z_0-9]//g;
46 my $type = 'service';
47
48 # Only process the following:
49 my @svc_ok = qw(smtp mysql ssh http https imap imaps pop pops);
50 exit unless grep(lc($opt{s}) eq $_, @svc_ok);
51
52 # Build the data
53 my $post;
54 $post .= sprintf("%d.nagios.perfdata.%s.%s.latency %f\n", $time, $type, lc($svc), $opt{l});
55 $post .= sprintf("%d.nagios.perfdata.%s.%s.execution %f\n", $time, $type, lc($svc), $opt{e});
56
57 # HTTP POST the data if asked to
58 print scalar(basic_http('POST',$opt{p},10,$post,$opt{q}))."\n" if $opt{p};
59
60 exit;
61
62
63 # Display help
64 sub HELP_MESSAGE {
65 print qq{Syntax: rrd-client-nagios-perfdata.pl [OPTIONS]
66 -l %SERVICELATENCY%
67 -e
68 -s
69 -c
70 -p <URL> HTTP POST data to the specified URL
71 -q Suppress all warning messages
72 -v Display version information
73 -h Display this help
74 \n};
75 }
76
77 # Display version
78 sub VERSION { &VERSION_MESSAGE; }
79 sub VERSION_MESSAGE {
80 print "$0 version $VERSION ".'($Id: rrd-client-nagios-perfdata.pl 775 2006-10-08 18:47:33Z nicolaw $)'."\n";
81 }
82
83 # Basic HTTP client if LWP is unavailable
84 sub basic_http {
85 my ($method,$url,$timeout,$data,$quiet) = @_;
86 $method ||= 'GET';
87 $url ||= 'http://localhost/';
88 $timeout ||= 5;
89
90 my ($scheme,$host,$port,$path) = $url =~ m,^(https?://)([\w\d\.\-]+)(?::(\d+))?(.*),i;
91 $scheme ||= 'http://';
92 $host ||= 'localhost';
93 $path ||= '/';
94 $port ||= 80;
95
96 my $str = '';
97 eval "use Socket";
98 return $str if $@;
99
100 eval {
101 local $SIG{ALRM} = sub { die "TIMEOUT\n" };
102 alarm $timeout;
103
104 my $iaddr = inet_aton($host) || die;
105 my $paddr = sockaddr_in($port, $iaddr);
106 my $proto = getprotobyname('tcp');
107 socket(SOCK, AF_INET(), SOCK_STREAM(), $proto) || die "socket: $!";
108 connect(SOCK, $paddr) || die "connect: $!";
109
110 select(SOCK); $| = 1;
111 select(STDOUT);
112
113 # Send the HTTP request
114 print SOCK "$method $path HTTP/1.1\n";
115 print SOCK "Host: $host". ("$port" ne "80" ? ":$port" : '') ."\n";
116 print SOCK "User-Agent: $0 version $VERSION ".'($Id: rrd-client-nagios-perfdata.pl 775 2006-10-08 18:47:33Z nicolaw $)'."\n";
117 if ($data && $method eq 'POST') {
118 print SOCK "Content-Length: ". length($data) ."\n";
119 print SOCK "Content-Type: application/x-www-form-urlencoded\n";
120 }
121 print SOCK "\n";
122 print SOCK $data if $data && $method eq 'POST';
123
124 my $body = 0;
125 while (local $_ = <SOCK>) {
126 s/[\n\n]+//g;
127 $str .= $_ if $_ && $body;
128 $body = 1 if /^\s*$/;
129 }
130 close(SOCK);
131 alarm 0;
132 };
133
134 warn "Warning [basic_http]: $@" if !$quiet && $@ && $data;
135 return wantarray ? split(/\n/,$str) : "$str";
136 }
137
138 1;
139

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26