2014-11-06 16:36:42 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2014-11-06 16:36:42 +01:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy 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. *
|
|
|
|
******************************************************************************/
|
2014-11-06 15:17:08 +01:00
|
|
|
#include <Windows.h>
|
|
|
|
#include <Shlwapi.h>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "thresholds.h"
|
|
|
|
|
2014-11-14 13:54:29 +01:00
|
|
|
#include "boost/program_options.hpp"
|
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;
|
|
|
|
|
|
|
|
using std::wcout; using std::endl;
|
|
|
|
using std::cout; using std::wstring;
|
|
|
|
|
2015-01-12 15:53:25 +01:00
|
|
|
static BOOL debug;
|
|
|
|
|
2014-11-06 16:36:42 +01:00
|
|
|
struct printInfoStruct
|
|
|
|
{
|
2014-11-06 15:17:08 +01:00
|
|
|
bool warn;
|
2015-02-17 15:20:30 +01:00
|
|
|
DWORD ServiceState;
|
2014-11-06 15:17:08 +01:00
|
|
|
wstring service;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int parseArguments(int, wchar_t **, po::variables_map&, printInfoStruct&);
|
|
|
|
static int printOutput(const printInfoStruct&);
|
2015-02-17 15:20:30 +01:00
|
|
|
static DWORD ServiceStatus(const printInfoStruct&);
|
2014-11-06 15:17:08 +01:00
|
|
|
|
|
|
|
int wmain(int argc, wchar_t **argv)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-11-06 16:36:42 +01:00
|
|
|
int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct& printInfo)
|
|
|
|
{
|
2014-11-06 15:17:08 +01:00
|
|
|
wchar_t namePath[MAX_PATH];
|
|
|
|
GetModuleFileName(NULL, namePath, MAX_PATH);
|
|
|
|
wchar_t *progName = PathFindFileName(namePath);
|
|
|
|
|
|
|
|
po::options_description desc;
|
|
|
|
|
|
|
|
desc.add_options()
|
2014-12-19 10:21:38 +01:00
|
|
|
("help,h", "print help message and exit")
|
|
|
|
("version,V", "print version and exit")
|
2015-01-12 15:53:25 +01:00
|
|
|
("debug,d", "Verbose/Debug output")
|
2014-11-06 15:17:08 +01:00
|
|
|
("service,s", po::wvalue<wstring>(), "service to check (required)")
|
2014-11-06 16:36:42 +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
|
|
|
;
|
|
|
|
|
|
|
|
po::basic_command_line_parser<wchar_t> parser(ac, av);
|
|
|
|
|
|
|
|
try {
|
|
|
|
po::store(
|
|
|
|
parser
|
|
|
|
.options(desc)
|
|
|
|
.style(
|
|
|
|
po::command_line_style::unix_style |
|
|
|
|
po::command_line_style::allow_long_disguise)
|
|
|
|
.run(),
|
|
|
|
vm);
|
|
|
|
vm.notify();
|
|
|
|
} catch (std::exception& e) {
|
|
|
|
cout << e.what() << endl << desc << endl;
|
|
|
|
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")) {
|
|
|
|
wcout << progName << " Help\n\tVersion: " << VERSION << endl;
|
|
|
|
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;
|
|
|
|
wprintf(
|
|
|
|
L"\nIt will then output a string looking something like this:\n\n"
|
2014-11-10 14:39:12 +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);
|
2014-11-06 15:17:08 +01:00
|
|
|
cout << endl;
|
|
|
|
return 0;
|
|
|
|
}
|
2015-02-17 15:20:30 +01:00
|
|
|
|
2014-11-06 15:17:08 +01:00
|
|
|
if (vm.count("version")) {
|
|
|
|
cout << "Version: " << VERSION << endl;
|
|
|
|
return 0;
|
2014-11-06 16:36:42 +01:00
|
|
|
}
|
2015-02-17 15:20:30 +01:00
|
|
|
|
|
|
|
if (!vm.count("service")) {
|
2014-11-06 15:17:08 +01:00
|
|
|
cout << "Missing argument: service" << endl << desc << endl;
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vm.count("warn"))
|
|
|
|
printInfo.warn = true;
|
|
|
|
|
|
|
|
printInfo.service = vm["service"].as<wstring>();
|
2015-01-12 15:53:25 +01:00
|
|
|
|
|
|
|
if (vm.count("debug"))
|
|
|
|
debug = TRUE;
|
2014-11-06 15:17:08 +01:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-06 16:36:42 +01:00
|
|
|
int printOutput(const printInfoStruct& printInfo)
|
|
|
|
{
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
|
|
|
wcout << L"Constructing output string" << endl;
|
|
|
|
|
2014-11-06 15:17:08 +01:00
|
|
|
wstring perf;
|
|
|
|
state state = OK;
|
2014-11-06 16:36:42 +01:00
|
|
|
|
2014-12-19 10:21:38 +01:00
|
|
|
if (!printInfo.ServiceState) {
|
2015-02-17 15:20:30 +01:00
|
|
|
wcout << L"SERVICE CRITICAL NOTFOUND | service=" << printInfo.ServiceState << ";!4;!4;1;7" << endl;
|
2014-12-19 10:21:38 +01:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2014-11-06 15:17:08 +01:00
|
|
|
if (printInfo.ServiceState != 0x04)
|
|
|
|
printInfo.warn ? state = WARNING : state = CRITICAL;
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case OK:
|
2014-12-19 10:21:38 +01:00
|
|
|
wcout << L"SERVICE OK RUNNING | service=4;!4;!4;1;7" << endl;
|
2014-11-06 15:17:08 +01:00
|
|
|
break;
|
|
|
|
case WARNING:
|
2015-02-17 15:20:30 +01:00
|
|
|
wcout << L"SERVICE WARNING NOT RUNNING | service=" << printInfo.ServiceState << ";!4;!4;1;7" << endl;
|
2014-11-06 15:17:08 +01:00
|
|
|
break;
|
|
|
|
case CRITICAL:
|
2015-02-17 15:20:30 +01:00
|
|
|
wcout << L"SERVICE CRITICAL NOT RUNNING | service=" << printInfo.ServiceState << ";!4;!4;1;7" << endl;
|
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;
|
|
|
|
}
|
|
|
|
|
2015-02-17 15:20:30 +01:00
|
|
|
DWORD ServiceStatus(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;
|
|
|
|
DWORD cbBufSize;
|
|
|
|
LPBYTE lpBuf = NULL;
|
|
|
|
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
|
|
|
wcout << L"Opening SC Manager" << endl;
|
|
|
|
|
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-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-02-17 15:20:30 +01:00
|
|
|
wcout << L"Getting Service Information" << endl;
|
2015-01-12 15:53:25 +01:00
|
|
|
|
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;
|
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:
|
2014-12-09 15:38:23 +01:00
|
|
|
die();
|
2015-02-17 15:20:30 +01:00
|
|
|
if (hSCM)
|
|
|
|
CloseServiceHandle(hSCM);
|
|
|
|
if (hService)
|
|
|
|
CloseServiceHandle(hService);
|
2015-03-02 13:23:26 +01:00
|
|
|
delete [] lpBuf;
|
2015-02-17 15:20:30 +01:00
|
|
|
|
2014-11-06 15:17:08 +01:00
|
|
|
return -1;
|
|
|
|
}
|