Source
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
static void retrieve_hostname(char *buffer, int len, char **error)
{
if (SUCCEED != gethostname(buffer, len))
{
zabbix_log(LOG_LEVEL_ERR, "gethostname() failed: %s", zbx_strerror_from_system(WSAGetLastError()));
*error = zbx_dsprintf(NULL, "Cannot obtain host name: %s", zbx_strerror_from_system(WSAGetLastError()));
}
}
int system_hostname(AGENT_REQUEST *request, AGENT_RESULT *result)
{
DWORD dwSize = 256;
wchar_t computerName[256];
char *type, *transform, buffer[256], *name, *error = NULL;
if (2 < request->nparam)
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters."));
return SYSINFO_RET_FAIL;
}
type = get_rparam(request, 0);
transform = get_rparam(request, 1);
if (NULL == type || '\0' == *type || 0 == strcmp(type, "netbios"))
{
/* Buffer size is chosen large enough to contain any DNS name, not just */
/* MAX_COMPUTERNAME_LENGTH + 1 characters. MAX_COMPUTERNAME_LENGTH is usually less than 32, */
/* but it varies among systems, so we cannot use the constant in a precompiled Windows agent,*/
/* which is expected to work on any system. */
if (0 == GetComputerName(computerName, &dwSize))
{
zabbix_log(LOG_LEVEL_ERR, "GetComputerName() failed: %s",
zbx_strerror_from_system(GetLastError()));
SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain computer name: %s",
zbx_strerror_from_system(GetLastError())));
return SYSINFO_RET_FAIL;
}
name = zbx_unicode_to_utf8(computerName);
}