2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2014-11-06 15:17:08 +01:00
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
#include "plugins/thresholds.hpp"
|
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
#include <iostream>
|
|
|
|
#include <windows.h>
|
|
|
|
#include <shlwapi.h>
|
2014-11-06 15:17:08 +01:00
|
|
|
|
2015-02-17 15:20:30 +01:00
|
|
|
#define VERSION 1.1
|
2014-11-06 15:17:08 +01:00
|
|
|
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
struct printInfoStruct
|
2014-11-06 15:17:08 +01:00
|
|
|
{
|
2018-02-01 08:45:09 +01:00
|
|
|
bool warn;
|
|
|
|
DWORD ServiceState;
|
|
|
|
std::wstring service;
|
|
|
|
};
|
2017-07-26 17:02:18 +02:00
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
static bool l_Debug;
|
2017-07-26 17:02:18 +02:00
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
static int parseArguments(int ac, WCHAR **av, po::variables_map& vm, printInfoStruct& printInfo)
|
2014-11-06 16:36:42 +01:00
|
|
|
{
|
2015-03-23 13:07:02 +01:00
|
|
|
WCHAR namePath[MAX_PATH];
|
2014-11-06 15:17:08 +01:00
|
|
|
GetModuleFileName(NULL, namePath, MAX_PATH);
|
2015-03-23 13:07:02 +01:00
|
|
|
WCHAR *progName = PathFindFileName(namePath);
|
2014-11-06 15:17:08 +01:00
|
|
|
|
|
|
|
po::options_description desc;
|
|
|
|
|
|
|
|
desc.add_options()
|
2015-03-23 13:07:02 +01:00
|
|
|
("help,h", "Print help message and exit")
|
|
|
|
("version,V", "Print version and exit")
|
2018-02-01 08:45:09 +01:00
|
|
|
("debug,D", "Verbose/Debug output")
|
2017-07-26 17:02:18 +02:00
|
|
|
("service,s", po::wvalue<std::wstring>(), "Service name to check")
|
|
|
|
("description,d", "Use \"service\" to match on description")
|
2015-03-23 13:07:02 +01:00
|
|
|
("warn,w", "Return warning (1) instead of critical (2),\n when service is not running")
|
2014-11-06 15:17:08 +01:00
|
|
|
;
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
po::wcommand_line_parser parser(ac, av);
|
2014-11-06 15:17:08 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
po::store(
|
|
|
|
parser
|
|
|
|
.options(desc)
|
|
|
|
.style(
|
2019-05-17 19:54:10 +02:00
|
|
|
po::command_line_style::unix_style &
|
|
|
|
~po::command_line_style::allow_guessing |
|
|
|
|
po::command_line_style::allow_long_disguise
|
|
|
|
)
|
2014-11-06 15:17:08 +01:00
|
|
|
.run(),
|
|
|
|
vm);
|
|
|
|
vm.notify();
|
2018-02-01 08:45:09 +01:00
|
|
|
} catch (const std::exception& e) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::cout << e.what() << '\n' << desc << '\n';
|
2014-11-06 15:17:08 +01:00
|
|
|
return 3;
|
2014-11-06 16:36:42 +01:00
|
|
|
}
|
2014-12-19 10:21:38 +01:00
|
|
|
|
2014-11-06 15:17:08 +01:00
|
|
|
if (vm.count("help")) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << progName << " Help\n\tVersion: " << VERSION << '\n';
|
2014-11-06 15:17:08 +01:00
|
|
|
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);
|
2015-03-23 13:07:02 +01:00
|
|
|
std::cout << desc;
|
2014-11-06 15:17:08 +01:00
|
|
|
wprintf(
|
|
|
|
L"\nIt will then output a string looking something like this:\n\n"
|
2015-03-06 12:14:09 +01:00
|
|
|
L"\tSERVICE CRITICAL NOT_RUNNING | service=4;!4;!4;1;7\n\n"
|
2014-11-06 15:17:08 +01:00
|
|
|
L"\"SERVICE\" being the type of the check, \"CRITICAL\" the returned status\n"
|
|
|
|
L"and \"1\" is the returned value.\n"
|
|
|
|
L"A service is either running (Code 0x04) or not running (any other).\n"
|
|
|
|
L"For more information consult the msdn on service state transitions.\n\n"
|
|
|
|
L"%s' exit codes denote the following:\n"
|
2014-11-06 16:36:42 +01:00
|
|
|
L" 0\tOK,\n\tNo Thresholds were broken or the programs check part was not executed\n"
|
2014-11-06 15:17:08 +01:00
|
|
|
L" 1\tWARNING,\n\tThe warning, but not the critical threshold was broken\n"
|
|
|
|
L" 2\tCRITICAL,\n\tThe critical threshold was broken\n"
|
2014-11-06 16:36:42 +01:00
|
|
|
L" 3\tUNKNOWN, \n\tThe program experienced an internal or input error\n\n"
|
2014-11-06 15:17:08 +01:00
|
|
|
L"%s' thresholds work differently, since a service is either running or not\n"
|
|
|
|
L"all \"-w\" and \"-c\" do is say whether a not running service is a warning\n"
|
2015-01-09 13:20:22 +01:00
|
|
|
L"or critical state respectively.\n\n"
|
2014-11-14 13:54:29 +01:00
|
|
|
, progName, progName);
|
2015-03-23 13:07:02 +01:00
|
|
|
std::cout << '\n';
|
2014-11-06 15:17:08 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2015-02-17 15:20:30 +01:00
|
|
|
|
2017-07-26 17:02:18 +02:00
|
|
|
if (vm.count("version")) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::cout << "Version: " << VERSION << '\n';
|
2014-11-06 15:17:08 +01:00
|
|
|
return 0;
|
2017-07-26 17:02:18 +02:00
|
|
|
}
|
2015-02-17 15:20:30 +01:00
|
|
|
|
|
|
|
if (!vm.count("service")) {
|
2017-07-26 17:02:18 +02:00
|
|
|
std::cout << "Argument \"service\" is required.\n" << desc << '\n';
|
2014-11-06 15:17:08 +01:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2015-03-23 13:07:02 +01:00
|
|
|
printInfo.service = vm["service"].as<std::wstring>();
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2017-12-13 17:48:16 +01:00
|
|
|
printInfo.warn = vm.count("warn");
|
2017-07-26 17:02:18 +02:00
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
l_Debug = vm.count("debug") > 0;
|
2017-12-13 12:54:14 +01:00
|
|
|
|
2014-11-06 15:17:08 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
static int printOutput(const printInfoStruct& printInfo)
|
2014-11-06 16:36:42 +01:00
|
|
|
{
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_Debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"Constructing output string" << '\n';
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wstring perf;
|
2014-11-06 15:17:08 +01:00
|
|
|
state state = OK;
|
2014-11-06 16:36:42 +01:00
|
|
|
|
2014-12-19 10:21:38 +01:00
|
|
|
if (!printInfo.ServiceState) {
|
2018-06-22 11:12:57 +02:00
|
|
|
std::wcout << L"SERVICE CRITICAL NOT FOUND | 'service'=" << printInfo.ServiceState << ";;;1;7" << '\n';
|
2014-12-19 10:21:38 +01:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2017-12-19 15:50:05 +01:00
|
|
|
if (printInfo.ServiceState != 0x04)
|
2014-11-06 15:17:08 +01:00
|
|
|
printInfo.warn ? state = WARNING : state = CRITICAL;
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case OK:
|
2018-06-22 11:12:57 +02:00
|
|
|
std::wcout << L"SERVICE \"" << printInfo.service << "\" OK RUNNING | 'service'=4;;;1;7" << '\n';
|
2014-11-06 15:17:08 +01:00
|
|
|
break;
|
|
|
|
case WARNING:
|
2018-06-22 11:12:57 +02:00
|
|
|
std::wcout << L"SERVICE \"" << printInfo.service << "\" WARNING NOT RUNNING | 'service'=" << printInfo.ServiceState << ";;;1;7" << '\n';
|
2014-11-06 15:17:08 +01:00
|
|
|
break;
|
|
|
|
case CRITICAL:
|
2018-06-22 11:12:57 +02:00
|
|
|
std::wcout << L"SERVICE \"" << printInfo.service << "\" CRITICAL NOT RUNNING | 'service'=" << printInfo.ServiceState << ";;;1;7" << '\n';
|
2014-11-06 15:17:08 +01:00
|
|
|
break;
|
|
|
|
}
|
2015-02-17 15:20:30 +01:00
|
|
|
|
2014-11-06 15:17:08 +01:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
static std::wstring getServiceByDescription(const std::wstring& description)
|
|
|
|
{
|
2017-07-26 17:02:18 +02:00
|
|
|
SC_HANDLE hSCM = NULL;
|
2018-02-01 08:45:09 +01:00
|
|
|
LPENUM_SERVICE_STATUSW lpServices = NULL;
|
2017-07-26 17:02:18 +02:00
|
|
|
LPBYTE lpBuf = NULL;
|
|
|
|
DWORD cbBufSize = 0;
|
|
|
|
DWORD lpServicesReturned = 0;
|
|
|
|
DWORD pcbBytesNeeded = 0;
|
|
|
|
DWORD lpResumeHandle = 0;;
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_Debug)
|
2017-07-26 17:02:18 +02:00
|
|
|
std::wcout << L"Opening SC Manager" << '\n';
|
|
|
|
|
|
|
|
hSCM = OpenSCManager(NULL, NULL, GENERIC_READ);
|
|
|
|
if (hSCM == NULL)
|
|
|
|
goto die;
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_Debug)
|
2017-07-26 17:02:18 +02:00
|
|
|
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;
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
lpServices = reinterpret_cast<LPENUM_SERVICE_STATUSW>(new BYTE[pcbBytesNeeded]);
|
2017-07-26 17:02:18 +02:00
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_Debug)
|
2017-07-26 17:02:18 +02:00
|
|
|
std::wcout << L"Requesting Service Information. Entry point: " << lpResumeHandle << '\n';
|
|
|
|
|
|
|
|
EnumServicesStatus(hSCM, SERVICE_WIN32 | SERVICE_DRIVER, SERVICE_STATE_ALL, lpServices, pcbBytesNeeded,
|
|
|
|
&pcbBytesNeeded, &lpServicesReturned, &lpResumeHandle);
|
|
|
|
|
2019-04-30 10:32:44 +02:00
|
|
|
for (decltype(lpServicesReturned) index = 0; index < lpServicesReturned; index++) {
|
2017-07-26 17:02:18 +02:00
|
|
|
LPWSTR lpCurrent = lpServices[index].lpServiceName;
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_Debug) {
|
2017-07-26 17:02:18 +02:00
|
|
|
std::wcout << L"Opening Service \"" << lpServices[index].lpServiceName << L"\"\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
SC_HANDLE hService = OpenService(hSCM, lpCurrent, SERVICE_QUERY_CONFIG);
|
2018-02-01 08:45:09 +01:00
|
|
|
if (!hService)
|
2017-07-26 17:02:18 +02:00
|
|
|
goto die;
|
|
|
|
|
|
|
|
DWORD dwBytesNeeded = 0;
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_Debug)
|
2017-07-26 17:02:18 +02:00
|
|
|
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);
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_Debug)
|
2017-07-26 17:02:18 +02:00
|
|
|
std::wcout << "Got description:\n" << desc << '\n';
|
|
|
|
size_t p = desc.find(description);
|
|
|
|
if (desc.find(description) != desc.npos)
|
|
|
|
return lpCurrent;
|
|
|
|
}
|
2018-02-01 08:45:09 +01:00
|
|
|
else if (l_Debug)
|
2017-07-26 17:02:18 +02:00
|
|
|
std::wcout << "No description found\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseServiceHandle(hSCM);
|
|
|
|
delete[] lpServices;
|
|
|
|
return L"";
|
|
|
|
|
|
|
|
die:
|
2018-02-01 08:45:09 +01:00
|
|
|
printErrorInfo();
|
2017-07-26 17:02:18 +02:00
|
|
|
if (hSCM)
|
|
|
|
CloseServiceHandle(hSCM);
|
|
|
|
if (lpServices)
|
|
|
|
delete[] lpServices;
|
|
|
|
return L"";
|
|
|
|
}
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
static DWORD getServiceStatus(const printInfoStruct& printInfo)
|
2014-11-06 16:36:42 +01:00
|
|
|
{
|
2015-02-17 15:20:30 +01:00
|
|
|
SC_HANDLE hSCM;
|
|
|
|
SC_HANDLE hService;
|
2017-12-13 12:47:51 +01:00
|
|
|
DWORD cbBufSize;
|
2017-07-26 17:02:18 +02:00
|
|
|
DWORD lpResumeHandle = 0;
|
2015-02-17 15:20:30 +01:00
|
|
|
LPBYTE lpBuf = NULL;
|
|
|
|
|
2018-02-01 08:45:09 +01:00
|
|
|
if (l_Debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << L"Opening SC Manager" << '\n';
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2015-02-17 15:20:30 +01:00
|
|
|
hSCM = OpenSCManager(NULL, NULL, GENERIC_READ);
|
|
|
|
if (hSCM == NULL)
|
2014-11-06 15:17:08 +01:00
|
|
|
goto die;
|
|
|
|
|
2015-02-17 15:20:30 +01:00
|
|
|
hService = OpenService(hSCM, printInfo.service.c_str(), SERVICE_QUERY_STATUS);
|
|
|
|
if (hService == NULL)
|
2014-11-06 15:17:08 +01:00
|
|
|
goto die;
|
2017-12-13 12:54:14 +01:00
|
|
|
|
2015-02-17 15:20:30 +01:00
|
|
|
QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, NULL, 0, &cbBufSize);
|
|
|
|
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
|
2014-11-06 15:17:08 +01:00
|
|
|
goto die;
|
|
|
|
|
2015-02-17 15:20:30 +01:00
|
|
|
lpBuf = new BYTE[cbBufSize];
|
|
|
|
if (QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, lpBuf, cbBufSize, &cbBufSize)) {
|
|
|
|
LPSERVICE_STATUS_PROCESS pInfo = (LPSERVICE_STATUS_PROCESS)lpBuf;
|
|
|
|
return pInfo->dwCurrentState;
|
2014-11-06 15:17:08 +01:00
|
|
|
}
|
2015-02-17 15:20:30 +01:00
|
|
|
|
2014-11-06 15:17:08 +01:00
|
|
|
die:
|
2018-02-01 08:45:09 +01:00
|
|
|
printErrorInfo();
|
2015-02-17 15:20:30 +01:00
|
|
|
if (hSCM)
|
|
|
|
CloseServiceHandle(hSCM);
|
|
|
|
if (hService)
|
|
|
|
CloseServiceHandle(hService);
|
2017-07-26 17:02:18 +02:00
|
|
|
if (lpBuf)
|
|
|
|
delete [] lpBuf;
|
2015-02-17 15:20:30 +01:00
|
|
|
|
2014-11-06 15:17:08 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2018-02-01 08:45:09 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|