/******************************************************************************
* Icinga 2 *
- * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
+ * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
-#include <Windows.h>
-#include <Shlwapi.h>
-#include <iostream>
-
-#include "thresholds.h"
-#include "boost/program_options.hpp"
+#include "plugins/thresholds.hpp"
+#include <boost/program_options.hpp>
+#include <iostream>
+#include <windows.h>
+#include <shlwapi.h>
#define VERSION 1.1
namespace po = boost::program_options;
-using std::wcout; using std::endl;
-using std::cout; using std::wstring;
-
-static BOOL debug;
-
-struct printInfoStruct
+struct printInfoStruct
{
bool warn;
DWORD ServiceState;
- wstring service;
+ std::wstring service;
};
-static int parseArguments(int, wchar_t **, po::variables_map&, printInfoStruct&);
-static int printOutput(const printInfoStruct&);
-static DWORD ServiceStatus(const printInfoStruct&);
+static bool l_Debug;
-int wmain(int argc, wchar_t **argv)
+static int parseArguments(int ac, WCHAR **av, po::variables_map& vm, printInfoStruct& printInfo)
{
- po::variables_map vm;
- printInfoStruct printInfo = { false, 0, L"" };
-
- int ret = parseArguments(argc, argv, vm, printInfo);
- if (ret != -1)
- return ret;
-
- printInfo.ServiceState = ServiceStatus(printInfo);
- if (printInfo.ServiceState == -1)
- return 3;
-
- return printOutput(printInfo);
-}
-
-int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct& printInfo)
-{
- wchar_t namePath[MAX_PATH];
+ WCHAR namePath[MAX_PATH];
GetModuleFileName(NULL, namePath, MAX_PATH);
- wchar_t *progName = PathFindFileName(namePath);
+ WCHAR *progName = PathFindFileName(namePath);
po::options_description desc;
desc.add_options()
- ("help,h", "print help message and exit")
- ("version,V", "print version and exit")
- ("debug,d", "Verbose/Debug output")
- ("service,s", po::wvalue<wstring>(), "service to check (required)")
- ("warn,w", "return warning (1) instead of critical (2),\n when service is not running")
+ ("help,h", "Print help message and exit")
+ ("version,V", "Print version and exit")
+ ("debug,D", "Verbose/Debug output")
+ ("service,s", po::wvalue<std::wstring>(), "Service name to check")
+ ("description,d", "Use \"service\" to match on description")
+ ("warn,w", "Return warning (1) instead of critical (2),\n when service is not running")
;
- po::basic_command_line_parser<wchar_t> parser(ac, av);
+ po::wcommand_line_parser parser(ac, av);
try {
po::store(
.run(),
vm);
vm.notify();
- } catch (std::exception& e) {
- cout << e.what() << endl << desc << endl;
+ } catch (const std::exception& e) {
+ std::cout << e.what() << '\n' << desc << '\n';
return 3;
}
if (vm.count("help")) {
- wcout << progName << " Help\n\tVersion: " << VERSION << endl;
+ std::wcout << progName << " Help\n\tVersion: " << VERSION << '\n';
wprintf(
L"%s is a simple program to check the status of a service.\n"
L"You can use the following options to define its behaviour:\n\n", progName);
- cout << desc;
+ std::cout << desc;
wprintf(
L"\nIt will then output a string looking something like this:\n\n"
L"\tSERVICE CRITICAL NOT_RUNNING | service=4;!4;!4;1;7\n\n"
L"all \"-w\" and \"-c\" do is say whether a not running service is a warning\n"
L"or critical state respectively.\n\n"
, progName, progName);
- cout << endl;
+ std::cout << '\n';
return 0;
}
- if (vm.count("version")) {
- cout << "Version: " << VERSION << endl;
+ if (vm.count("version")) {
+ std::cout << "Version: " << VERSION << '\n';
return 0;
- }
+ }
if (!vm.count("service")) {
- cout << "Missing argument: service" << endl << desc << endl;
+ std::cout << "Argument \"service\" is required.\n" << desc << '\n';
return 3;
}
- if (vm.count("warn"))
- printInfo.warn = true;
-
- printInfo.service = vm["service"].as<wstring>();
+ printInfo.service = vm["service"].as<std::wstring>();
+
+ printInfo.warn = vm.count("warn");
+
+ l_Debug = vm.count("debug") > 0;
- if (vm.count("debug"))
- debug = TRUE;
-
return -1;
}
-int printOutput(const printInfoStruct& printInfo)
+static int printOutput(const printInfoStruct& printInfo)
{
- if (debug)
- wcout << L"Constructing output string" << endl;
+ if (l_Debug)
+ std::wcout << L"Constructing output string" << '\n';
- wstring perf;
+ std::wstring perf;
state state = OK;
if (!printInfo.ServiceState) {
- wcout << L"SERVICE CRITICAL NOTFOUND | service=" << printInfo.ServiceState << ";!4;!4;1;7" << endl;
+ std::wcout << L"SERVICE CRITICAL NOT FOUND | 'service'=" << printInfo.ServiceState << ";;;1;7" << '\n';
return 3;
}
- if (printInfo.ServiceState != 0x04)
+ if (printInfo.ServiceState != 0x04)
printInfo.warn ? state = WARNING : state = CRITICAL;
switch (state) {
case OK:
- wcout << L"SERVICE OK RUNNING | service=4;!4;!4;1;7" << endl;
+ std::wcout << L"SERVICE \"" << printInfo.service << "\" OK RUNNING | 'service'=4;;;1;7" << '\n';
break;
case WARNING:
- wcout << L"SERVICE WARNING NOT RUNNING | service=" << printInfo.ServiceState << ";!4;!4;1;7" << endl;
+ std::wcout << L"SERVICE \"" << printInfo.service << "\" WARNING NOT RUNNING | 'service'=" << printInfo.ServiceState << ";;;1;7" << '\n';
break;
case CRITICAL:
- wcout << L"SERVICE CRITICAL NOT RUNNING | service=" << printInfo.ServiceState << ";!4;!4;1;7" << endl;
+ std::wcout << L"SERVICE \"" << printInfo.service << "\" CRITICAL NOT RUNNING | 'service'=" << printInfo.ServiceState << ";;;1;7" << '\n';
break;
}
return state;
}
-DWORD ServiceStatus(const printInfoStruct& printInfo)
+static std::wstring getServiceByDescription(const std::wstring& description)
+{
+ SC_HANDLE hSCM = NULL;
+ LPENUM_SERVICE_STATUSW lpServices = NULL;
+ LPBYTE lpBuf = NULL;
+ DWORD cbBufSize = 0;
+ DWORD lpServicesReturned = 0;
+ DWORD pcbBytesNeeded = 0;
+ DWORD lpResumeHandle = 0;;
+
+ if (l_Debug)
+ std::wcout << L"Opening SC Manager" << '\n';
+
+ hSCM = OpenSCManager(NULL, NULL, GENERIC_READ);
+ if (hSCM == NULL)
+ goto die;
+
+ if (l_Debug)
+ std::wcout << L"Determining initially required memory" << '\n';
+
+ EnumServicesStatus(hSCM, SERVICE_WIN32 | SERVICE_DRIVER, SERVICE_STATE_ALL, NULL, 0,
+ &pcbBytesNeeded, &lpServicesReturned, &lpResumeHandle);
+
+ /* This should always be ERROR_INSUFFICIENT_BUFFER... But for some reason it is sometimes ERROR_MORE_DATA
+ * See the MSDN on EnumServiceStatus for a glimpse of despair
+ */
+
+ if (GetLastError() != ERROR_INSUFFICIENT_BUFFER && GetLastError() != ERROR_MORE_DATA)
+ goto die;
+
+ lpServices = reinterpret_cast<LPENUM_SERVICE_STATUSW>(new BYTE[pcbBytesNeeded]);
+
+ if (l_Debug)
+ std::wcout << L"Requesting Service Information. Entry point: " << lpResumeHandle << '\n';
+
+ EnumServicesStatus(hSCM, SERVICE_WIN32 | SERVICE_DRIVER, SERVICE_STATE_ALL, lpServices, pcbBytesNeeded,
+ &pcbBytesNeeded, &lpServicesReturned, &lpResumeHandle);
+
+ for (int index = 0; index < lpServicesReturned; index++) {
+ LPWSTR lpCurrent = lpServices[index].lpServiceName;
+
+ if (l_Debug) {
+ std::wcout << L"Opening Service \"" << lpServices[index].lpServiceName << L"\"\n";
+ }
+
+ SC_HANDLE hService = OpenService(hSCM, lpCurrent, SERVICE_QUERY_CONFIG);
+ if (!hService)
+ goto die;
+
+ DWORD dwBytesNeeded = 0;
+ if (l_Debug)
+ std::wcout << "Accessing config\n";
+
+ if (!QueryServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, NULL, 0, &dwBytesNeeded) && GetLastError() != ERROR_INSUFFICIENT_BUFFER)
+ continue;
+
+ LPSERVICE_DESCRIPTION lpsd = reinterpret_cast<LPSERVICE_DESCRIPTION>(new BYTE[dwBytesNeeded]);
+
+ if (!QueryServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, (LPBYTE)lpsd, dwBytesNeeded, &dwBytesNeeded))
+ continue;
+
+ if (lpsd->lpDescription != NULL && lstrcmp(lpsd->lpDescription, L"") != 0) {
+ std::wstring desc(lpsd->lpDescription);
+ if (l_Debug)
+ std::wcout << "Got description:\n" << desc << '\n';
+ size_t p = desc.find(description);
+ if (desc.find(description) != desc.npos)
+ return lpCurrent;
+ }
+ else if (l_Debug)
+ std::wcout << "No description found\n";
+ }
+
+ CloseServiceHandle(hSCM);
+ delete[] lpServices;
+ return L"";
+
+die:
+ printErrorInfo();
+ if (hSCM)
+ CloseServiceHandle(hSCM);
+ if (lpServices)
+ delete[] lpServices;
+ return L"";
+}
+
+static DWORD getServiceStatus(const printInfoStruct& printInfo)
{
SC_HANDLE hSCM;
SC_HANDLE hService;
DWORD cbBufSize;
+ DWORD lpResumeHandle = 0;
LPBYTE lpBuf = NULL;
- if (debug)
- wcout << L"Opening SC Manager" << endl;
+ if (l_Debug)
+ std::wcout << L"Opening SC Manager" << '\n';
hSCM = OpenSCManager(NULL, NULL, GENERIC_READ);
if (hSCM == NULL)
goto die;
- if (debug)
- wcout << L"Getting Service Information" << endl;
-
hService = OpenService(hSCM, printInfo.service.c_str(), SERVICE_QUERY_STATUS);
if (hService == NULL)
goto die;
-
+
QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, NULL, 0, &cbBufSize);
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
goto die;
return pInfo->dwCurrentState;
}
-
die:
- die();
+ printErrorInfo();
if (hSCM)
CloseServiceHandle(hSCM);
if (hService)
CloseServiceHandle(hService);
- delete [] lpBuf;
+ if (lpBuf)
+ delete [] lpBuf;
return -1;
}
+
+int wmain(int argc, WCHAR **argv)
+{
+ po::variables_map vm;
+ printInfoStruct printInfo;
+
+ int ret = parseArguments(argc, argv, vm, printInfo);
+ if (ret != -1)
+ return ret;
+
+ if (vm.count("description"))
+ printInfo.service = getServiceByDescription(vm["service"].as<std::wstring>());
+
+ if (printInfo.service.empty()) {
+ std::wcout << "Could not find service matching description\n";
+ return 3;
+ }
+
+ printInfo.ServiceState = getServiceStatus(printInfo);
+ if (printInfo.ServiceState == -1)
+ return 3;
+
+ return printOutput(printInfo);
+}
+