1<?php
2/*
3 +-------------------------------------------------------------------------+
4 | Copyright (C) 2004-2021 The Cacti Group                                 |
5 |                                                                         |
6 | This program is free software; you can redistribute it and/or           |
7 | modify it under the terms of the GNU General Public License             |
8 | as published by the Free Software Foundation; either version 2          |
9 | of the License, or (at your option) any later version.                  |
10 |                                                                         |
11 | This program is distributed in the hope that it will be useful,         |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
14 | GNU General Public License for more details.                            |
15 +-------------------------------------------------------------------------+
16 | Cacti: The Complete RRDtool-based Graphing Solution                     |
17 +-------------------------------------------------------------------------+
18 | This code is designed, written, and maintained by the Cacti Group. See  |
19 | about.php and/or the AUTHORS file for specific developer information.   |
20 +-------------------------------------------------------------------------+
21 | http://www.cacti.net/                                                   |
22 +-------------------------------------------------------------------------+
23*/
24
25/* update_data_source_title_cache_from_template - updates the title cache for all data sources
26	that match a given data template
27   @arg $data_template_id - (int) the ID of the data template to match */
28function update_data_source_title_cache_from_template($data_template_id) {
29	$data = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . ' local_data_id
30		FROM data_template_data
31		WHERE data_template_id = ?
32		AND local_data_id > 0',
33		array($data_template_id));
34
35	if (cacti_sizeof($data)) {
36		foreach ($data as $item) {
37			update_data_source_title_cache($item['local_data_id']);
38		}
39	}
40}
41
42/* update_data_source_title_cache_from_query - updates the title cache for all data sources
43	that match a given data query/index combination
44   @arg $snmp_query_id - (int) the ID of the data query to match
45   @arg $snmp_index - the index within the data query to match */
46function update_data_source_title_cache_from_query($snmp_query_id, $snmp_index) {
47	$data = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . ' id
48		FROM data_local
49		WHERE snmp_query_id = ?
50		AND snmp_index = ?',
51		array($snmp_query_id, $snmp_index));
52
53	if (cacti_sizeof($data) > 0) {
54		foreach ($data as $item) {
55			update_data_source_title_cache($item['id']);
56		}
57	}
58}
59
60/* update_data_source_title_cache_from_host - updates the title cache for all data sources
61	that match a given host
62   @arg $host_id - (int) the ID of the host to match */
63function update_data_source_title_cache_from_host($host_id, $query_id = 0, $ids = array()) {
64	if ($query_id > 0 && !cacti_sizeof($ids)) {
65	$data = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . ' id
66		FROM data_local
67			WHERE host_id = ?
68			AND snmp_query_id = ?',
69			array($host_id, $query_id));
70	} elseif ($query_id > 0) {
71		$data = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . ' id
72			FROM data_local
73			WHERE host_id = ?
74			AND snmp_query_id = ?
75			AND id IN (?)',
76			array($host_id, $query_id, implode(',', $ids)));
77	} else {
78		$data = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . ' id
79			FROM data_local
80			WHERE host_id = ?',
81			array($host_id));
82	}
83
84	if (cacti_sizeof($data)) {
85		foreach ($data as $item) {
86			update_data_source_title_cache($item['id']);
87		}
88	}
89}
90
91/* update_data_source_title_cache - updates the title cache for a single data source
92   @arg $local_data_id - (int) the ID of the data source to update the title cache for */
93function update_data_source_title_cache($local_data_id) {
94	$old_title = db_fetch_cell_prepared('SELECT name_cache
95		FROM data_template_data
96		WHERE local_data_id = ?',
97		array($local_data_id));
98
99	$data_source = get_data_source_title($local_data_id);
100
101	if (strstr($data_source, '|query_') !== false || strstr($data_source, '|host_') !== false) {
102		if ($old_title == '') {
103			db_execute_prepared('UPDATE data_template_data
104				SET name_cache = ?
105				WHERE local_data_id = ?',
106				array($data_source, $local_data_id));
107
108			api_plugin_hook_function('update_data_source_title_cache', $local_data_id);
109		}
110	} else {
111		db_execute_prepared('UPDATE data_template_data
112			SET name_cache = ?
113			WHERE local_data_id = ?',
114			array($data_source, $local_data_id));
115
116		api_plugin_hook_function('update_data_source_title_cache', $local_data_id);
117	}
118}
119
120/* update_graph_title_cache_from_template - updates the title cache for all graphs
121	that match a given graph template
122   @arg $graph_template_id - (int) the ID of the graph template to match */
123function update_graph_title_cache_from_template($graph_template_id) {
124	$graphs = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . ' local_graph_id
125		FROM graph_templates_graph
126		WHERE graph_template_id = ?
127		AND local_graph_id > 0',
128		array($graph_template_id));
129
130	if (cacti_sizeof($graphs) > 0) {
131		foreach ($graphs as $item) {
132			update_graph_title_cache($item['local_graph_id']);
133		}
134	}
135}
136
137/* update_graph_title_cache_from_query - updates the title cache for all graphs
138	that match a given data query/index combination
139   @arg $snmp_query_id - (int) the ID of the data query to match
140   @arg $snmp_index - the index within the data query to match */
141function update_graph_title_cache_from_query($snmp_query_id, $snmp_index) {
142	$graphs = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . ' id
143		FROM graph_local
144		WHERE snmp_query_id = ?
145		AND snmp_index = ?',
146		array($snmp_query_id, $snmp_index));
147
148	if (cacti_sizeof($graphs) > 0) {
149		foreach ($graphs as $item) {
150			update_graph_title_cache($item['id']);
151		}
152	}
153}
154
155/* update_graph_title_cache_from_host - updates the title cache for all graphs
156	that match a given host
157   @arg $host_id - (int) the ID of the host to match */
158function update_graph_title_cache_from_host($host_id, $query_id = 0, $ids = array()) {
159	if ($query_id > 0 && !cacti_sizeof($ids)) {
160	$graphs = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . ' id
161		FROM graph_local
162			WHERE host_id = ?
163			AND snmp_query_id = ?',
164			array($host_id, $query_id));
165	} elseif ($query_id > 0) {
166		$graphs = db_fetch_assoc_prepared('SELECT DISTINCT ' . SQL_NO_CACHE . ' gl.id
167			FROM graph_local AS gl
168			INNER JOIN graph_templates_item AS gti
169			ON gti.local_graph_id = gl.id
170			INNER JOIN data_template_rrd AS dtr
171			ON gti.task_item_id = dtr.id
172			WHERE host_id = ?
173			AND snmp_query_id = ?
174			AND dtr.local_data_id IN(?)',
175			array($host_id, $query_id, implode(',', $ids)));
176	} else {
177		$graphs = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . ' id
178			FROM graph_local
179			WHERE host_id = ?',
180			array($host_id));
181	}
182
183	if (cacti_sizeof($graphs)) {
184		foreach ($graphs as $item) {
185			update_graph_title_cache($item['id']);
186		}
187	}
188}
189
190/* update_graph_title_cache - updates the title cache for a single graph
191   @arg $local_graph_id - (int) the ID of the graph to update the title cache for */
192function update_graph_title_cache($local_graph_id) {
193	$old_title = db_fetch_cell_prepared('SELECT title_cache
194		FROM graph_templates_graph
195		WHERE local_graph_id = ?',
196		array($local_graph_id));
197
198	$graph_title = get_graph_title($local_graph_id);
199
200	if (strstr($graph_title, '|query_') !== false || strstr($graph_title, '|host_') !== false) {
201		if ($old_title == '') {
202			db_execute_prepared('UPDATE graph_templates_graph
203				SET title_cache = ?
204				WHERE local_graph_id = ?',
205				array($graph_title, $local_graph_id));
206		}
207	} else {
208		db_execute_prepared('UPDATE graph_templates_graph
209			SET title_cache = ?
210			WHERE local_graph_id = ?',
211			array($graph_title, $local_graph_id));
212	}
213}
214
215/* null_out_substitutions - takes a string and cleans out any host variables that do not have values
216   @arg $string - the string to clean out unsubstituted variables for
217   @returns - the cleaned up string */
218function null_out_substitutions($string) {
219	return preg_replace("/\|host_" . VALID_HOST_FIELDS . "\|( - )?/i", '', $string);
220}
221
222/* expand_title - takes a string and substitutes all data query variables contained in it or cleans
223	them out if no data query is in use
224   @arg $host_id - (int) the host ID to match
225   @arg $snmp_query_id - (int) the data query ID to match
226   @arg $snmp_index - the data query index to match
227   @arg $title - the original string that contains the data query variables
228   @returns - the original string with all of the variable substitutions made */
229function expand_title($host_id, $snmp_query_id, $snmp_index, $title) {
230	if ((strstr($title, '|')) && (!empty($host_id))) {
231		if (($snmp_query_id != '0') && ($snmp_index != '')) {
232			$title = substitute_snmp_query_data(null_out_substitutions(substitute_host_data($title, '|', '|', $host_id)), $host_id, $snmp_query_id, $snmp_index, read_config_option('max_data_query_field_length'));
233		} else {
234			$title = null_out_substitutions(substitute_host_data($title, '|', '|', $host_id));
235		}
236	} else {
237		$title = null_out_substitutions($title);
238	}
239
240	$data = array(
241		'host_id'       => $host_id,
242		'snmp_query_id' => $snmp_query_id,
243		'snmp_index'    => $snmp_index,
244		'title'         => $title
245	);
246
247	$data = api_plugin_hook_function('expand_title', $data);
248	if (isset($data['title'])) {
249		$title = $data['title'];
250	}
251
252	return $title;
253}
254
255/* substitute_script_query_path - takes a string and substitutes all path variables contained in it
256   @arg $path - the string to make path variable substitutions on
257   @returns - the original string with all of the variable substitutions made */
258function substitute_script_query_path($path) {
259	global $config;
260
261	$path = clean_up_path(str_replace('|path_cacti|', $config['base_path'], $path));
262	$path = clean_up_path(str_replace('|path_php_binary|', read_config_option('path_php_binary'), $path));
263
264	return $path;
265}
266
267/* substitute_host_data - takes a string and substitutes all host variables contained in it
268   @arg $string - the string to make host variable substitutions on
269   @arg $l_escape_string - the character used to escape each variable on the left side
270   @arg $r_escape_string - the character used to escape each variable on the right side
271   @arg $host_id - (int) the host ID to match
272   @returns - the original string with all of the variable substitutions made */
273function substitute_host_data($string, $l_escape_string, $r_escape_string, $host_id) {
274	if (!empty($host_id)) {
275		$host = db_fetch_row_prepared('SELECT ' . SQL_NO_CACHE . ' h.*, s.name AS site_name
276			FROM host AS h
277			LEFT JOIN sites AS s
278			ON h.site_id = s.id
279			WHERE h.id = ?', array($host_id));
280
281		if (!cacti_sizeof($host)) {
282			return $string;
283		}
284
285		$search  = array();
286		$replace = array();
287
288		$search[]  = $l_escape_string . 'host_management_ip' . $r_escape_string; /* for compatibility */
289		$replace[] = $host['hostname']; /* for compatibility */
290
291		/* common host columns */
292		$search[]  = $l_escape_string . 'host_id' . $r_escape_string;
293		$replace[] = $host['id'];
294		$search[]  = $l_escape_string . 'host_hostname' . $r_escape_string;
295		$replace[] = $host['hostname'];
296		$search[]  = $l_escape_string . 'host_description' . $r_escape_string;
297		$replace[] = $host['description'];
298		$search[]  = $l_escape_string . 'host_site' . $r_escape_string;
299		$replace[] = $host['site_name'];
300		$search[]  = $l_escape_string . 'host_notes' . $r_escape_string;
301		$replace[] = $host['notes'];
302		$search[]  = $l_escape_string . 'host_location' . $r_escape_string;
303		$replace[] = $host['location'];
304		$search[]  = $l_escape_string . 'host_polling_time' . $r_escape_string;
305		$replace[] = $host['polling_time'];
306		$search[]  = $l_escape_string . 'host_avg_time' . $r_escape_string;
307		$replace[] = $host['avg_time'];
308		$search[]  = $l_escape_string . 'host_cur_time' . $r_escape_string;
309		$replace[] = $host['cur_time'];
310		$search[]  = $l_escape_string . 'host_availability' . $r_escape_string;
311		$replace[] = $host['availability'];
312		$search[]  = $l_escape_string . 'host_uptime' . $r_escape_string;
313		$replace[] = get_uptime($host);
314
315		/* snmp connectivity information */
316		$search[]  = $l_escape_string . 'host_snmp_community' . $r_escape_string;
317		$replace[] = $host['snmp_community'];
318		$search[]  = $l_escape_string . 'host_snmp_version' . $r_escape_string;
319		$replace[] = $host['snmp_version'];
320		$search[]  = $l_escape_string . 'host_snmp_username' . $r_escape_string;
321		$replace[] = $host['snmp_username'];
322		$search[]  = $l_escape_string . 'host_snmp_password' . $r_escape_string;
323		$replace[] = $host['snmp_password'];
324		$search[]  = $l_escape_string . 'host_snmp_auth_protocol' . $r_escape_string;
325		$replace[] = $host['snmp_auth_protocol'];
326		$search[]  = $l_escape_string . 'host_snmp_priv_passphrase' . $r_escape_string;
327		$replace[] = $host['snmp_priv_passphrase'];
328		$search[]  = $l_escape_string . 'host_snmp_priv_protocol' . $r_escape_string;
329		$replace[] = $host['snmp_priv_protocol'];
330		$search[]  = $l_escape_string . 'host_snmp_context' . $r_escape_string;
331		$replace[] = $host['snmp_context'];
332		$search[]  = $l_escape_string . 'host_snmp_engine_id' . $r_escape_string;
333		$replace[] = $host['snmp_engine_id'];
334		$search[]  = $l_escape_string . 'host_snmp_port' . $r_escape_string;
335		$replace[] = $host['snmp_port'];
336		$search[]  = $l_escape_string . 'host_snmp_timeout' . $r_escape_string;
337		$replace[] = $host['snmp_timeout'];
338
339		/* snmp system information */
340		$search[]  = $l_escape_string . 'host_snmp_sysDescr' . $r_escape_string;
341		$replace[] = $host['snmp_sysDescr'];
342		$search[]  = $l_escape_string . 'host_snmp_sysObjectID' . $r_escape_string;
343		$replace[] = $host['snmp_sysObjectID'];
344		$search[]  = $l_escape_string . 'host_snmp_sysContact' . $r_escape_string;
345		$replace[] = $host['snmp_sysContact'];
346		$search[]  = $l_escape_string . 'host_snmp_sysLocation' . $r_escape_string;
347		$replace[] = $host['snmp_sysLocation'];
348		$search[]  = $l_escape_string . 'host_snmp_sysName' . $r_escape_string;
349		$replace[] = $host['snmp_sysName'];
350		$search[]  = $l_escape_string . 'host_snmp_sysUpTimeInstance' . $r_escape_string;
351		$replace[] = $host['snmp_sysUpTimeInstance'];
352
353		$search[]  = $l_escape_string . 'host_ping_retries' . $r_escape_string;
354		$replace[] = $host['ping_retries'];
355		$search[]  = $l_escape_string . 'host_max_oids' . $r_escape_string;
356		$replace[] = $host['max_oids'];
357
358		/* handle the external id */
359		$search[]  = $l_escape_string . 'host_external_id' . $r_escape_string;
360		$replace[] = $host['external_id'];
361
362		$string = str_replace($search, $replace, $string);
363
364		$temp = api_plugin_hook_function(
365			'substitute_host_data',
366			array('string' => $string, 'l_escape_string' => $l_escape_string, 'r_escape_string' => $r_escape_string, 'host_id' => $host_id)
367		);
368
369		$string = $temp['string'];
370	}
371
372	return $string;
373}
374
375/* substitute_snmp_query_data - takes a string and substitutes all data query variables contained in it
376   @arg $string - the original string that contains the data query variables
377   @arg $host_id - (int) the host ID to match
378   @arg $snmp_query_id - (int) the data query ID to match
379   @arg $snmp_index - the data query index to match
380   @arg $max_chars - the maximum number of characters to substitute
381   @returns - the original string with all of the variable substitutions made */
382function substitute_snmp_query_data($string, $host_id, $snmp_query_id, $snmp_index, $max_chars = 0) {
383	if ($host_id > 0) {
384		$snmp_cache_data = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . ' field_name, field_value
385			FROM host_snmp_cache
386			WHERE host_id = ?
387			AND snmp_query_id = ?
388			AND snmp_index = ?',
389			array($host_id, $snmp_query_id, $snmp_index));
390	} else {
391		$snmp_cache_data = db_fetch_assoc_prepared('SELECT DISTINCT ' . SQL_NO_CACHE . ' field_name, field_value
392			FROM host_snmp_cache
393			WHERE snmp_query_id = ?
394			AND snmp_index = ?
395			AND host_id = 0',
396			array($snmp_query_id, $snmp_index));
397	}
398
399	if (cacti_sizeof($snmp_cache_data)) {
400		foreach ($snmp_cache_data as $data) {
401			if (trim($data['field_value']) != '') {
402				if ($max_chars > 0) {
403					$data['field_value'] = substr($data['field_value'], 0, $max_chars);
404				}
405
406				$string = stri_replace('|query_' . $data['field_name'] . '|', $data['field_value'], $string);
407
408				if (strpos($string, 'query_') === false) {
409					break;
410				}
411			}
412		}
413	}
414
415	return $string;
416}
417
418/* substitute_data_input_data - takes a string and substitutes all data input variables contained in it
419   @arg $string - the original string that contains the data input variables
420   @arg $local_data_id - (int) the local data id to match
421   @arg $max_chars - the maximum number of characters to substitute
422   @returns - the original string with all of the variable substitutions made */
423function substitute_data_input_data($string, $graph, $local_data_id, $max_chars = 0) {
424	if (empty($local_data_id)) {
425		if (isset($graph['local_graph_id'])) {
426			$local_data_ids = array_rekey(db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . ' DISTINCT local_data_id
427				FROM data_template_rrd
428				INNER JOIN graph_templates_item
429				ON data_template_rrd.id = graph_templates_item.task_item_id
430				WHERE local_graph_id = ?', array($graph['local_graph_id'])), 'local_data_id', 'local_data_id');
431
432			if (cacti_sizeof($local_data_ids)) {
433				$data_template_data_id = db_fetch_cell('SELECT ' . SQL_NO_CACHE . ' id
434					FROM data_template_data
435					WHERE local_data_id IN (' . implode(',', $local_data_ids) . ')');
436			} else {
437				$data_template_data_id = 0;
438			}
439		} else {
440			$data_template_data_id = 0;
441		}
442	} else {
443		$data_template_data_id = db_fetch_cell_prepared('SELECT ' . SQL_NO_CACHE . ' id
444			FROM data_template_data
445			WHERE local_data_id = ?',
446			array($local_data_id));
447	}
448
449	if (!empty($data_template_data_id)) {
450		$data = db_fetch_assoc_prepared("SELECT " . SQL_NO_CACHE . "
451			dif.data_name, did.value
452			FROM data_input_fields AS dif
453			INNER JOIN data_input_data AS did
454			ON dif.id = did.data_input_field_id
455			WHERE data_template_data_id = ?
456			AND input_output = 'in'",
457			array($data_template_data_id));
458
459		if (cacti_sizeof($data)) {
460			foreach ($data as $item) {
461				if ($item['value'] != '') {
462					if ($max_chars > 0) {
463						$item['value'] = substr($item['field_value'], 0, $max_chars);
464					}
465
466					$string = stri_replace('|input_' . $item['data_name'] . '|', $item['value'], $string);
467				}
468			}
469		}
470	}
471
472	return $string;
473}
474
475