Skip to content

Commit

Permalink
feature: support non-standard unix socket (librenms#5724)
Browse files Browse the repository at this point in the history
* Add support for custom MySQL unix-socket

* NULL must be lowercase!

* Naive edit of html/install.php

* fixup

* Refactor dbConnect
Use it everywhere

* $config needs to be global
Don't need to set $database_link

* small cleanups
murrant authored and laf committed Apr 6, 2017
1 parent 66d9d54 commit b1a414e
Showing 15 changed files with 197 additions and 130 deletions.
31 changes: 31 additions & 0 deletions LibreNMS/Exceptions/DatabaseConnectException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* DatabaseConnectException.php
*
* -Description-
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2017 Tony Murray
* @author Tony Murray <[email protected]>
*/

namespace LibreNMS\Exceptions;

class DatabaseConnectException extends \Exception
{

}
22 changes: 15 additions & 7 deletions LibreNMS/IRCBot.php
Original file line number Diff line number Diff line change
@@ -20,6 +20,8 @@

namespace LibreNMS;

use LibreNMS\Exceptions\DatabaseConnectException;

class IRCBot
{

@@ -365,7 +367,7 @@ private function getUser($param)
}//end getUser()


private function connect($try)
private function connect($try = 0)
{
if ($try > $this->max_retry) {
$this->log('Failed too many connection attempts, aborting');
@@ -437,15 +439,21 @@ private function log($msg)
private function chkdb()
{
if (!is_resource($this->sql)) {
if (($this->sql = mysqli_connect($this->config['db_host'], $this->config['db_user'], $this->config['db_pass'], null, $this->config['db_port'])) != false && mysqli_select_db($this->sql, $this->config['db_name'])) {
return true;
} else {
$this->log('Cannot connect to MySQL');
try {
$this->sql = dbConnect(
$this->config['db_host'],
$this->config['db_user'],
$this->config['db_pass'],
$this->config['db_name'],
$this->config['db_port'],
$this->config['db_socket']
);
} catch (DatabaseConnectException $e) {
$this->log('Cannot connect to MySQL: ' . $e->getMessage());
return die();
}
} else {
return true;
}
return true;
}//end chkdb()


69 changes: 31 additions & 38 deletions build-base.php
Original file line number Diff line number Diff line change
@@ -1,56 +1,49 @@
#!/usr/bin/env php
<?php

// MYSQL Check - FIXME
// 1 UNKNOWN
$config['db_port'] = null;
include 'config.php';
if (!isset($init_modules)) {
$init_modules = array();
require __DIR__ . '/includes/init.php';

$options = getopt('d');
$debug = isset($options['d']);
}

if (!isset($sql_file)) {
$sql_file = 'build.sql';
}

$sql_fh = fopen($sql_file, 'r');
if ($sql_fh === false) {
echo 'ERROR: Cannot open SQL build script '.$sql_file."\n";
exit(1);
}

$database_link = mysqli_connect('p:'.$config['db_host'], $config['db_user'], $config['db_pass'], null, $config['db_port']);
if ($database_link === false) {
echo 'ERROR: Cannot connect to database: '.mysqli_error($database_link)."\n";
echo 'ERROR: Cannot open SQL build script '.$sql_file.PHP_EOL;
exit(1);
}

$select = mysqli_select_db($database_link, $config['db_name']);
mysqli_query($database_link, "SET NAMES 'utf8'");
mysqli_query($database_link, "SET CHARACTER SET 'utf8'");
mysqli_query($database_link, "SET COLLATION_CONNECTION = 'utf8_unicode_ci'");
if ($select === false) {
echo 'ERROR: Cannot select database: '.mysqli_error($database_link)."\n";
exit(1);
}

$limit = 0;
while (!feof($sql_fh)) {
$line = fgetss($sql_fh);
if (isset($_SESSION['stage'])) {
$limit++;
if (isset($_SESSION['offset']) && $limit < $_REQUEST['offset']) {
continue;
} elseif (time()-$_SESSION['last'] > 45) {
$_SESSION['offset'] = $limit;
$GLOBALS['refresh'] = '<b>Installing, please wait..</b><sub>'.date('r').'</sub><script>window.location.href = "install.php?offset='.$limit.'";</script>';
return;
} else {
echo 'Step #'.$limit.' ...'.PHP_EOL;
// only import build.sql to an empty database
$tables = dbFetchRows("SHOW TABLES FROM {$config['db_name']}");
if (empty($tables)) {
$limit = 0;

while (!feof($sql_fh)) {
$line = fgetss($sql_fh);
if (isset($_SESSION['stage'])) {
$limit++;
if (isset($_SESSION['offset']) && $limit < $_REQUEST['offset']) {
continue;
} elseif (time()-$_SESSION['last'] > 45) {
$_SESSION['offset'] = $limit;
$GLOBALS['refresh'] = '<b>Installing, please wait..</b><sub>'.date('r').'</sub><script>window.location.href = "install.php?offset='.$limit.'";</script>';
return;
} else {
echo 'Step #'.$limit.' ...'.PHP_EOL;
}
}
}

if (!empty($line)) {
$creation = mysqli_query($database_link, $line);
if (!$creation) {
echo 'WARNING: Cannot execute query ('.$line.'): '.mysqli_error($database_link)."\n";
if (!empty($line)) {
$creation = dbQuery($line);
if (!$creation) {
echo 'WARNING: Cannot execute query ('.$line.'): '.mysqli_error($database_link)."\n";
}
}
}
}
3 changes: 3 additions & 0 deletions discovery-wrapper.py
Original file line number Diff line number Diff line change
@@ -92,6 +92,9 @@ def get_config_data():
if config['db_host'][:5].lower() == 'unix:':
db_server = config['db_host']
db_port = 0
elif config['db_socket']:
db_server = config['db_socket']
db_port = 0
else:
db_server = config['db_host']

7 changes: 7 additions & 0 deletions doc/Support/Configuration.md
Original file line number Diff line number Diff line change
@@ -32,6 +32,13 @@ $config['db_pass'] = '';
$config['db_name'] = '';
```

If you use a unix socket, you can specify it with these options:
```php
$config['db_host'] = NULL;
$config['db_port'] = NULL;
$config['db_socket'] = '/run/mysqld/mysqld.sock';
```

### Programs

A lot of these are self explanatory so no further information may be provided. Any extensions that have dedicated
44 changes: 31 additions & 13 deletions html/install.php
Original file line number Diff line number Diff line change
@@ -29,11 +29,20 @@
$dbpass = @$_POST['dbpass'] ?: '';
$dbname = @$_POST['dbname'] ?: 'librenms';
$dbport = @$_POST['dbport'] ?: 3306;
$dbsocket = @$_POST['dbsocket'] ?: '';
$config['db_host']=$dbhost;
$config['db_user']=$dbuser;
$config['db_pass']=$dbpass;
$config['db_name']=$dbname;
$config['db_port']=$dbport;
$config['db_socket']=$dbsocket;

if (!empty($config['db_socket'])) {
$config['db_host'] = '';
$config['db_port'] = null;
} else {
$config['db_socket'] = null;
}

$add_user = @$_POST['add_user'] ?: '';
$add_pass = @$_POST['add_pass'] ?: '';
@@ -42,18 +51,15 @@

// Check we can connect to MySQL DB, if not, back to stage 1 :)
if ($stage > 1) {
$database_link = mysqli_connect('p:'.$dbhost, $dbuser, $dbpass, $dbname, $dbport);
dbQuery("SET NAMES 'utf8'");
dbQuery("SET CHARACTER SET 'utf8'");
dbQuery("SET COLLATION_CONNECTION = 'utf8_unicode_ci'");
if (mysqli_connect_error()) {
$stage = 1;
$msg = "Couldn't connect to the database, please check your details<br /> " . mysqli_connect_error();
} elseif ($stage == 2) {
if ($_SESSION['build-ok'] == true) {
$stage = 3;
$msg = "It appears that the database is already setup so have moved onto stage $stage";
try {
dbConnect();
if ($stage == 2 && $_SESSION['build-ok'] == true) {
$stage = 3;
$msg = "It appears that the database is already setup so have moved onto stage $stage";
}
} catch (\LibreNMS\Exceptions\DatabaseConnectException $e) {
$stage = 1;
$msg = "Couldn't connect to the database, please check your details<br /> " . $e->getMessage();
}
$_SESSION['stage'] = $stage;
}
@@ -255,13 +261,19 @@
<div class="form-group">
<label for="dbhost" class="col-sm-4" control-label">DB Host: </label>
<div class="col-sm-8">
<input type="text" class="form-control" name="dbhost" id="dbhost" value="<?php echo $dbhost; ?>">
<input type="text" class="form-control" name="dbhost" id="dbhost" value="<?php echo $dbhost; ?>" placeholder="Leave empty if using Unix-Socket">
</div>
</div>
<div class="form-group">
<label for="dbport" class="col-sm-4" control-label">DB Port: </label>
<div class="col-sm-8">
<input type="text" class="form-control" name="dbport" id="dbport" value="<?php echo $dbport; ?>">
<input type="text" class="form-control" name="dbport" id="dbport" value="<?php echo $dbport; ?>" placeholder="Leave empty if using Unix-Socket">
</div>
</div>
<div class="form-group">
<label for="dbsocket" class="col-sm-4" control-label">DB Unix-Socket: </label>
<div class="col-sm-8">
<input type="text" class="form-control" name="dbsocket" id="dbsocket" value="<?php echo $dbsocket; ?>" placeholder="Leave empty if using Host">
</div>
</div>
<div class="form-group">
@@ -304,6 +316,7 @@
$config['db_pass'] = $dbpass;
$config['db_name'] = $dbname;
$config['db_port'] = $dbport;
$config['db_socket'] = $dbsocket;
$sql_file = '../build.sql';
$_SESSION['last'] = time();
ob_end_flush();
@@ -335,6 +348,7 @@
<input type="hidden" name="dbpass" value="<?php echo $dbpass; ?>">
<input type="hidden" name="dbname" value="<?php echo $dbname; ?>">
<input type="hidden" name="dbport" value="<?php echo $dbport; ?>">
<input type="hidden" name="dbsocket" value="<?php echo $dbsocket; ?>">
<button type="submit" class="btn btn-success">Goto Add User</button>
</form>
</div>
@@ -360,6 +374,7 @@
\$config\['db_user'\] = '$dbuser';
\$config\['db_pass'\] = '$dbpass';
\$config\['db_name'\] = '$dbname';
\$config\['db_socket'\] = '$dbsocket';
\$config\['db'\]\['extension'\] = "mysqli";// mysql or mysqli
// This is the user LibreNMS will run as
@@ -421,6 +436,7 @@
<input type="hidden" name="dbuser" value="<?php echo $dbuser; ?>">
<input type="hidden" name="dbpass" value="<?php echo $dbpass; ?>">
<input type="hidden" name="dbname" value="<?php echo $dbname; ?>">
<input type="hidden" name="dbsocket" value="<?php echo $dbsocket; ?>">
<button type="submit" class="btn btn-success">Finish install</button>
</form>
<?php
@@ -443,6 +459,7 @@
<input type="hidden" name="dbuser" value="<?php echo $dbuser; ?>">
<input type="hidden" name="dbpass" value="<?php echo $dbpass; ?>">
<input type="hidden" name="dbname" value="<?php echo $dbname; ?>">
<input type="hidden" name="dbsocket" value="<?php echo $dbsocket; ?>">
<div class="form-group">
<label for="add_user" class="col-sm-4 control-label">Username</label>
<div class="col-sm-8">
@@ -498,6 +515,7 @@
<input type="hidden" name="dbuser" value="<?php echo $dbuser; ?>">
<input type="hidden" name="dbpass" value="<?php echo $dbpass; ?>">
<input type="hidden" name="dbname" value="<?php echo $dbname; ?>">
<input type="hidden" name="dbsocket" value="<?php echo $dbsocket; ?>">
<button type="submit" class="btn btn-success" <?php if ($proceed == "1") {
echo "disabled='disabled'";
} ?>>Generate Config</button>
51 changes: 0 additions & 51 deletions includes/build-base.inc.php

This file was deleted.

Loading

0 comments on commit b1a414e

Please sign in to comment.