2015-05-22 16:35:38 +01:00
#!/usr/bin/php
< ? 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 .
*/
2015-07-13 20:10:26 +02:00
require 'includes/console_colour.php' ;
2015-05-22 16:35:38 +01:00
$console_color = new Console_Color2 ();
2015-07-13 20:10:26 +02:00
$options = getopt ( 'm:h::' );
2015-05-22 16:35:38 +01:00
if ( isset ( $options [ 'h' ])) {
2015-07-13 20:10:26 +02:00
print $console_color -> convert (
" \n Validate setup tool
2015-05-22 16:35:38 +01:00
2015-07-13 20:10:26 +02:00
Usage : ./ validate . php [ - m < module > ] [ - h ]
- h This help section .
- m Any sub modules you want to run , comma separated :
2015-07-24 07:39:20 +01:00
- mail : this will test your email settings ( uses default_mail option even if default_only is not set ) .
- dist - poller : this will test for the install running as a distributed poller .
2015-05-22 16:35:38 +01:00
2015-07-13 20:10:26 +02:00
Example : ./ validate . php - m 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
}
2015-07-13 20:10:26 +02:00
if ( strstr ( `php -ln config.php` , 'No syntax errors detected' )) {
2015-05-22 16:35:38 +01:00
$first_line = `head -n1 config.php` ;
2015-07-13 20:10:26 +02:00
$last_lines = explode ( PHP_EOL , `tail -n2 config.php` );
if ( strstr ( $first_line , '\<\?php' )) {
2015-05-22 16:35:38 +01:00
print_fail ( " config.php doesn't start with a <?php - please fix this ( $first_line ) " );
2015-07-13 20:10:26 +02:00
}
else if ( $last_lines [ 0 ] == '?>' && $last_lines [ 1 ] == '' ) {
2015-05-22 16:35:38 +01:00
print_fail ( 'config.php contains a new line at the end, please remove any whitespace at the end of the file and also remove ?>' );
2015-07-13 20:10:26 +02:00
}
else if ( $last_lines [ 1 ] == '?>' ) {
2015-05-22 16:35:38 +01:00
print_warn ( " It looks like you have ?> at the end of config.php, it's suggested you remove this " );
2015-07-13 20:10:26 +02:00
}
else {
2015-05-22 16:35:38 +01:00
print_ok ( 'config.php tested ok' );
}
2015-07-13 20:10:26 +02:00
}
else {
2015-05-22 16:35:38 +01:00
print_fail ( 'Syntax error in config.php' );
}
// load config.php now
2015-07-13 20:10:26 +02:00
require_once 'includes/defaults.inc.php' ;
require_once 'config.php' ;
require_once 'includes/definitions.inc.php' ;
require_once 'includes/functions.php' ;
require_once $config [ 'install_dir' ] . '/includes/alerts.inc.php' ;
2015-05-22 16:35:38 +01:00
// Run test on MySQL
2015-07-13 20:10:26 +02:00
$test_db = @ mysqli_connect ( $config [ 'db_host' ], $config [ 'db_user' ], $config [ 'db_pass' ], $config [ 'db_name' ]);
2015-05-22 16:35:38 +01:00
if ( mysqli_connect_error ()) {
2015-07-13 20:10:26 +02:00
print_fail ( 'Error connecting to your database ' . mysqli_connect_error ());
}
else {
2015-05-22 16:35:38 +01:00
print_ok ( 'Database connection successful' );
}
// Test transports
2015-07-13 20:10:26 +02:00
if ( $config [ 'alerts' ][ 'email' ][ 'enable' ] === true ) {
print_warn ( 'You have the old alerting system enabled - this is to be deprecated on the 1st of June 2015: https://groups.google.com/forum/#!topic/librenms-project/1llxos4m0p4' );
2015-05-22 16:35:38 +01:00
}
2015-05-24 21:41:42 +01:00
// Test rrdcached
2015-05-22 16:35:38 +01:00
if ( ! $config [ 'rrdcached' ]) {
$rrd_dir = stat ( $config [ 'rrd_dir' ]);
if ( $rrd_dir [ 4 ] == 0 || $rrd_dir [ 5 ] == 0 ) {
2015-07-13 20:10:26 +02:00
print_warn ( 'Your RRD directory is owned by root, please consider changing over to user a non-root user' );
2015-05-22 16:35:38 +01:00
}
2015-07-13 20:10:26 +02:00
2015-05-22 16:35:38 +01:00
if ( substr ( sprintf ( '%o' , fileperms ( $config [ 'rrd_dir' ])), - 3 ) != 775 ) {
2015-07-13 20:10:26 +02:00
print_warn ( 'Your RRD directory is not set to 0775, please check our installation instructions' );
2015-05-22 16:35:38 +01:00
}
}
2015-05-24 21:41:42 +01:00
// Disk space and permission checks
2015-05-22 16:35:38 +01:00
if ( substr ( sprintf ( '%o' , fileperms ( $config [ 'temp_dir' ])), - 3 ) != 777 ) {
2015-07-13 20:10:26 +02:00
print_warn ( 'Your tmp directory (' . $config [ 'temp_dir' ] . " ) is not set to 777 so graphs most likely won't be generated " );
2015-05-22 16:35:38 +01:00
}
2015-07-13 20:10:26 +02:00
$space_check = ( disk_free_space ( $config [ 'install_dir' ]) / 1024 / 1024 );
2015-05-22 16:35:38 +01:00
if ( $space_check < 512 && $space_check > 1 ) {
2015-07-13 20:10:26 +02:00
print_warn ( 'Disk space where ' . $config [ 'install_dir' ] . ' is located is less than 512Mb' );
2015-05-22 16:35:38 +01:00
}
2015-07-13 20:10:26 +02:00
2015-05-22 16:35:38 +01:00
if ( $space_check < 1 ) {
2015-07-13 20:10:26 +02:00
print_fail ( 'Disk space where ' . $config [ 'install_dir' ] . ' is located is empty!!!' );
2015-05-22 16:35:38 +01:00
}
2015-05-24 21:41:42 +01:00
// Check programs
$bins = array ( 'fping' );
foreach ( $bins as $bin ) {
if ( ! is_file ( $config [ $bin ])) {
2015-05-27 09:35:52 +02:00
print_fail ( " $bin location is incorrect or bin not installed " );
2015-07-13 20:10:26 +02:00
}
else {
2015-05-24 21:41:42 +01:00
print_ok ( " $bin has been found " );
}
}
// Modules test
2015-07-13 20:10:26 +02:00
$modules = explode ( ',' , $options [ 'm' ]);
2015-05-22 16:35:38 +01:00
foreach ( $modules as $module ) {
switch ( $module ) {
2015-07-13 20:10:26 +02:00
case 'mail' :
if ( $config [ 'alert' ][ 'transports' ][ 'mail' ] === true ) {
$run_test = 1 ;
if ( empty ( $config [ 'alert' ][ 'default_mail' ])) {
print_fail ( 'default_mail config option needs to be specified to test email' );
$run_test = 0 ;
}
else if ( $config [ 'email_backend' ] == 'sendmail' ) {
if ( empty ( $config [ 'email_sendmail_path' ]) || ! file_exists ( $config [ 'email_sendmail_path' ])) {
print_fail ( " You have selected sendmail but not configured email_sendmail_path or it's not valid " );
$run_test = 0 ;
}
}
else if ( $config [ 'email_backend' ] == 'smtp' ) {
if ( empty ( $config [ 'email_smtp_host' ])) {
print_fail ( 'You have selected smtp but not configured an smtp host' );
2015-05-22 16:35:38 +01:00
$run_test = 0 ;
}
2015-07-13 20:10:26 +02:00
if ( empty ( $config [ 'email_smtp_port' ])) {
print_fail ( 'You have selected smtp but not configured an smtp port' );
$run_test = 0 ;
}
if (( $config [ 'email_smtp_auth' ] === true ) && ( empty ( $config [ 'email_smtp_username' ]) || empty ( $config [ 'email_smtp_password' ]))) {
print_fail ( 'You have selected smtp but not configured a username or password' );
$run_test = 0 ;
}
} //end if
if ( $run_test == 1 ) {
if ( $err = send_mail ( $config [ 'alert' ][ 'default_mail' ], 'Test email' , 'Testing email from NMS' )) {
print_ok ( 'Email has been sent' );
}
else {
print_fail ( 'Issue sending email to ' . $config [ 'alert' ][ 'default_mail' ] . ' with error ' . $err );
2015-05-22 16:35:38 +01:00
}
}
2015-07-13 20:10:26 +02:00
} //end if
break ;
2015-07-24 07:39:20 +01:00
case 'dist-poller' :
if ( $config [ 'distributed_poller' ] !== true ) {
print_fail ( 'You have not enabled distributed_poller' );
}
else {
if ( empty ( $config [ 'distributed_poller_memcached_host' ])) {
print_fail ( 'You have not configured $config[\'distributed_poller_memcached_host\']' );
}
elseif ( empty ( $config [ 'distributed_poller_memcached_port' ])) {
print_fail ( 'You have not configured $config[\'distributed_poller_memcached_port\']' );
}
else {
$memcache = new Memcached ();
$memcache -> addServer ( $config [ 'distributed_poller_memcached_host' ], $config [ 'distributed_poller_memcached_port' ]);
if ( ! $memcache -> getStats ()) {
print_fail ( 'We could not get memcached stats, it is possible that we cannot connect to your memcached server, please check' );
}
}
if ( empty ( $config [ 'rrdcached' ])) {
print_fail ( 'You have not configured $config[\'rrdcached\']' );
}
elseif ( empty ( $config [ 'rrd_dir' ])) {
print_fail ( 'You have not configured $config[\'rrd_dir\']' );
}
else {
}
}
break ;
2015-07-13 20:10:26 +02:00
} //end switch
} //end foreach
2015-05-22 16:35:38 +01:00
// End
2015-07-13 20:10:26 +02:00
2015-05-22 16:35:38 +01:00
function print_ok ( $msg ) {
global $console_color ;
print $console_color -> convert ( " %g[OK]%n %W $msg\n " );
2015-07-13 20:10:26 +02:00
} //end print_ok()
2015-05-22 16:35:38 +01:00
function print_fail ( $msg ) {
global $console_color ;
print $console_color -> convert ( " %r[FAIL]%n %W $msg\n " );
2015-07-13 20:10:26 +02:00
} //end print_fail()
2015-05-22 16:35:38 +01:00
function print_warn ( $msg ) {
global $console_color ;
print $console_color -> convert ( " %y[WARN]%n %W $msg\n " );
2015-07-13 20:10:26 +02:00
} //end print_warn()