-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Add support for scheduled queries to run at startup #8554
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
Add support for scheduled queries to run at startup #8554
Conversation
f0d188e to
cb8c10d
Compare
zwass
left a comment
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.
Approved assuming no issue with my comment. Please take a look at that.
| Config::get().scheduledQueries(([&i, &first_query_runs]( | ||
| const std::string& name, | ||
| const ScheduledQuery& query) { | ||
| bool query_has_not_run = | ||
| first_query_runs.find(name) == first_query_runs.end(); | ||
| if ((query.splayed_interval > 0 && i % query.splayed_interval == 0) || | ||
| (query.startup_priority != UINT64_MAX && query_has_not_run)) { | ||
| if (query_has_not_run) { | ||
| first_query_runs.insert(name); | ||
| } |
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.
Is name here going to be the full name prefixed by the pack name? If not, is it possible that there's a bug in which two queries with the same name in different packs cause a collision in this code?
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.
name here is the full name with the prefix as well.
osquery/osquery/config/config.cpp
Lines 481 to 488 in 9e3535a
| void Config::scheduledQueries( | |
| std::function<void(std::string name, const ScheduledQuery& query)> | |
| predicate, | |
| bool denylisted) const { | |
| RecursiveLock lock(config_schedule_mutex_); | |
| for (PackRef& pack : *schedule_) { | |
| for (auto& it : pack->getSchedule()) { | |
| std::string name = getQueryName(pack->getName(), it.first); |
osquery/osquery/config/config.cpp
Lines 472 to 479 in 9e3535a
| std::string getQueryName(const std::string& packName, const std::string& name) { | |
| // The query name may be synthetic. | |
| if (packName != "main") { | |
| return "pack" + FLAGS_pack_delimiter + packName + FLAGS_pack_delimiter + | |
| name; | |
| } | |
| return name; | |
| } |
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.
A pack structure similar to:
"packs": {
"test_pack_2": {
"queries": {
"test_startup_2": {...}
}
}
}
outputs: pack_test_pack_2_test_startup_2
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.
Okay great! Thank you :)
This PR adds a new field for scheduled queries
startup_prioritywhich if set to a non-default value will run queries in ascending order once osqueryd starts theSchedulerRunner.I've tested this a fair amount on MacOS, and I've attached time table of my tests showing when osqueryd initialized and when it executed the defined scheduled queries.
Time Table.csv
Here are some logs also showing the example change: