Esempio n. 1
0
 /**
  * Add Hosts to HostGroups. All Hosts are added to all HostGroups.
  *
  * @param array $data
  * @param array $data['groups']
  * @param array $data['templates']
  * @param array $data['macros']
  * @return array
  */
 public static function massAdd($data)
 {
     $data['hosts'] = zbx_toArray($data['hosts']);
     try {
         self::BeginTransaction(__METHOD__);
         $options = array('hostids' => zbx_objectValues($data['hosts'], 'hostid'), 'editable' => 1, 'preservekeys' => 1);
         $upd_hosts = self::get($options);
         foreach ($data['hosts'] as $hnum => $host) {
             if (!isset($upd_hosts[$host['hostid']])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, 'You do not have enough rights for operation');
             }
         }
         if (isset($data['groups']) && !empty($data['groups'])) {
             $options = array('groups' => zbx_toArray($data['groups']), 'hosts' => $data['hosts']);
             $result = CHostGroup::massAdd($options);
             if (!$result) {
                 self::exception();
             }
         }
         if (isset($data['templates']) && !empty($data['templates'])) {
             $options = array('hosts' => $data['hosts'], 'templates' => zbx_toArray($data['templates']));
             $result = CTemplate::massAdd($options);
             if (!$result) {
                 self::exception();
             }
         }
         if (isset($data['macros']) && !empty($data['macros'])) {
             $options = array('hosts' => $data['hosts'], 'macros' => $data['macros']);
             $result = CUserMacro::massAdd($options);
             if (!$result) {
                 self::exception();
             }
         }
         self::EndTransaction(true, __METHOD__);
         return array('hostids' => zbx_objectValues($data['hosts'], 'hostid'));
     } catch (APIException $e) {
         self::EndTransaction(false, __METHOD__);
         $error = $e->getErrors();
         $error = reset($error);
         self::setError(__METHOD__, $e->getCode(), $error);
         return false;
     }
 }
 /**
  * Link Template to Hosts
  *
  * @param array $data
  * @param string $data['templates']
  * @param string $data['hosts']
  * @param string $data['groups']
  * @param string $data['templates_link']
  * @return boolean
  */
 public static function massAdd($data)
 {
     $templates = isset($data['templates']) ? zbx_toArray($data['templates']) : null;
     $templateids = is_null($templates) ? array() : zbx_objectValues($templates, 'templateid');
     try {
         self::BeginTransaction(__METHOD__);
         $upd_templates = self::get(array('templateids' => $templateids, 'editable' => 1, 'preservekeys' => 1));
         foreach ($templates as $tnum => $template) {
             if (!isset($upd_templates[$template['templateid']])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSION);
             }
         }
         if (isset($data['groups']) && !empty($data['groups'])) {
             $options = array('groups' => $data['groups'], 'templates' => $templates);
             $result = CHostGroup::massAdd($options);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, 'Can\'t link groups');
             }
         }
         if (isset($data['hosts']) && !empty($data['hosts'])) {
             $hostids = zbx_objectValues($data['hosts'], 'hostid');
             self::link($templateids, $hostids);
         }
         if (isset($data['templates_link']) && !empty($data['templates_link'])) {
             $templates_linkids = zbx_objectValues($data['templates_link'], 'templateid');
             self::link($templates_linkids, $templateids);
         }
         if (isset($data['macros']) && !empty($data['macros'])) {
             $options = array('templates' => zbx_toArray($data['templates']), 'macros' => $data['macros']);
             $result = CUserMacro::massAdd($options);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, 'Can\'t link macros');
             }
         }
         $result = self::EndTransaction(true, __METHOD__);
         return array('templateids' => zbx_objectValues($data['templates'], 'templateid'));
     } catch (APIException $e) {
         self::EndTransaction(false, __METHOD__);
         $error = $e->getErrors();
         $error = reset($error);
         self::setError(__METHOD__, $e->getCode(), $error);
         return false;
     }
 }