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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
|
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2024 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
/* update_data_source_title_cache_from_template - updates the title cache for all data sources
that match a given data template
@arg $data_template_id - (int) the ID of the data template to match */
function update_data_source_title_cache_from_template($data_template_id) {
$data = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . ' local_data_id
FROM data_template_data
WHERE data_template_id = ?
AND local_data_id > 0',
array($data_template_id));
if (cacti_sizeof($data)) {
foreach ($data as $item) {
update_data_source_title_cache($item['local_data_id']);
}
}
}
/* update_data_source_title_cache_from_query - updates the title cache for all data sources
that match a given data query/index combination
@arg $snmp_query_id - (int) the ID of the data query to match
@arg $snmp_index - the index within the data query to match */
function update_data_source_title_cache_from_query($snmp_query_id, $snmp_index) {
$data = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . ' id
FROM data_local
WHERE snmp_query_id = ?
AND snmp_index = ?',
array($snmp_query_id, $snmp_index));
if (cacti_sizeof($data) > 0) {
foreach ($data as $item) {
update_data_source_title_cache($item['id']);
}
}
}
/* update_data_source_title_cache_from_host - updates the title cache for all data sources
that match a given host
@arg $host_id - (int) the ID of the host to match */
function update_data_source_title_cache_from_host($host_id, $query_id = 0, $ids = array()) {
if ($query_id > 0 && !cacti_sizeof($ids)) {
$data = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . ' id
FROM data_local
WHERE host_id = ?
AND snmp_query_id = ?',
array($host_id, $query_id));
} elseif ($query_id > 0) {
$data = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . ' id
FROM data_local
WHERE host_id = ?
AND snmp_query_id = ?
AND id IN (?)',
array($host_id, $query_id, implode(',', $ids)));
} else {
$data = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . ' id
FROM data_local
WHERE host_id = ?',
array($host_id));
}
if (cacti_sizeof($data)) {
foreach ($data as $item) {
update_data_source_title_cache($item['id']);
}
}
}
/* update_data_source_title_cache - updates the title cache for a single data source
@arg $local_data_id - (int) the ID of the data source to update the title cache for */
function update_data_source_title_cache($local_data_id) {
$old_title = db_fetch_cell_prepared('SELECT name_cache
FROM data_template_data
WHERE local_data_id = ?',
array($local_data_id));
$data_source = get_data_source_title($local_data_id);
if (strstr($data_source, '|query_') !== false || strstr($data_source, '|host_') !== false) {
if ($old_title == '') {
db_execute_prepared('UPDATE data_template_data
SET name_cache = ?
WHERE local_data_id = ?',
array($data_source, $local_data_id));
api_plugin_hook_function('update_data_source_title_cache', $local_data_id);
}
} else {
db_execute_prepared('UPDATE data_template_data
SET name_cache = ?
WHERE local_data_id = ?',
array($data_source, $local_data_id));
api_plugin_hook_function('update_data_source_title_cache', $local_data_id);
}
}
/* update_graph_title_cache_from_template - updates the title cache for all graphs
that match a given graph template
@arg $graph_template_id - (int) the ID of the graph template to match */
function update_graph_title_cache_from_template($graph_template_id) {
$graphs = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . ' local_graph_id
FROM graph_templates_graph
WHERE graph_template_id = ?
AND local_graph_id > 0',
array($graph_template_id));
if (cacti_sizeof($graphs) > 0) {
foreach ($graphs as $item) {
update_graph_title_cache($item['local_graph_id']);
}
}
}
/* update_graph_title_cache_from_query - updates the title cache for all graphs
that match a given data query/index combination
@arg $snmp_query_id - (int) the ID of the data query to match
@arg $snmp_index - the index within the data query to match */
function update_graph_title_cache_from_query($snmp_query_id, $snmp_index) {
$graphs = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . ' id
FROM graph_local
WHERE snmp_query_id = ?
AND snmp_index = ?',
array($snmp_query_id, $snmp_index));
if (cacti_sizeof($graphs) > 0) {
foreach ($graphs as $item) {
update_graph_title_cache($item['id']);
}
}
}
/* update_graph_title_cache_from_host - updates the title cache for all graphs
that match a given host
@arg $host_id - (int) the ID of the host to match */
function update_graph_title_cache_from_host($host_id, $query_id = 0, $ids = array()) {
if ($query_id > 0 && !cacti_sizeof($ids)) {
$graphs = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . ' id
FROM graph_local
WHERE host_id = ?
AND snmp_query_id = ?',
array($host_id, $query_id));
} elseif ($query_id > 0) {
$graphs = db_fetch_assoc_prepared('SELECT DISTINCT ' . SQL_NO_CACHE . ' gl.id
FROM graph_local AS gl
INNER JOIN graph_templates_item AS gti
ON gti.local_graph_id = gl.id
INNER JOIN data_template_rrd AS dtr
ON gti.task_item_id = dtr.id
WHERE host_id = ?
AND snmp_query_id = ?
AND dtr.local_data_id IN(?)',
array($host_id, $query_id, implode(',', $ids)));
} else {
$graphs = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . ' id
FROM graph_local
WHERE host_id = ?',
array($host_id));
}
if (cacti_sizeof($graphs)) {
foreach ($graphs as $item) {
update_graph_title_cache($item['id']);
}
}
}
/* update_graph_title_cache - updates the title cache for a single graph
@arg $local_graph_id - (int) the ID of the graph to update the title cache for */
function update_graph_title_cache($local_graph_id) {
$old_title = db_fetch_cell_prepared('SELECT title_cache
FROM graph_templates_graph
WHERE local_graph_id = ?',
array($local_graph_id));
$graph_title = get_graph_title($local_graph_id);
if (strstr($graph_title, '|query_') !== false || strstr($graph_title, '|host_') !== false) {
if ($old_title == '') {
db_execute_prepared('UPDATE graph_templates_graph
SET title_cache = ?
WHERE local_graph_id = ?',
array($graph_title, $local_graph_id));
}
} else {
db_execute_prepared('UPDATE graph_templates_graph
SET title_cache = ?
WHERE local_graph_id = ?',
array($graph_title, $local_graph_id));
}
}
/* null_out_substitutions - takes a string and cleans out any host variables that do not have values
@arg $string - the string to clean out unsubstituted variables for
@returns - the cleaned up string */
function null_out_substitutions($string) {
if ($string != '') {
return preg_replace("/\|host_" . VALID_HOST_FIELDS . "\|( - )?/i", '', $string);
} else {
return $string;
}
}
/* expand_title - takes a string and substitutes all data query variables contained in it or cleans
them out if no data query is in use
@arg $host_id - (int) the host ID to match
@arg $snmp_query_id - (int) the data query ID to match
@arg $snmp_index - the data query index to match
@arg $title - the original string that contains the data query variables
@returns - the original string with all of the variable substitutions made */
function expand_title($host_id, $snmp_query_id, $snmp_index, $title) {
if ((strstr($title, '|')) && (!empty($host_id))) {
if (($snmp_query_id != '0') && ($snmp_index != '')) {
$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'));
} else {
$title = null_out_substitutions(substitute_host_data($title, '|', '|', $host_id));
}
} else {
$title = null_out_substitutions($title);
}
$data = array(
'host_id' => $host_id,
'snmp_query_id' => $snmp_query_id,
'snmp_index' => $snmp_index,
'title' => $title
);
$data = api_plugin_hook_function('expand_title', $data);
if (isset($data['title'])) {
$title = $data['title'];
}
return $title;
}
/* substitute_script_query_path - takes a string and substitutes all path variables contained in it
@arg $path - the string to make path variable substitutions on
@returns - the original string with all of the variable substitutions made */
function substitute_script_query_path($path) {
global $config;
$path = clean_up_path(str_replace('|path_cacti|', $config['base_path'], $path));
$path = clean_up_path(str_replace('|path_php_binary|', read_config_option('path_php_binary'), $path));
return $path;
}
/* substitute_host_data - takes a string and substitutes all host variables contained in it
@arg $string - the string to make host variable substitutions on
@arg $l_escape_string - the character used to escape each variable on the left side
@arg $r_escape_string - the character used to escape each variable on the right side
@arg $host_id - (int) the host ID to match
@returns - the original string with all of the variable substitutions made */
function substitute_host_data($string, $l_escape_string, $r_escape_string, $host_id) {
if (!empty($host_id)) {
$host = db_fetch_row_prepared('SELECT ' . SQL_NO_CACHE . ' h.*, s.name AS site_name
FROM host AS h
LEFT JOIN sites AS s
ON h.site_id = s.id
WHERE h.id = ?', array($host_id));
if (!cacti_sizeof($host)) {
return $string;
}
$search = array();
$replace = array();
$search[] = $l_escape_string . 'host_management_ip' . $r_escape_string; /* for compatibility */
$replace[] = $host['hostname']; /* for compatibility */
/* common host columns */
$search[] = $l_escape_string . 'host_id' . $r_escape_string;
$replace[] = $host['id'];
$search[] = $l_escape_string . 'host_hostname' . $r_escape_string;
$replace[] = $host['hostname'];
$search[] = $l_escape_string . 'host_description' . $r_escape_string;
$replace[] = $host['description'];
$search[] = $l_escape_string . 'host_site' . $r_escape_string;
$replace[] = $host['site_name'];
$search[] = $l_escape_string . 'host_notes' . $r_escape_string;
$replace[] = $host['notes'];
$search[] = $l_escape_string . 'host_location' . $r_escape_string;
$replace[] = $host['location'];
$search[] = $l_escape_string . 'host_polling_time' . $r_escape_string;
$replace[] = $host['polling_time'];
$search[] = $l_escape_string . 'host_avg_time' . $r_escape_string;
$replace[] = $host['avg_time'];
$search[] = $l_escape_string . 'host_cur_time' . $r_escape_string;
$replace[] = $host['cur_time'];
$search[] = $l_escape_string . 'host_availability' . $r_escape_string;
$replace[] = $host['availability'];
$search[] = $l_escape_string . 'host_uptime' . $r_escape_string;
$replace[] = get_uptime($host);
/* snmp connectivity information */
$search[] = $l_escape_string . 'host_snmp_community' . $r_escape_string;
$replace[] = $host['snmp_community'];
$search[] = $l_escape_string . 'host_snmp_version' . $r_escape_string;
$replace[] = $host['snmp_version'];
$search[] = $l_escape_string . 'host_snmp_username' . $r_escape_string;
$replace[] = $host['snmp_username'];
$search[] = $l_escape_string . 'host_snmp_password' . $r_escape_string;
$replace[] = $host['snmp_password'];
$search[] = $l_escape_string . 'host_snmp_auth_protocol' . $r_escape_string;
$replace[] = $host['snmp_auth_protocol'];
$search[] = $l_escape_string . 'host_snmp_priv_passphrase' . $r_escape_string;
$replace[] = $host['snmp_priv_passphrase'];
$search[] = $l_escape_string . 'host_snmp_priv_protocol' . $r_escape_string;
$replace[] = $host['snmp_priv_protocol'];
$search[] = $l_escape_string . 'host_snmp_context' . $r_escape_string;
$replace[] = $host['snmp_context'];
$search[] = $l_escape_string . 'host_snmp_engine_id' . $r_escape_string;
$replace[] = $host['snmp_engine_id'];
$search[] = $l_escape_string . 'host_snmp_port' . $r_escape_string;
$replace[] = $host['snmp_port'];
$search[] = $l_escape_string . 'host_snmp_timeout' . $r_escape_string;
$replace[] = $host['snmp_timeout'];
/* snmp system information */
$search[] = $l_escape_string . 'host_snmp_sysDescr' . $r_escape_string;
$replace[] = $host['snmp_sysDescr'];
$search[] = $l_escape_string . 'host_snmp_sysObjectID' . $r_escape_string;
$replace[] = $host['snmp_sysObjectID'];
$search[] = $l_escape_string . 'host_snmp_sysContact' . $r_escape_string;
$replace[] = $host['snmp_sysContact'];
$search[] = $l_escape_string . 'host_snmp_sysLocation' . $r_escape_string;
$replace[] = $host['snmp_sysLocation'];
$search[] = $l_escape_string . 'host_snmp_sysName' . $r_escape_string;
$replace[] = $host['snmp_sysName'];
$search[] = $l_escape_string . 'host_snmp_sysUpTimeInstance' . $r_escape_string;
$replace[] = $host['snmp_sysUpTimeInstance'];
$search[] = $l_escape_string . 'host_ping_retries' . $r_escape_string;
$replace[] = $host['ping_retries'];
$search[] = $l_escape_string . 'host_max_oids' . $r_escape_string;
$replace[] = $host['max_oids'];
/* handle the external id */
$search[] = $l_escape_string . 'host_external_id' . $r_escape_string;
$replace[] = $host['external_id'];
$string = str_replace($search, $replace, $string);
$temp = api_plugin_hook_function(
'substitute_host_data',
array('string' => $string, 'l_escape_string' => $l_escape_string, 'r_escape_string' => $r_escape_string, 'host_id' => $host_id)
);
$string = $temp['string'];
}
return $string;
}
/* substitute_snmp_query_data - takes a string and substitutes all data query variables contained in it
@arg $string - the original string that contains the data query variables
@arg $host_id - (int) the host ID to match
@arg $snmp_query_id - (int) the data query ID to match
@arg $snmp_index - the data query index to match
@arg $max_chars - the maximum number of characters to substitute
@returns - the original string with all of the variable substitutions made */
function substitute_snmp_query_data($string, $host_id, $snmp_query_id, $snmp_index, $max_chars = 0) {
if ($host_id > 0) {
$snmp_cache_data = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . ' field_name, field_value
FROM host_snmp_cache
WHERE host_id = ?
AND snmp_query_id = ?
AND snmp_index = ?',
array($host_id, $snmp_query_id, $snmp_index));
} else {
$snmp_cache_data = db_fetch_assoc_prepared('SELECT DISTINCT ' . SQL_NO_CACHE . ' field_name, field_value
FROM host_snmp_cache
WHERE snmp_query_id = ?
AND snmp_index = ?
AND host_id = 0',
array($snmp_query_id, $snmp_index));
}
if (cacti_sizeof($snmp_cache_data)) {
foreach ($snmp_cache_data as $data) {
if (trim($data['field_value']) != '') {
if ($max_chars > 0) {
$data['field_value'] = substr($data['field_value'], 0, $max_chars);
}
$string = stri_replace('|query_' . $data['field_name'] . '|', $data['field_value'], $string);
if (strpos($string, 'query_') === false) {
break;
}
}
}
}
return $string;
}
/* substitute_data_input_data - takes a string and substitutes all data input variables contained in it
@arg $string - the original string that contains the data input variables
@arg $local_data_id - (int) the local data id to match
@arg $max_chars - the maximum number of characters to substitute
@returns - the original string with all of the variable substitutions made */
function substitute_data_input_data($string, $graph, $local_data_id, $max_chars = 0) {
if (empty($local_data_id)) {
if (isset($graph['local_graph_id'])) {
$local_data_ids = array_rekey(db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . ' DISTINCT local_data_id
FROM data_template_rrd
INNER JOIN graph_templates_item
ON data_template_rrd.id = graph_templates_item.task_item_id
WHERE local_graph_id = ?', array($graph['local_graph_id'])), 'local_data_id', 'local_data_id');
if (cacti_sizeof($local_data_ids)) {
$data_template_data_id = db_fetch_cell('SELECT ' . SQL_NO_CACHE . ' id
FROM data_template_data
WHERE local_data_id IN (' . implode(',', $local_data_ids) . ')');
} else {
$data_template_data_id = 0;
}
} else {
$data_template_data_id = 0;
}
} else {
$data_template_data_id = db_fetch_cell_prepared('SELECT ' . SQL_NO_CACHE . ' id
FROM data_template_data
WHERE local_data_id = ?',
array($local_data_id));
}
if (!empty($data_template_data_id)) {
$data = db_fetch_assoc_prepared("SELECT " . SQL_NO_CACHE . "
dif.data_name, did.value
FROM data_input_fields AS dif
INNER JOIN data_input_data AS did
ON dif.id = did.data_input_field_id
WHERE data_template_data_id = ?
AND input_output = 'in'",
array($data_template_data_id));
if (cacti_sizeof($data)) {
foreach ($data as $item) {
if ($item['value'] != '') {
if ($max_chars > 0) {
$item['value'] = substr($item['field_value'], 0, $max_chars);
}
$string = stri_replace('|input_' . $item['data_name'] . '|', $item['value'], $string);
}
}
}
}
return $string;
}
|