/* * returns interface description encoded in UTF-8 format */ static char *get_if_description(MIB_IFROW *pIfRow) { static wchar_t *(*mb_to_unicode)(const char *) = NULL; wchar_t *wdescr; char *utf8_descr; if (NULL == mb_to_unicode) { const OSVERSIONINFOEX *vi; /* starting with Windows Vista (Windows Server 2008) the interface description */ /* is encoded in OEM codepage while earlier versions used ANSI codepage */ if (NULL != (vi = zbx_win_getversion()) && 6 <= vi->dwMajorVersion) mb_to_unicode = zbx_oemcp_to_unicode; else mb_to_unicode = zbx_acp_to_unicode; } wdescr = mb_to_unicode(pIfRow->bDescr); utf8_descr = zbx_unicode_to_utf8(wdescr); zbx_free(wdescr); return utf8_descr; }
int SYSTEM_UNAME(AGENT_REQUEST *request, AGENT_RESULT *result) { typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO); DWORD dwSize = 256; TCHAR computer_name[256]; SYSTEM_INFO si; const OSVERSIONINFOEX *vi; char *os = NULL, *utf8; size_t os_alloc = 256, os_offset = 0; PGNSI pGNSI; /* 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(computer_name, &dwSize)) *computer_name = '\0'; memset(&si, 0, sizeof(si)); if (NULL == (vi = zbx_win_getversion())) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot retrieve system version.")); return SYSINFO_RET_FAIL; } if (NULL != (pGNSI = (PGNSI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo"))) pGNSI(&si); else GetSystemInfo(&si); os = zbx_malloc(os, os_alloc); zbx_strcpy_alloc(&os, &os_alloc, &os_offset, "Windows"); if ('\0' != *computer_name) { utf8 = zbx_unicode_to_utf8(computer_name); zbx_snprintf_alloc(&os, &os_alloc, &os_offset, " %s", utf8); zbx_free(utf8); } zbx_snprintf_alloc(&os, &os_alloc, &os_offset, " %d.%d.%d", vi->dwMajorVersion, vi->dwMinorVersion, vi->dwBuildNumber); if (VER_PLATFORM_WIN32_NT == vi->dwPlatformId) { switch (vi->dwMajorVersion) { case 5: switch (vi->dwMinorVersion) { case 0: get_50_version(&os, &os_alloc, &os_offset, vi); break; case 1: get_51_version(&os, &os_alloc, &os_offset, vi); break; case 2: get_52_version(&os, &os_alloc, &os_offset, vi, &si); break; } break; case 6: get_6x_version(&os, &os_alloc, &os_offset, vi, &si); break; } } if ('\0' != *vi->szCSDVersion) { utf8 = zbx_unicode_to_utf8(vi->szCSDVersion); zbx_snprintf_alloc(&os, &os_alloc, &os_offset, " %s", utf8); zbx_free(utf8); } get_cpu_type(&os, &os_alloc, &os_offset, &si); SET_STR_RESULT(result, os); return SYSINFO_RET_OK; }
int PROC_INFO(AGENT_REQUEST *request, AGENT_RESULT *result) { HANDLE hProcessSnap, hProcess; PROCESSENTRY32 pe32; char *proc_name, *attr, *type, baseName[MAX_PATH]; const char *attrList[] = {"vmsize", "wkset", "pf", "ktime", "utime", "gdiobj", "userobj", "io_read_b", "io_read_op", "io_write_b", "io_write_op", "io_other_b", "io_other_op", NULL}, *typeList[] = {"min", "max", "avg", "sum", NULL}; double value; DWORD access; const OSVERSIONINFOEX *vi; int counter, attr_id, type_id, ret = SYSINFO_RET_OK; if (3 < request->nparam) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); return SYSINFO_RET_FAIL; } proc_name = get_rparam(request, 0); attr = get_rparam(request, 1); type = get_rparam(request, 2); if (NULL == proc_name || '\0' == *proc_name) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter.")); return SYSINFO_RET_FAIL; } /* Get attribute code from string */ if (NULL == attr || '\0' == *attr) { for (attr_id = 0; NULL != attrList[attr_id] && 0 != strcmp(attrList[attr_id], "vmsize"); attr_id++) ; } else { for (attr_id = 0; NULL != attrList[attr_id] && 0 != strcmp(attrList[attr_id], attr); attr_id++) ; } if (NULL == attrList[attr_id]) /* Unsupported attribute */ { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter.")); return SYSINFO_RET_FAIL; } /* Get type code from string */ if (NULL == type || '\0' == *type) { for (type_id = 0; NULL != typeList[type_id] && 0 != strcmp(typeList[type_id], "avg"); type_id++) ; } else { for (type_id = 0; NULL != typeList[type_id] && 0 != strcmp(typeList[type_id], type); type_id++) ; } if (NULL == typeList[type_id]) /* Unsupported type */ { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter.")); return SYSINFO_RET_FAIL; } if (INVALID_HANDLE_VALUE == (hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0))) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot obtain system information.")); return SYSINFO_RET_FAIL; } if (NULL == (vi = zbx_win_getversion())) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot retrieve system version.")); return SYSINFO_RET_FAIL; } if (6 > vi->dwMajorVersion) { /* PROCESS_QUERY_LIMITED_INFORMATION is not supported on Windows Server 2003 and XP */ access = PROCESS_QUERY_INFORMATION; } else access = PROCESS_QUERY_LIMITED_INFORMATION; pe32.dwSize = sizeof(PROCESSENTRY32); if (FALSE == Process32First(hProcessSnap, &pe32)) { CloseHandle(hProcessSnap); SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot obtain system information.")); return SYSINFO_RET_FAIL; } counter = 0; value = 0; do { zbx_unicode_to_utf8_static(pe32.szExeFile, baseName, MAX_NAME); if (0 == stricmp(baseName, proc_name)) { if (NULL != (hProcess = OpenProcess(access, FALSE, pe32.th32ProcessID))) { ret = GetProcessAttribute(hProcess, attr_id, type_id, counter++, &value); CloseHandle(hProcess); if (SYSINFO_RET_OK != ret) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot obtain process information.")); break; } } } } while (TRUE == Process32Next(hProcessSnap, &pe32)); CloseHandle(hProcessSnap); if (SYSINFO_RET_OK == ret) SET_DBL_RESULT(result, value); else SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot obtain process information.")); return ret; }