osquery-1/osquery/logger/logger.cpp

73 lines
2.0 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
2014-07-31 00:35:19 +00:00
#include <algorithm>
#include <thread>
#include <osquery/flags.h>
#include <osquery/logger.h>
2014-07-31 00:35:19 +00:00
2014-08-15 07:25:30 +00:00
namespace osquery {
2014-07-31 00:35:19 +00:00
/// `logger` defines the default log receiver plugin name.
DEFINE_osquery_flag(string,
logger_plugin,
"filesystem",
"The default logger plugin");
2014-07-31 00:35:19 +00:00
DEFINE_osquery_flag(bool,
log_result_events,
true,
"Log scheduled results as events");
2014-10-24 22:02:27 +00:00
2015-01-30 18:44:25 +00:00
Status LoggerPlugin::call(const PluginRequest& request,
PluginResponse& response) {
if (request.count("string") == 0) {
return Status(1, "Logger plugins only support a request string");
}
this->logString(request.at("string"));
return Status(0, "OK");
}
2014-07-31 00:35:19 +00:00
Status logString(const std::string& s) {
return logString(s, FLAGS_logger_plugin);
2014-07-31 00:35:19 +00:00
}
Status logString(const std::string& s, const std::string& receiver) {
if (!Registry::exists("logger", receiver)) {
2014-07-31 00:35:19 +00:00
LOG(ERROR) << "Logger receiver " << receiver << " not found";
return Status(1, "Logger receiver not found");
}
2015-01-30 18:44:25 +00:00
auto status = Registry::call("logger", receiver, {{"string", s}});
2014-07-31 00:35:19 +00:00
return Status(0, "OK");
}
2014-09-21 21:29:28 +00:00
Status logScheduledQueryLogItem(const osquery::ScheduledQueryLogItem& results) {
return logScheduledQueryLogItem(results, FLAGS_logger_plugin);
2014-07-31 00:35:19 +00:00
}
2014-09-21 21:29:28 +00:00
Status logScheduledQueryLogItem(const osquery::ScheduledQueryLogItem& results,
const std::string& receiver) {
2014-07-31 00:35:19 +00:00
std::string json;
2014-10-24 22:02:27 +00:00
Status status;
if (FLAGS_log_result_events) {
status = serializeScheduledQueryLogItemAsEventsJSON(results, json);
} else {
status = serializeScheduledQueryLogItemJSON(results, json);
}
if (!status.ok()) {
return status;
2014-07-31 00:35:19 +00:00
}
return logString(json, receiver);
}
2014-08-15 07:25:30 +00:00
}