Skip to content

Commit

Permalink
Logging and cron adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
austinwbest committed Nov 21, 2023
1 parent 10c9411 commit 44a9615
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
16 changes: 8 additions & 8 deletions root/app/www/public/classes/notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class Notifications

protected $platforms;
protected $platformSettings;
private $headers;
private $logfile;
protected $headers;
protected $logpath;
public function __construct()
{
global $platforms;
Expand All @@ -34,7 +34,7 @@ public function __construct()

$this->platforms = $platforms; //-- includes/platforms.php
$this->platformSettings = $settings['notifications']['platforms'];
$this->logfile = LOGS_PATH . 'notifications/';
$this->logpath = LOGS_PATH . 'notifications/';
}

public function __toString()
Expand All @@ -44,18 +44,18 @@ public function __toString()

public function notify($platform, $payload)
{
$platformData = $this->getNotificationPlatformFromId($platform);
$this->logfile = $this->logfile . $platformData['name'] . '-'. date('Ymd') .'.log';
$platformData = $this->getNotificationPlatformFromId($platform);
$logfile = $this->logpath . $platformData['name'] . '-'. date('Ymd') .'.log';

logger($this->logfile, 'notification request to ' . $platformData['name'], 'info');
logger($this->logfile, 'notification payload: ' . json_encode($payload), 'info');
logger($logfile, 'notification request to ' . $platformData['name'], 'info');
logger($logfile, 'notification payload: ' . json_encode($payload), 'info');

/*
Everything should return an array with code => ..., error => ... (if no error, just code is fine)
*/
switch ($platform) {
case 1: //-- Notifiarr
return $this->notifiarr($payload);
return $this->notifiarr($logfile, $payload);
}
}

Expand Down
4 changes: 2 additions & 2 deletions root/app/www/public/classes/traits/notification/notifiarr.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

trait Notifiarr
{
public function notifiarr($payload)
public function notifiarr($logfile, $payload)
{
$headers = ['x-api-key:' . $this->platformSettings[1]['apikey']];
$url = 'https://notifiarr.com/api/v1/notification/dockwatch';
$curl = curl($url, $headers, 'POST', json_encode($payload));

logger($this->logfile, 'notification response:' . json_encode($curl), ($curl['response']['code'] != 200 ? 'error' : 'info'));
logger($logfile, 'notification response:' . json_encode($curl), ($curl['response']['code'] != 200 ? 'error' : 'info'));

$return = ['code' => 200];
if ($curl['response']['code'] != 200) {
Expand Down
9 changes: 6 additions & 3 deletions root/app/www/public/crons/housekeeper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@
$settings = getFile(SETTINGS_FILE);

//-- LOG FILE CLEANUP (DAILY @ MIDNIGHT)
if (date('H') == 0) {
if (date('H') == 0 && date('i') <= 5) {
logger($logfile, 'Cron log file cleanup (daily @ midnight)');
$cronLength = $settings['global']['cronLogLength'] <= 1 ? 1 : $settings['global']['cronLogLength'];
logger($logfile, 'Allowed cron log age: ' . $cronLength);
$logDir = LOGS_PATH . 'crons/';
$dir = opendir($logDir);
while ($log = readdir($dir)) {
if ($log[0] != '.' && !is_dir($log)) {
$daysBetween = daysBetweenDates(date('Ymd', filemtime($logDir . $log)), date('Ymd'));
logger($logfile, 'logfile: ' . $logDir . $log . ', age: ' . $daysBetween);

if ($daysBetween > $cronLength) {
logger($logfile, 'Removing logfile: ' . $logDir . $log);
logger($logfile, 'removing logfile');
unlink($logDir . $log);
}
}
Expand All @@ -42,9 +44,10 @@
while ($log = readdir($dir)) {
if ($log[0] != '.' && !is_dir($log)) {
$daysBetween = daysBetweenDates(date('Ymd', filemtime($logDir . $log)), date('Ymd'));
logger($logfile, 'logfile: ' . $logDir . $log . ', age: ' . $daysBetween);

if ($daysBetween > $notificationLength) {
logger($logfile, 'Removing logfile: ' . $logDir . $log);
logger($logfile, 'removing logfile');
unlink($logDir . $log);
}
}
Expand Down
14 changes: 13 additions & 1 deletion root/app/www/public/crons/state.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
if ($added) {
$notify['state']['added'] = $added;
}
logger($logfile, 'Added containers: ' . json_encode($notify['state']['added']));

//-- CHECK FOR REMOVED CONTAINERS
foreach ($currentStates as $currentIndex => $currentState) {
Expand All @@ -59,8 +60,9 @@
if ($removed) {
$notify['state']['removed'] = $added;
}
logger($logfile, 'Removed containers: ' . json_encode($notify['state']['removed']));

//-- CHECK FOR STOPPED CONTAINERS
//-- CHECK FOR STATE CHANGED CONTAINERS
foreach ($currentStates as $currentState) {
foreach ($previousStates as $previousState) {
if ($settings['notifications']['triggers']['stateChange']['active'] && $currentState['Names'] == $previousState['Names']) {
Expand All @@ -70,6 +72,7 @@
}
}
}
logger($logfile, 'State changed containers: ' . json_encode($notify['state']['changed']));

foreach ($currentStates as $currentState) {
//-- CHECK FOR HIGH CPU USAGE CONTAINERS
Expand All @@ -92,25 +95,31 @@
}
}
}
logger($logfile, 'CPU issue containers: ' . json_encode($notify['usage']['cpu']));
logger($logfile, 'Mem issue containers: ' . json_encode($notify['usage']['mem']));

if ($notify['state']) {
//-- IF THEY USE THE SAME PLATFORM, COMBINE THEM
if ($settings['notifications']['triggers']['stateChange']['platform'] == $settings['notifications']['triggers']['added']['platform'] && $settings['notifications']['triggers']['stateChange']['platform'] == $settings['notifications']['triggers']['removed']['platform']) {
$payload = ['event' => 'state', 'changes' => $notify['state']['changed'], 'added' => $notify['state']['added'], 'removed' => $notify['state']['removed']];
logger($logfile, 'Notification payload: ' . json_encode($payload));
$notifications->notify($settings['notifications']['triggers']['stateChange']['platform'], $payload);
} else {
if ($notify['state']['changed']) {
$payload = ['event' => 'state', 'changes' => $notify['state']['changed']];
logger($logfile, 'Notification payload: ' . json_encode($payload));
$notifications->notify($settings['notifications']['triggers']['stateChange']['platform'], $payload);
}

if ($notify['state']['added']) {
$payload = ['event' => 'state', 'added' => $notify['state']['added']];
logger($logfile, 'Notification payload: ' . json_encode($payload));
$notifications->notify($settings['notifications']['triggers']['added']['platform'], $payload);
}

if ($notify['state']['removed']) {
$payload = ['event' => 'state', 'removed' => $notify['state']['removed']];
logger($logfile, 'Notification payload: ' . json_encode($payload));
$notifications->notify($settings['notifications']['triggers']['removed']['platform'], $payload);
}
}
Expand All @@ -120,15 +129,18 @@
//-- IF THEY USE THE SAME PLATFORM, COMBINE THEM
if ($settings['notifications']['triggers']['cpuHigh']['platform'] == $settings['notifications']['triggers']['memHigh']['platform']) {
$payload = ['event' => 'usage', 'cpu' => $notify['usage']['cpu'], 'cpuThreshold' => $settings['global']['cpuThreshold'], 'mem' => $notify['usage']['mem'], 'memThreshold' => $settings['global']['memThreshold']];
logger($logfile, 'Notification payload: ' . json_encode($payload));
$notifications->notify($settings['notifications']['triggers']['cpuHigh']['platform'], $payload);
} else {
if ($notify['usage']['cpu']) {
$payload = ['event' => 'usage', 'cpu' => $notify['usage']['cpu'], 'cpuThreshold' => $settings['global']['cpuThreshold']];
logger($logfile, 'Notification payload: ' . json_encode($payload));
$notifications->notify($settings['notifications']['triggers']['cpuHigh']['platform'], $payload);
}

if ($notify['usage']['mem']) {
$payload = ['event' => 'usage', 'mem' => $notify['usage']['mem'], 'memThreshold' => $settings['global']['memThreshold']];
logger($logfile, 'Notification payload: ' . json_encode($payload));
$notifications->notify($settings['notifications']['triggers']['memHigh']['platform'], $payload);
}
}
Expand Down

0 comments on commit 44a9615

Please sign in to comment.