#include "zbxsysinfo.h"
#include "../sysinfo.h"
#include "zbxlog.h"
#include "zbxstr.h"
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"))
{
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);
}
else
{
if (0 == strcmp(type, "shorthost"))
{
retrieve_hostname(buffer, sizeof(buffer), &error);
if (NULL != error)
{
SET_MSG_RESULT(result, error);
return SYSINFO_RET_FAIL;
}
char *dot;
if (NULL != (dot = strchr(buffer, '.')))
*dot = '\0';
name = zbx_strdup(NULL, buffer);
}
else if (0 == strcmp(type, "host"))
{
retrieve_hostname(buffer, sizeof(buffer), &error);
if (NULL != error)
{
SET_MSG_RESULT(result, error);
return SYSINFO_RET_FAIL;
}
name = zbx_strdup(NULL, buffer);