]> granicus.if.org Git - icinga2/blob - plugins/check_swap.cpp
Move new password functions into tlsutility
[icinga2] / plugins / check_swap.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19 #include <Shlwapi.h>
20 #include <iostream>
21 #include <WinBase.h>
22
23 #include "check_swap.h"
24
25 #define VERSION 1.0
26
27 namespace po = boost::program_options;
28
29 static BOOL debug = FALSE;
30
31 INT wmain(INT argc, WCHAR **argv) 
32 {
33         printInfoStruct printInfo = { };
34         po::variables_map vm;
35
36         INT ret = parseArguments(argc, argv, vm, printInfo);
37         if (ret != -1)
38                 return ret;
39
40         ret = check_swap(printInfo);
41         if (ret != -1)
42                 return ret;
43
44         return printOutput(printInfo);
45 }
46
47 INT parseArguments(INT ac, WCHAR **av, po::variables_map& vm, printInfoStruct& printInfo) 
48 {
49         WCHAR namePath[MAX_PATH];
50         GetModuleFileName(NULL, namePath, MAX_PATH);
51         WCHAR *progName = PathFindFileName(namePath);
52
53         po::options_description desc;
54
55         desc.add_options()
56                 ("help,h", "Print help message and exit")
57                 ("version,V", "Print version and exit")
58                 ("debug,d", "Verbose/Debug output")
59                 ("warning,w", po::wvalue<std::wstring>(), "Warning threshold")
60                 ("critical,c", po::wvalue<std::wstring>(), "Critical threshold")
61                 ("unit,u", po::wvalue<std::wstring>(), "The unit to use for display (default MB)")
62                 ;
63
64         po::basic_command_line_parser<WCHAR> parser(ac, av);
65
66         try {
67                 po::store(
68                         parser
69                         .options(desc)
70                         .style(
71                         po::command_line_style::unix_style |
72                         po::command_line_style::allow_long_disguise)
73                         .run(),
74                         vm);
75                 vm.notify();
76         } catch (std::exception& e) {
77                 std::cout << e.what() << '\n' << desc << '\n';
78                 return 3;
79         }
80
81         if (vm.count("help")) {
82                 std::wcout << progName << " Help\n\tVersion: " << VERSION << '\n';
83                 wprintf(
84                         L"%s is a simple program to check a machines swap in percent.\n"
85                         L"You can use the following options to define its behaviour:\n\n", progName);
86                 std::cout << desc;
87                 wprintf(
88                         L"\nIt will then output a string looking something like this:\n\n"
89                         L"\tSWAP WARNING - 20%% free | swap=2000B;3000;500;0;10000\n\n"
90                         L"\"SWAP\" being the type of the check, \"WARNING\" the returned status\n"
91                         L"and \"20%%\" is the returned value.\n"
92                         L"The performance data is found behind the \"|\", in order:\n"
93                         L"returned value, warning threshold, critical threshold, minimal value and,\n"
94                         L"if applicable, the maximal value. Performance data will only be displayed when\n"
95                         L"you set at least one threshold\n\n"
96                         L"%s' exit codes denote the following:\n"
97                         L" 0\tOK,\n\tNo Thresholds were broken or the programs check part was not executed\n"
98                         L" 1\tWARNING,\n\tThe warning, but not the critical threshold was broken\n"
99                         L" 2\tCRITICAL,\n\tThe critical threshold was broken\n"
100                         L" 3\tUNKNOWN, \n\tThe program experienced an internal or input error\n\n"
101                         L"Threshold syntax:\n\n"
102                         L"-w THRESHOLD\n"
103                         L"warn if threshold is broken, which means VALUE > THRESHOLD\n"
104                         L"(unless stated differently)\n\n"
105                         L"-w !THRESHOLD\n"
106                         L"inverts threshold check, VALUE < THRESHOLD (analogous to above)\n\n"
107                         L"-w [THR1-THR2]\n"
108                         L"warn is VALUE is inside the range spanned by THR1 and THR2\n\n"
109                         L"-w ![THR1-THR2]\n"
110                         L"warn if VALUE is outside the range spanned by THR1 and THR2\n\n"
111                         L"-w THRESHOLD%%\n"
112                         L"if the plugin accepts percentage based thresholds those will be used.\n"
113                         L"Does nothing if the plugin does not accept percentages, or only uses\n"
114                         L"percentage thresholds. Ranges can be used with \"%%\", but both range values need\n"
115                         L"to end with a percentage sign.\n\n"
116                         L"All of these options work with the critical threshold \"-c\" too.\n"
117                         , progName);
118                 std::cout << '\n';
119                 return 0;
120         }
121
122         if (vm.count("version"))
123                 std::wcout << L"Version: " << VERSION << '\n';
124
125         if (vm.count("warning")) {
126                 try {
127                         printInfo.warn = threshold(vm["warning"].as<std::wstring>());
128                 } catch (std::invalid_argument& e) {
129                         std::cout << e.what() << '\n';
130                         return 3;
131                 }
132                 printInfo.warn.legal = !printInfo.warn.legal;
133         }
134
135         if (vm.count("critical")) {
136                 try {
137                         printInfo.crit = threshold(vm["critical"].as<std::wstring>());
138                 } catch (std::invalid_argument& e) {
139                         std::cout << e.what() << '\n';
140                         return 3;
141                 }
142                 printInfo.crit.legal = !printInfo.crit.legal;
143         }
144
145         if (vm.count("debug"))
146                 debug = TRUE;
147
148         if (vm.count("unit")) {
149                 try {
150                         printInfo.unit = parseBUnit(vm["unit"].as<std::wstring>());
151                 } catch (std::invalid_argument& e) {
152                         std::cout << e.what() << '\n';
153                         return 3;
154                 }
155         }
156
157         return -1;
158 }
159
160 INT printOutput(printInfoStruct& printInfo) 
161 {
162         if (debug)
163                 std::wcout << L"Constructing output string" << '\n';
164
165         state state = OK;
166
167         if (printInfo.warn.rend(printInfo.aSwap, printInfo.tSwap))
168                 state = WARNING;
169
170         if (printInfo.crit.rend(printInfo.aSwap, printInfo.tSwap))
171                 state = CRITICAL;
172
173         switch (state) {
174         case OK:
175                 std::wcout << L"SWAP OK - " << printInfo.percentFree << L"% free | swap=" << printInfo.aSwap << BunitStr(printInfo.unit) << L";"
176                         << printInfo.warn.pString(printInfo.tSwap) << L";" << printInfo.crit.pString(printInfo.tSwap) 
177                         << L";0;" << printInfo.tSwap << '\n';
178                 break;
179         case WARNING:
180                 std::wcout << L"SWAP WARNING - " << printInfo.percentFree << L"% free | swap=" << printInfo.aSwap << BunitStr(printInfo.unit) << L";"
181                         << printInfo.warn.pString(printInfo.tSwap) << L";" << printInfo.crit.pString(printInfo.tSwap) 
182                         << L";0;" << printInfo.tSwap << '\n';
183                 break;
184         case CRITICAL:
185                 std::wcout << L"SWAP CRITICAL - " << printInfo.percentFree << L"% free | swap=" << printInfo.aSwap << BunitStr(printInfo.unit) << L";"
186                         << printInfo.warn.pString(printInfo.tSwap) << L";" << printInfo.crit.pString(printInfo.tSwap) 
187                         << L";0;" << printInfo.tSwap << '\n';
188                 break;
189         }
190
191         return state;
192 }
193
194 INT check_swap(printInfoStruct& printInfo) 
195 {
196         MEMORYSTATUSEX MemBuf;
197         MemBuf.dwLength = sizeof(MemBuf);
198
199         if (!GlobalMemoryStatusEx(&MemBuf)) {
200                 die();
201                 return 3;
202         }
203
204         printInfo.tSwap = round(MemBuf.ullTotalPageFile / pow(1024.0, printInfo.unit));
205         printInfo.aSwap = round(MemBuf.ullAvailPageFile / pow(1024.0, printInfo.unit));
206         printInfo.percentFree = 100.0 * MemBuf.ullAvailPageFile / MemBuf.ullTotalPageFile;
207
208         return -1;
209 }