#!/usr/bin/perl -w # Copyright (C) 2020.05.30 debizoh / Masami Ohkubo use Encode 'decode'; use utf8; use Net::Telnet(); my $rtx_host = $ENV{host} || undef; my $rtx_passwd_l = $ENV{login_password} || undef; my $rtx_passwd_a = $ENV{admin_password} || undef; my $DEBUG = 0; my $rtx_port = 23; if ($0 =~ /^(?:|.*\/)vpn_([^_]+)_vpn$/) { $rtx_host = $1; if ($rtx_host =~ /^([^:]+):(\d+)$/) { $rtx_host = $1; $rtx_port = $2; } } elsif (!defined($rtx_host)) { print "# Debug: $0 -- $1\n" if $DEBUG; die "# Error: couldn't understand what I'm supposed to monitor."; } if($ARGV[0] and $ARGV[0] eq "config") { print "host_name $rtx_host\n"; print "graph_title RTX vpn session\n"; print "graph_args --base 1000 -l 0 -u 60\n"; print "graph_vlabel session\n"; print "graph_order l2tp pptp\n"; print "graph_scale no\n"; print "graph_category system\n"; print "graph_total total\n"; print "l2tp.label L2TP\n"; print "l2tp.type GAUGE\n"; print "l2tp.draw AREA\n"; print "l2tp.min 0\n"; print "l2tp.max 60\n"; print "pptp.label PPTP\n"; print "pptp.type GAUGE\n"; print "pptp.draw STACK\n"; print "pptp.min 0\n"; print "pptp.max 60\n"; exit 0; } sub query_rtx { my ($host, $login_password, $admin_password) = @_; $host = $host; $login_password = $login_password; $admin_password = $admin_password; $natget_cmd = 'show status tunnel up'; use Net::Telnet (); my($t) = new Net::Telnet (Timeout => 10,Binmode=> 1); $t->input_record_separator("\n"); $t->output_record_separator("\n"); $t->open($host); $t->waitfor('/Password: ?$/i'); $t->print($login_password); $t->prompt('/> $/'); #doesn't require administrator #$t->print('administrator'); #$t->waitfor('/Password: ?$/i'); #$t->print($admin_password); #$t->waitfor('/\# ?$/i'); #$t->prompt('/# ?$/'); $t->print('console lines infinity'); #$t->open($host); #$t->login($username, $passwd); @lines = $t->cmd($natget_cmd); #print @lines; $vpncount = 0; $match = 0; foreach $text (@lines) { $str = decode('cp932', $text); #print $str; if ($str =~ m/^合計:\s(\d+).*?/) { $match = 0; $vpncount = $1; last; } #if ($match == 0) #{ # if ($str =~ m/^.*?(\d+)(\s)セッション.*?/) # { # $vpncount = $1; # last; # } #} } #get pptp $natget_cmd = 'show status pptp'; @lines = $t->cmd($natget_cmd); $pptpcount = 0; $match = 0; foreach $text (@lines) { $str = decode('cp932', $text); #print $str; if ($str =~ m/^\s+Tunnel Control:\s(\d+).*?/) { $match = 0; $pptpcount = $1; last; } #if ($match == 0) #{ # if ($str =~ m/^.*?(\d+)(\s)セッション.*?/) # { # $vpncount = $1; # last; # } #} } $return_value = qq{l2tp.value $vpncount\n}; $return_value .= qq{pptp.value $pptpcount\n}; return ($return_value); } $return_value = &query_rtx ($rtx_host, $rtx_passwd_l, $rtx_passwd_a); print $return_value;