feature: allow scripts to be run from any working directory (#4437)

* feature: allow validate.php to be run from any working directory

* remove redundant realpath() call

* re-add realpath() to remove symbolic links

* realpath() isn't needed...

* chdir() in all php scripts in ./ and ./scripts/

* update-sql.php clearly is not used, as it was broken.

* Change some scripts to executable
Remove extra chdir() call in snmp-scan.php

* Missed console-ui.php
Kind of fixed console-ui.php help output

* Re-add newline
This commit is contained in:
Tony Murray
2016-09-14 10:53:04 -05:00
committed by Neil Lathwood
parent f7b68c4714
commit d2193f76ca
23 changed files with 64 additions and 79 deletions

View File

@@ -13,7 +13,7 @@
use LibreNMS\Exceptions\HostUnreachableException;
chdir(dirname($argv[0]));
chdir(__DIR__); // cwd to the directory containing this script
require 'includes/defaults.inc.php';
require 'config.php';

View File

@@ -12,7 +12,7 @@
*
*/
chdir(dirname($argv[0]));
chdir(__DIR__); // cwd to the directory containing this script
require 'includes/defaults.inc.php';
require 'config.php';

View File

@@ -25,6 +25,8 @@
* @subpackage Alerts
*/
chdir(__DIR__); // cwd to the directory containing this script
require_once 'includes/defaults.inc.php';
require_once 'config.php';

View File

@@ -11,7 +11,7 @@
* @copyright (C) 2006 - 2012 Adam Armstrong
*/
chdir(dirname($argv[0]));
chdir(__DIR__); // cwd to the directory containing this script
require 'includes/defaults.inc.php';
require 'config.php';

3
build-base.php Normal file → Executable file
View File

@@ -1,5 +1,8 @@
#!/usr/bin/env php
<?php
chdir(__DIR__); // cwd to the directory containing this script
// MYSQL Check - FIXME
// 1 UNKNOWN
include 'config.php';

View File

@@ -13,7 +13,7 @@
* the source code distribution for details.
*/
chdir(dirname($argv[0]));
chdir(__DIR__); // cwd to the directory containing this script
require 'includes/defaults.inc.php';
require 'config.php';

24
config_to_json.php Normal file → Executable file
View File

@@ -1,3 +1,4 @@
#!/usr/bin/env php
<?php
/*
@@ -6,24 +7,11 @@
*
*/
$defaults_file = 'includes/defaults.inc.php';
$config_file = 'config.php';
// move to install dir
chdir(dirname($argv[0]));
function iscli()
{
if (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
return true;
} else {
return false;
}
}
chdir(__DIR__); // cwd to the directory containing this script
// check if we are running through the CLI, otherwise abort
if (iscli()) {
include_once $defaults_file;
include_once $config_file;
print (json_encode($config));
if (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
include_once 'includes/defaults.inc.php';
include_once 'config.php';
echo json_encode($config);
}

View File

@@ -1,3 +1,4 @@
#!/usr/bin/env php
<?php
/*
@@ -5,6 +6,8 @@
* (c) 2013 LibreNMS Contributors
*/
chdir(__DIR__); // cwd to the directory containing this script
require 'includes/defaults.inc.php';
require 'config.php';
require_once 'includes/definitions.inc.php';

View File

@@ -11,7 +11,7 @@
* @copyright (C) 2006 - 2012 Adam Armstrong
*/
chdir(dirname($argv[0]));
chdir(__DIR__); // cwd to the directory containing this script
require 'includes/defaults.inc.php';
require 'config.php';

View File

@@ -11,7 +11,7 @@
* @copyright (C) 2006 - 2012 Adam Armstrong
*/
chdir(dirname($argv[0]));
chdir(__DIR__); // cwd to the directory containing this script
require 'includes/defaults.inc.php';
require 'config.php';

View File

@@ -13,7 +13,7 @@
* the source code distribution for details.
*/
chdir(dirname($argv[0]));
chdir(__DIR__); // cwd to the directory containing this script
require 'includes/defaults.inc.php';
require 'config.php';

View File

@@ -19,7 +19,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* */
chdir(dirname($argv[0]));
chdir(__DIR__); // cwd to the directory containing this script
require_once 'includes/defaults.inc.php';
require_once 'config.php';
require_once 'includes/definitions.inc.php';

View File

@@ -11,7 +11,7 @@
* @copyright (C) 2006 - 2012 Adam Armstrong
*/
chdir(dirname($argv[0]));
chdir(__DIR__); // cwd to the directory containing this script
// FIXME - implement cli switches, debugging, etc.
require 'includes/defaults.inc.php';

View File

@@ -11,7 +11,7 @@
* @copyright (C) 2006 - 2012 Adam Armstrong
*/
chdir(dirname($argv[0]));
chdir(__DIR__); // cwd to the directory containing this script
require 'includes/defaults.inc.php';
require 'config.php';

View File

@@ -11,7 +11,7 @@
* @copyright (C) 2006 - 2012 Adam Armstrong
*/
chdir(dirname($argv[0]));
chdir(__DIR__); // cwd to the directory containing this script
require 'includes/defaults.inc.php';
require 'config.php';

View File

@@ -1,11 +1,13 @@
#!/usr/bin/env php
<?php
require '../includes/defaults.inc.php';
require '../config.php';
require_once '../includes/definitions.inc.php';
require '../includes/functions.php';
require '../html/includes/functions.inc.php';
chdir(realpath(__DIR__ . '/..')); // cwd to the parent directory of this script
require 'includes/defaults.inc.php';
require 'config.php';
require_once 'includes/definitions.inc.php';
require 'includes/functions.php';
require 'html/includes/functions.inc.php';
$console_color = new Console_Color2();
@@ -88,7 +90,7 @@ while ($end == 0) {
$port_speed = humanspeed($port['ifSpeed']);
}
if ($port[ifDuplex] != 'unknown') {
if ($port['ifDuplex'] != 'unknown') {
$port_duplex = $port['ifDuplex'];
}
@@ -108,22 +110,18 @@ while ($end == 0) {
echo $options['list'];
echo "Usage of console-ui.php:
-l What log type we want to see:
eventlog = Event log messages
syslog = Syslog messages
-l What log type we want to see:
eventlog = Event log messages
syslog = Syslog messages
-d Specify the device id to filter results
--list What to list
devices = list devices and device id's
--device-stats Lists the port statistics for a given device
-d Specify the device id to filter results
--list What to list
devices = list devices and device id's
--device-stats Lists the port statistics for a given device
Examples:
#1 php console-ui.php -l eventlog -d 1
#2 php console-ui.php --list=devices
";
Examples:
#1 php console-ui.php -l eventlog -d 1
#2 php console-ui.php --list=devices
";
exit;
}//end if
echo print $console_color->convert('%rLast update at '.date('Y-m-d h:i:s')."%n\n\n");

View File

@@ -11,9 +11,11 @@
* the source code distribution for details.
*/
include_once("../includes/defaults.inc.php");
include_once("../config.php");
include_once("../includes/definitions.inc.php");
chdir(realpath(__DIR__ . '/..')); // cwd to the parent directory of this script
include_once("includes/defaults.inc.php");
include_once("config.php");
include_once("includes/definitions.inc.php");
?>

View File

@@ -1,6 +1,8 @@
#!/usr/bin/env php
<?php
chdir(realpath(__DIR__ . '/..')); // cwd to the parent directory of this script
require 'includes/defaults.inc.php';
require 'config.php';
require 'includes/definitions.inc.php';
@@ -26,3 +28,5 @@ foreach (dbFetchRows("SELECT `device_id`,`hostname` FROM `devices` WHERE `hostna
rrdtool_tune('port', $rrdfile, $port['ifSpeed']);
}
}
rrdtool_close();

View File

@@ -1,22 +0,0 @@
#!/usr/bin/env php
<?php
// MYSQL Check - FIXME
// 1 UPDATE
require 'config.php';
require 'includes/functions.php';
if ($fd = @fopen($argv[1], 'r')) {
$data = fread($fd, 4096);
while (!feof($fd)) {
$data .= fread($fd, 4096);
}
foreach (explode("\n", $data) as $line) {
$update = mysql_query($line);
// FIXME check query success?
echo "$line \n";
}
} else {
echo "ERROR: Could not open file \"$argv[1]\".\n";
}

View File

@@ -29,9 +29,9 @@ use LibreNMS\Exceptions\HostExistsException;
use LibreNMS\Exceptions\HostUnreachableException;
use LibreNMS\Exceptions\HostUnreachablePingException;
$ts = microtime(true);
chdir(__DIR__); // cwd to the directory containing this script
chdir(dirname($argv[0]));
$ts = microtime(true);
require 'includes/defaults.inc.php';
require 'config.php';

View File

@@ -11,6 +11,8 @@
* @copyright (C) 2006 - 2012 Adam Armstrong
*/
chdir(__DIR__); // cwd to the directory containing this script
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('log_errors', 1);

View File

@@ -12,6 +12,8 @@
*
*/
chdir(__DIR__); // cwd to the directory containing this script
require 'includes/defaults.inc.php';
require 'config.php';
require 'includes/definitions.inc.php';

View File

@@ -1,4 +1,4 @@
#!/usr/bin/php
#!/usr/bin/env php
<?php
/*
@@ -13,6 +13,8 @@
* the source code distribution for details.
*/
chdir(__DIR__); // cwd to the directory containing this script
$options = getopt('m:h::');
if (isset($options['h'])) {
@@ -52,7 +54,7 @@ if (function_exists('posix_getpwuid')) {
$userinfo = posix_getpwuid(posix_geteuid());
$username = $userinfo['name'];
} else {
$username = getenv('USERNAME') ?: getenv('USER');//http://php.net/manual/en/function.get-current-user.php
$username = getenv('USERNAME') ?: getenv('USER'); //http://php.net/manual/en/function.get-current-user.php
}
if ($username !== 'root') {
print_fail("You need to run this script as root");