2018-05-09 13:05:17 +00: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
2021-02-08 23:29:04 +00:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2018-05-09 13:05:17 +00:00
*
2021-02-08 23:29:04 +00:00
* @ link https :// www . librenms . org
2021-09-10 18:09:53 +00:00
*
2018-05-09 13:05:17 +00:00
* @ copyright 2018 Tony Murray
* @ author Tony Murray < murraytony @ gmail . com >
*/
namespace App ;
use App\Models\Device ;
use App\Models\Notification ;
2021-10-29 14:13:56 +00:00
use App\Models\User ;
2018-09-11 12:51:35 +00:00
use Cache ;
2018-05-09 13:05:17 +00:00
use Carbon\Carbon ;
2021-03-31 19:30:04 +00:00
use Illuminate\Support\Facades\Auth ;
2018-05-09 13:05:17 +00:00
use LibreNMS\Config ;
2023-08-05 17:12:36 +00:00
use LibreNMS\Enum\Severity ;
2018-05-09 13:05:17 +00:00
class Checks
{
/**
* Post boot Toast messages
*/
public static function postAuth ()
{
2018-09-11 12:51:35 +00:00
// limit popup messages frequency
if ( Cache :: get ( 'checks_popup_timeout' ) || ! Auth :: check ()) {
return ;
2018-05-09 13:05:17 +00:00
}
2019-08-05 20:54:58 +00:00
Cache :: put ( 'checks_popup_timeout' , true , Config :: get ( 'checks_popup_timer' , 5 ) * 60 );
2018-05-09 13:05:17 +00:00
2021-10-29 14:13:56 +00:00
/** @var User $user */
2018-09-11 12:51:35 +00:00
$user = Auth :: user ();
2018-05-09 13:05:17 +00:00
2018-09-11 12:51:35 +00:00
if ( $user -> isAdmin ()) {
2023-08-05 17:12:36 +00:00
$notifications = Notification :: isUnread ( $user ) -> where ( 'severity' , '>' , Severity :: Ok -> value ) -> get ();
2018-09-11 12:51:35 +00:00
foreach ( $notifications as $notification ) {
2024-06-05 13:07:42 +00:00
toast () -> warning ( $notification -> title , " <a href='notifications/'> $notification->body </a> " );
2018-09-11 12:51:35 +00:00
}
2018-11-19 07:55:59 +00:00
$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 ;
2024-06-05 13:07:42 +00:00
toast () -> warning ( 'Devices unpolled' , '<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>' );
2018-09-11 12:51:35 +00:00
}
// Directory access checks
$rrd_dir = Config :: get ( 'rrd_dir' );
if ( ! is_dir ( $rrd_dir )) {
2024-06-05 13:07:42 +00:00
toast () -> error ( " RRD Directory is missing ( $rrd_dir ). Graphing may fail. <a href= " . url ( 'validate' ) . '>Validate your install</a>' );
2018-09-11 12:51:35 +00:00
}
$temp_dir = Config :: get ( 'temp_dir' );
if ( ! is_dir ( $temp_dir )) {
2024-06-05 13:07:42 +00:00
toast () -> error ( " Temp Directory is missing ( $temp_dir ). Graphing may fail. <a href= " . url ( 'validate' ) . '>Validate your install</a>' );
2018-09-11 12:51:35 +00:00
} elseif ( ! is_writable ( $temp_dir )) {
2024-06-05 13:07:42 +00:00
toast () -> error ( " Temp Directory is not writable ( $temp_dir ). Graphing may fail. <a href=' " . url ( 'validate' ) . " '>Validate your install</a> " );
2018-09-11 12:51:35 +00:00
}
2018-05-09 13:05:17 +00:00
}
}
2020-05-30 14:23:21 +00: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 ()) {
2020-06-22 19:38:20 +00:00
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 14:23:21 +00:00
self :: printMessage (
'Error: You must run lnms as the user ' . get_current_user (),
null ,
true
);
}
}
2018-05-09 13:05:17 +00:00
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 = " <h3 style='color: firebrick;'>%s</h3><p>%s</p> " ;
2018-05-10 07:18:57 +00:00
$message = '' ;
foreach ( $content as $line ) {
$message .= " <p style='margin:0.5em'> $line </p> \n " ;
}
2018-05-09 13:05:17 +00:00
}
printf ( $format , $title , $message );
if ( $exit ) {
exit ( 1 );
}
}
}