osquery-1/osquery/core/init_osquery.cpp

115 lines
3.5 KiB
C++
Raw Normal View History

// Copyright 2004-present Facebook. All Rights Reserved.
#include <glog/logging.h>
#include "osquery/core.h"
#include "osquery/flags.h"
#include "osquery/filesystem.h"
#include "osquery/registry.h"
2014-08-15 07:25:30 +00:00
namespace osquery {
2014-11-09 00:55:19 +00:00
#define __GFLAGS_NAMESPACE google
2014-10-28 00:37:36 +00:00
const std::string kDescription =
"your operating system as a high-performance "
"relational database";
const std::string kEpilog = "osquery project page <http://osquery.io>.";
2014-09-02 00:13:04 +00:00
2014-11-13 01:13:15 +00:00
DEFINE_osquery_flag(bool, debug, false, "Enable debug messages.");
DEFINE_osquery_flag(bool,
verbose_debug,
false,
"Enable verbose debug messages.")
DEFINE_osquery_flag(bool,
disable_logging,
false,
"Disable ERROR/INFO logging.");
2014-11-13 01:13:15 +00:00
DEFINE_osquery_flag(string,
osquery_log_dir,
"/var/log/osquery/",
2014-11-13 01:13:15 +00:00
"Directory to store ERROR/INFO and results logging.");
static const char* basename(const char* filename) {
const char* sep = strrchr(filename, '/');
return sep ? sep + 1 : filename;
}
2014-11-09 04:27:28 +00:00
void initOsquery(int argc, char* argv[], int tool) {
std::string binary(basename(argv[0]));
std::string first_arg = (argc > 1) ? std::string(argv[1]) : "";
2014-11-09 04:27:28 +00:00
if ((first_arg == "--help" || first_arg == "-h" || first_arg == "-help") &&
tool != OSQUERY_TOOL_TEST) {
// Parse help options before gflags. Only display osquery-related options.
2014-11-09 00:55:19 +00:00
fprintf(stdout, "osquery " OSQUERY_VERSION ", %s\n", kDescription.c_str());
2014-11-09 09:01:17 +00:00
if (tool == OSQUERY_TOOL_SHELL) {
// The shell allows a caller to run a single SQL statement and exit.
fprintf(
stdout, "Usage: %s [OPTION]... [SQL STATEMENT]\n\n", binary.c_str());
} else {
fprintf(stdout, "Usage: %s [OPTION]...\n\n", binary.c_str());
}
2014-10-28 00:37:36 +00:00
fprintf(stdout,
"The following options control the osquery "
"daemon and shell.\n\n");
2014-11-11 16:35:25 +00:00
Flag::printFlags(Flag::get().flags());
2014-11-09 04:27:28 +00:00
if (tool == OSQUERY_TOOL_SHELL) {
// Print shell flags.
2014-11-09 09:01:17 +00:00
fprintf(stdout, "\nThe following options control the osquery shell.\n\n");
2014-11-11 16:35:25 +00:00
Flag::printFlags(Flag::get().shellFlags());
}
2014-11-09 04:27:28 +00:00
fprintf(stdout, "\n%s\n", kEpilog.c_str());
::exit(0);
}
2014-09-02 00:13:04 +00:00
FLAGS_alsologtostderr = true;
FLAGS_logbufsecs = 0; // flush the log buffer immediately
FLAGS_stop_logging_if_full_disk = true;
FLAGS_max_log_size = 10; // max size for individual log file is 10MB
2014-10-27 01:39:03 +00:00
2014-11-09 00:55:19 +00:00
// Set version string from CMake build
__GFLAGS_NAMESPACE::SetVersionString(OSQUERY_VERSION);
// Let gflags parse the non-help options/flags.
2014-11-09 04:27:28 +00:00
__GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, false);
2014-10-27 01:39:03 +00:00
2014-11-13 01:13:15 +00:00
// The log dir is used for glogging and the filesystem results logs.
if (isWritable(FLAGS_osquery_log_dir.c_str()).ok()) {
FLAGS_log_dir = FLAGS_osquery_log_dir;
2014-10-27 01:39:03 +00:00
}
2014-11-13 01:13:15 +00:00
if (FLAGS_verbose_debug) {
2014-11-18 03:31:30 +00:00
// Turn verbosity up to 1.
// Do log DEBUG, INFO, WARNING, ERROR to their log files.
// Do log the above and verbose=1 to stderr.
2014-11-13 01:13:15 +00:00
FLAGS_debug = true;
FLAGS_v = 1;
}
if (!FLAGS_debug) {
2014-11-18 03:31:30 +00:00
// Do NOT log INFO, WARNING, ERROR to stderr.
// Do log to their log files.
FLAGS_minloglevel = 0; // INFO
FLAGS_alsologtostderr = false;
2014-11-13 01:13:15 +00:00
}
if (FLAGS_disable_logging) {
2014-11-18 03:31:30 +00:00
// Do log ERROR to stderr.
// Do NOT log INFO, WARNING, ERROR to their log files.
2014-11-13 01:13:15 +00:00
FLAGS_logtostderr = true;
2014-11-18 03:31:30 +00:00
FLAGS_minloglevel = 2; // ERROR
2014-11-13 01:13:15 +00:00
}
google::InitGoogleLogging(argv[0]);
osquery::InitRegistry::get().run();
}
2014-08-15 07:25:30 +00:00
}