-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
[virtual tables] Build cpu_time
table for Windows
#8040
Open
yux-m
wants to merge
6
commits into
osquery:master
Choose a base branch
from
yux-m:cpu-time-table-windows
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5268ae0
[virtual tables] cpu_time table for windows
yux-m 1200b5e
Move the spec file for cpu_time table
yux-m 3b1a0bc
Modify CMakeLists for specs
yux-m 0fe421d
Fix uptime
yux-m 0d3441a
Add aggregate stats as the first row
yux-m 9eea454
Fix formatting error
yux-m File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Copyright (c) 2014-present, The osquery authors | ||
* | ||
* This source code is licensed as defined by the LICENSE file found in the | ||
* root directory of this source tree. | ||
* | ||
* SPDX-License-Identifier: (Apache-2.0 OR GPL-2.0-only) | ||
*/ | ||
|
||
#include "osquery/core/windows/wmi.h" | ||
#include <osquery/core/system.h> | ||
#include <osquery/core/tables.h> | ||
#include <osquery/logger/logger.h> | ||
#include <osquery/sql/sql.h> | ||
#include <osquery/utils/conversions/tryto.h> | ||
|
||
#include <unordered_map> | ||
|
||
namespace osquery { | ||
namespace tables { | ||
|
||
QueryData genCpuTime(QueryContext& context) { | ||
QueryData results; | ||
|
||
const Expected<WmiRequest, WmiError> wmiSystemReq_uptime = | ||
WmiRequest::CreateWmiRequest( | ||
"select ElapsedTime, Name from " | ||
"Win32_PerfFormattedData_PerfProc_Process"); | ||
if (!wmiSystemReq_uptime || wmiSystemReq_uptime->results().empty()) { | ||
LOG(WARNING) << "Error retrieving information from WMI."; | ||
return results; | ||
} | ||
|
||
const std::vector<WmiResultItem>& uptimeData = wmiSystemReq_uptime->results(); | ||
std::unordered_map<std::string, long long> uptimeMap; | ||
for (const auto& data : uptimeData) { | ||
std::string name; | ||
data.GetString("Name", name); | ||
long long temp = 0; | ||
data.GetLongLong("ElapsedTime", temp); | ||
uptimeMap[name] = temp; // in seconds | ||
} | ||
|
||
const Expected<WmiRequest, WmiError> wmiSystemReq = | ||
WmiRequest::CreateWmiRequest( | ||
"select Name, PercentUserTime, PercentPrivilegedTime, " | ||
"PercentIdleTime, PercentInterruptTime, PercentPriorityTime " | ||
"from Win32_PerfFormattedData_Counters_ProcessorInformation"); | ||
if (!wmiSystemReq || wmiSystemReq->results().empty()) { | ||
LOG(WARNING) << "Error retrieving information from WMI."; | ||
return results; | ||
} | ||
|
||
const std::vector<WmiResultItem>& wmiResults = wmiSystemReq->results(); | ||
for (const auto& data : wmiResults) { | ||
std::string name; | ||
data.GetString("Name", name); | ||
r["core"] = name; | ||
long long uptime = uptimeMap[name]; | ||
long percent = 0; | ||
data.GetLongLong("PercentUserTime", percent); | ||
// Hundredths of a second, percent / 100 * uptime * 100 | ||
r["user"] = BIGINT(percent * uptime); | ||
data.GetLongLong("PercentPrivilegedTime", percent); | ||
r["system"] = BIGINT(percent * uptime); | ||
data.GetLongLong("PercentIdleTime", percent); | ||
r["idle"] = BIGINT(percent * uptime); | ||
long idle = percent; | ||
data.GetLongLong("PercentInterruptTime", percent); | ||
r["irq"] = BIGINT(percent * uptime); | ||
// Time spent on low priority threads | ||
data.GetLongLong("PercentPriorityTime", percent); | ||
r["nice"] = BIGINT((100 - percent - idle) * uptime); | ||
results.push_back(r); | ||
} | ||
|
||
return results; | ||
} | ||
} // namespace tables | ||
} // namespace osquery |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hrm. I think this is going to be weird. We already see some issues around
uptime
not behaving as expected, and I think the lack of precision in percent is going to make this jump around.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the help! I've moved the spec file.
I'm still looking for a better source of uptime or specific times in windows, and yeah, I think the precision is a real problem - elapsed time given by wmi is in seconds as int, and the percentages are also integers. (It probably didn't make sense to use 0.01 sec as the unit.)
In case it doesn't work out, do you think it's a good idea to provide the percents instead, maybe in a new table just for windows?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you move or add a spec file (any file that contributes in the build really), you also have to modify the respective CMakeLists.txt.
In this case https://github.com/osquery/osquery/blob/master/specs/CMakeLists.txt; you would have to move the file from the platform_dependent to the independent one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the guidance!