aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/inc/collectd.inc.php
blob: 695d79c2b42744a22eadf082d2ecd218d08ee75d (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
<?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 = array_diff(scandir($CONFIG['datadir']), array('.', '..'));
	foreach($dir as $k => $v) {
		if(!is_dir($CONFIG['datadir'].'/'.$v))
			unset($dir[$k]);
	}

	return $dir ? $dir : false;
}


# return files in directory. this will recurse into subdirs
# infinite loop may occur
function get_host_rrd_files($dir) {
	$files = array();

	$objects = new RegexIterator(
		new RecursiveIteratorIterator(
		new RecursiveDirectoryIterator($dir),
		RecursiveIteratorIterator::SELF_FIRST),
		'/\.rrd$/');

	foreach($objects as $object) {
		$files[] = str_replace($dir.'/', '', $object->getPathname());
	}

	return $files ? $files : false;
}


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

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

	$hostdir = $CONFIG['datadir'].'/'.$host;
	if (!$files = get_host_rrd_files($hostdir))
		return false;

	$data = array();
	foreach($files as $item) {
		preg_match('`
			(?P<p>[\w_]+)      # plugin
			(?:(?<=varnish)(?:\-(?P<c>[\w]+)))? # category
			(?:\-(?P<pi>.+))?  # plugin instance
			/
			(?P<t>[\w_]+)      # type
			(?:\-(?P<ti>.+))?  # type instance
			\.rrd
		`x', $item, $matches);

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

	# only return data about one plugin
	if (!is_null($plugin)) {
		$pdata = array();
		foreach($data as $item) {
			if ($item['p'] == $plugin)
				$pdata[] = $item;
		}
		$data = $pdata;
	}

	return $data ? $data : false;
}

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

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

	return $plugins ? $plugins : false;
}

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

	if (!$plugindata = collectd_plugindata($host))
		return false;

	$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];
			}
		}
	}

	return $return ? $return : false;
}

# group plugin files for graph generation
function group_plugindata($plugindata) {
	global $CONFIG;

	$data = array();
	# type instances should be grouped in 1 graph
	foreach ($plugindata as $item) {
		# backwards compatibility
		if ($CONFIG['version'] >= 5 || !preg_match('/^(df|interface)$/', $item['p']))
			if (
				$item['p'] != 'libvirt' &&
				($item['p'] != 'snmp' && $item['t'] != 'if_octets')
			)
				unset($item['ti']);
		$data[] = $item;
	}

	# remove duplicates
	$data = array_map("unserialize", array_unique(array_map("serialize", $data)));

	return $data;
}

function plugin_sort($data) {
	if (empty($data))
		return $data;

	foreach ($data as $key => $row) {
		$pi[$key] = (isset($row['pi'])) ? $row['pi'] : null;
		$c[$key]  = (isset($row['c']))  ? $row['c'] : null;
		$ti[$key] = (isset($row['ti'])) ? $row['ti'] : null;
		$t[$key]  = (isset($row['t']))  ? $row['t'] : null;
	}

	array_multisort($c, SORT_ASC, $pi, SORT_ASC, $t, SORT_ASC, $ti, SORT_ASC, $data);

	return $data;
}

function parse_typesdb_file($file = '/usr/share/collectd/types.db') {
	if (!file_exists($file))
		$file = 'inc/types.db';

	$types = array();
	foreach (file($file) as $type) {
		if(!preg_match('/^(?P<dataset>[\w_]+)\s+(?P<datasources>.*)/', $type, $matches))
			continue;
		$dataset = $matches['dataset'];
		$datasources = explode(', ', $matches['datasources']);

		foreach ($datasources as $ds) {
			if (!preg_match('/^(?P<dsname>\w+):(?P<dstype>[\w]+):(?P<min>[\-\dU\.]+):(?P<max>[\dU\.]+)/', $ds, $matches))
				error_log(sprintf('CGP Error: DS "%s" from dataset "%s" did not match', $ds, $dataset));
			$types[$dataset][$matches['dsname']] = array(
				'dstype' => $matches['dstype'],
				'min' => $matches['min'],
				'max' => $matches['max'],
			);
		}
	}
	return $types;
}