1 /******************************************************************************
3 * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "cli/clicommand.hpp"
21 #include "config/configcompilercontext.hpp"
22 #include "config/configcompiler.hpp"
23 #include "config/configitembuilder.hpp"
24 #include "base/application.hpp"
25 #include "base/logger.hpp"
26 #include "base/timer.hpp"
27 #include "base/utility.hpp"
28 #include "base/exception.hpp"
29 #include "base/convert.hpp"
30 #include "base/scriptglobal.hpp"
31 #include "base/context.hpp"
32 #include "base/console.hpp"
34 #include <boost/program_options.hpp>
35 #include <boost/tuple/tuple.hpp>
36 #include <boost/foreach.hpp>
39 # include <sys/types.h>
44 using namespace icinga;
45 namespace po = boost::program_options;
48 SERVICE_STATUS l_SvcStatus;
49 SERVICE_STATUS_HANDLE l_SvcStatusHandle;
52 static std::vector<String> GetLogLevelCompletionSuggestions(const String& arg)
54 std::vector<String> result;
56 String debugLevel = "debug";
57 if (debugLevel.Find(arg) == 0)
58 result.push_back(debugLevel);
60 String noticeLevel = "notice";
61 if (noticeLevel.Find(arg) == 0)
62 result.push_back(noticeLevel);
64 String informationLevel = "information";
65 if (informationLevel.Find(arg) == 0)
66 result.push_back(informationLevel);
68 String warningLevel = "warning";
69 if (warningLevel.Find(arg) == 0)
70 result.push_back(warningLevel);
72 String criticalLevel = "critical";
73 if (criticalLevel.Find(arg) == 0)
74 result.push_back(criticalLevel);
79 static std::vector<String> GlobalArgumentCompletion(const String& argument, const String& word)
81 if (argument == "include")
82 return GetBashCompletionSuggestions("directory", word);
83 else if (argument == "log-level")
84 return GetLogLevelCompletionSuggestions(word);
86 return std::vector<String>();
91 int argc = Application::GetArgC();
92 char **argv = Application::GetArgV();
94 bool autocomplete = false;
97 if (argc >= 4 && strcmp(argv[1], "--autocomplete") == 0) {
101 autoindex = Convert::ToLong(argv[2]);
102 } catch (const std::invalid_argument& ex) {
103 Log(LogCritical, "icinga-app")
104 << "Invalid index for --autocomplete: " << argv[2];
112 Application::SetStartTime(Utility::GetTime());
115 Application::SetResourceLimits();
117 /* Set thread title. */
118 Utility::SetThreadName("Main Thread", false);
120 /* Install exception handlers to make debugging easier. */
121 Application::InstallExceptionHandlers();
124 bool builtinPaths = true;
127 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Icinga Development Team\\ICINGA2", 0,
128 KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) {
129 BYTE pvData[MAX_PATH];
130 DWORD cbData = sizeof(pvData)-1;
132 if (RegQueryValueEx(hKey, NULL, NULL, &lType, pvData, &cbData) == ERROR_SUCCESS && lType == REG_SZ) {
133 pvData[cbData] = '\0';
135 String prefix = (char *)pvData;
136 Application::DeclarePrefixDir(prefix);
137 Application::DeclareSysconfDir(prefix + "\\etc");
138 Application::DeclareRunDir(prefix + "\\var\\run");
139 Application::DeclareLocalStateDir(prefix + "\\var");
140 Application::DeclarePkgDataDir(prefix + "\\share\\icinga2");
141 Application::DeclareIncludeConfDir(prefix + "\\share\\icinga2\\include");
143 builtinPaths = false;
150 Log(LogWarning, "icinga-app", "Registry key could not be read. Falling back to built-in paths.");
153 Application::DeclarePrefixDir(ICINGA_PREFIX);
154 Application::DeclareSysconfDir(ICINGA_SYSCONFDIR);
155 Application::DeclareRunDir(ICINGA_RUNDIR);
156 Application::DeclareLocalStateDir(ICINGA_LOCALSTATEDIR);
157 Application::DeclarePkgDataDir(ICINGA_PKGDATADIR);
158 Application::DeclareIncludeConfDir(ICINGA_INCLUDECONFDIR);
163 Application::DeclareZonesDir(Application::GetSysconfDir() + "/icinga2/zones.d");
164 Application::DeclareApplicationType("icinga/IcingaApplication");
165 Application::DeclareRunAsUser(ICINGA_USER);
166 Application::DeclareRunAsGroup(ICINGA_GROUP);
167 Application::DeclareConcurrency(boost::thread::hardware_concurrency());
169 LogSeverity logLevel = Logger::GetConsoleLogSeverity();
170 Logger::SetConsoleLogSeverity(LogWarning);
172 Utility::LoadExtensionLibrary("cli");
174 po::options_description visibleDesc("Global options");
176 visibleDesc.add_options()
177 ("help,h", "show this help message")
178 ("version,V", "show version information")
180 ("color", "use VT100 color codes even when stdout is not a terminal")
182 ("define,D", po::value<std::vector<std::string> >(), "define a constant")
183 ("library,l", po::value<std::vector<std::string> >(), "load a library")
184 ("include,I", po::value<std::vector<std::string> >(), "add include search directory")
185 ("log-level,x", po::value<std::string>(), "specify the log level for the console log");
187 po::options_description hiddenDesc("Hidden options");
189 hiddenDesc.add_options()
191 ("no-stack-rlimit", "used internally, do not specify manually")
193 ("no-stack-rlimit", "used internally, do not specify manually")
195 ("arg", po::value<std::vector<std::string> >(), "positional argument");
197 po::positional_options_description positionalDesc;
198 positionalDesc.add("arg", -1);
201 CLICommand::Ptr command;
202 po::variables_map vm;
205 CLICommand::ParseCommand(argc, argv, visibleDesc, hiddenDesc, positionalDesc,
206 vm, cmdname, command, autocomplete);
207 } catch (const std::exception& ex) {
208 Log(LogCritical, "icinga-app")
209 << "Error while parsing command-line options: " << ex.what();
213 String initconfig = Application::GetSysconfDir() + "/icinga2/init.conf";
215 if (Utility::PathExists(initconfig)) {
216 Expression *expression;
218 expression = ConfigCompiler::CompileFile(initconfig);
221 expression->Evaluate(frame);
222 } catch (const std::exception& ex) {
225 Log(LogCritical, "config", DiagnosticInformation(ex));
233 if (vm.count("color")) {
234 Console::SetType(std::cout, Console_VT100);
235 Console::SetType(std::cerr, Console_VT100);
239 if (vm.count("define")) {
240 BOOST_FOREACH(const String& define, vm["define"].as<std::vector<std::string> >()) {
242 size_t pos = define.FindFirstOf('=');
243 if (pos != String::NPos) {
244 key = define.SubStr(0, pos);
245 value = define.SubStr(pos + 1);
250 ScriptGlobal::Set(key, value);
254 Application::DeclareStatePath(Application::GetLocalStateDir() + "/lib/icinga2/icinga2.state");
255 Application::DeclareObjectsPath(Application::GetLocalStateDir() + "/cache/icinga2/icinga2.debug");
256 Application::DeclareVarsPath(Application::GetLocalStateDir() + "/cache/icinga2/icinga2.vars");
257 Application::DeclarePidPath(Application::GetRunDir() + "/icinga2/icinga2.pid");
259 ConfigCompiler::AddIncludeSearchDir(Application::GetIncludeConfDir());
261 if (!autocomplete && vm.count("include")) {
262 BOOST_FOREACH(const String& includePath, vm["include"].as<std::vector<std::string> >()) {
263 ConfigCompiler::AddIncludeSearchDir(includePath);
268 Logger::SetConsoleLogSeverity(logLevel);
270 if (vm.count("log-level")) {
271 String severity = vm["log-level"].as<std::string>();
273 LogSeverity logLevel = LogInformation;
275 logLevel = Logger::StringToSeverity(severity);
276 } catch (std::exception&) {
277 /* Inform user and exit */
278 Log(LogCritical, "icinga-app", "Invalid log level set. Default is 'information'.");
282 Logger::SetConsoleLogSeverity(logLevel);
285 if (vm.count("library")) {
286 BOOST_FOREACH(const String& libraryName, vm["library"].as<std::vector<std::string> >()) {
288 (void) Utility::LoadExtensionLibrary(libraryName);
289 } catch (const std::exception& ex) {
290 Log(LogCritical, "icinga-app")
291 << "Could not load library \"" << libraryName << "\": " << DiagnosticInformation(ex);
297 if (!command || vm.count("help") || vm.count("version")) {
301 appName = Utility::BaseName(Application::GetArgV()[0]);
302 } catch (const std::bad_alloc&) {
303 Log(LogCritical, "icinga-app", "Allocation failed.");
307 if (appName.GetLength() > 3 && appName.SubStr(0, 3) == "lt-")
308 appName = appName.SubStr(3, appName.GetLength() - 3);
310 std::cout << appName << " " << "- The Icinga 2 network monitoring daemon (version: "
311 << ConsoleColorTag(vm.count("version") ? Console_ForegroundRed : Console_Normal)
312 << Application::GetVersion()
315 #endif /* I2_DEBUG */
316 << ConsoleColorTag(Console_Normal)
317 << ")" << std::endl << std::endl;
319 if ((!command || vm.count("help")) && !vm.count("version")) {
320 std::cout << "Usage:" << std::endl
321 << " " << argv[0] << " ";
323 if (cmdname.IsEmpty())
324 std::cout << "<command>";
326 std::cout << cmdname;
328 std::cout << " [<arguments>]" << std::endl;
331 std::cout << std::endl
332 << command->GetDescription() << std::endl;
336 if (vm.count("version")) {
337 std::cout << "Copyright (c) 2012-2015 Icinga Development Team (https://www.icinga.org)" << std::endl
338 << "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl2.html>" << std::endl
339 << "This is free software: you are free to change and redistribute it." << std::endl
340 << "There is NO WARRANTY, to the extent permitted by law.";
343 std::cout << std::endl;
345 if (vm.count("version")) {
346 std::cout << std::endl;
348 Application::DisplayInfoMessage(std::cout, true);
354 if (!command || vm.count("help")) {
356 CLICommand::ShowCommands(argc, argv, NULL);
358 std::cout << visibleDesc << std::endl
359 << "Report bugs at <https://dev.icinga.org/>" << std::endl
360 << "Icinga home page: <https://www.icinga.org/>" << std::endl;
368 CLICommand::ShowCommands(argc, argv, &visibleDesc, &hiddenDesc,
369 &GlobalArgumentCompletion, true, autoindex);
371 } else if (command) {
372 Logger::DisableTimestamp(true);
374 if (command->GetImpersonationLevel() == ImpersonateRoot) {
376 Log(LogCritical, "cli", "This command must be run as root.");
379 } else if (command && command->GetImpersonationLevel() == ImpersonateIcinga) {
380 String group = Application::GetRunAsGroup();
381 String user = Application::GetRunAsUser();
384 struct group *gr = getgrnam(group.CStr());
388 Log(LogCritical, "cli")
389 << "Invalid group specified: " << group;
392 Log(LogCritical, "cli")
393 << "getgrnam() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
398 if (getgid() != gr->gr_gid) {
399 if (!vm.count("reload-internal") && setgroups(0, NULL) < 0) {
400 Log(LogCritical, "cli")
401 << "setgroups() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
402 Log(LogCritical, "cli")
403 << "Please re-run this command as a privileged user or using the \"" << user << "\" account.";
407 if (setgid(gr->gr_gid) < 0) {
408 Log(LogCritical, "cli")
409 << "setgid() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
415 struct passwd *pw = getpwnam(user.CStr());
419 Log(LogCritical, "cli")
420 << "Invalid user specified: " << user;
423 Log(LogCritical, "cli")
424 << "getpwnam() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
429 // also activate the additional groups the configured user is member of
430 if (getuid() != pw->pw_uid) {
431 if (!vm.count("reload-internal") && initgroups(user.CStr(), pw->pw_gid) < 0) {
432 Log(LogCritical, "cli")
433 << "initgroups() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
434 Log(LogCritical, "cli")
435 << "Please re-run this command as a privileged user or using the \"" << user << "\" account.";
439 if (setuid(pw->pw_uid) < 0) {
440 Log(LogCritical, "cli")
441 << "setuid() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
442 Log(LogCritical, "cli")
443 << "Please re-run this command as a privileged user or using the \"" << user << "\" account.";
450 std::vector<std::string> args;
452 args = vm["arg"].as<std::vector<std::string> >();
454 if (args.size() < command->GetMinArguments()) {
455 Log(LogCritical, "cli")
456 << "Too few arguments. Command needs at least " << command->GetMinArguments()
457 << " argument" << (command->GetMinArguments() != 1 ? "s" : "") << ".";
461 if (command->GetMaxArguments() >= 0 && args.size() > command->GetMaxArguments()) {
462 Log(LogCritical, "cli")
463 << "Too many arguments. At most " << command->GetMaxArguments()
464 << " argument" << (command->GetMaxArguments() != 1 ? "s" : "") << " may be specified.";
468 rc = command->Run(vm, args);
475 static int SetupService(bool install, int argc, char **argv)
477 SC_HANDLE schSCManager = OpenSCManager(
480 SC_MANAGER_ALL_ACCESS);
482 if (NULL == schSCManager) {
483 printf("OpenSCManager failed (%d)\n", GetLastError());
487 TCHAR szPath[MAX_PATH];
489 if (!GetModuleFileName(NULL, szPath, MAX_PATH)) {
490 printf("Cannot install service (%d)\n", GetLastError());
495 szArgs = Utility::EscapeShellArg(szPath) + " --scm";
497 for (int i = 0; i < argc; i++)
498 szArgs += " " + Utility::EscapeShellArg(argv[i]);
500 SC_HANDLE schService = OpenService(schSCManager, "icinga2", DELETE | SERVICE_STOP | SERVICE_QUERY_STATUS);
502 if (schService != NULL) {
503 SERVICE_STATUS status;
504 ControlService(schService, SERVICE_CONTROL_STOP, &status);
506 double start = Utility::GetTime();
507 while (status.dwCurrentState != SERVICE_STOPPED) {
508 double end = Utility::GetTime();
510 if (end - start > 30) {
511 printf("Could not stop the service.\n");
517 if (!QueryServiceStatus(schService, &status)) {
518 printf("QueryServiceStatus failed (%d)\n", GetLastError());
523 if (!DeleteService(schService)) {
524 printf("DeleteService failed (%d)\n", GetLastError());
525 CloseServiceHandle(schService);
526 CloseServiceHandle(schSCManager);
531 printf("Service uninstalled successfully\n");
533 CloseServiceHandle(schService);
537 schService = CreateService(
542 SERVICE_WIN32_OWN_PROCESS,
543 SERVICE_DEMAND_START,
544 SERVICE_ERROR_NORMAL,
549 "NT AUTHORITY\\NetworkService",
552 if (schService == NULL) {
553 printf("CreateService failed (%d)\n", GetLastError());
554 CloseServiceHandle(schSCManager);
557 printf("Service installed successfully\n");
559 ChangeServiceConfig(schService, SERVICE_NO_CHANGE, SERVICE_AUTO_START,
560 SERVICE_ERROR_NORMAL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
562 SERVICE_DESCRIPTION sdDescription = { "The Icinga 2 monitoring application" };
563 ChangeServiceConfig2(schService, SERVICE_CONFIG_DESCRIPTION, &sdDescription);
565 if (!StartService(schService, 0, NULL)) {
566 printf("StartService failed (%d)\n", GetLastError());
567 CloseServiceHandle(schService);
568 CloseServiceHandle(schSCManager);
572 CloseServiceHandle(schService);
575 CloseServiceHandle(schSCManager);
580 VOID ReportSvcStatus(DWORD dwCurrentState,
581 DWORD dwWin32ExitCode,
584 static DWORD dwCheckPoint = 1;
586 l_SvcStatus.dwCurrentState = dwCurrentState;
587 l_SvcStatus.dwWin32ExitCode = dwWin32ExitCode;
588 l_SvcStatus.dwWaitHint = dwWaitHint;
590 if (dwCurrentState == SERVICE_START_PENDING)
591 l_SvcStatus.dwControlsAccepted = 0;
593 l_SvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
595 if ((dwCurrentState == SERVICE_RUNNING) ||
596 (dwCurrentState == SERVICE_STOPPED))
597 l_SvcStatus.dwCheckPoint = 0;
599 l_SvcStatus.dwCheckPoint = dwCheckPoint++;
601 SetServiceStatus(l_SvcStatusHandle, &l_SvcStatus);
604 VOID WINAPI ServiceControlHandler(DWORD dwCtrl)
606 if (dwCtrl == SERVICE_CONTROL_STOP) {
607 ReportSvcStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
608 Application::RequestShutdown();
612 VOID WINAPI ServiceMain(DWORD argc, LPSTR *argv)
614 l_SvcStatusHandle = RegisterServiceCtrlHandler(
616 ServiceControlHandler);
618 l_SvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
619 l_SvcStatus.dwServiceSpecificExitCode = 0;
621 ReportSvcStatus(SERVICE_RUNNING, NO_ERROR, 0);
625 ReportSvcStatus(SERVICE_STOPPED, NO_ERROR, rc);
627 Application::Exit(rc);
632 * Entry point for the Icinga application.
634 * @params argc Number of command line arguments.
635 * @params argv Command line arguments.
636 * @returns The application's exit status.
638 int main(int argc, char **argv)
642 if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) {
643 rlim_t maxfds = rl.rlim_max;
645 if (maxfds == RLIM_INFINITY)
648 for (rlim_t i = 3; i < maxfds; i++) {
653 std::cerr << "Closed FD " << i << " which we inherited from our parent process." << std::endl;
654 #endif /* I2_DEBUG */
659 /* must be called before using any other libbase functions */
660 Application::InitializeBase();
662 /* Set command-line arguments. */
663 Application::SetArgC(argc);
664 Application::SetArgV(argv);
667 if (argc > 1 && strcmp(argv[1], "--scm-install") == 0) {
668 return SetupService(true, argc - 2, &argv[2]);
671 if (argc > 1 && strcmp(argv[1], "--scm-uninstall") == 0) {
672 return SetupService(false, argc - 2, &argv[2]);
675 if (argc > 1 && strcmp(argv[1], "--scm") == 0) {
676 SERVICE_TABLE_ENTRY dispatchTable[] = {
677 { "icinga2", ServiceMain },
681 StartServiceCtrlDispatcher(dispatchTable);
682 Application::Exit(EXIT_FAILURE);
688 Application::Exit(rc);