-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunifi-alerts-daemon.php
125 lines (103 loc) · 4.14 KB
/
unifi-alerts-daemon.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
require_once('vendor/autoload.php');
## Can you tell I've been coding lots of ruby lately? :P
$controller_user = getenv('UNIFI_USER');
$controller_password = getenv('UNIFI_PASS');
$controller_url = getenv('UNIFI_URL');
$site_id = getenv('SITE_ID');
$controller_version = getenv('CONTROLLER_VERSION');
$smtp_server = getenv('SMTP_HOST') ? getenv('SMTP_HOST') : 'localhost' ;
$smtp_port = getenv('SMTP_PORT') ? getenv('SMTP_PORT') : 25;
$smtp_user = getenv('SMTP_USER');
$smtp_pass = getenv('SMTP_PASS');
$from_email = getenv('FROM_EMAIL');
$from_name = getenv('FROM_NAME') ? getenv('FROM_NAME') : 'Unifi Alerts';
$to_email = getenv('TO_EMAIL');
$to_name = getenv('TO_NAME') ? GETENV('TO_NAME') : 'Unifi Administrator';
$daily_reset_time = getenv('DAILY_AP_RESTART_TIME');
$daily_reset_ap_mac = getenv('DAILY_AP_RESTART_MAC');
$last_day_reset = -1;
if(filter_var(getenv('SMTP_TLS'),FILTER_VALIDATE_BOOLEAN)) {
$smtp_tls = true;
$smtp_secure = 'tls';
} else {
$smtp_tls = false;
$smtp_secure = '';
}
if (filter_var(getenv('SMTPDEBUG'),FILTER_VALIDATE_BOOLEAN)) {
$smtpdebug = 2;
} else {
$smtpdebug = 0;
}
if ($controller_url == 'https://unifi.hostname' || $controller_url === false) {
exit("You must set the UNIFI_USER, UNIFI_PASS, UNIFI_URL, SITE_ID, and CONTROLLER_VERSION environment variables.\n");
}
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = $smtpdebug; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = $smtp_server; // Specify main and backup SMTP servers
$mail->SMTPAuth = ($smtp_user || $smtp_pass) ? true : false; // Enable SMTP authentication
$mail->SMTPAutoTLS = $smtp_tls;
$mail->Username = $smtp_user; // SMTP username
$mail->Password = $smtp_pass;
$mail->SMTPSecure = $smtp_false;
$mail->Port = $smtp_port; // TCP port to connect to
//Recipients
$mail->setFrom($from_email, $from_name);
$mail->addAddress($to_email, $to_name); // Add a recipient
//Content
$mail->isHTML(false); // Set email format to HTML
} catch (Exception $e) {
echo 'Email setup did not work... Mailer Error: ', $mail->ErrorInfo;
}
/* # debug stuff for lazy dev.
echo $controller_user;
echo "\n";
echo $controller_password;
echo "\n";
echo $controller_url;
echo "\n";
*/
$unifi_connection = new UniFi_API\Client($controller_user, $controller_password, $controller_url, $site_id, $controller_version, false);
$login = $unifi_connection->login();
$serialized_data = file_get_contents('data/hosts');
if ($serialized_data === false) {
$known_hosts = array();
} else {
$known_hosts = unserialize($serialized_data);
}
while(true) {
echo "Polling unifi controller for hosts...\n";
$results = $unifi_connection->list_clients(); // returns a PHP array containing alarm objects
if(!is_array($results)) {
exit("Results returning bad data...");
}
foreach($results as $client) {
if (isset($known_hosts[$client->mac])) {
continue;
}
echo "New host found - $client->mac - ($client->hostname) - sending email!\n";
$known_hosts[$client->mac] = $client;
$mail->Subject = "Never Seen $client->mac - ($client->hostname) connecting to $client->essid";
$mail->Body = "Full Info: " . json_encode($client, JSON_PRETTY_PRINT) . "\n\n";
try {
$mail->send();
} catch (Exception $e) {
echo 'Email sending did not work... Mailer Error: ', $mail->ErrorInfo;
}
}
file_put_contents('data/hosts',serialize($known_hosts));
if (!(empty($daily_reset_time) || empty($daily_reset_ap_mac)) && $daily_reset_time == date("H:i")) {
if($last_day_reset != date('d')) {
echo "Daily reset of AP enabled to $daily_reset_time which is now - resetting AP - $daily_reset_ap_mac";
$last_day_reset = date('d');
$unifi_connection->restart_ap($daily_reset_ap_mac);
}
}
sleep(15);
}
?>