Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debugging core issue #880

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/docs/reference/windows/CheckSystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ CPU Load ok|'total 5m'=16%;80;90 'total 1m'=13%;80;90 'total 5s'=13%;80;90
<a name="check_cpu_show-default"/>
<a name="check_cpu_help-short"/>
<a name="check_cpu_time"/>
<a name="check_cpu_cores"/>
<a name="check_cpu_options"/>
#### Command-line Arguments

Expand Down Expand Up @@ -131,6 +132,7 @@ CPU Load ok|'total 5m'=16%;80;90 'total 1m'=13%;80;90 'total 5s'=13%;80;90
| [detail-syntax](#check_cpu_detail-syntax) | ${time}: ${load}% | Detail level syntax. |
| [perf-syntax](#check_cpu_perf-syntax) | ${core} ${time} | Performance alias syntax. |
| time | | The time to check |
| cores | N/A | This will remove the filter to include the cores, if you use filter dont use this as well. |



Expand Down
42 changes: 31 additions & 11 deletions include/win_sysinfo/win_sysinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@
#include <win_sysinfo/win_defines.hpp>
#include <win_sysinfo/win_sysinfo.hpp>

#include <nscapi/nscapi_helper_singleton.hpp>
#include <nscapi/macros.hpp>
#include "str/xtos.hpp"

#include <boost/scoped_array.hpp>
#include <buffer.hpp>
#include <error/error.hpp>
#include <nsclient/nsclient_exception.hpp>
#include <buffer.hpp>
#include <utf8.hpp>

namespace windows {
Expand All @@ -46,6 +50,7 @@ namespace windows {
typedef DWORD(WINAPI *tGetProcessImageFileName)(HANDLE hProcess, LPWSTR lpImageFileName, DWORD nSize);
typedef LONG(NTAPI *tNtQuerySystemInformation)(SYSTEM_INFORMATION_CLASS SystemInformationClass, PVOID SystemInformation, ULONG SystemInformationLength, PULONG ReturnLength);
typedef DWORD(WINAPI *tWTSGetActiveConsoleSessionId)();
typedef LONG(NTAPI *tGetNativeSystemInfo)(LPSYSTEM_INFO lpSystemInfo);

typedef BOOL(*tWTSQueryUserToken)(ULONG SessionId,PHANDLE phToken);

Expand All @@ -58,6 +63,7 @@ namespace windows {
tIsWow64Process pIsWow64Process = NULL;
tGetProcessImageFileName pGetProcessImageFileName = NULL;
tNtQuerySystemInformation pNtQuerySystemInformation = NULL;
tGetNativeSystemInfo pGetNativeSystemInfo = NULL;
tWTSQueryUserToken pWTSQueryUserToken = NULL;
tWTSGetActiveConsoleSessionId pWTSGetActiveConsoleSessionId = NULL;

Expand Down Expand Up @@ -160,9 +166,17 @@ namespace windows {
throw nsclient::nsclient_exception("Failed to load: NtQuerySystemInformation: " + error::lookup::last_error());
return pNtQuerySystemInformation(SystemInformationClass, SystemInformation, SystemInformationLength, ReturnLength);
}

bool GetNativeSystemInfo(LPSYSTEM_INFO lpSystemInfo) {
if (pGetNativeSystemInfo == NULL)
pGetNativeSystemInfo = reinterpret_cast<tGetNativeSystemInfo>(GetProcAddress(LoadLibrary(L"Kernel32"), "GetNativeSystemInfo"));
if (pGetNativeSystemInfo == NULL)
return false;
pGetNativeSystemInfo(lpSystemInfo);
return true;
}
}

winapi::SYSTEM_BASIC_INFORMATION g_systemBasicInformation;
unsigned long g_windowsVersion;
// ACCESS_MASK ProcessQueryAccess;
// ACCESS_MASK ProcessAllAccess;
Expand All @@ -171,9 +185,6 @@ namespace windows {
// ACCESS_MASK ThreadAllAccess;
//RTL_OSVERSIONINFOEXW g_versionInfo;
RTL_OSVERSIONINFOEXW g_versionInfo;
void QuerySystemInformation() {
winapi::NtQuerySystemInformation(winapi::SystemBasicInformation, &g_systemBasicInformation, sizeof(winapi::SYSTEM_BASIC_INFORMATION), NULL);
}

// RTL_OSVERSIONINFOEXW is defined in winnt.h
bool GetOsVersion(RTL_OSVERSIONINFOEXW* pk_OsVer) {
Expand Down Expand Up @@ -238,7 +249,8 @@ namespace windows {
}

bool g_hasVersion = false;
bool g_hasBasicInfo = false;
unsigned long numberOfCores = 0;


boost::scoped_array<unsigned long long> g_CPUIdleTimeOld;
boost::scoped_array<unsigned long long> g_CPUTotalTimeOld;
Expand Down Expand Up @@ -352,12 +364,20 @@ namespace windows {
}


long system_info::get_numberOfProcessorscores() {
if (!g_hasBasicInfo) {
QuerySystemInformation();
g_hasBasicInfo = true;
unsigned long system_info::get_numberOfProcessorscores() {
if (numberOfCores == 0) {
SYSTEM_INFO si;
if (winapi::GetNativeSystemInfo(&si)) {
numberOfCores = si.dwNumberOfProcessors;
NSC_LOG_MESSAGE("Number of cores detected (new) as " + str::xtos(numberOfCores));
return numberOfCores;
}
winapi::SYSTEM_BASIC_INFORMATION systemBasicInformation;
winapi::NtQuerySystemInformation(winapi::SystemBasicInformation, &systemBasicInformation, sizeof(winapi::SYSTEM_BASIC_INFORMATION), NULL);
numberOfCores = static_cast<unsigned char>(systemBasicInformation.NumberOfProcessors);
NSC_LOG_MESSAGE("Number of cores detected (old) as " + str::xtos(numberOfCores));
}
return g_systemBasicInformation.NumberOfProcessors;
return numberOfCores;
}

hlp::buffer<BYTE, winapi::SYSTEM_PROCESS_INFORMATION*> system_info::get_system_process_information(int size) {
Expand Down
4 changes: 3 additions & 1 deletion include/win_sysinfo/win_sysinfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ namespace windows {
static std::string get_version_string();
static unsigned long get_version();
static OSVERSIONINFOEX* get_versioninfo();
static long get_numberOfProcessorscores();
static unsigned long get_numberOfProcessorscores();
static std::vector<std::string> get_suite_list();
static long long get_suite_i();

Expand All @@ -136,6 +136,8 @@ namespace windows {
bool IsWow64(HANDLE hProcess, bool def = false);
DWORD GetProcessImageFileName(HANDLE hProcess, LPWSTR lpImageFileName, DWORD nSize);
LONG NtQueryInformationProcess(HANDLE ProcessHandle, DWORD ProcessInformationClass, PVOID ProcessInformation, DWORD ProcessInformationLength, PDWORD ReturnLength);
bool GetNativeSystemInfo(LPSYSTEM_INFO lpSystemInfo);


INT VDMEnumTaskWOWEx(DWORD dwProcessId, tTASKENUMPROCEX fp, LPARAM lparam);
}
Expand Down
11 changes: 9 additions & 2 deletions modules/CheckSystem/CheckSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,17 +559,24 @@ void CheckSystem::check_cpu(const PB::Commands::QueryRequestMessage::Request &re
modern_filter::data_container data;
modern_filter::cli_helper<filter_type> filter_helper(request, response, data);
std::vector<std::string> times;
bool show_all_cores = false;

filter_type filter;
filter_helper.add_options("load > 80", "load > 90", "core = 'total'", filter.get_filter_syntax(), "ignored");
filter_helper.add_syntax("${status}: ${problem_list}", "${time}: ${load}%", "${core} ${time}", "", "%(status): CPU load is ok.");
filter_helper.add_syntax("${status}: ${problem_list}", "${time}: ${load}%", "${core} ${time}", "", "%(status): CPU load is ok.");
filter_helper.get_desc().add_options()
("time", po::value<std::vector<std::string>>(&times), "The time to check")
("time", po::value<std::vector<std::string>>(&times), "The time to check")
("cores", boost::program_options::bool_switch(&show_all_cores),
"This will remove the filter to include the cores, if you use filter dont use this as well.")
;

if (!filter_helper.parse_options())
return;

if (show_all_cores && filter_helper.data.filter_string.size() == 1) {
filter_helper.data.filter_string.clear();
}

if (times.empty()) {
times.push_back("5m");
times.push_back("1m");
Expand Down