aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/inc
diff options
context:
space:
mode:
authorPim van den Berg2014-05-03 09:51:41 +0200
committerPim van den Berg2014-05-03 10:11:07 +0200
commitd72e04ce95bfcbdca9bed70bf3eb5943463d6a86 (patch)
tree6215edd6bbba35f49a27472a8fb306229581b614 /inc
parentplugin/memory: add osx/bsd ds_names to order, set color of active to ff00ff (diff)
downloadapt-panopticon_cgp-d72e04ce95bfcbdca9bed70bf3eb5943463d6a86.zip
apt-panopticon_cgp-d72e04ce95bfcbdca9bed70bf3eb5943463d6a86.tar.gz
apt-panopticon_cgp-d72e04ce95bfcbdca9bed70bf3eb5943463d6a86.tar.bz2
apt-panopticon_cgp-d72e04ce95bfcbdca9bed70bf3eb5943463d6a86.tar.xz
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
Diffstat (limited to 'inc')
-rw-r--r--inc/collectd.inc.php32
1 files changed, 17 insertions, 15 deletions
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() {
16 if(!is_dir($CONFIG['datadir'].'/'.$v)) 16 if(!is_dir($CONFIG['datadir'].'/'.$v))
17 unset($dir[$k]); 17 unset($dir[$k]);
18 } 18 }
19 return($dir); 19
20 return $dir ? $dir : false;
20} 21}
21 22
22 23
@@ -35,7 +36,7 @@ function get_host_rrd_files($dir) {
35 $files[] = str_replace($dir.'/', '', $object->getPathname()); 36 $files[] = str_replace($dir.'/', '', $object->getPathname());
36 } 37 }
37 38
38 return $files; 39 return $files ? $files : false;
39} 40}
40 41
41 42
@@ -47,8 +48,7 @@ function collectd_plugindata($host, $plugin=NULL) {
47 return false; 48 return false;
48 49
49 $hostdir = $CONFIG['datadir'].'/'.$host; 50 $hostdir = $CONFIG['datadir'].'/'.$host;
50 $files = get_host_rrd_files($hostdir); 51 if (!$files = get_host_rrd_files($hostdir))
51 if (!$files)
52 return false; 52 return false;
53 53
54 $data = array(); 54 $data = array();
@@ -82,12 +82,13 @@ function collectd_plugindata($host, $plugin=NULL) {
82 $data = $pdata; 82 $data = $pdata;
83 } 83 }
84 84
85 return($data); 85 return $data ? $data : false;
86} 86}
87 87
88# returns an array of all plugins of a host 88# returns an array of all plugins of a host
89function collectd_plugins($host) { 89function collectd_plugins($host) {
90 $plugindata = collectd_plugindata($host); 90 if (!$plugindata = collectd_plugindata($host))
91 return false;
91 92
92 $plugins = array(); 93 $plugins = array();
93 foreach ($plugindata as $item) { 94 foreach ($plugindata as $item) {
@@ -95,7 +96,7 @@ function collectd_plugins($host) {
95 $plugins[] = $item['p']; 96 $plugins[] = $item['p'];
96 } 97 }
97 98
98 return $plugins; 99 return $plugins ? $plugins : false;
99} 100}
100 101
101# returns an array of all pi/t/ti of an plugin 102# returns an array of all pi/t/ti of an plugin
@@ -104,7 +105,8 @@ function collectd_plugindetail($host, $plugin, $detail, $where=NULL) {
104 if (!in_array($detail, $details)) 105 if (!in_array($detail, $details))
105 return false; 106 return false;
106 107
107 $plugindata = collectd_plugindata($host); 108 if (!$plugindata = collectd_plugindata($host))
109 return false;
108 110
109 $return = array(); 111 $return = array();
110 foreach ($plugindata as $item) { 112 foreach ($plugindata as $item) {
@@ -124,10 +126,7 @@ function collectd_plugindetail($host, $plugin, $detail, $where=NULL) {
124 } 126 }
125 } 127 }
126 128
127 if (empty($return)) 129 return $return ? $return : false;
128 return false;
129
130 return $return;
131} 130}
132 131
133# group plugin files for graph generation 132# group plugin files for graph generation
@@ -173,9 +172,12 @@ function plugin_sort($data) {
173function graphs_from_plugin($host, $plugin, $overview=false) { 172function graphs_from_plugin($host, $plugin, $overview=false) {
174 global $CONFIG; 173 global $CONFIG;
175 174
176 $plugindata = collectd_plugindata($host, $plugin); 175 if (!$plugindata = collectd_plugindata($host, $plugin))
177 $plugindata = group_plugindata($plugindata); 176 return false;
178 $plugindata = plugin_sort($plugindata); 177 if (!$plugindata = group_plugindata($plugindata))
178 return false;
179 if (!$plugindata = plugin_sort($plugindata))
180 return false;
179 181
180 foreach ($plugindata as $items) { 182 foreach ($plugindata as $items) {
181 183