Skip to content

Commit

Permalink
feature: allow scripts to be run from any working directory (#4437)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
murrant authored and laf committed Sep 14, 2016
1 parent f7b68c4 commit d2193f7
Show file tree
Hide file tree
Showing 23 changed files with 65 additions and 80 deletions.
2 changes: 1 addition & 1 deletion addhost.php
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion adduser.php
Expand Up @@ -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';
Expand Down
2 changes: 2 additions & 0 deletions alerts.php
Expand Up @@ -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';

Expand Down
2 changes: 1 addition & 1 deletion billing-calculate.php
Expand Up @@ -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';
Expand Down
3 changes: 3 additions & 0 deletions build-base.php 100644 → 100755
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion check-services.php
Expand Up @@ -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';
Expand Down
24 changes: 6 additions & 18 deletions config_to_json.php 100644 → 100755
@@ -1,3 +1,4 @@
#!/usr/bin/env php
<?php

/*
Expand All @@ -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);
}
3 changes: 3 additions & 0 deletions daily.php
@@ -1,10 +1,13 @@
#!/usr/bin/env php
<?php

/*
* Daily Task Checks
* (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';
Expand Down
2 changes: 1 addition & 1 deletion delhost.php
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion discovery.php
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion dist-pollers.php
Expand Up @@ -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';
Expand Down
3 changes: 2 additions & 1 deletion irc.php
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion poll-billing.php
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion poller.php
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion renamehost.php
Expand Up @@ -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';
Expand Down
42 changes: 20 additions & 22 deletions scripts/console-ui.php
@@ -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();

Expand Down Expand Up @@ -88,7 +90,7 @@
$port_speed = humanspeed($port['ifSpeed']);
}

if ($port[ifDuplex] != 'unknown') {
if ($port['ifDuplex'] != 'unknown') {
$port_duplex = $port['ifDuplex'];
}

Expand All @@ -108,22 +110,18 @@
echo $options['list'];
echo "Usage of console-ui.php:
-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
Examples:
#1 php console-ui.php -l eventlog -d 1
#2 php console-ui.php --list=devices
";
-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
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");
Expand Down
8 changes: 5 additions & 3 deletions scripts/gen_smokeping.php
Expand Up @@ -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");

?>

Expand Down
4 changes: 4 additions & 0 deletions scripts/tune_port.php
@@ -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';
Expand All @@ -26,3 +28,5 @@
rrdtool_tune('port', $rrdfile, $port['ifSpeed']);
}
}

rrdtool_close();
22 changes: 0 additions & 22 deletions scripts/update-sql.php

This file was deleted.

4 changes: 2 additions & 2 deletions snmp-scan.php
Expand Up @@ -29,9 +29,9 @@
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';
Expand Down
2 changes: 2 additions & 0 deletions snmptrap.php
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions syslog.php
Expand Up @@ -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';
Expand Down
6 changes: 4 additions & 2 deletions validate.php
@@ -1,4 +1,4 @@
#!/usr/bin/php
#!/usr/bin/env php
<?php

/*
Expand All @@ -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'])) {
Expand Down Expand Up @@ -52,7 +54,7 @@
$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");
Expand Down

0 comments on commit d2193f7

Please sign in to comment.