Use Config helper (#10339)

remove usage of global variable
This commit is contained in:
Tony Murray
2019-06-23 00:29:12 -05:00
committed by GitHub
parent 342acf50f1
commit f3ba8947f7
367 changed files with 1589 additions and 1857 deletions

View File

@@ -18,6 +18,7 @@
require 'includes/html/collectd/CollectdColor.php';
use LibreNMS\Config;
use LibreNMS\CollectdColor;
define('REGEXP_HOST', '/^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/');
@@ -84,10 +85,8 @@ function collectd_compare_host($a, $b)
*/
function collectd_list_hosts()
{
global $config;
$hosts = array();
foreach ($config['datadirs'] as $datadir) {
foreach (Config::get('datadirs') as $datadir) {
if ($d = @opendir($datadir)) {
while (($dent = readdir($d)) !== false) {
if ($dent != '.' && $dent != '..' && is_dir($datadir.'/'.$dent) && preg_match(REGEXP_HOST, $dent)) {
@@ -113,10 +112,8 @@ function collectd_list_hosts()
*/
function collectd_list_plugins($arg_host)
{
global $config;
$plugins = array();
foreach ($config['datadirs'] as $datadir) {
foreach (Config::get('datadirs') as $datadir) {
if (preg_match(REGEXP_HOST, $arg_host) && ($d = @opendir($datadir.'/'.$arg_host))) {
while (($dent = readdir($d)) !== false) {
if ($dent != '.' && $dent != '..' && is_dir($datadir.'/'.$arg_host.'/'.$dent)) {
@@ -147,10 +144,8 @@ function collectd_list_plugins($arg_host)
*/
function collectd_list_pinsts($arg_host, $arg_plugin)
{
global $config;
$pinsts = array();
foreach ($config['datadirs'] as $datadir) {
foreach (Config::get('datadirs') as $datadir) {
if (preg_match(REGEXP_HOST, $arg_host) && ($d = opendir($datadir.'/'.$arg_host))) {
while (($dent = readdir($d)) !== false) {
if ($dent != '.' && $dent != '..' && is_dir($datadir.'/'.$arg_host.'/'.$dent)) {
@@ -187,15 +182,13 @@ function collectd_list_pinsts($arg_host, $arg_plugin)
*/
function collectd_list_types($arg_host, $arg_plugin, $arg_pinst)
{
global $config;
$types = array();
$my_plugin = $arg_plugin.(strlen($arg_pinst) ? '-'.$arg_pinst : '');
if (!preg_match(REGEXP_PLUGIN, $my_plugin)) {
return $types;
}
foreach ($config['datadirs'] as $datadir) {
foreach (Config::get('datadirs') as $datadir) {
if (preg_match(REGEXP_HOST, $arg_host) && ($d = @opendir($datadir.'/'.$arg_host.'/'.$my_plugin))) {
while (($dent = readdir($d)) !== false) {
if ($dent != '.' && $dent != '..' && is_file($datadir.'/'.$arg_host.'/'.$my_plugin.'/'.$dent) && substr($dent, (strlen($dent) - 4)) == '.rrd') {
@@ -228,15 +221,13 @@ function collectd_list_types($arg_host, $arg_plugin, $arg_pinst)
*/
function collectd_list_tinsts($arg_host, $arg_plugin, $arg_pinst, $arg_type)
{
global $config;
$tinsts = array();
$my_plugin = $arg_plugin.(strlen($arg_pinst) ? '-'.$arg_pinst : '');
if (!preg_match(REGEXP_PLUGIN, $my_plugin)) {
return $tinsts;
}
foreach ($config['datadirs'] as $datadir) {
foreach (Config::get('datadirs') as $datadir) {
if (preg_match(REGEXP_HOST, $arg_host) && ($d = @opendir($datadir.'/'.$arg_host.'/'.$my_plugin))) {
while (($dent = readdir($d)) !== false) {
if ($dent != '.' && $dent != '..' && is_file($datadir.'/'.$arg_host.'/'.$my_plugin.'/'.$dent) && substr($dent, (strlen($dent) - 4)) == '.rrd') {
@@ -280,11 +271,10 @@ function collectd_list_tinsts($arg_host, $arg_plugin, $arg_pinst, $arg_type)
*/
function collectd_identifier($host, $plugin, $type, $pinst, $tinst)
{
global $config;
$rrd_realpath = null;
$orig_identifier = sprintf('%s/%s%s%s/%s%s%s', $host, $plugin, strlen($pinst) ? '-' : '', $pinst, $type, strlen($tinst) ? '-' : '', $tinst);
$identifier = null;
foreach ($config['datadirs'] as $datadir) {
foreach (Config::get('datadirs') as $datadir) {
if (is_file($datadir.'/'.$orig_identifier.'.rrd')) {
$rrd_realpath = realpath($datadir.'/'.$orig_identifier.'.rrd');
break;
@@ -317,9 +307,7 @@ function collectd_identifier($host, $plugin, $type, $pinst, $tinst)
*/
function collectd_flush($identifier)
{
global $config;
if (!$config['collectd_sock']) {
if (!Config::get('collectd_sock')) {
return false;
}
@@ -329,7 +317,7 @@ function collectd_flush($identifier)
$u_errno = 0;
$u_errmsg = '';
if ($socket = @fsockopen($config['collectd_sock'], 0, $u_errno, $u_errmsg)) {
if ($socket = @fsockopen(Config::get('collectd_sock'), 0, $u_errno, $u_errmsg)) {
$cmd = 'FLUSH plugin=rrdtool';
if (is_array($identifier)) {
foreach ($identifier as $val) {
@@ -448,17 +436,16 @@ function _rrd_info($file)
function rrd_get_color($code, $line = true)
{
global $config;
$name = ($line ? 'f_' : 'h_').$code;
if (!isset($config['rrd_colors'][$name])) {
if (!Config::has("rrd_colors.$name")) {
$c_f = new CollectdColor('random');
$c_h = new CollectdColor($c_f);
$c_h->fade();
$config['rrd_colors']['f_'.$code] = $c_f->toString();
$config['rrd_colors']['h_'.$code] = $c_h->toString();
Config::set("rrd_colors.f_$code", $c_f->toString());
Config::set("rrd_colors.h_$code", $c_h->toString());
}
return $config['rrd_colors'][$name];
return Config::get("rrd_colors.$name");
}//end rrd_get_color()
@@ -475,12 +462,12 @@ function rrd_get_color($code, $line = true)
*/
function collectd_draw_rrd($host, $plugin, $type, $pinst = null, $tinst = null, $opts = array())
{
global $config;
$timespan_def = null;
$timespans = Config::get('timespan');
if (!isset($opts['timespan'])) {
$timespan_def = reset($config['timespan']);
$timespan_def = reset($timespans);
} else {
foreach ($config['timespan'] as &$ts) {
foreach ($timespans as &$ts) {
if ($ts['name'] == $opts['timespan']) {
$timespan_def = $ts;
}
@@ -497,7 +484,7 @@ function collectd_draw_rrd($host, $plugin, $type, $pinst = null, $tinst = null,
$rrdinfo = null;
$rrdfile = sprintf('%s/%s%s%s/%s%s%s', $host, $plugin, is_null($pinst) ? '' : '-', $pinst, $type, is_null($tinst) ? '' : '-', $tinst);
foreach ($config['datadirs'] as $datadir) {
foreach (Config::get('datadirs') as $datadir) {
if (is_file($datadir.'/'.$rrdfile.'.rrd')) {
$rrdinfo = _rrd_info($datadir.'/'.$rrdfile.'.rrd');
if (isset($rrdinfo['RRA']) && is_array($rrdinfo['RRA'])) {
@@ -599,7 +586,7 @@ function collectd_draw_rrd($host, $plugin, $type, $pinst = null, $tinst = null,
}
}//end while
// $rrd_cmd = array(RRDTOOL, 'graph', '-', '-E', '-a', 'PNG', '-w', $config['rrd_width'], '-h', $config['rrd_height'], '-t', $rrdfile);
// $rrd_cmd = array(RRDTOOL, 'graph', '-', '-E', '-a', 'PNG', '-w', Config::get('rrd_width'), '-h', Config::get('rrd_height'), '-t', $rrdfile);
$rrd_cmd = array(
RRDTOOL,
'graph',
@@ -608,11 +595,11 @@ function collectd_draw_rrd($host, $plugin, $type, $pinst = null, $tinst = null,
'-a',
'PNG',
'-w',
$config['rrd_width'],
Config::get('rrd_width'),
'-h',
$config['rrd_height'],
Config::get('rrd_height'),
);
if ($config['rrd_width'] <= '300') {
if (Config::get('rrd_width') <= '300') {
$small_opts = array(
'--font',
'LEGEND:7:mono',
@@ -624,7 +611,7 @@ function collectd_draw_rrd($host, $plugin, $type, $pinst = null, $tinst = null,
$rrd_cmd = array_merge($rrd_cmd, $small_opts);
}
$rrd_cmd = array_merge($rrd_cmd, $config['rrd_opts_array'], $opts['rrd_opts'], $graph);
$rrd_cmd = array_merge($rrd_cmd, Config::get('rrd_opts_array'), $opts['rrd_opts'], $graph);
$cmd = RRDTOOL;
$count_rrd_cmd = count($rrd_cmd);
@@ -649,16 +636,17 @@ function collectd_draw_rrd($host, $plugin, $type, $pinst = null, $tinst = null,
*/
function collectd_draw_generic($timespan, $host, $plugin, $type, $pinst = null, $tinst = null)
{
global $config, $GraphDefs;
global $GraphDefs;
$timespan_def = null;
foreach ($config['timespan'] as &$ts) {
$timespans = Config::get('timespan');
foreach ($timespans as &$ts) {
if ($ts['name'] == $timespan) {
$timespan_def = $ts;
}
}
if (is_null($timespan_def)) {
$timespan_def = reset($config['timespan']);
$timespan_def = reset($timespans);
}
if (!isset($GraphDefs[$type])) {
@@ -666,7 +654,7 @@ function collectd_draw_generic($timespan, $host, $plugin, $type, $pinst = null,
}
$rrd_file = sprintf('%s/%s%s%s/%s%s%s', $host, $plugin, is_null($pinst) ? '' : '-', $pinst, $type, is_null($tinst) ? '' : '-', $tinst);
// $rrd_cmd = array(RRDTOOL, 'graph', '-', '-E', '-a', 'PNG', '-w', $config['rrd_width'], '-h', $config['rrd_height'], '-t', $rrd_file);
// $rrd_cmd = array(RRDTOOL, 'graph', '-', '-E', '-a', 'PNG', '-w', Config::get('rrd_width'), '-h', Config::get('rrd_height'), '-t', $rrd_file);
$rrd_cmd = array(
RRDTOOL,
'graph',
@@ -675,12 +663,12 @@ function collectd_draw_generic($timespan, $host, $plugin, $type, $pinst = null,
'-a',
'PNG',
'-w',
$config['rrd_width'],
Config::get('rrd_width'),
'-h',
$config['rrd_height'],
Config::get('rrd_height'),
);
if ($config['rrd_width'] <= '300') {
if (Config::get('rrd_width') <= '300') {
$small_opts = array(
'--font',
'LEGEND:7:mono',
@@ -692,10 +680,10 @@ function collectd_draw_generic($timespan, $host, $plugin, $type, $pinst = null,
$rrd_cmd = array_merge($rrd_cmd, $small_opts);
}
$rrd_cmd = array_merge($rrd_cmd, $config['rrd_opts_array']);
$rrd_cmd = array_merge($rrd_cmd, Config::get('rrd_opts_array'));
$rrd_args = $GraphDefs[$type];
foreach ($config['datadirs'] as $datadir) {
foreach (Config::get('datadirs') as $datadir) {
$file = $datadir.'/'.$rrd_file.'.rrd';
if (!is_file($file)) {
continue;
@@ -726,12 +714,12 @@ function collectd_draw_generic($timespan, $host, $plugin, $type, $pinst = null,
*/
function collectd_draw_meta_stack(&$opts, &$sources)
{
global $config;
$timespan_def = null;
$timespans = Config::get('timespan');
if (!isset($opts['timespan'])) {
$timespan_def = reset($config['timespan']);
$timespan_def = reset($timespans);
} else {
foreach ($config['timespan'] as &$ts) {
foreach ($timespans as &$ts) {
if ($ts['name'] == $opts['timespan']) {
$timespan_def = $ts;
}
@@ -754,7 +742,7 @@ function collectd_draw_meta_stack(&$opts, &$sources)
array_unshift($opts['rrd_opts'], '-o');
}
// $cmd = array(RRDTOOL, 'graph', '-', '-E', '-a', 'PNG', '-w', $config['rrd_width'], '-h', $config['rrd_height'],
// $cmd = array(RRDTOOL, 'graph', '-', '-E', '-a', 'PNG', '-w', Config::get('rrd_width'), '-h', Config::get('rrd_height'),
// '-t', $opts['title']);
$cmd = array(
RRDTOOL,
@@ -764,12 +752,12 @@ function collectd_draw_meta_stack(&$opts, &$sources)
'-a',
'PNG',
'-w',
$config['rrd_width'],
Config::get('rrd_width'),
'-h',
$config['rrd_height'],
Config::get('rrd_height'),
);
if ($config['rrd_width'] <= '300') {
if (Config::get('rrd_width') <= '300') {
$small_opts = array(
'--font',
'LEGEND:7:mono',
@@ -781,7 +769,7 @@ function collectd_draw_meta_stack(&$opts, &$sources)
$cmd = array_merge($cmd, $small_opts);
}
$cmd = array_merge($cmd, $config['rrd_opts_array'], $opts['rrd_opts']);
$cmd = array_merge($cmd, Config::get('rrd_opts_array'), $opts['rrd_opts']);
$max_inst_name = 0;
foreach ($sources as &$inst_data) {
@@ -863,12 +851,12 @@ function collectd_draw_meta_stack(&$opts, &$sources)
*/
function collectd_draw_meta_line(&$opts, &$sources)
{
global $config;
$timespan_def = null;
$timespans = Config::get('timespan');
if (!isset($opts['timespan'])) {
$timespan_def = reset($config['timespan']);
$timespan_def = reset($timespans);
} else {
foreach ($config['timespan'] as &$ts) {
foreach ($timespans as &$ts) {
if ($ts['name'] == $opts['timespan']) {
$timespan_def = $ts;
}
@@ -891,8 +879,8 @@ function collectd_draw_meta_line(&$opts, &$sources)
array_unshift($opts['rrd_opts'], '-o');
}
// $cmd = array(RRDTOOL, 'graph', '-', '-E', '-a', 'PNG', '-w', $config['rrd_width'], '-h', $config['rrd_height'], '-t', $opts['title']);
// $cmd = array_merge($cmd, $config['rrd_opts_array'], $opts['rrd_opts']);
// $cmd = array(RRDTOOL, 'graph', '-', '-E', '-a', 'PNG', '-w', Config::get('rrd_width'), '-h', Config::get('rrd_height'), '-t', $opts['title']);
// $cmd = array_merge($cmd, Config::get('rrd_opts_array'), $opts['rrd_opts']);
$cmd = array(
RRDTOOL,
'graph',
@@ -901,12 +889,12 @@ function collectd_draw_meta_line(&$opts, &$sources)
'-a',
'PNG',
'-w',
$config['rrd_width'],
Config::get('rrd_width'),
'-h',
$config['rrd_height'],
Config::get('rrd_height'),
);
if ($config['rrd_width'] <= '300') {
if (Config::get('rrd_width') <= '300') {
$small_opts = array(
'--font',
'LEGEND:7:mono',