PDA

View Full Version : ubiquiti and mrtg


danielh
09-17-2009, 06:54 AM
so im at the point where im integrating ubnt with mrtg, since the snmp agent values are pretty much useless i wrote this script to get values by "screen scraping" the airos page (untested with airos V).

simple but effective



#!/usr/bin/perl
use strict;
use warnings;


use HTML::TokeParser;
use LWP;
use Getopt::Long;
use Pod::Usage;


#default values
my $login='ubnt';
my $pass='ubnt';
my $debug=0;
my $help=0;
#parse options
my $options= GetOptions ("login|l=s" => \$login,
"pass|p=s" => \$pass,
"debug|d" => \$debug,
"help|h"=>\$help);
my $host= shift;
my @wantedvalues = @ARGV;


pod2usage(-exitval => 1, -verbose => 2, -noperldoc=>1) if $help;





# gather data
my $url="http://$host/status.cgi";
my $browser = LWP::UserAgent->new;
$browser->credentials(
"$host:80",
'', # realm is null for ubiquiti
$login,$pass
);
my $response = $browser->get($url);
die "Error: ", $response->header('WWW-Authenticate') ||
'Error accessing',
"\n ", $response->status_line, "\n at $url\n Aborting"
unless $response->is_success;



# tokeparser is a bit overkill for this, but it stated as something else
my $stream = HTML::TokeParser->new(\$response->content);

my %data;
$stream->get_tag("script");
my $e= $stream->get_text;

# strip quotations and whitespaces, then put one value per line
$e =~ s/[\s+]+|["']//g;
@_ = split(/;/,$e);
# values starting with parent are javascript calls used by ubnt, we don't need them
@_ = grep !/^parent/, @_;

# make a nice hash
map { my ($key,$data)=split(/=/,$_); $data{$key}=$data } @_;

# if we specified the "all" keyword display everything
if ( grep( /all/i,@wantedvalues) || scalar @wantedvalues == 0 ) {
@wantedvalues = keys %data;
}

# print one value per line
# by default, no key= is displayed as it's meant to be used by mrtg
foreach my $key (@wantedvalues) {
next if !defined($data{$key});
print "$key=" if $debug == 1;
print "$data{$key}\n";
}

__END__

=head1 NAME

ubntstats
return status values from an ubiquiti device.
it is mainly used to integrate with MRTG, as such the default output values reflect that format

configuring mrtg itself is left to another tool

=head1 SYNOPSIS

ubntstat.pl -[lpdh] host values

Options:
--help|h this help
--login|l
--pass|p specify the credentials used to log into the radio
--debug|d add a "key=" prefix to the data output lines
useful for knowing which values are available

host
host is an ip address or hostname, optionally suffixed with a ":port"
no attemp is made to validate its correctness

values
one or more keywords specifying which values we should display in the output
optionall the keyword "ALL" can be used to show everything