diff options
author | Pim van den Berg | 2014-05-03 09:51:41 +0200 |
---|---|---|
committer | Pim van den Berg | 2014-05-03 10:11:07 +0200 |
commit | d72e04ce95bfcbdca9bed70bf3eb5943463d6a86 (patch) | |
tree | 6215edd6bbba35f49a27472a8fb306229581b614 | |
parent | plugin/memory: add osx/bsd ds_names to order, set color of active to ff00ff (diff) | |
download | apt-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
-rw-r--r-- | detail.php | 4 | ||||
-rw-r--r-- | host.php | 4 | ||||
-rw-r--r-- | inc/collectd.inc.php | 32 |
3 files changed, 19 insertions, 21 deletions
@@ -30,9 +30,7 @@ html_start(); | |||
30 | printf('<fieldset id="%s">', $host); | 30 | printf('<fieldset id="%s">', $host); |
31 | printf('<legend>%s</legend>', $host); | 31 | printf('<legend>%s</legend>', $host); |
32 | 32 | ||
33 | $plugins = collectd_plugins($host); | 33 | if (!$plugins = collectd_plugins($host)) { |
34 | |||
35 | if(!$plugins) { | ||
36 | echo "Unknown host\n"; | 34 | echo "Unknown host\n"; |
37 | return false; | 35 | return false; |
38 | } | 36 | } |
@@ -15,9 +15,7 @@ printf("<fieldset id=\"%s\">", $host); | |||
15 | printf("<legend>%s</legend>", $host); | 15 | printf("<legend>%s</legend>", $host); |
16 | 16 | ||
17 | 17 | ||
18 | $plugins = collectd_plugins($host); | 18 | if (!$plugins = collectd_plugins($host)) { |
19 | |||
20 | if(!$plugins) { | ||
21 | echo "Unknown host\n"; | 19 | echo "Unknown host\n"; |
22 | return false; | 20 | return false; |
23 | } | 21 | } |
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 |
89 | function collectd_plugins($host) { | 89 | function 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) { | |||
173 | function graphs_from_plugin($host, $plugin, $overview=false) { | 172 | function 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 | ||