Files
librenms-librenms/validate.php
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

172 lines
5.2 KiB
PHP
Raw Normal View History

#!/usr/bin/env php
2015-05-22 16:35:38 +01:00
<?php
/*
* LibreNMS
*
* Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa>
*
* 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. Please see LICENSE.txt at the top level of
* the source code distribution for details.
*/
2017-10-26 01:56:09 -05:00
use LibreNMS\Config;
use LibreNMS\ValidationResult;
2017-10-26 01:56:09 -05:00
use LibreNMS\Validator;
chdir(__DIR__); // cwd to the directory containing this script
ini_set('display_errors', 1);
2017-10-26 01:56:09 -05:00
$options = getopt('g:m:s::h::');
2015-05-22 16:35:38 +01:00
if (isset($options['h'])) {
2016-08-28 17:32:55 -05:00
echo
2020-06-06 23:49:03 -05:00
"\n Validate setup tool
2015-05-22 16:35:38 +01:00
2017-10-26 01:56:09 -05:00
Usage: ./validate.php [-g <group>] [-s] [-h]
2015-05-22 16:35:38 +01:00
-h This help section.
2017-10-26 01:56:09 -05:00
-s Print the status of each group
-g Any validation groups you want to run, comma separated:
Non-default groups:
- mail: this will test your email settings (uses default_mail option even if default_only is not set)
- distributedpoller: this will test for the install running as a distributed poller
2016-02-01 23:29:37 +00:00
- rrdcheck: this will check to see if your rrd files are corrupt
2017-10-26 01:56:09 -05:00
Default groups:
- configuration: checks various config settings are correct
- database: checks the database for errors
- dependencies: checks that all required libraries are installed and up-to-date
2017-10-26 01:56:09 -05:00
- disk: checks for disk space and other disk related issues
- php: check that various PHP modules and functions exist
- poller: check that the poller and discovery are running properly
- programs: check that external programs exist and are executable
2020-05-06 17:58:17 +02:00
- python: check that various Python modules and functions exist
2020-11-23 09:46:22 -06:00
- system: checks system related items
2017-10-26 01:56:09 -05:00
- updates: checks the status of git and updates
- user: check that the LibreNMS user is set properly
Example: ./validate.php -g mail.
2015-05-22 16:35:38 +01:00
";
2015-07-13 20:10:26 +02:00
exit;
2015-05-22 16:35:38 +01:00
}
2020-11-19 15:41:32 -06:00
if (function_exists('posix_getuid') && posix_getuid() === 0) {
echo 'Do not run validate.php as root' . PHP_EOL;
exit(1);
}
2020-06-06 23:49:03 -05:00
// Check autoload
if (! file_exists('vendor/autoload.php')) {
print_fail('Composer has not been run, dependencies are missing', './scripts/composer_wrapper.php install --no-dev');
exit;
}
require_once 'vendor/autoload.php';
require_once 'includes/common.php';
require_once 'includes/functions.php';
2017-07-17 13:02:28 -05:00
// Buffer output
ob_start();
2017-10-26 01:56:09 -05:00
$precheck_complete = false;
2017-07-17 13:02:28 -05:00
register_shutdown_function(function () {
2017-10-26 01:56:09 -05:00
global $precheck_complete;
2017-07-17 13:02:28 -05:00
2017-10-26 01:56:09 -05:00
if (! $precheck_complete) {
2019-01-19 09:27:00 -06:00
// use this in case composer autoloader isn't available
2019-01-19 12:04:49 -06:00
spl_autoload_register(function ($class) {
2019-05-22 13:31:24 -05:00
@include str_replace('\\', '/', $class) . '.php';
2019-01-19 09:27:00 -06:00
});
print_header();
2017-10-26 01:56:09 -05:00
}
2017-07-17 13:02:28 -05:00
});
2020-06-27 22:24:54 -05:00
$pre_checks_failed = false;
2020-06-28 22:37:44 -05:00
// config.php checks
if (file_exists('config.php')) {
2020-06-27 22:24:54 -05:00
$syntax_check = `php -ln config.php`;
if (strpos($syntax_check, 'No syntax errors detected') === false) {
print_fail('Syntax error in config.php');
echo $syntax_check;
$pre_checks_failed = true;
}
2015-05-22 16:35:38 +01:00
2020-06-27 22:24:54 -05:00
$first_line = rtrim(`head -n1 config.php`);
if (! strpos($first_line, '<?php') === 0) {
print_fail("config.php doesn't start with a <?php - please fix this ($first_line)");
$pre_checks_failed = true;
}
if (strpos(`tail config.php`, '?>') !== false) {
print_fail('Remove the ?> at the end of config.php');
$pre_checks_failed = true;
}
2017-07-17 13:02:28 -05:00
}
2020-06-06 23:49:03 -05:00
// Composer check
$validator = new Validator();
$validator->validate(['dependencies']);
if ($validator->getGroupStatus('dependencies') == ValidationResult::FAILURE) {
$pre_checks_failed = true;
}
2017-07-17 13:02:28 -05:00
if ($pre_checks_failed) {
exit;
}
2019-03-14 08:06:27 -05:00
$init_modules = [];
require 'includes/init.php';
2016-04-29 12:53:02 -05:00
// make sure install_dir is set correctly, or the next includes will fail
2020-06-27 22:24:54 -05:00
if (! file_exists(Config::get('install_dir') . '/.env')) {
$suggested = realpath(__DIR__);
2019-06-23 00:29:12 -05:00
print_fail('\'install_dir\' config setting is not set correctly.', "It should probably be set to: $suggested");
2016-04-29 12:53:02 -05:00
exit;
}
2018-08-17 15:29:20 -05:00
if (\LibreNMS\DB\Eloquent::isConnected()) {
2017-10-26 01:56:09 -05:00
$validator->ok('Database connection successful', null, 'database');
2018-08-17 15:29:20 -05:00
} else {
2018-09-05 13:29:27 -05:00
$validator->fail('Error connecting to your database.', null, 'database');
2017-07-17 13:02:28 -05:00
}
2017-10-26 01:56:09 -05:00
$precheck_complete = true; // disable shutdown function
print_header();
2017-07-17 13:02:28 -05:00
2017-10-26 01:56:09 -05:00
if (isset($options['g'])) {
$modules = explode(',', $options['g']);
} elseif (isset($options['m'])) {
$modules = explode(',', $options['m']); // backwards compat
2017-07-17 13:02:28 -05:00
} else {
2017-10-26 01:56:09 -05:00
$modules = []; // all modules
}
2017-10-26 01:56:09 -05:00
// run checks
$validator->validate($modules, isset($options['s']) || ! empty($modules));
2015-07-13 20:10:26 +02:00
function print_header()
2017-10-26 01:56:09 -05:00
{
$output = ob_get_clean();
@ob_end_clean();
echo \LibreNMS\Util\Version::get()->header() . PHP_EOL;
echo $output;
}
2017-10-26 01:56:09 -05:00
// output matches that of ValidationResult
function print_fail($msg, $fix = null)
2016-08-28 17:32:55 -05:00
{
echo "[\033[31;1mFAIL\033[0m] $msg";
if ($fix && strlen($msg) > 72) {
echo PHP_EOL . ' ';
}
2015-05-22 16:35:38 +01:00
if (! empty($fix)) {
echo " [\033[34;1mFIX\033[0m] \033[34;1m$fix\033[0m";
}
echo PHP_EOL;
}