Files

174 lines
5.7 KiB
PHP
Raw Permalink Normal View History

2018-05-09 08:05:17 -05:00
<?php
/**
* Checks.php
*
* Pre-flight checks at various stages of booting
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @link http://librenms.org
* @copyright 2018 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace App;
use App\Models\Device;
use App\Models\Notification;
use Auth;
2018-09-11 07:51:35 -05:00
use Cache;
2018-05-09 08:05:17 -05:00
use Carbon\Carbon;
use LibreNMS\Config;
2018-09-13 08:27:43 -05:00
use Toastr;
2018-05-09 08:05:17 -05:00
class Checks
{
2019-08-01 06:58:22 -05:00
public static function preAutoload()
2018-05-09 08:05:17 -05:00
{
2019-08-01 06:58:22 -05:00
// Check PHP version otherwise it will just say server error
2020-05-23 19:05:18 +02:00
if (version_compare('7.2.5', PHP_VERSION, '>=')) {
2018-05-10 02:18:57 -05:00
self::printMessage(
2020-05-23 19:05:18 +02:00
'PHP version 7.2.5 or newer is required to run LibreNMS',
2019-08-01 06:58:22 -05:00
null,
2018-05-10 02:18:57 -05:00
true
);
2020-09-21 14:54:51 +02:00
}
2018-05-09 08:05:17 -05:00
}
/**
* Pre-boot dependency check
*/
public static function postAutoload()
{
2020-09-21 14:54:51 +02:00
if (! class_exists(\Illuminate\Foundation\Application::class)) {
2018-05-09 08:05:17 -05:00
self::printMessage(
'Error: Missing dependencies! Run the following command to fix:',
'./scripts/composer_wrapper.php install --no-dev',
true
);
}
}
2019-08-01 06:58:22 -05:00
public static function preBoot()
{
// check php extensions
if ($missing = self::missingPhpExtensions()) {
self::printMessage(
2020-09-21 15:59:34 +02:00
'Missing PHP extensions. Please install and enable them on your LibreNMS server.',
2019-08-01 06:58:22 -05:00
$missing,
true
);
}
}
2018-05-09 08:05:17 -05:00
/**
* Post boot Toast messages
*/
public static function postAuth()
{
2018-09-11 07:51:35 -05:00
// limit popup messages frequency
2020-09-21 14:54:51 +02:00
if (Cache::get('checks_popup_timeout') || ! Auth::check()) {
2018-09-11 07:51:35 -05:00
return;
2018-05-09 08:05:17 -05:00
}
Cache::put('checks_popup_timeout', true, Config::get('checks_popup_timer', 5) * 60);
2018-05-09 08:05:17 -05:00
2018-09-11 07:51:35 -05:00
$user = Auth::user();
2018-05-09 08:05:17 -05:00
2018-09-11 07:51:35 -05:00
if ($user->isAdmin()) {
2020-05-23 03:08:05 +02:00
$notifications = Notification::isUnread($user)->where('severity', '>', \LibreNMS\Enum\Alert::OK)->get();
2018-09-11 07:51:35 -05:00
foreach ($notifications as $notification) {
Toastr::error("<a href='notifications/'>$notification->body</a>", $notification->title);
}
$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;
Toastr::warning('<a href="poller/log?filter=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 :)</a>', 'Devices unpolled');
2018-09-11 07:51:35 -05:00
}
// Directory access checks
$rrd_dir = Config::get('rrd_dir');
2020-09-21 14:54:51 +02:00
if (! is_dir($rrd_dir)) {
2020-09-21 15:59:34 +02:00
Toastr::error("RRD Directory is missing ($rrd_dir). Graphing may fail. <a href=" . url('validate') . '>Validate your install</a>');
2018-09-11 07:51:35 -05:00
}
$temp_dir = Config::get('temp_dir');
2020-09-21 14:54:51 +02:00
if (! is_dir($temp_dir)) {
2020-09-21 15:59:34 +02:00
Toastr::error("Temp Directory is missing ($temp_dir). Graphing may fail. <a href=" . url('validate') . '>Validate your install</a>');
2020-09-21 14:54:51 +02:00
} elseif (! is_writable($temp_dir)) {
2018-09-11 07:51:35 -05:00
Toastr::error("Temp Directory is not writable ($temp_dir). Graphing may fail. <a href='" . url('validate') . "'>Validate your install</a>");
}
2018-05-09 08:05:17 -05:00
}
}
2020-05-30 09:23:21 -05:00
/**
* 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
);
}
2020-05-30 09:23:21 -05:00
self::printMessage(
'Error: You must run lnms as the user ' . get_current_user(),
null,
true
);
}
}
2018-05-09 08:05:17 -05:00
private static function printMessage($title, $content, $exit = false)
{
2020-09-21 14:54:51 +02:00
$content = (array) $content;
2018-05-09 08:05:17 -05:00
if (PHP_SAPI == 'cli') {
$format = "%s\n\n%s\n\n";
$message = implode(PHP_EOL, $content);
} else {
$format = "<h3 style='color: firebrick;'>%s</h3><p>%s</p>";
2018-05-10 02:18:57 -05:00
$message = '';
foreach ($content as $line) {
$message .= "<p style='margin:0.5em'>$line</p>\n";
}
2018-05-09 08:05:17 -05:00
}
printf($format, $title, $message);
if ($exit) {
exit(1);
}
}
2018-05-10 02:18:57 -05:00
private static function missingPhpExtensions()
{
2018-08-29 15:16:30 -05:00
// allow mysqli, but prefer mysqlnd
2020-09-21 14:54:51 +02:00
if (! extension_loaded('mysqlnd') && ! extension_loaded('mysqli')) {
2018-08-29 15:16:30 -05:00
return ['mysqlnd'];
}
2020-06-28 20:07:20 -05:00
$required_modules = ['mbstring', 'pcre', 'curl', 'xml', 'gd'];
2018-05-10 02:18:57 -05:00
return array_filter($required_modules, function ($module) {
2020-09-21 14:54:51 +02:00
return ! extension_loaded($module);
2018-05-10 02:18:57 -05:00
});
}
2018-05-09 08:05:17 -05:00
}