1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
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 #ifndef WIN32_LEAN_AND_MEAN
21 #define WIN32_LEAN_AND_MEAN //else winsock will be included with windows.h and conflict with winsock2
34 #include "check_ping.h"
38 namespace po = boost::program_options;
40 static BOOL debug = FALSE;
42 INT wmain(INT argc, WCHAR **argv)
45 printInfoStruct printInfo;
50 if (WSAStartup(MAKEWORD(2, 2), &dat)) {
51 std::cout << "WSAStartup failed\n";
55 if (parseArguments(argc, argv, vm, printInfo) != -1)
58 if (!resolveHostname(printInfo.host, printInfo.ipv6, printInfo.ip))
62 if (check_ping6(printInfo, response) != -1)
65 if (check_ping4(printInfo, response) != -1)
70 return printOutput(printInfo, response);
73 INT parseArguments(INT ac, WCHAR **av, po::variables_map& vm, printInfoStruct& printInfo)
75 WCHAR namePath[MAX_PATH];
76 GetModuleFileName(NULL, namePath, MAX_PATH);
77 WCHAR *progName = PathFindFileName(namePath);
79 po::options_description desc;
82 ("help,h", "Print usage message and exit")
83 ("version,V", "Print version and exit")
84 ("debug,d", "Verbose/Debug output")
85 ("host,H", po::wvalue<std::wstring>()->required(), "Target hostname or IP. If an IPv6 address is given, the '-6' option must be set")
86 (",4", "--Host is an IPv4 address or if it's a hostname: Resolve it to an IPv4 address (default)")
87 (",6", "--Host is an IPv6 address or if it's a hostname: Resolve it to an IPv6 address")
88 ("timeout,t", po::value<INT>(), "Specify timeout for requests in ms (default=1000)")
89 ("packets,p", po::value<INT>(), "Declare ping count (default=5)")
90 ("warning,w", po::wvalue<std::wstring>(), "Warning values: rtt,package loss")
91 ("critical,c", po::wvalue<std::wstring>(), "Critical values: rtt,package loss")
94 po::basic_command_line_parser<WCHAR> parser(ac, av);
101 po::command_line_style::unix_style |
102 po::command_line_style::allow_long_disguise &
103 ~po::command_line_style::allow_guessing
108 } catch (std::exception& e) {
109 std::cout << e.what() << '\n' << desc << '\n';
113 if (vm.count("help")) {
114 std::wcout << progName << " Help\n\tVersion: " << VERSION << '\n';
116 L"%s is a simple program to ping an ip4 address.\n"
117 L"You can use the following options to define its behaviour:\n\n", progName);
120 L"\nIt will take at least timeout times number of pings to run\n"
121 L"Then it will output a string looking something like this:\n\n"
122 L"\tPING WARNING RTA: 72ms Packet loss: 20%% | ping=72ms;40;80;71;77 pl=20%%;20;50;0;100\n\n"
123 L"\"PING\" being the type of the check, \"WARNING\" the returned status\n"
124 L"and \"RTA: 72ms Packet loss: 20%%\" the relevant information.\n"
125 L"The performance data is found behind the \"|\", in order:\n"
126 L"returned value, warning threshold, critical threshold, minimal value and,\n"
127 L"if applicable, the maximal value. \n\n"
128 L"%s' exit codes denote the following:\n"
129 L" 0\tOK,\n\tNo Thresholds were broken or the programs check part was not executed\n"
130 L" 1\tWARNING,\n\tThe warning, but not the critical threshold was broken\n"
131 L" 2\tCRITICAL,\n\tThe critical threshold was broken\n"
132 L" 3\tUNKNOWN, \n\tThe program experienced an internal or input error\n\n"
133 L"Threshold syntax:\n\n"
135 L"warn if threshold is broken, which means VALUE > THRESHOLD\n"
136 L"(unless stated differently)\n\n"
138 L"inverts threshold check, VALUE < THRESHOLD (analogous to above)\n\n"
140 L"warn is VALUE is inside the range spanned by THR1 and THR2\n\n"
142 L"warn if VALUE is outside the range spanned by THR1 and THR2\n\n"
144 L"if the plugin accepts percentage based thresholds those will be used.\n"
145 L"Does nothing if the plugin does not accept percentages, or only uses\n"
146 L"percentage thresholds. Ranges can be used with \"%%\", but both range values need\n"
147 L"to end with a percentage sign.\n\n"
148 L"All of these options work with the critical threshold \"-c\" too.",
154 if (vm.count("version")) {
155 std::cout << progName << " Version: " << VERSION << '\n';
159 if (vm.count("-4") && vm.count("-6")) {
160 std::cout << "Conflicting options \"4\" and \"6\"" << '\n';
164 printInfo.ipv6 = TRUE;
166 if (vm.count("warning")) {
167 std::vector<std::wstring> sVec = splitMultiOptions(vm["warning"].as<std::wstring>());
168 if (sVec.size() != 2) {
169 std::cout << "Wrong format for warning thresholds" << '\n';
173 printInfo.warn = threshold(*sVec.begin());
174 printInfo.wpl = threshold(sVec.back());
175 if (!printInfo.wpl.perc) {
176 std::cout << "Packet loss must be percentage" << '\n';
179 } catch (std::invalid_argument& e) {
180 std::cout << e.what() << '\n';
184 if (vm.count("critical")) {
185 std::vector<std::wstring> sVec = splitMultiOptions(vm["critical"].as<std::wstring>());
186 if (sVec.size() != 2) {
187 std::cout << "Wrong format for critical thresholds" << '\n';
191 printInfo.crit = threshold(*sVec.begin());
192 printInfo.cpl = threshold(sVec.back());
193 if (!printInfo.wpl.perc) {
194 std::cout << "Packet loss must be percentage" << '\n';
197 } catch (std::invalid_argument& e) {
198 std::cout << e.what() << '\n';
203 if (vm.count("timeout"))
204 printInfo.timeout = vm["timeout"].as<INT>();
205 if (vm.count("packets"))
206 printInfo.num = vm["packets"].as<INT>();
209 printInfo.host = vm["host"].as<std::wstring>();
211 if (vm.count("debug"))
217 INT printOutput(printInfoStruct& printInfo, response& response)
220 std::wcout << L"Constructing output string" << '\n';
224 double plp = ((double)response.dropped / printInfo.num) * 100.0;
226 if (printInfo.warn.rend(response.avg) || printInfo.wpl.rend(plp))
229 if (printInfo.crit.rend(response.avg) || printInfo.cpl.rend(plp))
232 std::wstringstream perf;
233 perf << L"rta=" << response.avg << L"ms;" << printInfo.warn.pString() << L";"
234 << printInfo.crit.pString() << L";0;" << " pl=" << removeZero(plp) << "%;"
235 << printInfo.wpl.pString() << ";" << printInfo.cpl.pString() << ";0;100";
237 if (response.dropped == printInfo.num) {
238 std::wcout << L"PING CRITICAL ALL CONNECTIONS DROPPED | " << perf.str() << '\n';
244 std::wcout << L"PING OK RTA: " << response.avg << L"ms Packet loss: " << removeZero(plp) << "% | " << perf.str() << '\n';
247 std::wcout << L"PING WARNING RTA: " << response.avg << L"ms Packet loss: " << removeZero(plp) << "% | " << perf.str() << '\n';
250 std::wcout << L"PING CRITICAL RTA: " << response.avg << L"ms Packet loss: " << removeZero(plp) << "% | " << perf.str() << '\n';
257 BOOL resolveHostname(CONST std::wstring hostname, BOOL ipv6, std::wstring& ipaddr)
259 ADDRINFOW *result = NULL;
260 ADDRINFOW *ptr = NULL;
262 ZeroMemory(&hints, sizeof(hints));
263 wchar_t ipstringbuffer[46];
266 hints.ai_family = AF_INET6;
268 hints.ai_family = AF_INET;
271 std::wcout << L"Resolving hostname \"" << hostname << L"\"\n";
273 DWORD ret = GetAddrInfoW(hostname.c_str(), NULL, &hints, &result);
276 std::cout << "Failed to resolve hostname. Winsock Error Code: " << ret << '\n';
281 struct sockaddr_in6 *address6 = (struct sockaddr_in6 *) result->ai_addr;
282 InetNtop(AF_INET6, &address6->sin6_addr, ipstringbuffer, 46);
284 struct sockaddr_in *address4 = (struct sockaddr_in *) result->ai_addr;
285 InetNtop(AF_INET, &address4->sin_addr, ipstringbuffer, 46);
289 std::wcout << L"Resolved to \"" << ipstringbuffer << L"\"\n";
291 ipaddr = ipstringbuffer;
295 INT check_ping4(CONST printInfoStruct& pi, response& response)
299 DWORD dwRet = 0, dwRepSize = 0;
300 LPVOID repBuf = NULL;
303 LARGE_INTEGER frequency, timer1, timer2;
307 std::wcout << L"Parsing ip address" << '\n';
309 if (RtlIpv4StringToAddress(pi.ip.c_str(), TRUE, &term, &ipDest4) == STATUS_INVALID_PARAMETER) {
310 std::wcout << pi.ip << " is not a valid ip address\n";
314 if (*term != L'\0') {
315 std::wcout << pi.ip << " is not a valid ip address\n";
320 std::wcout << L"Creating Icmp File\n";
322 if ((hIcmp = IcmpCreateFile()) == INVALID_HANDLE_VALUE)
325 dwRepSize = sizeof(ICMP_ECHO_REPLY) + 8;
326 repBuf = reinterpret_cast<VOID *>(new BYTE[dwRepSize]);
331 QueryPerformanceFrequency(&frequency);
333 QueryPerformanceCounter(&timer1);
336 std::wcout << L"Sending Icmp echo\n";
338 if (!IcmpSendEcho2(hIcmp, NULL, NULL, NULL, ipDest4.S_un.S_addr,
339 NULL, 0, NULL, repBuf, dwRepSize, pi.timeout)) {
342 std::wcout << L"Dropped: Response was 0" << '\n';
347 std::wcout << "Ping recieved" << '\n';
349 PICMP_ECHO_REPLY pEchoReply = static_cast<PICMP_ECHO_REPLY>(repBuf);
351 if (pEchoReply->Status != IP_SUCCESS) {
354 std::wcout << L"Dropped: echo reply status " << pEchoReply->Status << '\n';
359 std::wcout << L"Recorded rtt of " << pEchoReply->RoundTripTime << '\n';
361 rtt += pEchoReply->RoundTripTime;
362 if (response.pMin == 0 || pEchoReply->RoundTripTime < response.pMin)
363 response.pMin = pEchoReply->RoundTripTime;
364 else if (pEchoReply->RoundTripTime > response.pMax)
365 response.pMax = pEchoReply->RoundTripTime;
367 QueryPerformanceCounter(&timer2);
368 if (((timer2.QuadPart - timer1.QuadPart) * 1000 / frequency.QuadPart) < pi.timeout)
369 Sleep(pi.timeout - ((timer2.QuadPart - timer1.QuadPart) * 1000 / frequency.QuadPart));
373 std::wcout << L"All pings sent. Cleaning up and returning" << '\n';
376 IcmpCloseHandle(hIcmp);
378 delete reinterpret_cast<VOID *>(repBuf);
380 response.avg = ((double)rtt / pi.num);
387 IcmpCloseHandle(hIcmp);
389 delete reinterpret_cast<VOID *>(repBuf);
394 INT check_ping6(CONST printInfoStruct& pi, response& response)
396 sockaddr_in6 ipDest6, ipSource6;
397 IP_OPTION_INFORMATION ipInfo = { 30, 0, 0, 0, NULL };
398 DWORD dwRepSize = sizeof(ICMPV6_ECHO_REPLY) + 8;
399 LPVOID repBuf = reinterpret_cast<VOID *>(new BYTE[dwRepSize]);
402 LARGE_INTEGER frequency, timer1, timer2;
407 std::wcout << L"Parsing ip address" << '\n';
409 if (RtlIpv6StringToAddressEx(pi.ip.c_str(), &ipDest6.sin6_addr, &ipDest6.sin6_scope_id, &ipDest6.sin6_port)) {
410 std::wcout << pi.ip << " is not a valid ipv6 address" << '\n';
414 ipDest6.sin6_family = AF_INET6;
416 ipSource6.sin6_addr = in6addr_any;
417 ipSource6.sin6_family = AF_INET6;
418 ipSource6.sin6_flowinfo = 0;
419 ipSource6.sin6_port = 0;
422 std::wcout << L"Creating Icmp File" << '\n';
424 hIcmp = Icmp6CreateFile();
425 if (hIcmp == INVALID_HANDLE_VALUE) {
429 QueryPerformanceFrequency(&frequency);
431 QueryPerformanceCounter(&timer1);
434 std::wcout << L"Sending Icmp echo" << '\n';
436 if (!Icmp6SendEcho2(hIcmp, NULL, NULL, NULL, &ipSource6, &ipDest6,
437 NULL, 0, &ipInfo, repBuf, dwRepSize, pi.timeout)) {
440 std::wcout << L"Dropped: Response was 0" << '\n';
445 std::wcout << "Ping recieved" << '\n';
447 Icmp6ParseReplies(repBuf, dwRepSize);
449 ICMPV6_ECHO_REPLY *pEchoReply = static_cast<ICMPV6_ECHO_REPLY *>(repBuf);
451 if (pEchoReply->Status != IP_SUCCESS) {
454 std::wcout << L"Dropped: echo reply status " << pEchoReply->Status << '\n';
458 rtt += pEchoReply->RoundTripTime;
461 std::wcout << L"Recorded rtt of " << pEchoReply->RoundTripTime << '\n';
463 if (response.pMin == 0 || pEchoReply->RoundTripTime < response.pMin)
464 response.pMin = pEchoReply->RoundTripTime;
465 else if (pEchoReply->RoundTripTime > response.pMax)
466 response.pMax = pEchoReply->RoundTripTime;
468 QueryPerformanceCounter(&timer2);
469 if (((timer2.QuadPart - timer1.QuadPart) * 1000 / frequency.QuadPart) < pi.timeout)
470 Sleep(pi.timeout - ((timer2.QuadPart - timer1.QuadPart) * 1000 / frequency.QuadPart));
474 std::wcout << L"All pings sent. Cleaning up and returning" << '\n';
477 IcmpCloseHandle(hIcmp);
479 delete reinterpret_cast<VOID *>(repBuf);
480 response.avg = ((double)rtt / pi.num);
487 IcmpCloseHandle(hIcmp);
489 delete reinterpret_cast<VOID *>(repBuf);