#!/usr/bin/perl -w # Copyright (C) 2020 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 =~ /^(?:|.*\/)nat_([^_]+)_nat$/) { $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 nat requests\n"; print "graph_args --base 1000 -l 0 -u 65535\n"; print "graph_vlabel requests\n"; print "graph_order requests\n"; print "graph_scale no\n"; print "graph_category system\n"; print "requests.label requests\n"; print "requests.cdef requests\n"; print "requests.type GAUGE\n"; print "requests.draw AREA\n"; print "requests.min 0\n"; print "requests.max 65535\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 nat descriptor address'; 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'); @lines = $t->cmd($natget_cmd); #print @lines; $natcount = 0; $match = 0; foreach $text (@lines) { $str = decode('cp932', $text); #print $str; if ($str =~ m/^.*?(\d+)個使用中.*?/) { $match = 0; $natcount = $1; last; } if ($match == 0) { if ($str =~ m/^.*?(\d+)(\s)セッション.*?/) { $natcount = $1; last; } } } #print "Nat: $natcount" . "\n"; return ($natcount); } #print $rtx_host; #print $rtx_passwd_l; #print $rtx_passwd_a; print "requests.value ", &query_rtx ($rtx_host, $rtx_passwd_l, $rtx_passwd_a), "\n";