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 25function getHostTemplates() { 26 $tmpArray = db_fetch_assoc('SELECT id, name FROM host_template ORDER BY id'); 27 28 $host_templates[0] = 'None'; 29 30 if ($tmpArray !== false && cacti_sizeof($tmpArray)) { 31 foreach ($tmpArray as $template) { 32 $host_templates[$template['id']] = $template['name']; 33 } 34 } 35 36 return $host_templates; 37} 38 39function getHostsByDescription($hostTemplateIds = false) { 40 $hosts = array(); 41 42 if ($hostTemplateIds !== false) { 43 if (!is_array($hostTemplateIds)) { 44 $hostTemplateIds = array($hostTemplateIds); 45 } 46 } 47 48 if ($hostTemplateIds !== false && cacti_sizeof($hostTemplateIds)) { 49 foreach($hostTemplateIds as $id) { 50 if (!is_numeric($id)) { 51 return false; 52 } 53 } 54 55 $sql_where = 'WHERE ht.id IN (' . implode(',', $hostTemplateIds) . ')'; 56 } else { 57 $sql_where = ''; 58 } 59 60 $tmpArray = db_fetch_assoc("SELECT h.id, h.description 61 FROM host AS h 62 INNER JOIN host_template AS ht 63 ON h.host_template_id = ht.id 64 $sql_where 65 ORDER BY h.description"); 66 67 if ($tmpArray !== false && cacti_sizeof($tmpArray)) { 68 foreach ($tmpArray as $tmp) { 69 $hosts[$tmp['description']] = $tmp['id']; 70 } 71 } 72 73 return $hosts; 74} 75 76function getSites() { 77 $sites = array(); 78 $tmpArray = db_fetch_assoc('SELECT * FROM sites ORDER BY id'); 79 80 if ($tmpArray !== false && cacti_sizeof($tmpArray)) { 81 foreach ($tmpArray as $site) { 82 $sites[$site['id']] = $site; 83 } 84 } 85 86 return $sites; 87} 88 89function getHosts($hostTemplateIds = false) { 90 $hosts = array(); 91 92 if ($hostTemplateIds !== false) { 93 if (!is_array($hostTemplateIds)) { 94 $hostTemplateIds = array($hostTemplateIds); 95 } 96 } 97 98 if ($hostTemplateIds !== false && cacti_sizeof($hostTemplateIds)) { 99 foreach($hostTemplateIds as $id) { 100 if (!is_numeric($id)) { 101 return false; 102 } 103 } 104 105 $sql_where = 'WHERE ht.id IN (' . implode(',', $hostTemplateIds) . ')'; 106 } else { 107 $sql_where = ''; 108 } 109 110 $tmpArray = db_fetch_assoc("SELECT h.id, h.hostname, h.description, h.host_template_id 111 FROM host AS h 112 LEFT JOIN host_template AS ht 113 ON h.host_template_id = ht.id 114 $sql_where 115 ORDER BY h.id"); 116 117 if ($tmpArray !== false && cacti_sizeof($tmpArray)) { 118 foreach ($tmpArray as $host) { 119 $hosts[$host['id']] = $host; 120 } 121 } 122 123 return $hosts; 124} 125 126function getInputFields($templateId) { 127 $fields = array(); 128 129 $tmpArray = db_fetch_assoc_prepared("SELECT DISTINCT dif.data_name AS `name`, dif.name AS `description`, 130 did.value AS `default`, dtd.data_template_id, dif.id AS `data_input_field_id` 131 FROM data_input_fields AS dif 132 INNER JOIN ( 133 SELECT data_input_field_id, data_template_data_id, value 134 FROM data_input_data 135 WHERE t_value = 'on' 136 ) AS did 137 ON did.data_input_field_id = dif.id 138 INNER JOIN ( 139 SELECT id, data_input_id, data_template_id 140 FROM data_template_data 141 WHERE local_data_id = 0 142 ) AS dtd 143 ON did.data_template_data_id = dtd.id 144 AND dtd.data_input_id = dif.data_input_id 145 INNER JOIN ( 146 SELECT data_template_id, id 147 FROM data_template_rrd 148 WHERE local_data_id = 0 AND hash != '' 149 ) AS dtr 150 ON dtr.data_template_id = dtd.data_template_id 151 INNER JOIN graph_templates_item AS gti 152 ON dtr.id = gti.task_item_id 153 INNER JOIN graph_templates AS gt 154 ON gt.id = gti.graph_template_id 155 WHERE gt.id = ? 156 AND dif.input_output IN ('in', 'inout')", 157 array($templateId)); 158 159 if ($tmpArray !== false && cacti_sizeof($tmpArray)) { 160 foreach ($tmpArray as $row) { 161 $fields[$row['data_template_id'] . ':' . $row['name']] = $row; 162 } 163 } 164 165 return $fields; 166} 167 168function getAddresses() { 169 $addresses = array(); 170 $tmpArray = db_fetch_assoc('SELECT id, hostname FROM host ORDER BY hostname'); 171 172 if ($tmpArray !== false && cacti_sizeof($tmpArray)) { 173 foreach ($tmpArray as $tmp) { 174 $addresses[$tmp['hostname']] = $tmp['id']; 175 } 176 } 177 178 return $addresses; 179} 180 181function getSNMPFields($hostId, $snmp_query_id = '') { 182 $fieldNames = array(); 183 184 if ($snmp_query_id != '') { 185 $sql_where = " AND snmp_query_id=$snmp_query_id"; 186 } else { 187 $sql_where = ''; 188 } 189 190 $tmpArray = db_fetch_assoc('SELECT DISTINCT field_name 191 FROM host_snmp_cache 192 WHERE host_id = ' . $hostId . " 193 $sql_where 194 ORDER BY field_name"); 195 196 if ($tmpArray !== false && cacti_sizeof($tmpArray)) { 197 foreach ($tmpArray as $f) { 198 $fieldNames[$f['field_name']] = 1; 199 } 200 } 201 202 return $fieldNames; 203} 204 205function getSNMPValues($hostId, $field, $snmp_query_id = '') { 206 $values = array(); 207 208 if ($snmp_query_id != '') { 209 $sql_where = " AND snmp_query_id=$snmp_query_id"; 210 } else { 211 $sql_where = ''; 212 } 213 214 $tmpArray = db_fetch_assoc('SELECT field_value 215 FROM host_snmp_cache 216 WHERE host_id=' . $hostId . " 217 AND field_name='" . $field . "' 218 $sql_where 219 ORDER BY field_value"); 220 221 if ($tmpArray !== false && cacti_sizeof($tmpArray)) { 222 foreach ($tmpArray as $v) { 223 $values[$v['field_value']] = 1; 224 } 225 } 226 227 return $values; 228} 229 230function getSNMPQueries() { 231 $queries = array(); 232 $tmpArray = db_fetch_assoc('SELECT id, name FROM snmp_query ORDER by id'); 233 234 if ($tmpArray !== false && cacti_sizeof($tmpArray)) { 235 foreach ($tmpArray as $q) { 236 $queries[$q['id']] = $q['name']; 237 } 238 } 239 240 return $queries; 241} 242 243function getSNMPQueryTypes($snmpQueryId) { 244 $types = array(); 245 246 $tmpArray = db_fetch_assoc_prepared('SELECT id, name 247 FROM snmp_query_graph 248 WHERE snmp_query_id = ? 249 ORDER BY id', 250 array($snmpQueryId)); 251 252 if ($tmpArray !== false && cacti_sizeof($tmpArray)) { 253 foreach ($tmpArray as $type) { 254 $types[$type['id']] = $type['name']; 255 } 256 } 257 258 return $types; 259} 260 261function getGraphTemplates() { 262 $graph_templates = array(); 263 264 $tmpArray = db_fetch_assoc('SELECT id, name FROM graph_templates ORDER BY id'); 265 266 if ($tmpArray !== false && cacti_sizeof($tmpArray)) { 267 foreach ($tmpArray as $t) { 268 $graph_templates[$t['id']] = $t['name']; 269 } 270 } 271 272 return $graph_templates; 273} 274 275function getGraphTemplatesByHostTemplate($host_template_ids = false) { 276 $graph_templates = array(); 277 278 if ($host_template_ids !== false) { 279 if (!is_array($host_template_ids)) { 280 $host_template_ids = array($host_template_ids); 281 } 282 } 283 284 if ($host_template_ids !== false && cacti_sizeof($host_template_ids)) { 285 foreach($host_template_ids as $id) { 286 if (!is_numeric($id)) { 287 return false; 288 } 289 } 290 291 $sql_where = 'WHERE htg.host_template_id IN (' . implode(',', $host_template_ids) . ')'; 292 } else { 293 $sql_where = ''; 294 } 295 296 $tmpArray = db_fetch_assoc_prepared("SELECT htg.graph_template_id AS id, gt.name AS name 297 FROM host_template_graph AS htg 298 LEFT JOIN graph_templates AS gt 299 ON htg.graph_template_id = gt.id 300 $sql_where 301 ORDER by gt.name ASC"); 302 303 if (cacti_sizeof($tmpArray)) { 304 foreach ($tmpArray as $t) { 305 $graph_templates[$t['id']] = $t['name']; 306 } 307 } 308 309 return $graph_templates; 310} 311 312function displayQueryTypes($types, $quietMode = false) { 313 if (!$quietMode) { 314 print 'Known SNMP Query Types: (id, name)' . PHP_EOL; 315 } 316 317 if ($types !== false && cacti_sizeof($types)) { 318 foreach ($types as $id => $name) { 319 print $id . "\t" . $name . PHP_EOL; 320 } 321 } 322 323 if (!$quietMode) { 324 print PHP_EOL; 325 } 326} 327 328function displayHostTemplates($host_templates, $quietMode = false) { 329 if (!$quietMode) { 330 print 'Valid Device Templates: (id, name)' . PHP_EOL; 331 } 332 333 if ($host_templates !== false && cacti_sizeof($host_templates)) { 334 foreach ($host_templates as $id => $name) { 335 print "$id\t$name" . PHP_EOL; 336 } 337 } 338 339 if (!$quietMode) { 340 print PHP_EOL; 341 } 342} 343 344function displayCommunities($quietMode = false) { 345 if (!$quietMode) { 346 print 'Known SNMP Communities: (community)' . PHP_EOL; 347 } 348 349 $communities = db_fetch_assoc('SELECT DISTINCT snmp_community 350 FROM host 351 ORDER BY snmp_community'); 352 353 if ($communities !== false &&cacti_sizeof($communities)) { 354 foreach ($communities as $community) { 355 print $community['snmp_community'] . PHP_EOL; 356 } 357 } 358 359 if (!$quietMode) { 360 print PHP_EOL; 361 } 362} 363 364function displaySNMPFields($fields, $hostId, $quietMode = false) { 365 if (!$quietMode) { 366 print 'Known SNMP Fields for host-id ' . $hostId . ': (name)' . PHP_EOL; 367 } 368 369 if ($fields !== false && cacti_sizeof($fields)) { 370 foreach ($fields as $field => $values) { 371 print $field . PHP_EOL; 372 } 373 } 374 375 if (!$quietMode) { 376 print PHP_EOL; 377 } 378} 379 380function displaySNMPValues($values, $hostId, $field, $quietMode = false) { 381 if (!$quietMode) { 382 print 'Known SNMP Values for Field ' . $field . ' and host-id ' . $hostId . ': (name)' . PHP_EOL; 383 } 384 385 if ($values !== false && cacti_sizeof($values)) { 386 foreach ($values as $value => $foo) { 387 print $value . PHP_EOL; 388 } 389 } 390 391 if (!$quietMode) { 392 print PHP_EOL; 393 } 394} 395 396function displaySNMPQueries($queries, $quietMode = false) { 397 if (!$quietMode) { 398 print 'Known SNMP Queries: (id, name)' . PHP_EOL; 399 } 400 401 if ($queries !== false &&cacti_sizeof($queries)) { 402 foreach ($queries as $id => $name) { 403 print $id . "\t" . $name . PHP_EOL; 404 } 405 } 406 407 if (!$quietMode) { 408 print PHP_EOL; 409 } 410} 411 412function displayInputFields($input_fields, $quietMode = false) { 413 if (!$quietMode) { 414 print 'Known Input Fields: (name, default, description)' . PHP_EOL; 415 } 416 417 if ($input_fields !== false &&cacti_sizeof($input_fields)) { 418 foreach ($input_fields as $row) { 419 print $row['data_template_id'] . ':' . $row['name'] . "\t" . $row['default'] . "\t" . $row['description'] . PHP_EOL; 420 } 421 } 422 423 if (!$quietMode) { 424 print PHP_EOL; 425 } 426} 427 428function displayGraphTemplates($templates, $quietMode = false) { 429 if (!$quietMode) { 430 print 'Known Graph Templates: (id, name)' . PHP_EOL; 431 } 432 433 if ($templates !== false && cacti_sizeof($templates)) { 434 foreach ($templates as $id => $name) { 435 print $id . "\t" . $name . PHP_EOL; 436 } 437 } 438 439 if (!$quietMode) { 440 print PHP_EOL; 441 } 442} 443 444function displayHosts($hosts, $quietMode = false) { 445 if (!$quietMode) { 446 print 'Known Devices: (id, hostname, template, description)' . PHP_EOL; 447 } 448 449 if ($hosts !== false && cacti_sizeof($hosts)) { 450 foreach($hosts as $host) { 451 print $host['id'] . "\t" . $host['hostname'] . "\t" . $host['host_template_id'] . "\t" . $host['description'] . PHP_EOL; 452 } 453 } 454 455 if (!$quietMode) { 456 print PHP_EOL; 457 } 458} 459 460function displaySites($sites, $quietMode = false) { 461 if (!$quietMode) { 462 print 'Known Sites: (id, name)' . PHP_EOL; 463 } 464 465 if ($sites !== false && cacti_sizeof($sites)) { 466 foreach($sites as $site) { 467 print $site['id'] . "\t" . $site['name'] . PHP_EOL; 468 } 469 } 470 471 if (!$quietMode) { 472 print PHP_EOL; 473 } 474} 475 476function displayTrees($quietMode = false) { 477 global $tree_sort_types; 478 479 if (!$quietMode) { 480 print 'Known Trees: (id, sort method, name)' . PHP_EOL; 481 } 482 483 $trees = db_fetch_assoc('SELECT id, sort_type, name 484 FROM graph_tree 485 ORDER BY id'); 486 487 if (cacti_sizeof($trees)) { 488 foreach ($trees as $tree) { 489 print $tree['id'] . "\t"; 490 print $tree_sort_types[$tree['sort_type']] . "\t"; 491 print $tree['name'] . PHP_EOL; 492 } 493 } 494 495 if (!$quietMode) { 496 print PHP_EOL; 497 } 498} 499 500function displayTreeNodes($tree_id, $nodeType = '', $parentNode = 0, $quietMode = false) { 501 global $tree_sort_types, $tree_item_types, $host_group_types; 502 503 if ($parentNode == 0) { 504 if (!$quietMode) { 505 print 'Known Tree Nodes: (type, id, parentid, title, attribs)' . PHP_EOL; 506 } 507 } 508 509 $parentID = 0; 510 511 $nodes = db_fetch_assoc_prepared('SELECT id, local_graph_id, title, 512 host_id, host_grouping_type, sort_children_type 513 FROM graph_tree_items 514 WHERE graph_tree_id = ? 515 AND parent = ? 516 ORDER BY position', array($tree_id, $parentNode)); 517 518 if (cacti_sizeof($nodes)) { 519 foreach ($nodes as $node) { 520 /* taken from tree.php, funtion item_edit() */ 521 $current_type = TREE_ITEM_TYPE_HEADER; 522 if ($node['local_graph_id'] > 0) { $current_type = TREE_ITEM_TYPE_GRAPH; } 523 if ($node['host_id'] > 0) { $current_type = TREE_ITEM_TYPE_HOST; } 524 525 switch ($current_type) { 526 case TREE_ITEM_TYPE_HEADER: 527 if ($nodeType == '' || $nodeType == 'header') { 528 print $tree_item_types[$current_type]."\t"; 529 print $node['id']."\t"; 530 if ($parentNode == 0) { 531 print "N/A\t"; 532 } else { 533 print $parentNode . "\t"; 534 } 535 536 print $node['title'] . "\t"; 537 print $tree_sort_types[$node['sort_children_type']] . "\t"; 538 print PHP_EOL; 539 } 540 541 displayTreeNodes($tree_id, $nodeType, $node['id'], $quietMode); 542 543 break; 544 case TREE_ITEM_TYPE_GRAPH: 545 if ($nodeType == '' || $nodeType == 'graph') { 546 print $tree_item_types[$current_type] . "\t"; 547 print $node['id'] . "\t"; 548 if ($parentNode == 0) { 549 print "N/A\t"; 550 } else { 551 print $parentNode . "\t"; 552 } 553 554 /* fetch the title for that graph */ 555 $graph_title = db_fetch_cell_prepared('SELECT gtg.title_cache AS name 556 FROM graph_templates_graph AS gtg 557 WHERE gtg.local_graph_id = ?', array($node['local_graph_id'])); 558 559 print $graph_title . "\t"; 560 print PHP_EOL; 561 } 562 563 break; 564 case TREE_ITEM_TYPE_HOST: 565 if ($nodeType == '' || $nodeType == 'host') { 566 print $tree_item_types[$current_type] . "\t"; 567 print $node['id'] . "\t"; 568 if ($parentNode == 0) { 569 print "N/A\t"; 570 } else { 571 print $parentNode . "\t"; 572 } 573 574 $name = db_fetch_cell_prepared('SELECT hostname FROM host WHERE id = ?', array($node['host_id'])); 575 576 print $name . "\t"; 577 print $host_group_types[$node['host_grouping_type']] . "\t"; 578 print PHP_EOL; 579 } 580 break; 581 } 582 } 583 } 584 585 if ($parentNode == 0) { 586 if (!$quietMode) { 587 print PHP_EOL; 588 } 589 } 590} 591 592function displayRRAs($quietMode = false) { 593 if (!$quietMode) { 594 print 'Known RRAs: (id, steps, rows, name)' . PHP_EOL; 595 } 596 597 $rras = db_fetch_assoc('SELECT id, name, steps, `rows` FROM data_source_profiles_rra ORDER BY id'); 598 599 if (cacti_sizeof($rras)) { 600 foreach ($rras as $rra) { 601 print $rra['id'] . "\t"; 602 print $rra['steps'] . "\t"; 603 print $rra['rows'] . "\t"; 604 print $rra['name'] . PHP_EOL; 605 } 606 } 607 608 if (!$quietMode) { 609 print PHP_EOL; 610 } 611} 612 613function displayHostGraphs($host_id, $quietMode = false) { 614 if (!$quietMode) { 615 print 'Known Device Graphs: (id, name, template)' . PHP_EOL; 616 } 617 618 $graphs = db_fetch_assoc_prepared('SELECT 619 graph_templates_graph.local_graph_id AS id, 620 graph_templates_graph.title_cache AS name, 621 graph_templates.name AS template_name 622 FROM (graph_local, graph_templates_graph) 623 LEFT JOIN graph_templates ON (graph_local.graph_template_id = graph_templates.id) 624 WHERE graph_local.id = graph_templates_graph.local_graph_id 625 AND graph_local.host_id = ? 626 ORDER BY graph_templates_graph.local_graph_id', 627 array($host_id)); 628 629 if (cacti_sizeof($graphs)) { 630 foreach ($graphs as $graph) { 631 print $graph['id'] . "\t"; 632 print $graph['name'] . "\t"; 633 print $graph['template_name'] . "\t"; 634 print PHP_EOL; 635 } 636 } 637 638 if (!$quietMode) { 639 print PHP_EOL; 640 } 641} 642 643function displayUsers($quietMode = false) { 644 if (!$quietMode) { 645 print 'Known Users: (id, username, full_name)'. PHP_EOL; 646 } 647 648 $groups = db_fetch_assoc('SELECT id, username, full_name 649 FROM user_auth 650 ORDER BY id'); 651 652 if (cacti_sizeof($groups)) { 653 foreach ($groups as $group) { 654 print $group['id'] ."\t"; 655 print $group['username'] ."\t"; 656 print $group['full_name'] . PHP_EOL; 657 } 658 } 659 660 if (!$quietMode) { 661 print PHP_EOL; 662 } 663} 664 665