--- operations.c.orig 2007-08-20 12:22:22.000000000 -0700 +++ operations.c 2008-01-07 15:34:51.000000000 -0800 @@ -440,6 +440,49 @@ /****************************************************************************** * * + * Function: find uname value for hostname if system.uname service exists * + * * + * Purpose: work around broken reverse DNS and/or multihoming * + * * + * Parameters: dhostid - discovered host id * + * host - output buffer for hostname * + * hostlen - size of output buffer in bytes * + * * + * Return value: none * + * * + * Author: Lamont Granquist * + * * + * Comments: assumes uname return is single-space delimited with the second * + * value equal to the hostname * + * * + ******************************************************************************/ +void try_gethost_from_uname(zbx_uint64_t dhostid, char *host, size_t hostlen) +{ + DB_RESULT result; + DB_ROW row; + char *uname; + char *hostname; + char *cp; + + host[0] = '\0'; + result = DBselect("select value from dhosts join dservices on dhosts.dhostid = dservices.dhostid where dservices.key_='system.uname' and dservices.dhostid=" ZBX_FS_UI64, + dhostid); + row = DBfetch(result); + if(row && DBis_null(row[0]) != SUCCEED) + { + uname = row[0]; + hostname = strchr(uname, ' '); + if (!hostname) return; + hostname++; + cp = strchr(hostname, ' '); + if (!cp) return; + *cp = '\0'; + zbx_strlcpy(host, hostname, hostlen); + } +} + +/****************************************************************************** + * * * Function: add host if not added already * * * * Purpose: add discovered host * @@ -462,6 +505,7 @@ zbx_uint64_t hostid = 0; char *ip; struct hostent* host; + char host_str[MAX_STRING_LEN]; char host_esc[MAX_STRING_LEN]; zabbix_log(LOG_LEVEL_DEBUG, "In add_discovered_host(dhostid:" ZBX_FS_UI64 ")", @@ -474,17 +518,26 @@ { ip=row[0]; - alarm(CONFIG_TIMEOUT); - host = zbx_gethost_by_ip(ip); - alarm(0); + try_gethost_from_uname(dhostid, host_str, sizeof(host_str)); - if(host != NULL) + if (host_str[0] != '\0') { - DBescape_string(host->h_name, host_esc, sizeof(host_esc)); + DBescape_string(host_str, host_esc, sizeof(host_esc)); + zabbix_log(LOG_LEVEL_INFORMATION, "Adding server %s resolved from uname", host_esc); } - else + else { - host_esc[0]='\0'; + alarm(CONFIG_TIMEOUT); + host = zbx_gethost_by_ip(ip); + alarm(0); + if(host != NULL) + { + DBescape_string(host->h_name, host_esc, sizeof(host_esc)); + } + else + { + host_esc[0]='\0'; + } } result2 = DBselect("select hostid from hosts where ip='%s' and " ZBX_COND_NODEID,