. * * @link https://www.librenms.org * * @copyright 2018 Tony Murray * @author Tony Murray */ namespace App; use App\Models\Device; use App\Models\Notification; use App\Models\User; use Cache; use Carbon\Carbon; use Illuminate\Support\Facades\Auth; use LibreNMS\Config; use LibreNMS\Enum\Severity; class Checks { /** * Post boot Toast messages */ public static function postAuth() { // limit popup messages frequency if (Cache::get('checks_popup_timeout') || ! Auth::check()) { return; } Cache::put('checks_popup_timeout', true, Config::get('checks_popup_timer', 5) * 60); /** @var User $user */ $user = Auth::user(); if ($user->isAdmin()) { $notifications = Notification::isUnread($user)->where('severity', '>', Severity::Ok->value)->get(); foreach ($notifications as $notification) { toast()->warning($notification->title, "$notification->body"); } $warn_sec = Config::get('rrd.step', 300) * 3; if (Device::isUp()->where('last_polled', '<=', Carbon::now()->subSeconds($warn_sec))->exists()) { $warn_min = $warn_sec / 60; toast()->warning('Devices unpolled', 'It appears as though you have some devices that haven\'t completed polling within the last ' . $warn_min . ' minutes, you may want to check that out :)'); } // Directory access checks $rrd_dir = Config::get('rrd_dir'); if (! is_dir($rrd_dir)) { toast()->error("RRD Directory is missing ($rrd_dir). Graphing may fail. Validate your install"); } } } /** * Check the script is running as the right user (works before config is available) */ public static function runningUser() { if (function_exists('posix_getpwuid') && posix_getpwuid(posix_geteuid())['name'] !== get_current_user()) { if (get_current_user() == 'root') { self::printMessage( 'Error: lnms file is owned by root, it should be owned and ran by a non-privileged user.', null, true ); } self::printMessage( 'Error: You must run lnms as the user ' . get_current_user(), null, true ); } } private static function printMessage($title, $content, $exit = false) { $content = (array) $content; if (PHP_SAPI == 'cli') { $format = "%s\n\n%s\n\n"; $message = implode(PHP_EOL, $content); } else { $format = "

%s

%s

"; $message = ''; foreach ($content as $line) { $message .= "

$line

\n"; } } printf($format, $title, $message); if ($exit) { exit(1); } } }