/usr/share/zabbix/api/classes/class.cmaintenance.php is in zabbix-frontend-php 1:1.8.11-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | <?php
/*
** ZABBIX
** Copyright (C) 2000-2010 SIA Zabbix
**
** 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.
**
** 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.
**/
?>
<?php
/**
* File containing CMaintenance class for API.
* @package API
*/
/**
* Class containing methods for operations with maintenances
*
*/
class CMaintenance extends CZBXAPI{
/**
* Get maintenances data
*
* @param array $options
* @param array $options['itemids']
* @param array $options['hostids']
* @param array $options['groupids']
* @param array $options['triggerids']
* @param array $options['maintenanceids']
* @param boolean $options['status']
* @param boolean $options['editable']
* @param boolean $options['count']
* @param string $options['pattern']
* @param int $options['limit']
* @param string $options['order']
* @return array|int item data as array or false if error
*/
public static function get($options=array()){
global $USER_DETAILS;
$result = array();
$user_type = $USER_DETAILS['type'];
$userid = $USER_DETAILS['userid'];
$sort_columns = array('maintenanceid', 'name'); // allowed columns for sorting
$subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND); // allowed output options for [ select_* ] params
$sql_parts = array(
'select' => array('maintenance' => 'm.maintenanceid'),
'from' => array('maintenances' => 'maintenances m'),
'where' => array(),
'group' => array(),
'order' => array(),
'limit' => null
);
$def_options = array(
'nodeids' => null,
'groupids' => null,
'hostids' => null,
'maintenanceids' => null,
'editable' => null,
'nopermissions' => null,
// filter
'filter' => null,
'search' => null,
'startSearch' => null,
'excludeSearch' => null,
'filter' => null,
'searchWildcardsEnabled' => null,
// OutPut
'output' => API_OUTPUT_REFER,
'extendoutput' => null,
'select_groups' => null,
'select_hosts' => null,
'countOutput' => null,
'groupCount' => null,
'preservekeys' => null,
'sortfield' => '',
'sortorder' => '',
'limit' => null,
);
$options = zbx_array_merge($def_options, $options);
if(!is_null($options['extendoutput'])){
$options['output'] = API_OUTPUT_EXTEND;
if(!is_null($options['select_groups'])){
$options['select_groups'] = API_OUTPUT_EXTEND;
}
if(!is_null($options['select_hosts'])){
$options['select_hosts'] = API_OUTPUT_EXTEND;
}
}
// editable + PERMISSION CHECK
$maintenanceids = array();
if((USER_TYPE_SUPER_ADMIN == $user_type) || $options['nopermissions']){
if(!is_null($options['groupids']) || !is_null($options['hostids'])){
if(!is_null($options['groupids'])){
zbx_value2array($options['groupids']);
$sql = ' SELECT mmg.maintenanceid '.
' FROM maintenances_groups mmg '.
' WHERE '.DBcondition('mmg.groupid', $options['groupids']);
$res = DBselect($sql);
while($maintenance = DBfetch($res)){
$maintenanceids[] = $maintenance['maintenanceid'];
}
}
$sql = ' SELECT mmh.maintenanceid '.
' FROM maintenances_hosts mmh, hosts_groups hg '.
' WHERE hg.hostid=mmh.hostid ';
if(!is_null($options['groupids'])){
zbx_value2array($options['groupids']);
$sql.=' AND '.DBcondition('hg.groupid', $options['groupids']);
}
if(!is_null($options['hostids'])){
zbx_value2array($options['hostids']);
$sql.=' AND '.DBcondition('hg.hostid', $options['hostids']);
}
$res = DBselect($sql);
while($maintenance = DBfetch($res)){
$maintenanceids[] = $maintenance['maintenanceid'];
}
$sql_parts['where'][] = DBcondition('m.maintenanceid',$maintenanceids);
}
}
else{
$permission = $options['editable']?PERM_READ_WRITE:PERM_READ_ONLY;
$sql =
'SELECT DISTINCT m.maintenanceid'.
' FROM maintenances m'.
' WHERE'.
' NOT EXISTS ('.
' SELECT mh3.maintenanceid'.
' FROM maintenances_hosts mh3, rights r3, users_groups ug3, hosts_groups hg3'.
' WHERE mh3.maintenanceid = m.maintenanceid'.
' AND r3.groupid = ug3.usrgrpid'.
' AND hg3.hostid = mh3.hostid'.
' AND r3.id = hg3.groupid'.
' AND ug3.userid = '.$userid.
' AND r3.permission < '.$permission.
' ) '.
' AND NOT EXISTS ( '.
' SELECT mh4.maintenanceid '.
' FROM maintenances_hosts mh4 '.
' WHERE mh4.maintenanceid = m.maintenanceid '.
' AND NOT EXISTS( '.
' SELECT r5.id '.
' FROM rights r5, users_groups ug5, hosts_groups hg5 '.
' WHERE r5.groupid = ug5.usrgrpid '.
' AND hg5.hostid = mh4.hostid '.
' AND r5.id = hg5.groupid '.
' AND ug5.userid = '.$userid.
' ) '.
' ) '.
' AND NOT EXISTS ( '.
' SELECT mg2.maintenanceid '.
' FROM maintenances_groups mg2, rights r3, users_groups ug3 '.
' WHERE mg2.maintenanceid = m.maintenanceid '.
' AND r3.groupid = ug3.usrgrpid '.
' AND r3.id = mg2.groupid '.
' AND ug3.userid = '.$userid.
' AND r3.permission < '.$permission.
' ) '.
' AND NOT EXISTS ( '.
' SELECT mg3.maintenanceid '.
' FROM maintenances_groups mg3 '.
' WHERE mg3.maintenanceid = m.maintenanceid '.
' AND NOT EXISTS( '.
' SELECT r5.id '.
' FROM rights r5, users_groups ug5, hosts_groups hg5 '.
' WHERE r5.groupid = ug5.usrgrpid '.
' AND r5.id = mg3.groupid '.
' AND ug5.userid = '.$userid.
' ) '.
' ) ';
if(!is_null($options['groupids'])){
zbx_value2array($options['groupids']);
//filtering using groups attached to maintenence
$sql .= 'AND ( '.
' EXISTS ('.
' SELECT mgf.maintenanceid '.
' FROM maintenances_groups mgf '.
' WHERE mgf.maintenanceid = m.maintenanceid '.
' AND '.DBcondition('mgf.groupid', $options['groupids']).
' ) ';
//filtering by hostgroups of hosts attached to maintenance
$sql .= 'OR EXISTS ('.
' SELECT mh.maintenanceid '.
' FROM maintenances_hosts mh, hosts_groups hg '.
' WHERE mh.maintenanceid = m.maintenanceid '.
' AND hg.hostid = mh.hostid '.
' AND '.DBcondition('hg.groupid', $options['groupids']).
' ) '.
' ) ';
}
if(!is_null($options['hostids'])){
zbx_value2array($options['hostids']);
$sql .= 'AND EXISTS ('.
' SELECT mh.maintenanceid '.
' FROM maintenances_hosts mh'.
' WHERE mh.maintenanceid = m.maintenanceid '.
' AND '.DBcondition('mh.hostid', $options['hostids']).
' ) ';
}
$res = DBselect($sql);
while($miantenace = DBfetch($res)){
$maintenanceids[] = $miantenace['maintenanceid'];
}
$sql_parts['where'][] = DBcondition('m.maintenanceid',$maintenanceids);
}
// nodeids
$nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
// groupids
if(!is_null($options['groupids'])){
$options['select_groups'] = 1;
}
// hostids
if(!is_null($options['hostids'])){
$options['select_hosts'] = 1;
}
// maintenanceids
if(!is_null($options['maintenanceids'])){
zbx_value2array($options['maintenanceids']);
$sql_parts['where'][] = DBcondition('m.maintenanceid', $options['maintenanceids']);
}
// output
if($options['output'] == API_OUTPUT_EXTEND){
$sql_parts['select']['maintenance'] = 'm.*';
}
// countOutput
if(!is_null($options['countOutput'])){
$options['sortfield'] = '';
$sql_parts['select'] = array('count(DISTINCT m.maintenanceid) as rowscount');
//groupCount
if(!is_null($options['groupCount'])){
foreach($sql_parts['group'] as $key => $fields){
$sql_parts['select'][$key] = $fields;
}
}
}
// filter
if(is_array($options['filter'])){
zbx_db_filter('maintenances m', $options, $sql_parts);
}
// search
if(is_array($options['search'])){
zbx_db_search('maintenances m', $options, $sql_parts);
}
// order
// restrict not allowed columns for sorting
$options['sortfield'] = str_in_array($options['sortfield'], $sort_columns) ? $options['sortfield'] : '';
if(!zbx_empty($options['sortfield'])){
$sortorder = ($options['sortorder'] == ZBX_SORT_DOWN)?ZBX_SORT_DOWN:ZBX_SORT_UP;
$sql_parts['order'][] = 'm.'.$options['sortfield'].' '.$sortorder;
if(!str_in_array('m.'.$options['sortfield'], $sql_parts['select']) && !str_in_array('m.*', $sql_parts['select'])){
$sql_parts['select'][] = 'm.'.$options['sortfield'];
}
}
// limit
if(zbx_ctype_digit($options['limit']) && $options['limit']){
$sql_parts['limit'] = $options['limit'];
}
//----------
$maintenanceids = array();
$sql_parts['select'] = array_unique($sql_parts['select']);
$sql_parts['from'] = array_unique($sql_parts['from']);
$sql_parts['where'] = array_unique($sql_parts['where']);
$sql_parts['group'] = array_unique($sql_parts['group']);
$sql_parts['order'] = array_unique($sql_parts['order']);
$sql_select = '';
$sql_from = '';
$sql_where = '';
$sql_group = '';
$sql_order = '';
if(!empty($sql_parts['select'])) $sql_select.= implode(',',$sql_parts['select']);
if(!empty($sql_parts['from'])) $sql_from.= implode(',',$sql_parts['from']);
if(!empty($sql_parts['where'])) $sql_where.= ' AND '.implode(' AND ',$sql_parts['where']);
if(!empty($sql_parts['group'])) $sql_where.= ' GROUP BY '.implode(',',$sql_parts['group']);
if(!empty($sql_parts['order'])) $sql_order.= ' ORDER BY '.implode(',',$sql_parts['order']);
$sql_limit = $sql_parts['limit'];
$sql = 'SELECT '.zbx_db_distinct($sql_parts).' '.$sql_select.
' FROM '.$sql_from.
' WHERE '.DBin_node('m.maintenanceid', $nodeids).
$sql_where.
$sql_order;
$res = DBselect($sql, $sql_limit);
while($maintenance = DBfetch($res)){
if(!is_null($options['countOutput'])){
if(!is_null($options['groupCount']))
$result[] = $maintenance;
else
$result = $maintenance['rowscount'];
}
else{
$maintenanceids[$maintenance['maintenanceid']] = $maintenance['maintenanceid'];
if($options['output'] == API_OUTPUT_SHORTEN){
$result[$maintenance['maintenanceid']] = array('maintenanceid' => $maintenance['maintenanceid']);
}
else{
if(!isset($result[$maintenance['maintenanceid']]))
$result[$maintenance['maintenanceid']]= array();
if(!is_null($options['select_groups']) && !isset($result[$maintenance['maintenanceid']]['groups'])){
$result[$maintenance['maintenanceid']]['groups'] = array();
}
if(!is_null($options['select_hosts']) && !isset($result[$maintenance['maintenanceid']]['hosts'])){
$result[$maintenance['maintenanceid']]['hosts'] = array();
}
// groupids
if(isset($maintenance['groupid']) && is_null($options['select_groups'])){
if(!isset($result[$maintenance['maintenanceid']]['groups']))
$result[$maintenance['maintenanceid']]['groups'] = array();
$result[$maintenance['maintenanceid']]['groups'][] = array('groupid' => $maintenance['groupid']);
unset($maintenance['groupid']);
}
// hostids
if(isset($maintenance['hostid']) && is_null($options['select_hosts'])){
if(!isset($result[$maintenance['maintenanceid']]['hosts']))
$result[$maintenance['maintenanceid']]['hosts'] = array();
$result[$maintenance['maintenanceid']]['hosts'][] = array('hostid' => $maintenance['hostid']);
unset($maintenance['hostid']);
}
$result[$maintenance['maintenanceid']] += $maintenance;
}
}
}
Copt::memoryPick();
if(!is_null($options['countOutput'])){
if(is_null($options['preservekeys'])) $result = zbx_cleanHashes($result);
return $result;
}
// select_groups
if(is_array($options['select_groups']) || str_in_array($options['select_groups'], $subselects_allowed_outputs)){
$obj_params = array(
'nodeids' => $nodeids,
'maintenanceids' => $maintenanceids,
'preservekeys' => 1,
'output' => $options['select_groups'],
);
$groups = CHostGroup::get($obj_params);
foreach($groups as $groupid => $group){
$gmaintenances = $group['maintenances'];
unset($group['maintenances']);
foreach($gmaintenances as $num => $maintenance){
$result[$maintenance['maintenanceid']]['groups'][] = $group;
}
}
}
// select_hosts
if(is_array($options['select_hosts']) || str_in_array($options['select_hosts'], $subselects_allowed_outputs)){
$obj_params = array(
'nodeids' => $nodeids,
'maintenanceids' => $maintenanceids,
'preservekeys' => 1,
'output' => $options['select_hosts'],
);
$hosts = CHost::get($obj_params);
foreach($hosts as $hostid => $host){
$hmaintenances = $host['maintenances'];
unset($host['maintenances']);
foreach($hmaintenances as $num => $maintenance){
$result[$maintenance['maintenanceid']]['hosts'][] = $host;
}
}
}
Copt::memoryPick();
// removing keys (hash -> array)
if(is_null($options['preservekeys'])){
$result = zbx_cleanHashes($result);
}
return $result;
}
/**
* Determine, whether an object already exists
*
* @param array $object
* @return bool
*/
public static function exists($object){
$keyFields = array(array('maintenanceid', 'name'));
$options = array(
'filter' => zbx_array_mintersect($keyFields, $object),
'output' => API_OUTPUT_SHORTEN,
'nopermissions' => 1,
'limit' => 1
);
$objs = self::get($options);
return !empty($objs);
}
/**
* Add maintenances
*
* @param array $maintenances
* @return boolean
*/
public static function create($maintenances){
global $USER_DETAILS;
$maintenances = zbx_toArray($maintenances);
try{
self::BeginTransaction(__METHOD__);
if($USER_DETAILS['type'] == USER_TYPE_ZABBIX_USER){
self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSION);
}
$hostids = array();
$groupids = array();
foreach($maintenances as $maintenance){
$hostids = array_merge($hostids, $maintenance['hostids']);
$groupids = array_merge($groupids, $maintenance['groupids']);
}
if(empty($hostids) && empty($groupids)){
self::exception(ZBX_API_ERROR_PERMISSIONS, S_GROUP_OR_HOST_NEEDED);
}
// hosts permissions
$options = array(
'hostids' => $hostids,
'editable' => 1,
'output' => API_OUTPUT_SHORTEN,
'preservekeys' => 1,
);
$upd_hosts = CHost::get($options);
foreach($hostids as $hostid){
if(!isset($upd_hosts[$hostid])){
self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSION);
}
}
// groups permissions
$options = array(
'groupids' => $groupids,
'editable' => 1,
'output' => API_OUTPUT_SHORTEN,
'preservekeys' => 1,
);
$upd_groups = CHostGroup::get($options);
foreach($groupids as $groupid){
if(!isset($upd_groups[$groupid])){
self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSION);
}
}
self::removeSecondsFromTimes($maintenances);
$tid = 0;
$insert = array();
$timeperiods = array();
$insert_timeperiods = array();
$now = time();
$now -= $now % SEC_PER_MIN;
foreach ($maintenances as $mnum => $maintenance) {
$db_fields = array(
'name' => null,
'active_since' => $now,
'active_till' => $now + SEC_PER_DAY
);
if (!check_db_fields($db_fields, $maintenance)) {
self::exception(ZBX_API_ERROR_PARAMETERS, 'Incorrect parameters used for Maintenance');
}
//checkig wheter a maintence with this name already exists
if (self::exists(array('name' => $maintenance['name']))) {
self::exception(ZBX_API_ERROR_PARAMETERS, S_MAINTENANCE.' [ '.$maintenance['name'].' ] '.S_ALREADY_EXISTS_SMALL);
}
$insert[$mnum] = $maintenance;
foreach ($maintenance['timeperiods'] as $timeperiod) {
$db_fields = array(
'timeperiod_type' => TIMEPERIOD_TYPE_ONETIME,
'period' => SEC_PER_HOUR,
'start_date' => $now
);
check_db_fields($db_fields, $timeperiod);
$tid++;
$insert_timeperiods[$tid] = $timeperiod;
$timeperiods[$tid] = $mnum;
}
}
$maintenanceids = DB::insert('maintenances', $insert);
$timeperiodids = DB::insert('timeperiods', $insert_timeperiods);
$insert_windows = array();
foreach($timeperiods as $tid => $mnum){
$insert_windows[] = array(
'timeperiodid' => $timeperiodids[$tid],
'maintenanceid' => $maintenanceids[$mnum],
);
}
DB::insert('maintenances_windows', $insert_windows);
$insert_hosts = array();
$insert_groups = array();
foreach($maintenances as $mnum => $maintenance){
foreach($maintenance['hostids'] as $hostid){
$insert_hosts[] = array(
'hostid' => $hostid,
'maintenanceid' => $maintenanceids[$mnum],
);
}
foreach($maintenance['groupids'] as $groupid){
$insert_groups[] = array(
'groupid' => $groupid,
'maintenanceid' => $maintenanceids[$mnum],
);
}
}
DB::insert('maintenances_hosts', $insert_hosts);
DB::insert('maintenances_groups', $insert_groups);
self::EndTransaction(true, __METHOD__);
return array('maintenanceids'=>$maintenanceids);
}
catch(APIException $e){
self::EndTransaction(false, __METHOD__);
$error = $e->getErrors();
$error = reset($error);
self::setError(__METHOD__, $e->getCode(), $error);
return false;
}
}
/**
* Update maintenances
*
* @param _array $maintenances
* @return boolean
*/
public static function update($maintenances){
global $USER_DETAILS;
$maintenances = zbx_toArray($maintenances);
$maintenanceids = zbx_objectValues($maintenances, 'maintenanceid');
try{
self::BeginTransaction(__METHOD__);
if($USER_DETAILS['type'] == USER_TYPE_ZABBIX_USER){
self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSION);
}
// Maintenance permissions
$hostids = array();
$groupids = array();
$options = array(
'maintenanceids' => zbx_objectValues($maintenances, 'maintenanceid'),
'editable' => 1,
'output' => API_OUTPUT_SHORTEN,
'preservekeys' => 1,
);
$upd_maintenances = self::get($options);
foreach($maintenances as $maintenance){
if(!isset($upd_maintenances[$maintenance['maintenanceid']])){
self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSION);
}
// checking whether a maintenance with this name and different id already exists
// first, getting all maintenances with the same name as this
$options = array(
'filter' => array(
'name'=>$maintenance['name']
)
);
$received_maintenaces = CMaintenance::get($options);
// now going through a result, to find records with different id than our object
foreach($received_maintenaces as $r_maintenace){
if ($r_maintenace['maintenanceid'] != $maintenance['maintenanceid']) {
//error! Maintenance with this name already exists
self::exception(ZBX_API_ERROR_PARAMETERS, S_MAINTENANCE.' [ '.$maintenance['name'].' ] '.S_ALREADY_EXISTS_SMALL);
}
}
$hostids = array_merge($hostids, $maintenance['hostids']);
$groupids = array_merge($groupids, $maintenance['groupids']);
}
if(empty($hostids) && empty($groupids)){
self::exception(ZBX_API_ERROR_PERMISSIONS, S_GROUP_OR_HOST_NEEDED);
}
// hosts permissions
$options = array(
'hostids' => $hostids,
'editable' => 1,
'output' => API_OUTPUT_SHORTEN,
'preservekeys' => 1,
);
$upd_hosts = CHost::get($options);
foreach($hostids as $hostid){
if(!isset($upd_hosts[$hostid])){
self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSION);
}
}
// groups permissions
$options = array(
'groupids' => $groupids,
'editable' => 1,
'output' => API_OUTPUT_SHORTEN,
'preservekeys' => 1,
);
$upd_groups = CHostGroup::get($options);
foreach($groupids as $groupid){
if(!isset($upd_groups[$groupid])){
self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSION);
}
}
self::removeSecondsFromTimes($maintenances);
$timeperiodids = array();
$sql = 'SELECT DISTINCT tp.timeperiodid '.
' FROM timeperiods tp, maintenances_windows mw '.
' WHERE '.DBcondition('mw.maintenanceid',$maintenanceids).
' AND tp.timeperiodid=mw.timeperiodid ';
$db_timeperiods = DBselect($sql);
while($timeperiod = DBfetch($db_timeperiods)){
$timeperiodids[] = $timeperiod['timeperiodid'];
}
DB::delete('timeperiods', DBcondition('timeperiodid', $timeperiodids));
DB::delete('maintenances_windows', DBcondition('maintenanceid', $maintenanceids));
$tid = 0;
$update = array();
$timeperiods = array();
$insert_timeperiods = array();
foreach($maintenances as $mnum => $maintenance){
$db_fields = array(
'maintenanceid' => null,
);
if(!check_db_fields($db_fields, $maintenance)){
self::exception(ZBX_API_ERROR_PARAMETERS, 'Incorrect parameters used for Maintenance');
}
$update[$mnum] = array(
'values' => $maintenance,
'where' => array('maintenanceid='.$maintenance['maintenanceid']),
);
foreach($maintenance['timeperiods'] as $timeperiod){
$tid++;
$insert_timeperiods[$tid] = $timeperiod;
$timeperiods[$tid] = $mnum;
}
}
DB::update('maintenances', $update);
$timeperiodids = DB::insert('timeperiods', $insert_timeperiods);
$insert_windows = array();
foreach($timeperiods as $tid => $mnum){
$insert_windows[] = array(
'timeperiodid' => $timeperiodids[$tid],
'maintenanceid' => $maintenances[$mnum]['maintenanceid'],
);
}
DB::insert('maintenances_windows', $insert_windows);
DB::delete('maintenances_hosts', DBcondition('maintenanceid', $maintenanceids));
DB::delete('maintenances_groups', DBcondition('maintenanceid', $maintenanceids));
$insert_hosts = array();
$insert_groups = array();
foreach($maintenances as $mnum => $maintenance){
foreach($maintenance['hostids'] as $hostid){
$insert_hosts[] = array(
'hostid' => $hostid,
'maintenanceid' => $maintenance['maintenanceid'],
);
}
foreach($maintenance['groupids'] as $groupid){
$insert_groups[] = array(
'groupid' => $groupid,
'maintenanceid' => $maintenance['maintenanceid'],
);
}
}
DB::insert('maintenances_hosts', $insert_hosts);
DB::insert('maintenances_groups', $insert_groups);
self::EndTransaction(true, __METHOD__);
return true;
}
catch(APIException $e){
self::EndTransaction(false, __METHOD__);
$error = $e->getErrors();
$error = reset($error);
self::setError(__METHOD__, $e->getCode(), $error);
return false;
}
}
/**
* Delete maintenances
*
* @param _array $maintenanceids
* @param _array $maintenanceids['maintenanceids']
* @return boolean
*/
public static function delete($maintenanceids){
global $USER_DETAILS;
$maintenanceids = zbx_toArray($maintenanceids);
try{
self::BeginTransaction(__METHOD__);
if($USER_DETAILS['type'] == USER_TYPE_ZABBIX_USER){
self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSION);
}
$options = array(
'maintenanceids' => $maintenanceids,
'editable' => 1,
'output' => API_OUTPUT_SHORTEN,
'preservekeys' => 1
);
$del_maintenances = self::get($options);
foreach($maintenanceids as $snum => $maintenanceid){
if(!isset($del_maintenances[$maintenanceid])){
self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSION);
}
}
$timeperiodids = array();
$sql = 'SELECT DISTINCT tp.timeperiodid '.
' FROM timeperiods tp, maintenances_windows mw '.
' WHERE '.DBcondition('mw.maintenanceid',$maintenanceids).
' AND tp.timeperiodid=mw.timeperiodid ';
$db_timeperiods = DBselect($sql);
while($timeperiod = DBfetch($db_timeperiods)){
$timeperiodids[] = $timeperiod['timeperiodid'];
}
$mid_cond = DBcondition('maintenanceid', $maintenanceids);
DB::delete('timeperiods', DBcondition('timeperiodid', $timeperiodids));
DB::delete('maintenances_windows', $mid_cond);
DB::delete('maintenances_hosts', $mid_cond);
DB::delete('maintenances_groups', $mid_cond);
DB::delete('maintenances', $mid_cond);
self::EndTransaction(true, __METHOD__);
return true;
}
catch(APIException $e){
self::EndTransaction(false, __METHOD__);
$error = $e->getErrors();
$error = reset($error);
self::setError(__METHOD__, $e->getCode(), $error);
return false;
}
}
protected static function removeSecondsFromTimes(array &$maintenances) {
foreach ($maintenances as &$maintenance) {
if (isset($maintenance['active_since'])) {
$maintenance['active_since'] -= $maintenance['active_since'] % SEC_PER_MIN;
}
if (isset($maintenance['active_till'])) {
$maintenance['active_till'] -= $maintenance['active_till'] % SEC_PER_MIN;
}
if (isset($maintenance['timeperiods'])) {
foreach ($maintenance['timeperiods'] as &$timeperiod) {
if (isset($timeperiod['start_date'])) {
$timeperiod['start_date'] -= $timeperiod['start_date'] % SEC_PER_MIN;
}
}
unset($timeperiod);
}
}
unset($maintenance);
}
}
?>
|