1 /******************************************************************************
3 * Copyright (C) 2012-2014 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 ******************************************************************************/
23 #include "thresholds.h"
25 #include "boost/program_options.hpp"
29 namespace po = boost::program_options;
31 using std::wcout; using std::endl;
32 using std::cout; using std::wstring;
34 struct printInfoStruct
41 static int parseArguments(int, wchar_t **, po::variables_map&, printInfoStruct&);
42 static int printOutput(const printInfoStruct&);
43 static int ServiceStatus(const printInfoStruct&);
45 int wmain(int argc, wchar_t **argv)
48 printInfoStruct printInfo = { false, 0, L"" };
50 int ret = parseArguments(argc, argv, vm, printInfo);
54 printInfo.ServiceState = ServiceStatus(printInfo);
55 if (printInfo.ServiceState == -1)
58 return printOutput(printInfo);
61 int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct& printInfo)
63 wchar_t namePath[MAX_PATH];
64 GetModuleFileName(NULL, namePath, MAX_PATH);
65 wchar_t *progName = PathFindFileName(namePath);
67 po::options_description desc;
70 (",h", "print help message and exit")
71 ("help", "print verbose help and exit")
72 ("version,v", "print version and exit")
73 ("service,s", po::wvalue<wstring>(), "service to check (required)")
74 ("warn,w", "return warning (1) instead of critical (2),\n when service is not running")
77 po::basic_command_line_parser<wchar_t> parser(ac, av);
84 po::command_line_style::unix_style |
85 po::command_line_style::allow_long_disguise)
89 } catch (std::exception& e) {
90 cout << e.what() << endl << desc << endl;
99 if (vm.count("help")) {
100 wcout << progName << " Help\n\tVersion: " << VERSION << endl;
102 L"%s is a simple program to check the status of a service.\n"
103 L"You can use the following options to define its behaviour:\n\n", progName);
106 L"\nIt will then output a string looking something like this:\n\n"
107 L"\tSERVICE CRITICAL NOT_RUNNING|service=4;!4;!4;1;7\n\n"
108 L"\"SERVICE\" being the type of the check, \"CRITICAL\" the returned status\n"
109 L"and \"1\" is the returned value.\n"
110 L"A service is either running (Code 0x04) or not running (any other).\n"
111 L"For more information consult the msdn on service state transitions.\n\n"
112 L"%s' exit codes denote the following:\n"
113 L" 0\tOK,\n\tNo Thresholds were broken or the programs check part was not executed\n"
114 L" 1\tWARNING,\n\tThe warning, but not the critical threshold was broken\n"
115 L" 2\tCRITICAL,\n\tThe critical threshold was broken\n"
116 L" 3\tUNKNOWN, \n\tThe program experienced an internal or input error\n\n"
117 L"%s' thresholds work differently, since a service is either running or not\n"
118 L"all \"-w\" and \"-c\" do is say whether a not running service is a warning\n"
119 L"or critical state respectively.\n"
120 , progName, progName);
125 if (vm.count("version")) {
126 cout << "Version: " << VERSION << endl;
130 if (!vm.count("service")) {
131 cout << "Missing argument: service" << endl << desc << endl;
135 if (vm.count("warn"))
136 printInfo.warn = true;
138 printInfo.service = vm["service"].as<wstring>();
143 int printOutput(const printInfoStruct& printInfo)
148 if (printInfo.ServiceState != 0x04)
149 printInfo.warn ? state = WARNING : state = CRITICAL;
153 wcout << L"SERVICE OK RUNNING|service=4;!4;!4;1;7" << endl;
156 wcout << L"SERVICE WARNING NOT_RUNNING|service=" << printInfo.ServiceState << ";!4;!4;1;7" << endl;
159 wcout << L"SERVICE CRITICAL NOT_RUNNING|service=" << printInfo.ServiceState << ";!4;!4;1;7" << endl;
166 int ServiceStatus(const printInfoStruct& printInfo)
168 SC_HANDLE service_api = OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
169 if (service_api == NULL)
172 LPBYTE lpServices = NULL;
174 DWORD pcbBytesNeeded = NULL, ServicesReturned = NULL, ResumeHandle = NULL;
176 if (!EnumServicesStatusEx(service_api, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_STATE_ALL,
177 lpServices, cbBufSize, &pcbBytesNeeded, &ServicesReturned, &ResumeHandle, NULL)
178 && GetLastError() != ERROR_MORE_DATA)
181 lpServices = reinterpret_cast<LPBYTE>(new BYTE[pcbBytesNeeded]);
182 cbBufSize = pcbBytesNeeded;
184 if (!EnumServicesStatusEx(service_api, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_STATE_ALL,
185 lpServices, cbBufSize, &pcbBytesNeeded, &ServicesReturned, &ResumeHandle, NULL))
188 LPENUM_SERVICE_STATUS_PROCESS pInfo = (LPENUM_SERVICE_STATUS_PROCESS)lpServices;
190 for (DWORD i = 0; i < ServicesReturned; i++) {
191 if (!wcscmp(printInfo.service.c_str(), pInfo[i].lpServiceName)) {
192 int state = pInfo[i].ServiceStatusProcess.dwCurrentState;
197 wcout << L"Service " << printInfo.service << L" could not be found\n";
198 delete[] reinterpret_cast<LPBYTE>(lpServices);
204 delete[] reinterpret_cast<LPBYTE>(lpServices);