/****************************************************************************** * * * Function: select_discovered_host * * * * Purpose: select hostid of discovered host * * * * Parameters: dhostid - discovered host id * * * * Return value: hostid - existing hostid, 0 - if not found * * * * Author: Alexei Vladishev * * * ******************************************************************************/ static zbx_uint64_t select_discovered_host(const DB_EVENT *event) { const char *__function_name = "select_discovered_host"; DB_RESULT result; DB_ROW row; zbx_uint64_t hostid = 0; char *sql = NULL; zabbix_log(LOG_LEVEL_DEBUG, "In %s() eventid:" ZBX_FS_UI64, __function_name, event->eventid); switch (event->object) { case EVENT_OBJECT_DHOST: sql = zbx_dsprintf(sql, "select h.hostid" " from hosts h,interface i,dservices ds,dchecks dc,drules dr" " where h.hostid=i.hostid" " and i.ip=ds.ip" " and ds.dcheckid=dc.dcheckid" " and dc.druleid=dr.druleid" " and " ZBX_SQL_NULLCMP("dr.proxy_hostid", "h.proxy_hostid") " and i.useip=1" " and ds.dhostid=" ZBX_FS_UI64 ZBX_SQL_NODE " order by i.hostid", event->objectid, DBand_node_local("i.interfaceid")); break; case EVENT_OBJECT_DSERVICE: sql = zbx_dsprintf(sql, "select h.hostid" " from hosts h,interface i,dservices ds,dchecks dc,drules dr" " where h.hostid=i.hostid" " and i.ip=ds.ip" " and ds.dcheckid=dc.dcheckid" " and dc.druleid=dr.druleid" " and " ZBX_SQL_NULLCMP("dr.proxy_hostid", "h.proxy_hostid") " and i.useip=1" " and ds.dserviceid =" ZBX_FS_UI64 ZBX_SQL_NODE " order by i.hostid", event->objectid, DBand_node_local("i.interfaceid")); break; default: goto exit; } result = DBselectN(sql, 1); zbx_free(sql); if (NULL != (row = DBfetch(result))) ZBX_STR2UINT64(hostid, row[0]); DBfree_result(result); exit: zabbix_log(LOG_LEVEL_DEBUG, "End of %s():" ZBX_FS_UI64, __function_name, hostid); return hostid; }
static int get_dynamic_hostid(DB_EVENT *event, DC_HOST *host, char *error, size_t max_error_len) { const char *__function_name = "get_dynamic_hostid"; DB_RESULT result; DB_ROW row; char sql[512]; size_t offset; int ret = SUCCEED; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); offset = zbx_snprintf(sql, sizeof(sql), "select distinct h.hostid,h.host"); #ifdef HAVE_OPENIPMI offset += zbx_snprintf(sql + offset, sizeof(sql) - offset, ",h.ipmi_authtype,h.ipmi_privilege,h.ipmi_username,h.ipmi_password"); #endif switch (event->source) { case EVENT_SOURCE_TRIGGERS: zbx_snprintf(sql + offset, sizeof(sql) - offset, " from functions f,items i,hosts h" " where f.itemid=i.itemid" " and i.hostid=h.hostid" " and h.status=%d" " and f.triggerid=" ZBX_FS_UI64, HOST_STATUS_MONITORED, event->objectid); break; case EVENT_SOURCE_DISCOVERY: offset += zbx_snprintf(sql + offset, sizeof(sql) - offset, " from hosts h,interface i,dservices ds" " where h.hostid=i.hostid" " and i.ip=ds.ip" " and i.useip=1" " and h.status=%d", HOST_STATUS_MONITORED); switch (event->object) { case EVENT_OBJECT_DHOST: zbx_snprintf(sql + offset, sizeof(sql) - offset, " and ds.dhostid=" ZBX_FS_UI64 DB_NODE, event->objectid, DBnode_local("h.hostid")); break; case EVENT_OBJECT_DSERVICE: zbx_snprintf(sql + offset, sizeof(sql) - offset, " and ds.dserviceid=" ZBX_FS_UI64 DB_NODE, event->objectid, DBnode_local("h.hostid")); break; } break; case EVENT_SOURCE_AUTO_REGISTRATION: zbx_snprintf(sql + offset, sizeof(sql) - offset, " from autoreg_host a,hosts h" " where " ZBX_SQL_NULLCMP("a.proxy_hostid", "h.proxy_hostid") " and a.host=h.host" " and h.status=%d" " and a.autoreg_hostid=" ZBX_FS_UI64 DB_NODE, HOST_STATUS_MONITORED, event->objectid, DBnode_local("h.hostid")); break; default: zbx_snprintf(error, max_error_len, "Unsupported event source [%d]", event->source); return FAIL; } host->hostid = 0; result = DBselect("%s", sql); while (NULL != (row = DBfetch(result))) { if (0 != host->hostid) { switch (event->source) { case EVENT_SOURCE_TRIGGERS: zbx_strlcpy(error, "Too many hosts in a trigger expression", max_error_len); break; case EVENT_SOURCE_DISCOVERY: zbx_strlcpy(error, "Too many hosts with same IP addresses", max_error_len); break; } ret = FAIL; break; } ZBX_STR2UINT64(host->hostid, row[0]); strscpy(host->host, row[1]); #ifdef HAVE_OPENIPMI host->ipmi_authtype = (signed char)atoi(row[2]); host->ipmi_privilege = (unsigned char)atoi(row[3]); strscpy(host->ipmi_username, row[4]); strscpy(host->ipmi_password, row[5]); #endif } DBfree_result(result); if (FAIL == ret) { host->hostid = 0; *host->host = '\0'; } else if (0 == host->hostid) { *host->host = '\0'; zbx_strlcpy(error, "Cannot find a corresponding host", max_error_len); ret = FAIL; } zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }