protected function updateOperations($operations, $actionsDb) { $operationsUpdate = array(); // messages $opmessageCreate = array(); $opmessageUpdate = array(); $opmessageDeleteByOpId = array(); $opmessageGrpCreate = array(); $opmessageUsrCreate = array(); $opmessageGrpDeleteByOpId = array(); $opmessageUsrDeleteByOpId = array(); // commands $opcommandCreate = array(); $opcommandUpdate = array(); $opcommandDeleteByOpId = array(); $opcommandGrpCreate = array(); $opcommandHstCreate = array(); $opcommandGrpDeleteByOpId = array(); $opcommandHstDeleteByOpId = array(); // groups $opgroupCreate = array(); $opgroupDeleteByOpId = array(); // templates $optemplateCreate = array(); $optemplateDeleteByOpId = array(); $opconditionsCreate = array(); foreach ($operations as $operation) { $operationsDb = zbx_toHash($actionsDb[$operation['actionid']]['operations'], 'operationid'); $operationDb = $operationsDb[$operation['operationid']]; $typeChanged = false; if (isset($operation['operationtype']) && $operation['operationtype'] != $operationDb['operationtype']) { $typeChanged = true; switch ($operationDb['operationtype']) { case OPERATION_TYPE_MESSAGE: $opmessageDeleteByOpId[] = $operationDb['operationid']; $opmessageGrpDeleteByOpId[] = $operationDb['operationid']; $opmessageUsrDeleteByOpId[] = $operationDb['operationid']; break; case OPERATION_TYPE_COMMAND: $opcommandDeleteByOpId[] = $operationDb['operationid']; $opcommandHstDeleteByOpId[] = $operationDb['operationid']; $opcommandGrpDeleteByOpId[] = $operationDb['operationid']; break; case OPERATION_TYPE_GROUP_ADD: if ($operation['operationtype'] == OPERATION_TYPE_GROUP_REMOVE) { break; } case OPERATION_TYPE_GROUP_REMOVE: if ($operation['operationtype'] == OPERATION_TYPE_GROUP_ADD) { break; } $opgroupDeleteByOpId[] = $operationDb['operationid']; break; case OPERATION_TYPE_TEMPLATE_ADD: if ($operation['operationtype'] == OPERATION_TYPE_TEMPLATE_REMOVE) { break; } case OPERATION_TYPE_TEMPLATE_REMOVE: if ($operation['operationtype'] == OPERATION_TYPE_TEMPLATE_ADD) { break; } $optemplateDeleteByOpId[] = $operationDb['operationid']; break; } } if (!isset($operation['operationtype'])) { $operation['operationtype'] = $operationDb['operationtype']; } switch ($operation['operationtype']) { case OPERATION_TYPE_MESSAGE: if (!isset($operation['opmessage_grp'])) { $operation['opmessage_grp'] = array(); } else { zbx_array_push($operation['opmessage_grp'], array('operationid' => $operation['operationid'])); } if (!isset($operation['opmessage_usr'])) { $operation['opmessage_usr'] = array(); } else { zbx_array_push($operation['opmessage_usr'], array('operationid' => $operation['operationid'])); } if (!isset($operationDb['opmessage_usr'])) { $operationDb['opmessage_usr'] = array(); } if (!isset($operationDb['opmessage_grp'])) { $operationDb['opmessage_grp'] = array(); } if ($typeChanged) { $operation['opmessage']['operationid'] = $operation['operationid']; $opmessageCreate[] = $operation['opmessage']; $opmessageGrpCreate = array_merge($opmessageGrpCreate, $operation['opmessage_grp']); $opmessageUsrCreate = array_merge($opmessageUsrCreate, $operation['opmessage_usr']); } else { $opmessageUpdate[] = array('values' => $operation['opmessage'], 'where' => array('operationid' => $operation['operationid'])); $diff = zbx_array_diff($operation['opmessage_grp'], $operationDb['opmessage_grp'], 'usrgrpid'); $opmessageGrpCreate = array_merge($opmessageGrpCreate, $diff['first']); foreach ($diff['second'] as $omgrp) { DB::delete('opmessage_grp', array('usrgrpid' => $omgrp['usrgrpid'], 'operationid' => $operation['operationid'])); } $diff = zbx_array_diff($operation['opmessage_usr'], $operationDb['opmessage_usr'], 'userid'); $opmessageUsrCreate = array_merge($opmessageUsrCreate, $diff['first']); foreach ($diff['second'] as $omusr) { DB::delete('opmessage_usr', array('userid' => $omusr['userid'], 'operationid' => $operation['operationid'])); } } break; case OPERATION_TYPE_COMMAND: if (!isset($operation['opcommand_grp'])) { $operation['opcommand_grp'] = array(); } else { zbx_array_push($operation['opcommand_grp'], array('operationid' => $operation['operationid'])); } if (!isset($operation['opcommand_hst'])) { $operation['opcommand_hst'] = array(); } else { zbx_array_push($operation['opcommand_hst'], array('operationid' => $operation['operationid'])); } if (!isset($operationDb['opcommand_grp'])) { $operationDb['opcommand_grp'] = array(); } if (!isset($operationDb['opcommand_hst'])) { $operationDb['opcommand_hst'] = array(); } if ($typeChanged) { $operation['opcommand']['operationid'] = $operation['operationid']; $opcommandCreate[] = $operation['opcommand']; $opcommandGrpCreate = array_merge($opcommandGrpCreate, $operation['opcommand_grp']); $opcommandHstCreate = array_merge($opcommandHstCreate, $operation['opcommand_hst']); } else { // clear and reset fields to default values on type change if ($operation['opcommand']['type'] == ZBX_SCRIPT_TYPE_GLOBAL_SCRIPT) { $operation['opcommand']['command'] = ''; } else { $operation['opcommand']['scriptid'] = null; } if ($operation['opcommand']['type'] != ZBX_SCRIPT_TYPE_CUSTOM_SCRIPT) { $operation['opcommand']['execute_on'] = ZBX_SCRIPT_EXECUTE_ON_AGENT; } if ($operation['opcommand']['type'] != ZBX_SCRIPT_TYPE_SSH && $operation['opcommand']['type'] != ZBX_SCRIPT_TYPE_TELNET) { $operation['opcommand']['port'] = ''; $operation['opcommand']['username'] = ''; $operation['opcommand']['password'] = ''; } if (!isset($operation['opcommand']['authtype'])) { $operation['opcommand']['authtype'] = ITEM_AUTHTYPE_PASSWORD; } if ($operation['opcommand']['authtype'] == ITEM_AUTHTYPE_PASSWORD) { $operation['opcommand']['publickey'] = ''; $operation['opcommand']['privatekey'] = ''; } $opcommandUpdate[] = array('values' => $operation['opcommand'], 'where' => array('operationid' => $operation['operationid'])); $diff = zbx_array_diff($operation['opcommand_grp'], $operationDb['opcommand_grp'], 'groupid'); $opcommandGrpCreate = array_merge($opcommandGrpCreate, $diff['first']); foreach ($diff['second'] as $omgrp) { DB::delete('opcommand_grp', array('groupid' => $omgrp['groupid'], 'operationid' => $operation['operationid'])); } $diff = zbx_array_diff($operation['opcommand_hst'], $operationDb['opcommand_hst'], 'hostid'); $opcommandHstCreate = array_merge($opcommandHstCreate, $diff['first']); $opCommandHostIds = zbx_objectValues($diff['second'], 'opcommand_hstid'); if ($opCommandHostIds) { DB::delete('opcommand_hst', array('opcommand_hstid' => $opCommandHostIds)); } } break; case OPERATION_TYPE_GROUP_ADD: case OPERATION_TYPE_GROUP_REMOVE: if (!isset($operation['opgroup'])) { $operation['opgroup'] = array(); } else { zbx_array_push($operation['opgroup'], array('operationid' => $operation['operationid'])); } if (!isset($operationDb['opgroup'])) { $operationDb['opgroup'] = array(); } $diff = zbx_array_diff($operation['opgroup'], $operationDb['opgroup'], 'groupid'); $opgroupCreate = array_merge($opgroupCreate, $diff['first']); foreach ($diff['second'] as $ogrp) { DB::delete('opgroup', array('groupid' => $ogrp['groupid'], 'operationid' => $operation['operationid'])); } break; case OPERATION_TYPE_TEMPLATE_ADD: case OPERATION_TYPE_TEMPLATE_REMOVE: if (!isset($operation['optemplate'])) { $operation['optemplate'] = array(); } else { zbx_array_push($operation['optemplate'], array('operationid' => $operation['operationid'])); } if (!isset($operationDb['optemplate'])) { $operationDb['optemplate'] = array(); } $diff = zbx_array_diff($operation['optemplate'], $operationDb['optemplate'], 'templateid'); $optemplateCreate = array_merge($optemplateCreate, $diff['first']); foreach ($diff['second'] as $otpl) { DB::delete('optemplate', array('templateid' => $otpl['templateid'], 'operationid' => $operation['operationid'])); } break; } if (!isset($operation['opconditions'])) { $operation['opconditions'] = array(); } else { zbx_array_push($operation['opconditions'], array('operationid' => $operation['operationid'])); } self::validateOperationConditions($operation['opconditions']); $diff = zbx_array_diff($operation['opconditions'], $operationDb['opconditions'], 'opconditionid'); $opconditionsCreate = array_merge($opconditionsCreate, $diff['first']); $opconditionsidDelete = zbx_objectValues($diff['second'], 'opconditionid'); if (!empty($opconditionsidDelete)) { DB::delete('opconditions', array('opconditionid' => $opconditionsidDelete)); } $operationid = $operation['operationid']; unset($operation['operationid']); if (!empty($operation)) { $operationsUpdate[] = array('values' => $operation, 'where' => array('operationid' => $operationid)); } } DB::update('operations', $operationsUpdate); if (!empty($opmessageDeleteByOpId)) { DB::delete('opmessage', array('operationid' => $opmessageDeleteByOpId)); } if (!empty($opcommandDeleteByOpId)) { DB::delete('opcommand', array('operationid' => $opcommandDeleteByOpId)); } if (!empty($opmessageGrpDeleteByOpId)) { DB::delete('opmessage_grp', array('operationid' => $opmessageGrpDeleteByOpId)); } if (!empty($opmessageUsrDeleteByOpId)) { DB::delete('opmessage_usr', array('operationid' => $opmessageUsrDeleteByOpId)); } if (!empty($opcommandHstDeleteByOpId)) { DB::delete('opcommand_hst', array('operationid' => $opcommandHstDeleteByOpId)); } if (!empty($opcommandGrpDeleteByOpId)) { DB::delete('opcommand_grp', array('operationid' => $opcommandGrpDeleteByOpId)); } if (!empty($opcommandGrpDeleteByOpId)) { DB::delete('opcommand_grp', array('opcommand_grpid' => $opcommandGrpDeleteByOpId)); } if (!empty($opcommandHstDeleteByOpId)) { DB::delete('opcommand_hst', array('opcommand_hstid' => $opcommandHstDeleteByOpId)); } if (!empty($opgroupDeleteByOpId)) { DB::delete('opgroup', array('operationid' => $opgroupDeleteByOpId)); } if (!empty($optemplateDeleteByOpId)) { DB::delete('optemplate', array('operationid' => $optemplateDeleteByOpId)); } DB::insert('opmessage', $opmessageCreate, false); DB::insert('opcommand', $opcommandCreate, false); DB::insert('opmessage_grp', $opmessageGrpCreate); DB::insert('opmessage_usr', $opmessageUsrCreate); DB::insert('opcommand_grp', $opcommandGrpCreate); DB::insert('opcommand_hst', $opcommandHstCreate); DB::insert('opgroup', $opgroupCreate); DB::insert('optemplate', $optemplateCreate); DB::update('opmessage', $opmessageUpdate); DB::update('opcommand', $opcommandUpdate); DB::insert('opconditions', $opconditionsCreate); }
protected function updateOperations($operations, $actionsDb) { $operationsUpdate = array(); // messages $opmessageCreate = array(); $opmessageUpdate = array(); $opmessageDeleteByOpId = array(); $opmessageGrpCreate = array(); $opmessageUsrCreate = array(); $opmessageGrpDeleteByOpId = array(); $opmessageUsrDeleteByOpId = array(); // commands $opcommandCreate = array(); $opcommandUpdate = array(); $opcommandDeleteByOpId = array(); $opcommandGrpCreate = array(); $opcommandHstCreate = array(); $opcommandGrpDeleteByOpId = array(); $opcommandHstDeleteByOpId = array(); // groups $opgroupCreate = array(); $opgroupDeleteByOpId = array(); // templates $optemplateCreate = array(); $optemplateDeleteByOpId = array(); $opconditionsCreate = array(); foreach ($operations as $operation) { $operationDb = $actionsDb[$operation['actionid']]['operations'][$operation['operationid']]; $typeChanged = false; if (isset($operation['operationtype']) && $operation['operationtype'] != $operationDb['operationtype']) { $typeChanged = true; switch ($operationDb['operationtype']) { case OPERATION_TYPE_MESSAGE: $opmessageDeleteByOpId[] = $operationDb['operationid']; $opmessageGrpDeleteByOpId[] = $operationDb['operationid']; $opmessageUsrDeleteByOpId[] = $operationDb['operationid']; break; case OPERATION_TYPE_COMMAND: $opcommandDeleteByOpId[] = $operationDb['operationid']; $opcommandHstDeleteByOpId[] = $operationDb['operationid']; $opcommandGrpDeleteByOpId[] = $operationDb['operationid']; break; case OPERATION_TYPE_GROUP_ADD: if ($operation['operationtype'] == OPERATION_TYPE_GROUP_REMOVE) { break; } case OPERATION_TYPE_GROUP_REMOVE: if ($operation['operationtype'] == OPERATION_TYPE_GROUP_ADD) { break; } $opgroupDeleteByOpId[] = $operationDb['operationid']; break; case OPERATION_TYPE_TEMPLATE_ADD: if ($operation['operationtype'] == OPERATION_TYPE_TEMPLATE_REMOVE) { break; } case OPERATION_TYPE_TEMPLATE_REMOVE: if ($operation['operationtype'] == OPERATION_TYPE_TEMPLATE_ADD) { break; } $optemplateDeleteByOpId[] = $operationDb['operationid']; break; } } if (!isset($operation['operationtype'])) { $operation['operationtype'] = $operationDb['operationtype']; } switch ($operation['operationtype']) { case OPERATION_TYPE_MESSAGE: if (!isset($operation['opmessage_grp'])) { $operation['opmessage_grp'] = array(); } else { zbx_array_push($operation['opmessage_grp'], array('operationid' => $operation['operationid'])); } if (!isset($operation['opmessage_usr'])) { $operation['opmessage_usr'] = array(); } else { zbx_array_push($operation['opmessage_usr'], array('operationid' => $operation['operationid'])); } if (!isset($operationDb['opmessage_usr'])) { $operationDb['opmessage_usr'] = array(); } if (!isset($operationDb['opmessage_grp'])) { $operationDb['opmessage_grp'] = array(); } if ($typeChanged) { $operation['opmessage']['operationid'] = $operation['operationid']; $opmessageCreate[] = $operation['opmessage']; $opmessageGrpCreate = array_merge($opmessageGrpCreate, $operation['opmessage_grp']); $opmessageUsrCreate = array_merge($opmessageUsrCreate, $operation['opmessage_usr']); } else { $opmessageUpdate[] = array('values' => $operation['opmessage'], 'where' => array('operationid' => $operation['operationid'])); $diff = zbx_array_diff($operation['opmessage_grp'], $operationDb['opmessage_grp'], 'usrgrpid'); $opmessageGrpCreate = array_merge($opmessageGrpCreate, $diff['first']); foreach ($diff['second'] as $omgrp) { DB::delete('opmessage_grp', array('usrgrpid' => $omgrp['usrgrpid'], 'operationid' => $operation['operationid'])); } $diff = zbx_array_diff($operation['opmessage_usr'], $operationDb['opmessage_usr'], 'userid'); $opmessageUsrCreate = array_merge($opmessageUsrCreate, $diff['first']); foreach ($diff['second'] as $omusr) { DB::delete('opmessage_usr', array('userid' => $omusr['userid'], 'operationid' => $operation['operationid'])); } } break; case OPERATION_TYPE_COMMAND: if (!isset($operation['opcommand_grp'])) { $operation['opcommand_grp'] = array(); } else { zbx_array_push($operation['opcommand_grp'], array('operationid' => $operation['operationid'])); } if (!isset($operation['opcommand_hst'])) { $operation['opcommand_hst'] = array(); } else { zbx_array_push($operation['opcommand_hst'], array('operationid' => $operation['operationid'])); } if (!isset($operationDb['opcommand_grp'])) { $operationDb['opcommand_grp'] = array(); } if (!isset($operationDb['opcommand_hst'])) { $operationDb['opcommand_hst'] = array(); } if ($typeChanged) { $operation['opcommand']['operationid'] = $operation['operationid']; $opcommandCreate[] = $operation['opcommand']; $opcommandGrpCreate = array_merge($opcommandGrpCreate, $operation['opcommand_grp']); $opcommandHstCreate = array_merge($opcommandHstCreate, $operation['opcommand_hst']); } else { $opcommandUpdate[] = array('values' => $operation['opcommand'], 'where' => array('operationid' => $operation['operationid'])); $diff = zbx_array_diff($operation['opcommand_grp'], $operationDb['opcommand_grp'], 'groupid'); $opcommandGrpCreate = array_merge($opcommandGrpCreate, $diff['first']); foreach ($diff['second'] as $omgrp) { DB::delete('opcommand_grp', array('groupid' => $omgrp['groupid'], 'operationid' => $operation['operationid'])); } $diff = zbx_array_diff($operation['opcommand_hst'], $operationDb['opcommand_hst'], 'hostid'); $opcommandHstCreate = array_merge($opcommandHstCreate, $diff['first']); $opCommandHostIds = zbx_objectValues($diff['second'], 'opcommand_hstid'); if ($opCommandHostIds) { DB::delete('opcommand_hst', array('opcommand_hstid' => $opCommandHostIds)); } } break; case OPERATION_TYPE_GROUP_ADD: case OPERATION_TYPE_GROUP_REMOVE: if (!isset($operation['opgroup'])) { $operation['opgroup'] = array(); } else { zbx_array_push($operation['opgroup'], array('operationid' => $operation['operationid'])); } if (!isset($operationDb['opgroup'])) { $operationDb['opgroup'] = array(); } $diff = zbx_array_diff($operation['opgroup'], $operationDb['opgroup'], 'groupid'); $opgroupCreate = array_merge($opgroupCreate, $diff['first']); foreach ($diff['second'] as $ogrp) { DB::delete('opgroup', array('groupid' => $ogrp['groupid'], 'operationid' => $operation['operationid'])); } break; case OPERATION_TYPE_TEMPLATE_ADD: case OPERATION_TYPE_TEMPLATE_REMOVE: if (!isset($operation['optemplate'])) { $operation['optemplate'] = array(); } else { zbx_array_push($operation['optemplate'], array('operationid' => $operation['operationid'])); } if (!isset($operationDb['optemplate'])) { $operationDb['optemplate'] = array(); } $diff = zbx_array_diff($operation['optemplate'], $operationDb['optemplate'], 'templateid'); $optemplateCreate = array_merge($optemplateCreate, $diff['first']); foreach ($diff['second'] as $otpl) { DB::delete('optemplate', array('templateid' => $otpl['templateid'], 'operationid' => $operation['operationid'])); } break; } if (!isset($operation['opconditions'])) { $operation['opconditions'] = array(); } else { zbx_array_push($operation['opconditions'], array('operationid' => $operation['operationid'])); } self::validateOperationConditions($operation['opconditions']); $diff = zbx_array_diff($operation['opconditions'], $operationDb['opconditions'], 'opconditionid'); $opconditionsCreate = array_merge($opconditionsCreate, $diff['first']); $opconditionsidDelete = zbx_objectValues($diff['second'], 'opconditionid'); if (!empty($opconditionsidDelete)) { DB::delete('opconditions', array('opconditionid' => $opconditionsidDelete)); } $operationid = $operation['operationid']; unset($operation['operationid']); if (!empty($operation)) { $operationsUpdate[] = array('values' => $operation, 'where' => array('operationid' => $operationid)); } } DB::update('operations', $operationsUpdate); if (!empty($opmessageDeleteByOpId)) { DB::delete('opmessage', array('operationid' => $opmessageDeleteByOpId)); } if (!empty($opcommandDeleteByOpId)) { DB::delete('opcommand', array('operationid' => $opcommandDeleteByOpId)); } if (!empty($opmessageGrpDeleteByOpId)) { DB::delete('opmessage_grp', array('operationid' => $opmessageGrpDeleteByOpId)); } if (!empty($opmessageUsrDeleteByOpId)) { DB::delete('opmessage_usr', array('operationid' => $opmessageUsrDeleteByOpId)); } if (!empty($opcommandHstDeleteByOpId)) { DB::delete('opcommand_hst', array('operationid' => $opcommandHstDeleteByOpId)); } if (!empty($opcommandGrpDeleteByOpId)) { DB::delete('opcommand_grp', array('operationid' => $opcommandGrpDeleteByOpId)); } if (!empty($opcommandGrpDeleteByOpId)) { DB::delete('opcommand_grp', array('opcommand_grpid' => $opcommandGrpDeleteByOpId)); } if (!empty($opcommandHstDeleteByOpId)) { DB::delete('opcommand_hst', array('opcommand_hstid' => $opcommandHstDeleteByOpId)); } if (!empty($opgroupDeleteByOpId)) { DB::delete('opgroup', array('operationid' => $opgroupDeleteByOpId)); } if (!empty($optemplateDeleteByOpId)) { DB::delete('optemplate', array('operationid' => $optemplateDeleteByOpId)); } DB::insert('opmessage', $opmessageCreate, false); DB::insert('opcommand', $opcommandCreate, false); DB::insert('opmessage_grp', $opmessageGrpCreate); DB::insert('opmessage_usr', $opmessageUsrCreate); DB::insert('opcommand_grp', $opcommandGrpCreate); DB::insert('opcommand_hst', $opcommandHstCreate); DB::insert('opgroup', $opgroupCreate); DB::insert('optemplate', $optemplateCreate); DB::update('opmessage', $opmessageUpdate); DB::update('opcommand', $opcommandUpdate); DB::insert('opconditions', $opconditionsCreate); }