/** * Add Host * * @param _array $hosts multidimensional array with Hosts data * @param string $hosts['host'] Host name. * @param array $hosts['groups'] array of HostGroup objects with IDs add Host to. * @param int $hosts['port'] Port. OPTIONAL * @param int $hosts['status'] Host Status. OPTIONAL * @param int $hosts['useip'] Use IP. OPTIONAL * @param string $hosts['dns'] DNS. OPTIONAL * @param string $hosts['ip'] IP. OPTIONAL * @param int $hosts['proxy_hostid'] Proxy Host ID. OPTIONAL * @param int $hosts['useipmi'] Use IPMI. OPTIONAL * @param string $hosts['ipmi_ip'] IPMAI IP. OPTIONAL * @param int $hosts['ipmi_port'] IPMI port. OPTIONAL * @param int $hosts['ipmi_authtype'] IPMI authentication type. OPTIONAL * @param int $hosts['ipmi_privilege'] IPMI privilege. OPTIONAL * @param string $hosts['ipmi_username'] IPMI username. OPTIONAL * @param string $hosts['ipmi_password'] IPMI password. OPTIONAL * @return boolean */ public static function create($hosts) { $hosts = zbx_toArray($hosts); $hostids = array(); $groupids = array(); try { self::BeginTransaction(__METHOD__); // BASIC VALIDATION {{{ foreach ($hosts as $hnum => $host) { // CHECK IF HOSTS HAVE AT LEAST 1 GROUP if (empty($host['groups'])) { self::exception(ZBX_API_ERROR_PARAMETERS, sprintf(S_NO_GROUPS_FOR_HOST, $host['host'])); } // Check if host name isn't longer then 64 chars if (zbx_strlen($host['host']) > 64) { self::exception(ZBX_API_ERROR_PARAMETERS, sprintf(S_HOST_NAME_MUST_BE_LONGER, 64, $host['host'], zbx_strlen($host['host']))); } $hosts[$hnum]['groups'] = zbx_toArray($hosts[$hnum]['groups']); foreach ($hosts[$hnum]['groups'] as $gnum => $group) { $groupids[$group['groupid']] = $group['groupid']; } } // }}} // PERMISSIONS {{{ $upd_groups = CHostGroup::get(array('groupids' => $groupids, 'editable' => 1, 'preservekeys' => 1)); foreach ($groupids as $gnum => $groupid) { if (!isset($upd_groups[$groupid])) { self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSIONS); } } // }}} PERMISSIONS foreach ($hosts as $num => $host) { $host_db_fields = array('host' => null, 'port' => 0, 'status' => 0, 'useip' => 0, 'dns' => '', 'ip' => '0.0.0.0', 'proxy_hostid' => 0, 'useipmi' => 0, 'ipmi_ip' => '', 'ipmi_port' => 623, 'ipmi_authtype' => 0, 'ipmi_privilege' => 0, 'ipmi_username' => '', 'ipmi_password' => ''); if (!check_db_fields($host_db_fields, $host)) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Wrong fields for host [ ' . $host['host'] . ' ]'); } if (!preg_match('/^' . ZBX_PREG_HOST_FORMAT . '$/i', $host['host'])) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Incorrect characters used for Hostname [ ' . $host['host'] . ' ]'); } if (!empty($host['dns']) && !preg_match('/^' . ZBX_PREG_DNS_FORMAT . '$/i', $host['dns'])) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Incorrect characters used for DNS [ ' . $host['dns'] . ' ]'); } if (self::exists(array('host' => $host['host']))) { self::exception(ZBX_API_ERROR_PARAMETERS, S_HOST . ' [ ' . $host['host'] . ' ] ' . S_ALREADY_EXISTS_SMALL); } if (CTemplate::exists(array('host' => $host['host']))) { self::exception(ZBX_API_ERROR_PARAMETERS, S_TEMPLATE . ' [ ' . $host['host'] . ' ] ' . S_ALREADY_EXISTS_SMALL); } $hostid = get_dbid('hosts', 'hostid'); $hostids[] = $hostid; $result = DBexecute('INSERT INTO hosts (hostid, proxy_hostid, host, port, status, useip, dns, ip, disable_until, available,' . 'useipmi,ipmi_port,ipmi_authtype,ipmi_privilege,ipmi_username,ipmi_password,ipmi_ip) VALUES (' . $hostid . ',' . $host['proxy_hostid'] . ',' . zbx_dbstr($host['host']) . ',' . $host['port'] . ',' . $host['status'] . ',' . $host['useip'] . ',' . zbx_dbstr($host['dns']) . ',' . zbx_dbstr($host['ip']) . ',0,' . HOST_AVAILABLE_UNKNOWN . ',' . $host['useipmi'] . ',' . $host['ipmi_port'] . ',' . $host['ipmi_authtype'] . ',' . $host['ipmi_privilege'] . ',' . zbx_dbstr($host['ipmi_username']) . ',' . zbx_dbstr($host['ipmi_password']) . ',' . zbx_dbstr($host['ipmi_ip']) . ')'); if (!$result) { self::exception(ZBX_API_ERROR_PARAMETERS, 'DBerror'); } foreach ($host['groups'] as $group) { $hostgroupid = get_dbid('hosts_groups', 'hostgroupid'); $result = DBexecute("INSERT INTO hosts_groups (hostgroupid, hostid, groupid) VALUES ({$hostgroupid}, {$hostid}, {$group['groupid']})"); if (!$result) { self::exception(ZBX_API_ERROR_PARAMETERS, 'DBerror'); } } $host['hostid'] = $hostid; $options = array(); $options['hosts'] = $host; if (isset($host['templates']) && !is_null($host['templates'])) { $options['templates'] = $host['templates']; } if (isset($host['macros']) && !is_null($host['macros'])) { $options['macros'] = $host['macros']; } $result = CHost::massAdd($options); if (!$result) { self::exception(); } if (isset($host['profile']) && !empty($host['extendedProfile'])) { $fields = array_keys($host['profile']); $fields = implode(', ', $fields); $values = array_map('zbx_dbstr', $host['profile']); $values = implode(', ', $values); DBexecute('INSERT INTO hosts_profiles (hostid, ' . $fields . ') VALUES (' . $hostid . ', ' . $values . ')'); } if (isset($host['extendedProfile']) && !empty($host['extendedProfile'])) { $fields = array_keys($host['extendedProfile']); $fields = implode(', ', $fields); $values = array_map('zbx_dbstr', $host['extendedProfile']); $values = implode(', ', $values); DBexecute('INSERT INTO hosts_profiles_ext (hostid, ' . $fields . ') VALUES (' . $hostid . ', ' . $values . ')'); } } self::EndTransaction(true, __METHOD__); return array('hostids' => $hostids); } catch (APIException $e) { self::EndTransaction(false, __METHOD__); $error = $e->getErrors(); $error = reset($error); self::setError(__METHOD__, $e->getCode(), $error); return false; } }
$hosts['templates'] = $templates; } $result = CHost::massUpdate(array_merge($hosts, $new_values)); if ($result === false) { throw new Exception(); } $add = array(); if (!empty($templates) && isset($visible['template_table'])) { $add['templates'] = $templates; } if (!empty($newgroup) && !isset($visible['groups'])) { $add['groups'][] = $newgroup; } if (!empty($add)) { $add['hosts'] = $hosts['hosts']; $result = CHost::massAdd($add); if ($result === false) { throw new Exception(); } } DBend(true); show_messages(true, S_HOSTS . SPACE . S_UPDATED, S_CANNOT_UPDATE . SPACE . S_HOSTS); unset($_REQUEST['massupdate']); unset($_REQUEST['form']); unset($_REQUEST['hosts']); $url = new CUrl(); $path = $url->getPath(); insert_js('cookie.eraseArray("' . $path . '")'); } catch (Exception $e) { DBend(false); show_messages(false, S_HOSTS . SPACE . S_UPDATED, S_CANNOT_UPDATE . SPACE . S_HOSTS);