aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/inc/collectd.inc.php
blob: 7f8845629fa88443595fc14abed2d68fd4b5b3a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php

# collectd related functions

require_once 'conf/common.inc.php';

# returns an array of all collectd hosts
function collectd_hosts() {
	global $CONFIG;

	if (!is_dir($CONFIG['datadir']))
		return false;

	$dir = scandir($CONFIG['datadir']);
	foreach($dir as $k => $v) {
		if(!is_dir($CONFIG['datadir'].'/'.$v) || $v == '.' || $v == '..')
			unset($dir[$k]);
	}
	return($dir);
}

# returns an array of plugins/pinstances/types/tinstances
function collectd_plugindata($host) {
	global $CONFIG;

	if (!is_dir($CONFIG['datadir'].'/'.$host))
		return false;

	chdir($CONFIG['datadir'].'/'.$host);
	$files = glob("*/*.rrd");
	if (!$files)
		return false;
	
	$data = array();
	foreach($files as $item) {
		preg_match('#([\w_]+)(?:\-(.+))?/([\w_]+)(?:\-(.+))?\.rrd#', $item, $matches);

		$data[] = array(
			'p'  => $matches[1],
			'pi' => isset($matches[2]) ? $matches[2] : '',
			't'  => $matches[3],
			'ti' => isset($matches[4]) ? $matches[4] : '',
		);
	}

	return($data);
}

# returns an array of all plugins of a host
function collectd_plugins($host) {
	$plugindata = collectd_plugindata($host);

	$plugins = array();
	foreach ($plugindata as $item) {
		if (!in_array($item['p'], $plugins))
			$plugins[] = $item['p'];
	}
	
	return $plugins;
}

# returns an array of all pi/t/ti of an plugin
function collectd_plugindetail($host, $plugin, $detail, $where=NULL) {
	$details = array('pi', 't', 'ti');
	if (!in_array($detail, $details))
		return false;

	$plugindata = collectd_plugindata($host);

	$return = array();
	foreach ($plugindata as $item) {
		if ($item['p'] == $plugin && !in_array($item[$detail], $return) && isset($item[$detail])) {
			if ($where) {
				$add = true;
				# add detail to returnvalue if all where is true
				foreach($where as $key => $value) {
					if ($item[$key] != $value)
						$add = false;
				}
				if ($add)
					$return[] = $item[$detail];
			} else {
				$return[] = $item[$detail];
			}
		}
	}

	if (empty($return))
		return false;
	
	return $return;
}

# generate graph url's for a plugin of a host
function graphs_from_plugin($host, $plugin) {
	global $CONFIG;

	$pis = collectd_plugindetail($host, $plugin, 'pi');
	$ts = collectd_plugindetail($host, $plugin, 't');
	$tis = collectd_plugindetail($host, $plugin, 'ti');
	if (!$pis) $pis = array('NULL');
	if (!$tis) $tis = array('NULL');
	# backwards compatibility
	if ($CONFIG['version'] >= 5 || !preg_match('/^(df|interface)$/', $plugin))
		$tis = array('NULL');

	foreach($pis as $pi) {
		foreach ($tis as $ti) {
			foreach ($ts as $t) {
				$items = array(
					'h' => $host,
					'p' => $plugin,
					'pi' => $pi,
					't' => $t,
					'ti' => $ti
				);

				$time = array_key_exists($plugin, $CONFIG['time_range'])
					? $CONFIG['time_range'][$plugin]
					: $CONFIG['time_range']['default'];

				printf('<a href="%s/%s"><img src="%s/%s"></a>'."\n",
					$CONFIG['weburl'],
					build_url('detail.php', $items, $time),
					$CONFIG['weburl'],
					build_url('graph.php', $items, $time)
				);
			}
		}
	}
}

# generate an url with GET values from $items
function build_url($base, $items, $s=NULL) {
	global $CONFIG;

	if (!is_array($items))
		return false;
	
	if (!is_numeric($s))
		$s = $CONFIG['time_range']['default'];

	$i=0;
	foreach ($items as $key => $value) {
		# don't include empty values
		if ($value == 'NULL')
			continue;

		$base .= sprintf('%s%s=%s', $i==0 ? '?' : '&', $key, $value);
		$i++;
	}
	if (!isset($items['s']))
		$base .= '&s='.$s;

	return $base;
}

# generate identifier that collectd's FLUSH command understands
function collectd_identifier($host, $plugin, $pinst, $type, $tinst) {
	global $CONFIG;

	$identifier = sprintf('%s/%s%s%s/%s%s%s', $host,
		$plugin, strlen($pinst) ? '-' : '', $pinst,
		$type, strlen($tinst) ? '-' : '', $tinst);

	if (is_file($CONFIG['datadir'].'/'.$identifier.'.rrd'))
		return $identifier;
	else
		return FALSE;
}

# tell collectd to FLUSH all data of the identifier(s)
function collectd_flush($identifier) {
	global $CONFIG;

	if (!$CONFIG['socket'])
		return FALSE;

	if (!$identifier || (is_array($identifier) && count($identifier) == 0) ||
			!(is_string($identifier) || is_array($identifier)))
		return FALSE;

	$u_errno  = 0;
	$u_errmsg = '';
	if ($socket = @fsockopen($CONFIG['socket'], 0, $u_errno, $u_errmsg)) {
		$cmd = 'FLUSH plugin=rrdtool';
		if (is_array($identifier)) {
			foreach ($identifier as $val)
				$cmd .= sprintf(' identifier="%s"', $val);
		} else
			$cmd .= sprintf(' identifier="%s"', $identifier);
		$cmd .= "\n";

		$r = fwrite($socket, $cmd, strlen($cmd));
		if ($r === false || $r != strlen($cmd)) {
			printf('ERROR: Failed to write full command to unix-socket: %d out of %d written',
				$r === false ? -1 : $r, strlen($cmd));
			return FALSE;
		}

		$resp = fgets($socket);
		if ($resp === false) {
			printf('ERROR: Failed to read response from collectd for command: %s',
				trim($cmd));
			return FALSE;
		}

		$n = (int)$resp;
		while ($n-- > 0)
			fgets($socket);

		fclose($socket);

		return TRUE;
	} else {
		printf('ERROR: Failed to open unix-socket to collectd: %d: %s',
			$u_errno, $u_errmsg);
		return FALSE;
	}
}

?>