osquery-1/osquery/core/init_osquery.cpp

80 lines
2.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
DEFINE_osquery_flag(string,
osquery_log_dir,
"/var/log/osquery/",
2014-10-28 00:37:36 +00:00
"Directory to store 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-09 04:27:28 +00:00
Flag::print_flags(Flag::get().flags());
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");
Flag::print_flags(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 = 1024; // max size for individual log file is 1GB
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
if (isWritable(FLAGS_osquery_log_dir.c_str()).ok()) {
FLAGS_log_dir = FLAGS_osquery_log_dir;
2014-10-27 01:39:03 +00:00
}
google::InitGoogleLogging(argv[0]);
osquery::InitRegistry::get().run();
}
2014-08-15 07:25:30 +00:00
}