From d72e04ce95bfcbdca9bed70bf3eb5943463d6a86 Mon Sep 17 00:00:00 2001
From: Pim van den Berg
Date: Sat, 3 May 2014 09:51:41 +0200
Subject: inc/collectd: consistently return false instead of an empty array or
false
This means we have to check the return value of these functions for
example before doing a foreach. We can't just do a foreach and assume the
return value of a function is an array, while it can be false and results
in a PHP warning.
Closes: https://github.com/pommi/CGP/pull/68
---
detail.php | 4 +---
host.php | 4 +---
inc/collectd.inc.php | 32 +++++++++++++++++---------------
3 files changed, 19 insertions(+), 21 deletions(-)
diff --git a/detail.php b/detail.php
index 4eacc80..33db2b9 100644
--- a/detail.php
+++ b/detail.php
@@ -30,9 +30,7 @@ html_start();
printf('
', $host);
printf('%s ', $host);
-$plugins = collectd_plugins($host);
-
-if(!$plugins) {
+if (!$plugins = collectd_plugins($host)) {
echo "Unknown host\n";
return false;
}
diff --git a/host.php b/host.php
index cc6fa50..b398630 100644
--- a/host.php
+++ b/host.php
@@ -15,9 +15,7 @@ printf("", $host);
printf("%s ", $host);
-$plugins = collectd_plugins($host);
-
-if(!$plugins) {
+if (!$plugins = collectd_plugins($host)) {
echo "Unknown host\n";
return false;
}
diff --git a/inc/collectd.inc.php b/inc/collectd.inc.php
index e364180..5927bf6 100644
--- a/inc/collectd.inc.php
+++ b/inc/collectd.inc.php
@@ -16,7 +16,8 @@ function collectd_hosts() {
if(!is_dir($CONFIG['datadir'].'/'.$v))
unset($dir[$k]);
}
- return($dir);
+
+ return $dir ? $dir : false;
}
@@ -35,7 +36,7 @@ function get_host_rrd_files($dir) {
$files[] = str_replace($dir.'/', '', $object->getPathname());
}
- return $files;
+ return $files ? $files : false;
}
@@ -47,8 +48,7 @@ function collectd_plugindata($host, $plugin=NULL) {
return false;
$hostdir = $CONFIG['datadir'].'/'.$host;
- $files = get_host_rrd_files($hostdir);
- if (!$files)
+ if (!$files = get_host_rrd_files($hostdir))
return false;
$data = array();
@@ -82,12 +82,13 @@ function collectd_plugindata($host, $plugin=NULL) {
$data = $pdata;
}
- return($data);
+ return $data ? $data : false;
}
# returns an array of all plugins of a host
function collectd_plugins($host) {
- $plugindata = collectd_plugindata($host);
+ if (!$plugindata = collectd_plugindata($host))
+ return false;
$plugins = array();
foreach ($plugindata as $item) {
@@ -95,7 +96,7 @@ function collectd_plugins($host) {
$plugins[] = $item['p'];
}
- return $plugins;
+ return $plugins ? $plugins : false;
}
# returns an array of all pi/t/ti of an plugin
@@ -104,7 +105,8 @@ function collectd_plugindetail($host, $plugin, $detail, $where=NULL) {
if (!in_array($detail, $details))
return false;
- $plugindata = collectd_plugindata($host);
+ if (!$plugindata = collectd_plugindata($host))
+ return false;
$return = array();
foreach ($plugindata as $item) {
@@ -124,10 +126,7 @@ function collectd_plugindetail($host, $plugin, $detail, $where=NULL) {
}
}
- if (empty($return))
- return false;
-
- return $return;
+ return $return ? $return : false;
}
# group plugin files for graph generation
@@ -173,9 +172,12 @@ function plugin_sort($data) {
function graphs_from_plugin($host, $plugin, $overview=false) {
global $CONFIG;
- $plugindata = collectd_plugindata($host, $plugin);
- $plugindata = group_plugindata($plugindata);
- $plugindata = plugin_sort($plugindata);
+ if (!$plugindata = collectd_plugindata($host, $plugin))
+ return false;
+ if (!$plugindata = group_plugindata($plugindata))
+ return false;
+ if (!$plugindata = plugin_sort($plugindata))
+ return false;
foreach ($plugindata as $items) {
--
cgit v1.1