PSR-2 Final cleanup (#4247)

refactor: Final PSR2 cleanup
This commit is contained in:
Tony Murray
2016-08-28 17:32:55 -05:00
committed by Neil Lathwood
parent aadd627863
commit abc6a5b799
41 changed files with 1339 additions and 1397 deletions

View File

@@ -16,7 +16,7 @@
$options = getopt('m:h::');
if (isset($options['h'])) {
echo
echo
"\n Validate setup tool
Usage: ./validate.php [-m <module>] [-h]
@@ -38,15 +38,12 @@ if (strstr(`php -ln config.php`, 'No syntax errors detected')) {
$last_lines = explode(PHP_EOL, `tail -n2 config.php`);
if (substr($first_line, 0, 5) !== '<?php') {
print_fail("config.php doesn't start with a <?php - please fix this ($first_line)");
}
else if ($last_lines[0] == '?>' && $last_lines[1] == '') {
} elseif ($last_lines[0] == '?>' && $last_lines[1] == '') {
print_fail('config.php contains a new line at the end, please remove any whitespace at the end of the file and also remove ?>');
}
else if ($last_lines[1] == '?>') {
} elseif ($last_lines[1] == '?>') {
print_warn("It looks like you have ?> at the end of config.php, it is suggested you remove this");
}
}
else {
} else {
print_fail('Syntax error in config.php');
}
@@ -61,7 +58,7 @@ require_once 'includes/defaults.inc.php';
require_once 'config.php';
// make sure install_dir is set correctly, or the next includes will fail
if(!file_exists($config['install_dir'].'/config.php')) {
if (!file_exists($config['install_dir'].'/config.php')) {
print_fail('$config[\'install_dir\'] is not set correctly. It should probably be set to: ' . getcwd());
exit;
}
@@ -79,21 +76,20 @@ $cur_sha = $versions['local_sha'];
if ($config['update_channel'] == 'master' && $cur_sha != $versions['github']['sha']) {
$commit_date = new DateTime('@'.$versions['local_date'], new DateTimeZone(date_default_timezone_get()));
print_warn("Your install is out of date: $cur_sha " . $commit_date->format('(r)'));
}
else {
} else {
echo "Commit SHA: $cur_sha\n";
}
if($versions['local_branch'] != 'master') {
if ($versions['local_branch'] != 'master') {
print_warn("Your local git branch is not master, this will prevent automatic updates.");
}
// check for modified files
$modifiedcmd = 'git diff --name-only --exit-code';
if($username === 'root') {
if ($username === 'root') {
$modifiedcmd = 'su '.$config['user'].' -c "'.$modifiedcmd.'"';
}
exec($modifiedcmd, $cmdoutput, $code);
if($code !== 0 && !empty($cmdoutput)) {
if ($code !== 0 && !empty($cmdoutput)) {
print_warn("Your local git contains modified files, this could prevent automatic updates.\nModified files:");
echo(' ' . implode("\n ", $cmdoutput) . "\n");
}
@@ -142,8 +138,7 @@ if (isset($config['user'])) {
echo "\n";
}
}
}
else {
} else {
print_warn('You don\'t have $config["user"] set, this most likely needs to be set to librenms');
}
@@ -151,14 +146,13 @@ else {
$test_db = @mysqli_connect($config['db_host'], $config['db_user'], $config['db_pass'], $config['db_name']);
if (mysqli_connect_error()) {
print_fail('Error connecting to your database '.mysqli_connect_error());
}
else {
} else {
print_ok('Database connection successful');
}
// Test for MySQL Strict mode
$strict_mode = dbFetchCell("SELECT @@global.sql_mode");
if(strstr($strict_mode, 'STRICT_TRANS_TABLES')) {
if (strstr($strict_mode, 'STRICT_TRANS_TABLES')) {
print_fail('You have MySQL STRICT_TRANS_TABLES enabled, please disable this until full support has been added: https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html');
}
@@ -229,101 +223,89 @@ if (!function_exists('openssl_random_pseudo_bytes')) {
$modules = explode(',', $options['m']);
foreach ($modules as $module) {
switch ($module) {
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'])) {
print_fail("You have selected sendmail but not configured email_sendmail_path");
$run_test = 0;
}
elseif (!file_exists($config['email_sendmail_path'])) {
print_fail("The configured email_sendmail_path is 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');
$run_test = 0;
}
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 auth but have not configured both username and password');
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;
} elseif ($config['email_backend'] == 'sendmail') {
if (empty($config['email_sendmail_path'])) {
print_fail("You have selected sendmail but not configured email_sendmail_path");
$run_test = 0;
} elseif (!file_exists($config['email_sendmail_path'])) {
print_fail("The configured email_sendmail_path is not valid");
$run_test = 0;
}
} elseif ($config['email_backend'] == 'smtp') {
if (empty($config['email_smtp_host'])) {
print_fail('You have selected SMTP but not configured an SMTP host');
$run_test = 0;
}
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 auth but have not configured both username and 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);
}
}
}//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');
break;
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 {
$connection = @fsockopen($config['distributed_poller_memcached_host'], $config['distributed_poller_memcached_port']);
if (!is_resource($connection)) {
print_fail('We could not get memcached stats, it is possible that we cannot connect to your memcached server, please check');
} else {
fclose($connection);
print_ok('Connection to memcached is ok');
}
}
else {
print_fail('Issue sending email to '.$config['alert']['default_mail'].' with error '.$err);
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 {
check_rrdcached();
}
}
}//end if
break;
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 {
$connection = @fsockopen($config['distributed_poller_memcached_host'], $config['distributed_poller_memcached_port']);
if (!is_resource($connection)) {
print_fail('We could not get memcached stats, it is possible that we cannot connect to your memcached server, please check');
}
else {
fclose($connection);
print_ok('Connection to memcached is ok');
}
}
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 {
check_rrdcached();
}
}
break;
case 'rrdcheck':
break;
case 'rrdcheck':
// Loop through the rrd_dir
$rrd_directory = new RecursiveDirectoryIterator($config['rrd_dir']);
// Filter out any non rrd files
$rrd_directory_filter = new LibreNMS\RRDRecursiveFilterIterator($rrd_directory);
$rrd_iterator = new RecursiveIteratorIterator($rrd_directory_filter);
$rrd_total = iterator_count($rrd_iterator);
$rrd_iterator->rewind(); // Rewind iterator in case iterator_count left iterator in unknown state
// Loop through the rrd_dir
$rrd_directory = new RecursiveDirectoryIterator($config['rrd_dir']);
// Filter out any non rrd files
$rrd_directory_filter = new LibreNMS\RRDRecursiveFilterIterator($rrd_directory);
$rrd_iterator = new RecursiveIteratorIterator($rrd_directory_filter);
$rrd_total = iterator_count($rrd_iterator);
$rrd_iterator->rewind(); // Rewind iterator in case iterator_count left iterator in unknown state
echo "\nScanning ".$rrd_total." rrd files in ".$config['rrd_dir']."...\n";
echo "\nScanning ".$rrd_total." rrd files in ".$config['rrd_dir']."...\n";
// Count loops so we can push status to the user
$loopcount = 0;
$screenpad = 0;
foreach ($rrd_iterator as $filename => $file) {
// Count loops so we can push status to the user
$loopcount = 0;
$screenpad = 0;
foreach ($rrd_iterator as $filename => $file) {
$rrd_test_result = rrdtest($filename, $output, $error);
$loopcount++;
if (($loopcount % 50) == 0 ) {
if (($loopcount % 50) == 0) {
//This lets us update the previous status update without spamming in most consoles
echo "\033[".$screenpad."D";
$test_status = 'Status: '.$loopcount.'/'.$rrd_total;
@@ -332,51 +314,53 @@ foreach ($modules as $module) {
}
// A non zero result means there was some kind of error
if ($rrd_test_result > 0) {
if ($rrd_test_result > 0) {
echo "\033[".$screenpad."D";
print_fail('Error parsing "'.$filename.'" RRD '.trim($error));
$screenpad = 0;
}
}
echo "\033[".$screenpad."D";
echo "Status: ".$loopcount."/".$rrd_total." - Complete\n";
}
echo "\033[".$screenpad."D";
echo "Status: ".$loopcount."/".$rrd_total." - Complete\n";
break;
break;
}//end switch
}//end foreach
// End
function print_ok($msg) {
function print_ok($msg)
{
echo "[OK] $msg\n";
}//end print_ok()
function print_fail($msg) {
function print_fail($msg)
{
echo "[FAIL] $msg\n";
}//end print_fail()
function print_warn($msg) {
function print_warn($msg)
{
echo "[WARN] $msg\n";
}//end print_warn()
function check_rrdcached() {
function check_rrdcached()
{
global $config;
list($host,$port) = explode(':',$config['rrdcached']);
list($host,$port) = explode(':', $config['rrdcached']);
if ($host == 'unix') {
// Using socket, check that file exists
if (!file_exists($port)) {
print_fail("$port doesn't appear to exist, rrdcached test failed");
}
}
else {
} else {
$connection = @fsockopen($host, $port);
if (is_resource($connection)) {
fclose($connection);
}
else {
} else {
print_fail('Cannot connect to rrdcached instance');
}
}