diff --git a/src/libs/zbxsysinfo/linux/hostname.c b/src/libs/zbxsysinfo/linux/hostname.c index c0ccec5..b06a590 100644 --- a/src/libs/zbxsysinfo/linux/hostname.c +++ b/src/libs/zbxsysinfo/linux/hostname.c @@ -18,6 +18,7 @@ **/ #include "sysinfo.h" +#include "log.h" #ifdef HAVE_SYS_UTSNAME_H # include @@ -25,16 +26,62 @@ ZBX_METRIC parameter_hostname = /* KEY FLAG FUNCTION TEST PARAMETERS */ - {"system.hostname", 0, SYSTEM_HOSTNAME, NULL}; + {"system.hostname", CF_HAVEPARAMS, SYSTEM_HOSTNAME, NULL}; int SYSTEM_HOSTNAME(AGENT_REQUEST *request, AGENT_RESULT *result) { - struct utsname name; + int ret = SYSINFO_RET_FAIL; // Request result code + const char *__function_name = "SYSTEM_HOSTNAME"; // Function name for log file + char *param; // Request parameter + char buffer[MAX_STRING_LEN]; // Parser buffer + char *c; // Buffer cursor + struct utsname name; // Hostname structure + + zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); - if (-1 == uname(&name)) - return SYSINFO_RET_FAIL; - - SET_STR_RESULT(result, zbx_strdup(NULL, name.nodename)); - - return SYSINFO_RET_OK; + // Validate parameter count + if (1 < request->nparam) { + zabbix_log(LOG_LEVEL_ERR, "Too many parameters in %s", __function_name); + goto clean; + } + + // Get hostname + if (-1 == uname(&name)) { + zabbix_log(LOG_LEVEL_ERR, "Failed to retrieve hostname in %s", __function_name); + goto clean; + } + + zbx_strlcpy(buffer, name.nodename, MAX_STRING_LEN); + + // Parse hostname + param = get_rparam(request, 0); + if(NULL != param) { + if (0 == strncmp(param, "uc", 3)) { + for(c = &buffer[0]; *c; ++c) { + *c = toupper(*c); + } + } + + else if (0 == strncmp(param, "lc", 3)) { + for(c = &buffer[0]; *c; ++c) { + *c = tolower(*c); + } + } + + else { + zabbix_log(LOG_LEVEL_ERR, "Unknown character casing '%s' (use 'lc' or 'uc') in %s", param, __function_name); + } + } + + // Success + SET_STR_RESULT(result, zbx_strdup(NULL, buffer)); + ret = SYSINFO_RET_OK; + +clean: + /* + * Free allocated memory and handles + */ + zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); + + return ret; } diff --git a/src/libs/zbxsysinfo/win32/hostname.c b/src/libs/zbxsysinfo/win32/hostname.c index 70fa078..c1bfc6d 100644 --- a/src/libs/zbxsysinfo/win32/hostname.c +++ b/src/libs/zbxsysinfo/win32/hostname.c @@ -28,14 +28,15 @@ int SYSTEM_HOSTNAME(AGENT_REQUEST *request, AGENT_RESULT *result) { DWORD dwSize = 256; TCHAR computerName[256]; - char *type, buffer[256]; + char *type, *casing, *c, buffer[256]; int netbios, ret; WSADATA sockInfo; - if (1 < request->nparam) + if (2 < request->nparam) return SYSINFO_RET_FAIL; type = get_rparam(request, 0); + casing = get_rparam(request, 1); if (NULL == type || '\0' == *type || 0 == strcmp(type, "netbios")) netbios = 1; @@ -63,6 +64,22 @@ int SYSTEM_HOSTNAME(AGENT_REQUEST *request, AGENT_RESULT *result) else SET_STR_RESULT(result, zbx_strdup(NULL, buffer)); } + + if(NULL != casing && ISSET_STR(result)) { + if(0 == strncmp(casing, "uc", 2)) { + for(c = result->str; *c; ++c) { + *c = toupper(*c); + } + } + else if(0 == strncmp(casing, "lc", 2)) { + for(c = result->str; *c; ++c) { + *c = tolower(*c); + } + } + else { + return SYSINFO_RET_FAIL; + } + } if (ISSET_STR(result)) return SYSINFO_RET_OK;