7 #include "thresholds.h"
9 #include "boost/program_options.hpp"
13 #define CRITERIA L"(IsInstalled = 0 and CategoryIDs contains '0fa1201d-4330-4fa8-8ae9-b877473b6441') or (IsInstalled = 0 and CategoryIDs contains 'E6CF1350-C01B-414D-A61F-263D14D133B4')"
15 namespace po = boost::program_options;
17 using std::wcout; using std::endl;
18 using std::wstring; using std::cout;
20 struct printInfoStruct {
23 BOOL important, reboot, careForCanRequest;
26 static int parseArguments(int, wchar_t **, po::variables_map&, printInfoStruct&);
27 static int printOutput(const printInfoStruct&);
28 static int check_update(printInfoStruct&);
30 int main(int argc, wchar_t **argv)
33 printInfoStruct printInfo = { FALSE, FALSE, 0, FALSE, FALSE, FALSE };
35 int ret = parseArguments(argc, argv, vm, printInfo);
39 ret = check_update(printInfo);
43 return printOutput(printInfo);
46 int printOutput(const printInfoStruct& printInfo)
49 wstring output = L"UPDATE ";
51 if (printInfo.important)
60 output.append(L"OK ");
63 output.append(L"WARNING ");
66 output.append(L"CRITICAL ");
70 wcout << output << printInfo.numUpdates << L"|update=" << printInfo.numUpdates << L";"
71 << printInfo.warn << L";" << printInfo.crit << L";0" << endl;
75 int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct& printInfo)
77 wchar_t namePath[MAX_PATH];
78 GetModuleFileName(NULL, namePath, MAX_PATH);
79 wchar_t *progName = PathFindFileName(namePath);
81 po::options_description desc;
84 (",h", "print help message and exit")
85 ("help", "print verbose help and exit")
86 ("version,v", "print version and exit")
87 ("warning,w", "warn if there are important updates available")
88 ("critical,c", "critical if there are important updates that require a reboot")
89 ("possible-reboot", "treat \"update may need to reboot\" as \"update needs to reboot\"")
92 po::basic_command_line_parser<wchar_t> parser(ac, av);
99 po::command_line_style::unix_style |
100 po::command_line_style::allow_long_disguise)
106 catch (std::exception& e) {
107 cout << e.what() << endl << desc << endl;
112 cout << desc << endl;
115 if (vm.count("help")) {
116 wcout << progName << " Help\n\tVersion: " << VERSION << endl;
118 L"%s is a simple program to check a machines required updates.\n"
119 L"You can use the following options to define its behaviour:\n\n", progName);
122 L"\nAfter some time, it will then output a string like this one:\n\n"
123 L"\tUPDATE WARNING 8|updates=8;1;1;0\n\n"
124 L"\"UPDATE\" being the type of the check, \"WARNING\" the returned status\n"
125 L"and \"8\" is the number of important updates updates.\n"
126 L"The performance data is found behind the \"|\", in order:\n"
127 L"returned value, warning threshold, critical threshold, minimal value and,\n"
128 L"if applicable, the maximal value.\n\n"
129 L"An update counts as important when it is part of the Security- or\n"
130 L"CriticalUpdates group.\n"
131 L"Consult the msdn on WSUS Classification GUIDs for more information.\n"
132 L"%s' exit codes denote the following:\n"
133 L" 0\tOK,\n\tno Thresholds were broken or the programs check part was not executed\n"
134 L" 1\tWARNING,\n\tThe warning, but not the critical threshold was broken\n"
135 L" 2\tCRITICAL,\n\tThe critical threshold was broken\n"
136 L" 3\tUNKNOWN, \n\tThe programme experienced an internal or input error\n\n"
137 L"%s works different from other plugins in that you do not set thresholds\n"
138 L"but only activate them. Using \"-w\" triggers warning state if there are not\n"
139 L"installed and non-optional updates. \"-c\" triggers critical if there are\n"
140 L"non-optional updates that require a reboot.\n"
141 L"The \"possible-reboot\" option is not recommended since this true for nearly\n"
143 , progName, progName);
146 } if (vm.count("version")) {
147 cout << "Version: " << VERSION << endl;
151 if (vm.count("warning"))
152 printInfo.warn = TRUE;
154 if (vm.count("critical"))
155 printInfo.crit = TRUE;
157 if (vm.count("possible-reboot"))
158 printInfo.careForCanRequest = TRUE;
163 int check_update(printInfoStruct& printInfo)
165 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
166 ISearchResult *pResult;
167 IUpdateSession *pSession;
168 IUpdateSearcher *pSearcher;
170 CoCreateInstance(CLSID_UpdateSession, NULL, CLSCTX_INPROC_SERVER, IID_IUpdateSession, (LPVOID*)&pSession);
171 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 BSTR criteria = SysAllocString(CRITERIA);
182 // http://msdn.microsoft.com/en-us/library/windows/desktop/aa386526%28v=vs.85%29.aspx
183 // http://msdn.microsoft.com/en-us/library/ff357803%28v=vs.85%29.aspx
185 if (pSearcher->Search(criteria, &pResult) != S_OK)
187 SysFreeString(criteria);
189 IUpdateCollection *pCollection;
193 pResult->get_Updates(&pCollection);
194 pCollection->get_Count(&updateSize);
199 printInfo.numUpdates = updateSize;
200 printInfo.important = printInfo.warn;
205 IInstallationBehavior *pIbehav;
206 InstallationRebootBehavior updateReboot;
208 for (LONG i = 0; i < updateSize; i++)
210 pCollection->get_Item(i, &pUpdate);
211 pUpdate->get_InstallationBehavior(&pIbehav);
212 pIbehav->get_RebootBehavior(&updateReboot);
213 if (updateReboot == irbAlwaysRequiresReboot) {
214 printInfo.reboot = TRUE;
217 if (printInfo.careForCanRequest && updateReboot == irbCanRequestReboot)
218 printInfo.reboot = TRUE;
225 SysFreeString(criteria);