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 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
|
<?php
/*****************************************************************************
*
* GlobalBackendmerlinmy.php - backend class for handling object and state
* information stored in the Merlin database.
*
* Copyright (c) 2004-2011 NagVis Project (Contact: info@nagvis.org)
*
* License:
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*****************************************************************************/
/**
* @author Roman Kyrylych <rkyrylych@op5.com>
*/
class GlobalBackendmerlinmy implements GlobalBackendInterface {
private $CORE;
private $CONN;
private $backendId;
private $dbName;
private $dbUser;
private $dbPass;
private $dbHost;
private $hostCache;
private $serviceCache;
private $hostAckCache;
// Define the backend local configuration options
private static $validConfig = Array(
'dbhost' => Array('must' => 1,
'editable' => 1,
'default' => 'localhost',
'match' => MATCH_STRING_NO_SPACE),
'dbport' => Array('must' => 0,
'editable' => 1,
'default' => '3306',
'match' => MATCH_INTEGER),
'dbname' => Array('must' => 1,
'editable' => 1,
'default' => 'merlin',
'match' => MATCH_STRING_NO_SPACE),
'dbuser' => Array('must' => 1,
'editable' => 1,
'default' => 'merlin',
'match' => MATCH_STRING_NO_SPACE),
'dbpass' => Array('must' => 0,
'editable' => 1,
'default' => 'merlin',
'match' => MATCH_STRING_EMPTY),
'maxtimewithoutupdate' => Array('must' => 0,
'editable' => 1,
'default' => '180',
'match' => MATCH_INTEGER));
/**
* Constructor
* Reads needed configuration parameters, connects to the Database
* and checks that Nagios is running
*
* @param config $MAINCFG
* @param String $backendId
* @author Lars Michelsen <lars@vertical-visions.de>
* @author Roman Kyrylych <rkyrylych@op5.com>
*/
public function __construct($CORE, $backendId) {
$this->CORE = $CORE;
$this->backendId = $backendId;
$this->hostCache = Array();
$this->serviceCache = Array();
$this->hostAckCache = Array();
$this->dbName = cfg('backend_'.$backendId, 'dbname');
$this->dbUser = cfg('backend_'.$backendId, 'dbuser');
$this->dbPass = cfg('backend_'.$backendId, 'dbpass');
$this->dbHost = cfg('backend_'.$backendId, 'dbhost');
$this->dbPort = cfg('backend_'.$backendId, 'dbport');
$this->checkMysqlSupport();
$this->connectDB();
$this->checkTablesExists();
}
/**
* PRIVATE Method checkTablesExists
*
* Checks if the needed tables are in the DB
*
* @return Boolean
* @author Lars Michelsen <lars@vertical-visions.de>
* @author Roman Kyrylych <rkyrylych@op5.com>
*/
private function checkTablesExists() {
if(mysql_num_rows($this->mysqlQuery("SHOW TABLES LIKE 'program_status'")) == 0) {
throw new BackendConnectionProblem(l('noTablesExists', Array('BACKENDID' => $this->backendId, 'PREFIX' => '')));
}
}
/**
* PRIVATE Method connectDB
*
* Connects to DB
*
* @return Boolean
* @author Lars Michelsen <lars@vertical-visions.de>
*/
private function connectDB() {
// don't want to see mysql errors from connecting - only want our error messages
$oldLevel = error_reporting(0);
$this->CONN = mysql_connect($this->dbHost.':'.$this->dbPort, $this->dbUser, $this->dbPass);
if(!$this->CONN){
throw BackendConnectionProblem(l('errorConnectingMySQL', Array('BACKENDID' => $this->backendId,'MYSQLERR' => mysql_error())));
}
$returnCode = mysql_select_db($this->dbName, $this->CONN);
// set the old level of reporting back
error_reporting($oldLevel);
if(!$returnCode){
throw BackendConnectionProblem(l('errorSelectingDb', Array('BACKENDID' => $this->backendId, 'MYSQLERR' => mysql_error($this->CONN))));
}
}
/**
* PRIVATE Method checkMysqlSupport
*
* Checks if MySQL is supported in this PHP version
*
* @return Boolean
* @author Lars Michelsen <lars@vertical-visions.de>
*/
private function checkMysqlSupport() {
// Check availability of PHP MySQL
if (!extension_loaded('mysql')) {
throw BackendConnectionProblem(l('mysqlNotSupported', Array('BACKENDID' => $this->backendId)));
}
}
/**
* PRIVATE Method mysqlQuery
*
* @param String MySQL Query
* @return Handle Query Handle
* @author Lars Michelsen <lars@vertical-visions.de>
*/
private function mysqlQuery($query) {
$QUERYHANDLE = mysql_query($query, $this->CONN) or die(mysql_error());
return $QUERYHANDLE;
}
/**
* PUBLIC Method getValidConfig
*
* Returns the valid config for this backend
*
* @return Array
* @author Lars Michelsen <lars@vertical-visions.de>
*/
public static function getValidConfig() {
return self::$validConfig;
}
/**
* PUBLIC Method getObjects
*
* Return the objects configured at Nagios which are matching the given pattern.
* This is needed for WUI, e.g. to populate drop down lists.
*
* @param string $type, string $name1Pattern, string $name2Pattern
* @return array $ret
* @author Roman Kyrylych <rkyrylych@op5.com>
*/
public function getObjects($type,$name1Pattern='',$name2Pattern='') {
$ret = Array();
$filter = '';
if(in_array($type, array('host', 'hostgroup', 'servicegroup'))) {
if($name1Pattern != '') {
$filter = " WHERE {$type}_name = '$name1Pattern'";
}
$QUERYHANDLE = $this->mysqlQuery("SELECT {$type}_name AS name1, alias AS name2 FROM $type".$filter." ORDER BY {$type}_name");
} elseif($type == 'service') {
if($name1Pattern != '' && $name2Pattern != '') {
$filter = " WHERE host_name = '$name1Pattern' AND service_description = '$name2Pattern'";
} else if($name1Pattern != '') {
$filter = " WHERE host_name = '$name1Pattern'";
}
$QUERYHANDLE = $this->mysqlQuery('SELECT host_name AS name1, service_description AS name2 FROM service'.$filter.' ORDER BY host_name');
} else {
return Array();
}
while($data = mysql_fetch_array($QUERYHANDLE)) {
$ret[] = Array('name1' => $data['name1'],'name2' => $data['name2']);
}
// Free memory
mysql_free_result($QUERYHANDLE);
return $ret;
}
/**
* PUBLIC Method getObjectsEx
*
* Return all objects configured at Nagios plus some additional information.
* This is needed for gmap, e.g. to populate lists.
*
* @param string $type
* @return array $ret
* @author Roman Kyrylych <rkyrylych@op5.com>
*/
public function getObjectsEx($type) {
$ret = Array();
if($type == 'host') {
$QUERYHANDLE = $this->mysqlQuery('SELECT host_name AS name, address, alias FROM host ORDER BY host_name');
} elseif($type == 'service') {
$QUERYHANDLE = $this->mysqlQuery('SELECT host_name AS host, service_description AS description FROM service ORDER BY host_name');
} elseif(in_array($type, array('hostgroup', 'servicegroup'))) {
$QUERYHANDLE = $this->mysqlQuery("SELECT {$type}_name AS name, alias FROM $type ORDER BY {$type}_name");
} else {
return Array();
}
while($data = mysql_fetch_assoc($QUERYHANDLE)) {
$ret[] = $data;
}
// Free memory
mysql_free_result($QUERYHANDLE);
return $ret;
}
/**
* PRIVATE Method getHostAckByHostname
*
* Returns if a host state has been acknowledged. The method doesn't check
* if the host is in OK/DOWN, it only checks the has_been_acknowledged flag.
*
* @param string $hostName
* @return bool $ack
* @author Lars Michelsen <lars@vertical-visions.de>
* @author Roman Kyrylych <rkyrylych@op5.com>
*/
private function getHostAckByHostname($hostName) {
$return = FALSE;
// Read from cache or fetch from NDO
if(isset($this->hostAckCache[$hostName])) {
$return = $this->hostAckCache[$hostName];
} else {
$QUERYHANDLE = $this->mysqlQuery("SELECT problem_has_been_acknowledged FROM host WHERE host_name = '$hostName' LIMIT 1");
$data = mysql_fetch_array($QUERYHANDLE);
// Free memory
mysql_free_result($QUERYHANDLE);
// It's unnessecary to check if the value is 0, everything not equal to 1 is FALSE
if(isset($data['problem_has_been_acknowledged']) && $data['problem_has_been_acknowledged'] == '1') {
$return = TRUE;
} else {
$return = FALSE;
}
// Save to cache
$this->hostAckCache[$hostName] = $return;
}
return $return;
}
public function getHostState($query, $options, $filters) {
}
/**
* PUBLIC getHostState()
*
* Returns the Nagios state and additional information for the requested host
*
* @param String $hostName
* @param Boolean $onlyHardstates
* @param Array Optional array of filters (Not implemented in this backend)
* @return array $state
* @author Lars Michelsen <lars@vertical-visions.de>
* @author Roman Kyrylych <rkyrylych@op5.com>
*/
public function getHostStateOld($hostName, $onlyHardstates = null, $filter = null) {
if(isset($this->hostCache[$hostName.'-'.$onlyHardstates])) {
return $this->hostCache[$hostName.'-'.$onlyHardstates];
} else {
$arrReturn = Array();
$QUERYHANDLE = $this->mysqlQuery('SELECT host.id AS id, alias, display_name, address, has_been_checked,'
.' last_hard_state, UNIX_TIMESTAMP(last_hard_state_change) AS last_hard_state_change,'
.' UNIX_TIMESTAMP(last_state_change) AS last_state_change, current_state,'
.' output, perf_data, notes, problem_has_been_acknowledged, statusmap_image,'
.' UNIX_TIMESTAMP(last_check) AS last_check, UNIX_TIMESTAMP(next_check) AS next_check,'
.' state_type, current_attempt, max_check_attempts,'
.' UNIX_TIMESTAMP(sdt.start_time) AS downtime_start, UNIX_TIMESTAMP(sdt.end_time) AS downtime_end,'
.' sdt.author_name AS downtime_author, sdt.comment_data AS downtime_data'
.' FROM host LEFT JOIN scheduled_downtime AS sdt ON sdt.host_name = host.host_name'
.' AND NOW() > sdt.start_time AND NOW() < sdt.end_time'
." WHERE host.host_name = '$hostName' LIMIT 1");
if(mysql_num_rows($QUERYHANDLE) == 0) {
$arrReturn['state'] = 'ERROR';
$arrReturn['output'] = l('hostNotFoundInDB', Array('BACKENDID' => $this->backendId, 'HOST' => $hostName));
} else {
$data = mysql_fetch_array($QUERYHANDLE);
// Free memory
mysql_free_result($QUERYHANDLE);
$arrReturn['alias'] = $data['alias'];
$arrReturn['display_name'] = $data['display_name'];
$arrReturn['address'] = $data['address'];
$arrReturn['statusmap_image'] = $data['statusmap_image'];
$arrReturn['notes'] = $data['notes'];
// Add Additional information to array
$arrReturn['perfdata'] = $data['perf_data'];
$arrReturn['last_check'] = $data['last_check'];
$arrReturn['next_check'] = $data['next_check'];
$arrReturn['state_type'] = $data['state_type'];
$arrReturn['current_check_attempt'] = $data['current_attempt'];
$arrReturn['max_check_attempts'] = $data['max_check_attempts'];
$arrReturn['last_state_change'] = $data['last_state_change'];
$arrReturn['last_hard_state_change'] = $data['last_hard_state_change'];
// If there is a downtime for this object, save the data
if(isset($data['downtime_start']) && $data['downtime_start'] != '') {
$arrReturn['in_downtime'] = 1;
$arrReturn['downtime_start'] = $data['downtime_start'];
$arrReturn['downtime_end'] = $data['downtime_end'];
$arrReturn['downtime_author'] = $data['downtime_author'];
$arrReturn['downtime_data'] = $data['downtime_data'];
}
/**
* Only recognize hard states. There was a discussion about the implementation
* This is a new handling of only_hard_states. For more details, see:
* http://www.nagios-portal.de/wbb/index.php?page=Thread&threadID=8524
*
* Thanks to Andurin and fredy82
*/
if($onlyHardstates == 1) {
if($data['state_type'] != '0') {
$data['current_state'] = $data['current_state'];
} else {
$data['current_state'] = $data['last_hard_state'];
}
}
if($data['current_state'] == '') {
$arrReturn['state'] = 'UNCHECKED';
$arrReturn['output'] = l('hostIsPending', Array('HOST' => $hostName));
} elseif($data['current_state'] == '0') {
// Host is UP
$arrReturn['state'] = 'UP';
$arrReturn['output'] = $data['output'];
} else {
// Host is DOWN/UNREACHABLE/UNKNOWN
// Store acknowledgement state in array
$arrReturn['problem_has_been_acknowledged'] = $data['problem_has_been_acknowledged'];
// Save to host ack cache
$this->hostAckCache[$hostName] = $data['problem_has_been_acknowledged'];
// Store state and output in array
switch($data['current_state']) {
case '1':
$arrReturn['state'] = 'DOWN';
$arrReturn['output'] = $data['output'];
break;
case '2':
$arrReturn['state'] = 'UNREACHABLE';
$arrReturn['output'] = $data['output'];
break;
case '3':
$arrReturn['state'] = 'UNKNOWN';
$arrReturn['output'] = $data['output'];
break;
default:
$arrReturn['state'] = 'UNKNOWN';
$arrReturn['output'] = 'GlobalBackendndomy::getHostState: Undefined state!';
break;
}
}
}
// Write return array to cache
$this->hostCache[$hostName.'-'.$onlyHardstates] = $arrReturn;
return $arrReturn;
}
}
public function getServiceState($query, $options, $filters) {
}
/**
* PUBLIC getServiceState()
*
* Returns the state and additional information of the requested service
*
* @param String $hostName
* @param String $serviceName
* @param Boolean $onlyHardstates
* @return Array $state
* @author Lars Michelsen <lars@vertical-visions.de>
* @author Roman Kyrylych <rkyrylych@op5.com>
*/
public function getServiceStateOld($hostName, $serviceName = null, $onlyHardstates = null) {
if(isset($this->serviceCache[$hostName.'-'.$serviceName.'-'.$onlyHardstates])) {
return $this->serviceCache[$hostName.'-'.$serviceName.'-'.$onlyHardstates];
} else {
$arrReturn = Array();
$QUERYHANDLE = $this->mysqlQuery('SELECT s.id, s.host_name AS name1, s.service_description AS name2,'
.' s.display_name, s.notes, h.address, s.has_been_checked, s.last_hard_state, s.current_state,'
.' UNIX_TIMESTAMP(s.last_hard_state_change) AS last_hard_state_change, UNIX_TIMESTAMP(s.last_state_change) AS last_state_change,'
.' s.output, s.perf_data, s.problem_has_been_acknowledged,'
.' UNIX_TIMESTAMP(s.last_check) AS last_check, UNIX_TIMESTAMP(s.next_check) AS next_check,'
.' s.state_type, s.current_attempt, s.max_check_attempts,'
.' UNIX_TIMESTAMP(sdt.start_time) AS downtime_start, UNIX_TIMESTAMP(sdt.end_time) AS downtime_end,'
.' sdt.author_name AS downtime_author, sdt.comment_data AS downtime_data'
.' FROM host AS h, service AS s LEFT JOIN scheduled_downtime AS sdt ON sdt.host_name = s.host_name'
.' AND sdt.service_description = s.service_description AND NOW() > sdt.start_time AND NOW() < sdt.end_time'
." WHERE s.host_name = '$hostName' AND h.host_name = s.host_name"
.((isset($serviceName) && $serviceName != '')? " AND s.service_description = '$serviceName' LIMIT 1" : ''));
if(mysql_num_rows($QUERYHANDLE) == 0) {
if(isset($serviceName) && $serviceName != '') {
$arrReturn['state'] = 'ERROR';
$arrReturn['output'] = l('serviceNotFoundInDB', Array('BACKENDID' => $this->backendId, 'SERVICE' => $serviceName, 'HOST' => $hostName));
} else {
// If the method should fetch all services of the host and does not find
// any services for this host, don't return anything => The message
// that the host has no services is added by the frontend
}
} else {
while($data = mysql_fetch_array($QUERYHANDLE)) {
$arrTmpReturn = Array();
$arrTmpReturn['service_description'] = $data['name2'];
$arrTmpReturn['display_name'] = $data['display_name'];
$arrTmpReturn['alias'] = $data['display_name'];
$arrTmpReturn['address'] = $data['address'];
$arrTmpReturn['notes'] = $data['notes'];
// Add additional information to array
$arrTmpReturn['perfdata'] = $data['perf_data'];
$arrTmpReturn['last_check'] = $data['last_check'];
$arrTmpReturn['next_check'] = $data['next_check'];
$arrTmpReturn['state_type'] = $data['state_type'];
$arrTmpReturn['current_check_attempt'] = $data['current_attempt'];
$arrTmpReturn['max_check_attempts'] = $data['max_check_attempts'];
$arrTmpReturn['last_state_change'] = $data['last_state_change'];
$arrTmpReturn['last_hard_state_change'] = $data['last_hard_state_change'];
// If there is a downtime for this object, save the data
if(isset($data['downtime_start']) && $data['downtime_start'] != '') {
$arrTmpReturn['in_downtime'] = 1;
$arrTmpReturn['downtime_start'] = $data['downtime_start'];
$arrTmpReturn['downtime_end'] = $data['downtime_end'];
$arrTmpReturn['downtime_author'] = $data['downtime_author'];
$arrTmpReturn['downtime_data'] = $data['downtime_data'];
}
/**
* Only recognize hard states. There was a discussion about the implementation
* This is a new handling of only_hard_states. For more details, see:
* http://www.nagios-portal.de/wbb/index.php?page=Thread&threadID=8524
*
* Thanks to Andurin and fredy82
*/
if($onlyHardstates == 1) {
if($data['state_type'] != '0') {
$data['current_state'] = $data['current_state'];
} else {
$data['current_state'] = $data['last_hard_state'];
}
}
if($data['current_state'] == '') {
$arrTmpReturn['state'] = 'PENDING';
$arrTmpReturn['output'] = l('serviceNotChecked', Array('SERVICE' => $data['name2']));
} elseif($data['current_state'] == '0') {
// Host is UP
$arrTmpReturn['state'] = 'OK';
$arrTmpReturn['output'] = $data['output'];
} else {
// Host is DOWN/UNREACHABLE/UNKNOWN
/**
* If state is not OK (=> WARN, CRIT, UNKNOWN) and service is not
* acknowledged => check for acknowledged host
*/
if($data['problem_has_been_acknowledged'] != 1) {
$arrTmpReturn['problem_has_been_acknowledged'] = $this->getHostAckByHostname($hostName);
} else {
$arrTmpReturn['problem_has_been_acknowledged'] = $data['problem_has_been_acknowledged'];
}
// Store state and output in array
switch($data['current_state']) {
case '1':
$arrTmpReturn['state'] = 'WARNING';
$arrTmpReturn['output'] = $data['output'];
break;
case '2':
$arrTmpReturn['state'] = 'CRITICAL';
$arrTmpReturn['output'] = $data['output'];
break;
case '3':
$arrTmpReturn['state'] = 'UNKNOWN';
$arrTmpReturn['output'] = $data['output'];
break;
default:
$arrTmpReturn['state'] = 'UNKNOWN';
$arrTmpReturn['output'] = 'GlobalBackendndomy::getServiceState: Undefined state!';
break;
}
}
// If more than one service is expected, append the current return information to return array
if(isset($serviceName) && $serviceName != '') {
$arrReturn = $arrTmpReturn;
} else {
// Assign actual dataset to return array
$arrReturn[strtr($data['name2'],' ','_')] = $arrTmpReturn;
}
}
}
// Free memory
mysql_free_result($QUERYHANDLE);
// Write return array to cache
$this->serviceCache[$hostName.'-'.$serviceName.'-'.$onlyHardstates] = &$arrReturn;
return $arrReturn;
}
}
public function getHostStateCounts($query, $options, $filters) {}
public function getHostgroupStateCounts($query, $options, $filters) {}
public function getServicegroupStateCounts($query, $options, $filters) {}
public function getDirectParentNamesByHostName($hostName) {}
/**
* PUBLIC getHostgroupState()
*
* Returns the Nagios state and additional information for the requested hostgroup
*
* @param String $hostgroupName
* @param Boolean $onlyHardstates
* @param Array Optional array of filters (Not implemented in this backend)
* @return array $state
* @author Roman Kyrylych <rkyrylych@op5.com>
*/
public function getHostgroupState($hostgroupName, $onlyHardstates, $filter = null)
{
$arrReturn = Array();
$QUERYHANDLE = $this->mysqlQuery('SELECT host.host_name, last_hard_state, current_state, state_type'
.' FROM host LEFT JOIN scheduled_downtime AS sdt ON sdt.host_name = host.host_name'
.' AND NOW() > sdt.start_time AND NOW() < sdt.end_time'
.' LEFT JOIN host_hostgroup AS hhg ON hhg.host = host.id'
.' LEFT JOIN hostgroup AS hg ON hg.id = hhg.hostgroup'
." WHERE hg.hostgroup_name = '$hostgroupName'");
if(mysql_num_rows($QUERYHANDLE) == 0) {
$arrReturn = false;
} else {
while($data = mysql_fetch_array($QUERYHANDLE))
{
$arrRow = Array('name' => $data['host_name']);
/**
* Only recognize hard states. There was a discussion about the implementation
* This is a new handling of only_hard_states. For more details, see:
* http://www.nagios-portal.de/wbb/index.php?page=Thread&threadID=8524
*
* Thanks to Andurin and fredy82
*/
if($onlyHardstates == 1) {
if($data['state_type'] != '0') {
$data['current_state'] = $data['current_state'];
} else {
$data['current_state'] = $data['last_hard_state'];
}
}
if($data['current_state'] == '') {
$arrRow['state'] = 'UNCHECKED';
} elseif($data['current_state'] == '0') {
// Host is UP
$arrRow['state'] = 'UP';
} else {
// Host is DOWN/UNREACHABLE/UNKNOWN
// Store state and output in array
switch($data['current_state']) {
case '1':
$arrRow['state'] = 'DOWN';
break;
case '2':
$arrRow['state'] = 'UNREACHABLE';
break;
case '3':
$arrRow['state'] = 'UNKNOWN';
break;
default:
$arrRow['state'] = 'UNKNOWN';
break;
}
}
$arrReturn[] = $arrRow;
}
// Free memory
mysql_free_result($QUERYHANDLE);
}
return $arrReturn;
}
/**
* PUBLIC getServicegroupState()
*
* Returns the Nagios state and additional information for the requested servicegroup
*
* @param String $servicegroupName
* @param Boolean $onlyHardstates
* @param Array Optional array of filters (Not implemented in this backend)
* @return array $state
* @author Roman Kyrylych <rkyrylych@op5.com>
*/
public function getServicegroupState($servicegroupName, $onlyHardstates, $filter = null)
{
$arrReturn = Array();
$QUERYHANDLE = $this->mysqlQuery('SELECT s.host_name, s.service_description,'
.' s.last_hard_state, s.current_state, s.state_type'
.' FROM host AS h, service AS s LEFT JOIN scheduled_downtime AS sdt ON sdt.host_name = s.host_name'
.' AND sdt.service_description = s.service_description AND NOW() > sdt.start_time AND NOW() < sdt.end_time'
.' LEFT JOIN service_servicegroup AS ssg ON ssg.service = service.id'
.' LEFT JOIN servicegroup AS sg ON sg.id = ssg.servicegroup'
." WHERE sg.servicegroup_name = '$servicegroupName'");
if(mysql_num_rows($QUERYHANDLE) == 0) {
$arrReturn = false;
} else {
while($data = mysql_fetch_array($QUERYHANDLE)) {
$arrRow = Array('host' => $data['host_name'],
'description' => $data['service_description']);
/**
* Only recognize hard states. There was a discussion about the implementation
* This is a new handling of only_hard_states. For more details, see:
* http://www.nagios-portal.de/wbb/index.php?page=Thread&threadID=8524
*
* Thanks to Andurin and fredy82
*/
if($onlyHardstates == 1) {
if($data['state_type'] != '0') {
$data['current_state'] = $data['current_state'];
} else {
$data['current_state'] = $data['last_hard_state'];
}
}
if($data['current_state'] == '') {
$arrRow['state'] = 'PENDING';
} elseif($data['current_state'] == '0') {
// Host is UP
$arrRow['state'] = 'OK';
} else {
// Host is DOWN/UNREACHABLE/UNKNOWN
// Store state and output in array
switch($data['current_state']) {
case '1':
$arrRow['state'] = 'WARNING';
break;
case '2':
$arrRow['state'] = 'CRITICAL';
break;
case '3':
$arrRow['state'] = 'UNKNOWN';
break;
default:
$arrRow['state'] = 'UNKNOWN';
break;
}
}
$arrReturn[] = $arrRow;
}
}
// Free memory
mysql_free_result($QUERYHANDLE);
return $arrReturn;
}
/**
* PUBLIC Method getHostNamesWithNoParent
*
* Gets all hosts with no parent host. This method is needed by the automap
* to get the root host.
*
* @author Roman Kyrylych <rkyrylych@op5.com>
*/
public function getHostNamesWithNoParent() {
$arrReturn = Array();
$QUERYHANDLE = $this->mysqlQuery('SELECT host_name FROM host'
.' LEFT JOIN host_parents ON host.id = host_parents.host'
.' WHERE host_parents.parents IS NULL');
while($data = mysql_fetch_array($QUERYHANDLE)) {
$arrReturn[] = $data['host_name'];
}
// Free memory
mysql_free_result($QUERYHANDLE);
return $arrReturn;
}
/**
* PUBLIC Method getDirectChildNamesByHostName
*
* Gets the names of all child hosts
*
* @param String Name of host to get the children of
* @return Array Array with hostnames
* @author Roman Kyrylych <rkyrylych@op5.com>
*/
public function getDirectChildNamesByHostName($hostName) {
$arrChildNames = Array();
$QUERYHANDLE = $this->mysqlQuery('SELECT host_name FROM host'
.' LEFT JOIN host_parents ON host.id = host_parents.host'
." WHERE host_parents.parents = (SELECT id FROM host WHERE host_name = '$hostName')");
while($data = mysql_fetch_array($QUERYHANDLE)) {
$arrChildNames[] = $data['host_name'];
}
// Free memory
mysql_free_result($QUERYHANDLE);
return $arrChildNames;
}
/**
* PUBLIC Method getHostsByHostgroupName
*
* Gets all hosts of a hostgroup
*
* @param String Name of hostgroup to get the hosts of
* @return Array Array with hostnames
* @author Roman Kyrylych <rkyrylych@op5.com>
*/
public function getHostsByHostgroupName($hostgroupName) {
$arrReturn = Array();
$QUERYHANDLE = $this->mysqlQuery('SELECT host_name FROM host'
.' LEFT JOIN host_hostgroup ON id = host WHERE hostgroup ='
." (SELECT id FROM hostgroup WHERE hostgroup_name = '$hostgroupName')");
while($data = mysql_fetch_array($QUERYHANDLE)) {
// Assign actual dataset to return array
$arrReturn[] = $data['host_name'];
}
// Free memory
mysql_free_result($QUERYHANDLE);
return $arrReturn;
}
/**
* PUBLIC Method getServicesByServicegroupName
*
* Gets all services of a servicegroup
*
* @param String Name of servicegroup to get the services of
* @return Array Array with hostnames and service descriptions
* @author Roman Kyrylych <rkyrylych@op5.com>
*/
public function getServicesByServicegroupName($servicegroupName) {
$arrReturn = Array();
$QUERYHANDLE = $this->mysqlQuery('SELECT host_name, service_description FROM service'
.' LEFT JOIN service_servicegroup ON id = service WHERE servicegroup ='
." (SELECT id FROM servicegroup WHERE servicegroup_name = '$servicegroupName')");
while($data = mysql_fetch_array($QUERYHANDLE)) {
// Assign actual dataset to return array
$arrReturn[] = $data;
}
// Free memory
mysql_free_result($QUERYHANDLE);
return $arrReturn;
}
/**
* PUBLIC Method getServicegroupInformations
*
* Gets information like the alias for a servicegroup
*
* @param String Name of servicegroup
* @return Array Array with object information
* @author Roman Kyrylych <rkyrylych@op5.com>
*/
public function getServicegroupInformations($servicegroupName) {
$arrReturn = Array();
$QUERYHANDLE = $this->mysqlQuery("SELECT id, alias FROM servicegroup WHERE servicegroup_name = '$servicegroupName' LIMIT 1");
$data = mysql_fetch_array($QUERYHANDLE);
// Free memory
mysql_free_result($QUERYHANDLE);
$arrReturn['alias'] = $data['alias'];
return $arrReturn;
}
/**
* PUBLIC Method getHostgroupInformations
*
* Gets information like the alias for a hostgroup
*
* @param String Name of group
* @return Array Array with object information
* @author Roman Kyrylych <rkyrylych@op5.com>
*/
public function getHostgroupInformations($groupName) {
$arrReturn = Array();
$QUERYHANDLE = $this->mysqlQuery("SELECT id, alias FROM hostgroup WHERE hostgroup_name = '$groupName' LIMIT 1");
$data = mysql_fetch_array($QUERYHANDLE);
// Free memory
mysql_free_result($QUERYHANDLE);
$arrReturn['alias'] = $data['alias'];
return $arrReturn;
}
/**
* PUBLIC getDirectChildDependenciesNamesByHostName()
*
* @param String Hostname
* @return Array List of hostnames
* @author Thibault Cohen <thibault.cohen@savoirfairelinux.com>
*/
public function getDirectChildDependenciesNamesByHostName($hostName, $min_business_impact=false) {
return $this->getDirectChildNamesByHostName($hostName);
}
/*
* PUBLIC getDirectParentNamesByHostName()
*
* @param String Hostname
* @return Array List of hostnames
* @author Mathias Kettner <mk@mathias-kettner.de>
* @author Lars Michelsen <lars@vertical-visions.de>
*/
public function getDirectParentDependenciesNamesByHostName($hostName, $min_business_impact=false) {
return $this->getDirectParentNamesByHostName($hostName);
}
}
?>
|