2 * Copyright (c) 2014, Facebook, Inc.
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
14 #include <osquery/filesystem.h>
15 #include <osquery/logger.h>
17 namespace pt = boost::property_tree;
18 namespace fs = boost::filesystem;
22 const std::string kFilesystemLoggerFilename = "osqueryd.results.log";
23 const std::string kFilesystemLoggerSnapshots = "osqueryd.snapshots.log";
24 const std::string kFilesystemLoggerHealth = "osqueryd.health.log";
26 std::mutex filesystemLoggerPluginMutex;
28 class FilesystemLoggerPlugin : public LoggerPlugin {
31 Status logString(const std::string& s);
32 Status logStringToFile(const std::string& s, const std::string& filename);
33 Status logSnapshot(const std::string& s);
34 Status logHealth(const std::string& s);
35 Status init(const std::string& name, const std::vector<StatusLogLine>& log);
36 Status logStatus(const std::vector<StatusLogLine>& log);
42 REGISTER(FilesystemLoggerPlugin, "logger", "filesystem");
44 Status FilesystemLoggerPlugin::setUp() {
45 return Status(0, "OK");
48 Status FilesystemLoggerPlugin::logString(const std::string& s) {
49 return logStringToFile(s, kFilesystemLoggerFilename);
52 Status FilesystemLoggerPlugin::logStringToFile(const std::string& s,
53 const std::string& filename) {
54 std::lock_guard<std::mutex> lock(filesystemLoggerPluginMutex);
56 // The results log may contain sensitive information if run as root.
57 auto status = writeTextFile((log_path_ / filename).string(), s, 0640, true);
61 } catch (const std::exception& e) {
62 return Status(1, e.what());
64 return Status(0, "OK");
67 Status FilesystemLoggerPlugin::logStatus(
68 const std::vector<StatusLogLine>& log) {
69 for (const auto& item : log) {
70 // Emit this intermediate log to the Glog filesystem logger.
71 google::LogMessage(item.filename.c_str(),
73 (google::LogSeverity)item.severity).stream()
77 return Status(0, "OK");
80 Status FilesystemLoggerPlugin::logSnapshot(const std::string& s) {
81 // Send the snapshot data to a separate filename.
82 return logStringToFile(s, kFilesystemLoggerSnapshots);
85 Status FilesystemLoggerPlugin::logHealth(const std::string& s) {
86 return logStringToFile(s, kFilesystemLoggerHealth);
89 Status FilesystemLoggerPlugin::init(const std::string& name,
90 const std::vector<StatusLogLine>& log) {
91 // Stop the internal Glog facilities.
92 google::ShutdownGoogleLogging();
94 // Restart the Glog facilities using the name `init` was provided.
95 google::InitGoogleLogging(name.c_str());
97 // We may violate Glog global object assumptions. So set names manually.
98 auto basename = (log_path_ / name).string();
99 google::SetLogDestination(google::INFO, (basename + ".INFO.").c_str());
100 google::SetLogDestination(google::WARNING, (basename + ".WARNING.").c_str());
101 google::SetLogDestination(google::ERROR, (basename + ".ERROR.").c_str());
103 // Now funnel the intermediate status logs provided to `init`.
106 // The filesystem logger cheats and uses Glog to log to the filesystem so
107 // we can return failure here and stop the custom log sink.
108 return Status(1, "No status logger used for filesystem");