1#!/usr/local/bin/perl
2# -*- perl -*-
3# ---------------------------------------------------- #
4# File : netscaler_conn
5# Author: Sandro Wefel
6# based on Script by Damien SIAUD
7# Date : 05/05/2011
8# Modified : 05/05/2011
9# ---------------------------------------------------- #
10# This script require Net::SNMP
11#
12# Netscaler plugin for munin
13#
14# License Information:
15# This program is free software; you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation; either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program.  If not, see <http://www.gnu.org/licenses/>.
27#
28# ---------------------------------------------------- #
29
30=head1 NAME
31
32netscaler_conn - Munin plugin to monitor netscaler connections
33
34=head1 CONFIGURATION
35
36Make a symlink from netscaler_conn_ to /etc/munin/plugins/netscaler_conn_<nsfqdn>.
37You can omit <nsfqdn>, then you need the env variable B<host>.
38
39To configure the plugin, use ENV variables.
40
41Add something lile this to /etc/munin/plugin-conf.d/<conf>
42 [netscaler_conn_*]
43 env.community CommunityName
44
45Variables:
46
47=over
48
49=item ENV B<host> netscaler host as FQDN or IP I<default is the nsfqdn-suffix from pluginname>
50
51=item ENV B<port> I<default 161>
52
53=item ENV B<community> I<default "public">
54
55=item ENV B<timeout> in seconds I<default 5>
56
57=back
58
59=head1 AUTHORS
60
61=over
62
63=item Sandro Wefel <wefel@unixos.de>
64
65=item based on scripts by Jimmy Olsen, Damien SIAUD
66
67=back
68
69=head1 LICENSE
70
71GNU General Public License
72
73=head1 MAGIC MARKERS
74
75 #%# family=auto
76 #%# capabilities=autoconf
77
78=cut
79
80use Net::SNMP;
81
82use Munin::Plugin;
83
84use vars qw($script_name $script_version $o_host $o_community $o_port $o_timeout);
85
86use strict;
87
88
89# --------------------------- globals -------------------------- #
90
91$script_name = "netscaler_conn";
92$script_version = "0.1";
93
94$o_host = undef;
95$o_community = undef;
96$o_port = 161;
97
98my $return_str = "";
99
100# ---------------------------- snmp ---------------------------- #
101
102my $oid_prefix = "1.3.6.1.4.1.5951";
103my $oid_build_version = $oid_prefix.".4.1.1.1.0";
104my $oid_ssl_session = $oid_prefix.".4.1.1.47.296.0";	# current ssl sessions
105my $oid_client_conn = $oid_prefix.".4.1.1.46.2.0";	# current client connections
106my $oid_server_conn = $oid_prefix.".4.1.1.46.1.0";	# current server connections
107
108# ---------------------------- main ----------------------------- #
109
110
111my $DEBUG = 0;
112
113my $o_host      = $ENV{host}      || undef;
114my $o_port      = $ENV{port}      || 161;
115my $o_community = $ENV{community} || "public";
116my $o_timeout   = $ENV{timeout}   || 5;
117
118if ($ARGV[0] and $ARGV[0] eq "autoconf") {
119		print "yes\n";
120		exit 0;
121}
122
123if (! defined $o_host ) {
124	$0 =~ /.*onnections?_(.+)*$/;
125	$o_host = $1;
126	die "No host provided or unabled to extract hostname from $0" unless defined $o_host;
127}
128
129if ($ARGV[0] and $ARGV[0] eq "config") {
130    print "graph_args --base 1024 -l 0\n";
131    print "graph_vlabel Connections\n";
132    print "graph_title Netscaler Connections for $o_host\n";
133    print "graph_category loadbalancer\n";
134    print "graph_period second\n";
135    print "graph_info This graph shows the netscaler TCP connections.\n";
136    print "graph_order ",
137	"client ",
138	"server ",
139	"ssl ",
140	"\n";
141    print "client.label client\n";
142    print "client.draw AREA\n";
143    print "client.info Client connections.\n";
144    print "server.label server\n";
145    print "server.draw STACK\n";
146    print "server.info Server connections.\n";
147    print "ssl.label SSL sessions\n";
148    print "ssl.draw LINE2\n";
149    print "ssl.info Currently active SSL sessions.\n";
150
151    for my $field (qw(client server ssl)) {
152        print_thresholds($field);
153    }
154    exit 0;
155}
156
157
158my $session = &open_session();
159if (!defined($session)) {
160	print "ERROR opening session\n";
161	exit 1;
162}
163
164my $counter1;
165# TCP
166$counter1 = &get_oid_values($session,$oid_client_conn);
167$return_str .= "client.value $counter1\n";
168$counter1 = &get_oid_values($session,$oid_server_conn);
169$return_str .= "server.value $counter1\n";
170# SSL
171$counter1 = &get_oid_values($session,$oid_ssl_session);
172$return_str .= "ssl.value $counter1\n";
173
174&close_session($session);
175
176print "$return_str";
177exit 0;
178
179# --------------------------- functions ------------------------- #
180
181sub open_session {
182	my ($sess, $str) = Net::SNMP->session(
183		-hostname	=> $o_host,
184		-community	=> $o_community,
185		-port		=> $o_port,
186		-timeout	=> $o_timeout
187	);
188
189	$return_str = $str;
190
191	return $sess;
192}
193
194
195sub close_session {
196	my ($sess) = @_;
197
198	if(defined($sess)){
199		$session->close;
200	}
201}
202
203sub get_buildversion {
204	my ($session) = @_;
205       	my $build_version;
206
207	my $result = $session->get_request(
208		      -varbindlist => [$oid_build_version]
209	);
210
211	if (!defined($result)) {
212		return "na";
213	}
214	else {
215		$build_version = $result->{$oid_build_version};
216		return"Build version : ".$build_version;
217	}
218}
219
220sub get_oid_values {
221        my ($session,$oid_string) = @_;
222        my $return_value;
223
224        my $result = $session->get_request(
225                -varbindlist => [$oid_string]
226        );
227
228        if (!defined($result)) {
229                return "na";
230        }
231        else {
232                $return_value = $result->{$oid_string};
233                return $return_value;
234        }
235}
236
237