1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://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 #include "plugins/thresholds.hpp"
21 #include <boost/program_options.hpp>
30 #define CRITERIA L"(IsInstalled = 0 and CategoryIDs contains '0fa1201d-4330-4fa8-8ae9-b877473b6441') or (IsInstalled = 0 and CategoryIDs contains 'E6CF1350-C01B-414D-A61F-263D14D133B4')"
32 namespace po = boost::program_options;
34 struct printInfoStruct
39 bool important{false};
41 bool careForCanRequest{false};
46 static int parseArguments(int ac, WCHAR **av, po::variables_map& vm, printInfoStruct& printInfo)
48 WCHAR namePath[MAX_PATH];
49 GetModuleFileName(NULL, namePath, MAX_PATH);
50 WCHAR *progName = PathFindFileName(namePath);
52 po::options_description desc;
55 ("help,h", "Print help message and exit")
56 ("version,V", "Print version and exit")
57 ("debug,d", "Verbose/Debug output")
58 ("warning,w", "Warn if there are important updates available")
59 ("critical,c", "Critical if there are important updates that require a reboot")
60 ("possible-reboot", "Treat \"update may need reboot\" as \"update needs reboot\"")
63 po::wcommand_line_parser parser(ac, av);
70 po::command_line_style::unix_style |
71 po::command_line_style::allow_long_disguise)
75 } catch (const std::exception& e) {
76 std::cout << e.what() << '\n' << desc << '\n';
80 if (vm.count("help")) {
81 std::wcout << progName << " Help\n\tVersion: " << VERSION << '\n';
83 L"%s is a simple program to check a machines required updates.\n"
84 L"You can use the following options to define its behaviour:\n\n", progName);
87 L"\nAfter some time, it will then output a string like this one:\n\n"
88 L"\tUPDATE WARNING 8 | updates=8;1;1;0\n\n"
89 L"\"UPDATE\" being the type of the check, \"WARNING\" the returned status\n"
90 L"and \"8\" is the number of important updates updates.\n"
91 L"The performance data is found behind the \"|\", in order:\n"
92 L"returned value, warning threshold, critical threshold, minimal value and,\n"
93 L"if applicable, the maximal value. Performance data will only be displayed when\n"
94 L"you set at least one threshold\n\n"
95 L"An update counts as important when it is part of the Security- or\n"
96 L"CriticalUpdates group.\n"
97 L"Consult the msdn on WSUS Classification GUIDs for more information.\n"
98 L"%s' exit codes denote the following:\n"
99 L" 0\tOK,\n\tNo Thresholds were broken or the programs check part was not executed\n"
100 L" 1\tWARNING,\n\tThe warning, but not the critical threshold was broken\n"
101 L" 2\tCRITICAL,\n\tThe critical threshold was broken\n"
102 L" 3\tUNKNOWN, \n\tThe program experienced an internal or input error\n\n"
103 L"%s works different from other plugins in that you do not set thresholds\n"
104 L"but only activate them. Using \"-w\" triggers warning state if there are not\n"
105 L"installed and non-optional updates. \"-c\" triggers critical if there are\n"
106 L"non-optional updates that require a reboot.\n"
107 L"The \"possible-reboot\" option is not recommended since this true for nearly\n"
109 , progName, progName);
112 } if (vm.count("version")) {
113 std::cout << "Version: " << VERSION << '\n';
117 printInfo.warn = vm.count("warning") > 0;
118 printInfo.crit = vm.count("critical") > 0;
119 printInfo.careForCanRequest = vm.count("possible-reboot") > 0;
121 l_Debug = vm.count("debug") > 0;
126 static int printOutput(const printInfoStruct& printInfo)
129 std::wcout << L"Constructing output string" << '\n';
132 std::wstring output = L"UPDATE ";
134 if (printInfo.important)
137 if (printInfo.reboot)
142 output.append(L"OK ");
145 output.append(L"WARNING ");
148 output.append(L"CRITICAL ");
152 std::wcout << output << printInfo.numUpdates << L" | 'update'=" << printInfo.numUpdates << L";"
153 << printInfo.warn << L";" << printInfo.crit << L";0;" << '\n';
158 static int check_update(printInfoStruct& printInfo)
161 std::wcout << "Initializing COM library" << '\n';
162 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
163 ISearchResult *pResult;
164 IUpdateSession *pSession;
165 IUpdateSearcher *pSearcher;
166 BSTR criteria = NULL;
170 std::wcout << "Creating UpdateSession and UpdateSearcher" << '\n';
171 CoCreateInstance(CLSID_UpdateSession, NULL, CLSCTX_INPROC_SERVER, IID_IUpdateSession, (void **)&pSession);
172 pSession->CreateUpdateSearcher(&pSearcher);
175 * IsInstalled = 0: All updates, including languagepacks and features
176 * BrowseOnly = 0: No features or languagepacks, security and unnamed
177 * BrowseOnly = 1: Nothing, broken
178 * RebootRequired = 1: Reboot required
181 criteria = SysAllocString(CRITERIA);
182 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa386526%28v=vs.85%29.aspx
183 // https://msdn.microsoft.com/en-us/library/ff357803%28v=vs.85%29.aspx
186 std::wcout << L"Querying updates from server" << '\n';
188 err = pSearcher->Search(criteria, &pResult);
191 SysFreeString(criteria);
193 IUpdateCollection *pCollection;
197 pResult->get_Updates(&pCollection);
198 pCollection->get_Count(&updateSize);
203 printInfo.numUpdates = updateSize;
204 // printInfo.important = printInfo.warn;
206 IInstallationBehavior *pIbehav;
207 InstallationRebootBehavior updateReboot;
209 for (LONG i = 0; i < updateSize; i++) {
210 pCollection->get_Item(i, &pUpdate);
212 std::wcout << L"Checking reboot behaviour of update number " << i << '\n';
214 pUpdate->get_InstallationBehavior(&pIbehav);
215 pIbehav->get_RebootBehavior(&updateReboot);
216 if (updateReboot == irbAlwaysRequiresReboot) {
217 printInfo.reboot = true;
219 std::wcout << L"It requires reboot" << '\n';
222 if (printInfo.careForCanRequest && updateReboot == irbCanRequestReboot)
224 std::wcout << L"It requires reboot" << '\n';
225 printInfo.reboot = true;
229 std::wcout << L"Cleaning up and returning" << '\n';
231 SysFreeString(criteria);
239 SysFreeString(criteria);
243 int wmain(int argc, WCHAR **argv)
245 printInfoStruct printInfo;
246 po::variables_map vm;
248 int ret = parseArguments(argc, argv, vm, printInfo);
252 ret = check_update(printInfo);
256 return printOutput(printInfo);