Created
January 5, 2022 20:08
-
-
Save voodoojello/93d3cfb9294e20b4c4e5178a4ebf08ef to your computer and use it in GitHub Desktop.
Unifi AP Status Checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# | |
# Unifi AP Status Checker w/email notification | |
# (c)2022 Mark Page [mark@very3.net] | |
# | |
use strict; | |
use warnings; | |
use LWP::UserAgent; | |
use Email::Sender::Simple qw(sendmail); | |
use Email::Simple; | |
use Email::Simple::Creator; | |
use JSON; | |
my $app = { | |
'username' => 'XXXXXXXX', | |
'password' => 'XXXXXXXX', | |
'email' => 'XXXXXXXX', | |
'ls_thres' => 120, | |
'nodes' => ['192.168.1.100','192.168.1.101',], | |
'summary' => [], | |
}; | |
for my $n (0 .. (scalar(@{$app->{'nodes'}}) - 1)) { | |
my $_js = JSON->new->allow_nonref; | |
my $_ua = new LWP::UserAgent; | |
$_ua->timeout(20); | |
$_ua->agent("Perl Unifi API Scraper"); | |
$_ua->ssl_opts(verify_hostname => 0,SSL_verify_mode => 0x00); | |
$_ua->cookie_jar({}); | |
my $_node = 'https://'.$app->{'nodes'}->[$n].':8443'; | |
my $_auth = $_ua->post("$_node/api/login",Content => '{"username":"'.$app->{'username'}.'","password":"'.$app->{'password'}.'"}'); | |
my $_data = $_js->decode($_ua->post("$_node/api/s/default/stat/device")->content); | |
for (@{$_data->{'data'}}) { | |
my $_last_seen = int(time - $_->{'last_seen'}); | |
if ($_last_seen > $app->{'ls_thres'}) { | |
if (!defined($_->{'name'})) { $_->{'name'} = 'NO_ALIAS'; } | |
push(@{$app->{'summary'}},uc($app->{'nodes'}->[$n]).": $_->{'name'} [$_->{'mac'}] ($_last_seen secs)"); | |
} | |
} | |
} | |
if (defined($app->{'summary'}->[0])) { | |
print localtime(time)."\n".join("\n",@{$app->{'summary'}})."\n"; | |
my $_subject = 'Unifi AP Status Report ['.localtime(time).']'; | |
my $_message = '<html><body><h3>'.$_subject.'</h3>'.join('<br>',@{$app->{'summary'}}).'</body></html>'; | |
my $_to_eml = Email::Simple->create( | |
header => [ | |
To => $app->{'email'}, | |
From => 'webservices@hisd.com', | |
Subject => $_subject, | |
Importance => 'High', | |
'Content-Type' => 'text/html', | |
], | |
body => $_message, | |
); | |
sendmail($_to_eml); | |
} | |
__END__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment