]> granicus.if.org Git - icinga2/blob - plugins/check_update.cpp
Add Windows plugins
[icinga2] / plugins / check_update.cpp
1 #include <windows.h>
2 #include <Shlwapi.h>
3 #include <iostream>
4 #include <wuapi.h>
5 #include <wuerror.h>
6
7 #include "thresholds.h"
8
9 #include "boost/program_options.hpp"
10
11 #define VERSION 1.0
12
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')"
14
15 namespace po = boost::program_options;
16
17 using std::wcout; using std::endl;
18 using std::wstring; using std::cout;
19
20 struct printInfoStruct {
21         BOOL warn, crit;
22         LONG numUpdates;
23         BOOL important, reboot, careForCanRequest;
24 };
25
26 static int parseArguments(int, wchar_t **, po::variables_map&, printInfoStruct&);
27 static int printOutput(const printInfoStruct&);
28 static int check_update(printInfoStruct&);
29
30 int main(int argc, wchar_t **argv) 
31 {
32         po::variables_map vm;
33         printInfoStruct printInfo = { FALSE, FALSE, 0, FALSE, FALSE, FALSE };
34
35         int ret = parseArguments(argc, argv, vm, printInfo);
36         if (ret != -1)
37                 return ret;
38         
39         ret = check_update(printInfo);
40         if (ret != -1)
41                 return ret;
42
43         return printOutput(printInfo);
44 }
45
46 int printOutput(const printInfoStruct& printInfo) 
47 {
48         state state = OK;
49         wstring output = L"UPDATE ";
50
51         if (printInfo.important)
52                 state = WARNING;
53
54         if (printInfo.reboot)
55                 state = CRITICAL;
56
57         switch (state)
58         {
59         case OK:
60                 output.append(L"OK ");
61                 break;
62         case WARNING:
63                 output.append(L"WARNING ");
64                 break;
65         case CRITICAL:
66                 output.append(L"CRITICAL ");
67                 break;
68         }
69
70         wcout << output << printInfo.numUpdates << L"|update=" << printInfo.numUpdates << L";"
71                 << printInfo.warn << L";" << printInfo.crit << L";0" << endl;
72         return state;
73 }
74
75 int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct& printInfo) 
76 {
77         wchar_t namePath[MAX_PATH];
78         GetModuleFileName(NULL, namePath, MAX_PATH);
79         wchar_t *progName = PathFindFileName(namePath);
80
81         po::options_description desc;
82
83         desc.add_options()
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\"")
90                 ;
91
92         po::basic_command_line_parser<wchar_t> parser(ac, av);
93
94         try {
95                 po::store(
96                         parser
97                         .options(desc)
98                         .style(
99                         po::command_line_style::unix_style |
100                         po::command_line_style::allow_long_disguise)
101                         .run(),
102                         vm);
103                 vm.notify();
104         }
105
106         catch (std::exception& e) {
107                 cout << e.what() << endl << desc << endl;
108                 return 3;
109         }
110
111         if (vm.count("h")) {
112                 cout << desc << endl;
113                 return 0;
114         } 
115         if (vm.count("help")) {
116                 wcout << progName << " Help\n\tVersion: " << VERSION << endl;
117                 wprintf(
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);
120                 cout << desc;
121                 wprintf(
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"
142                         L"every update."
143                         , progName, progName);
144                 cout << endl;
145                 return 0;
146         }  if (vm.count("version")) {
147                 cout << "Version: " << VERSION << endl;
148                 return 0;
149         }
150
151         if (vm.count("warning"))
152                 printInfo.warn = TRUE;
153
154         if (vm.count("critical"))
155                 printInfo.crit = TRUE;
156
157         if (vm.count("possible-reboot"))
158                 printInfo.careForCanRequest = TRUE;
159
160         return -1;
161 }
162
163 int check_update(printInfoStruct& printInfo) 
164 {
165         CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
166         ISearchResult *pResult;
167         IUpdateSession *pSession;
168         IUpdateSearcher *pSearcher;
169
170         CoCreateInstance(CLSID_UpdateSession, NULL, CLSCTX_INPROC_SERVER, IID_IUpdateSession, (LPVOID*)&pSession);
171         pSession->CreateUpdateSearcher(&pSearcher);
172
173
174         /*
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
179         */
180
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
184
185         if (pSearcher->Search(criteria, &pResult) != S_OK)
186                 goto die;
187         SysFreeString(criteria);
188
189         IUpdateCollection *pCollection;
190         IUpdate *pUpdate;
191
192         LONG updateSize;
193         pResult->get_Updates(&pCollection);
194         pCollection->get_Count(&updateSize);
195
196         if (updateSize == 0)
197                 return -1;
198
199         printInfo.numUpdates = updateSize;
200         printInfo.important = printInfo.warn;
201
202         if (!printInfo.crit)
203                 return -1;
204
205         IInstallationBehavior *pIbehav;
206         InstallationRebootBehavior updateReboot;
207
208         for (LONG i = 0; i < updateSize; i++)
209         {
210                 pCollection->get_Item(i, &pUpdate);
211                 pUpdate->get_InstallationBehavior(&pIbehav);
212                 pIbehav->get_RebootBehavior(&updateReboot);
213                 if (updateReboot == irbAlwaysRequiresReboot) {
214                         printInfo.reboot = TRUE;
215                         continue;
216                 }
217                 if (printInfo.careForCanRequest && updateReboot == irbCanRequestReboot)
218                         printInfo.reboot = TRUE;
219         }
220
221         return 0;
222
223 die:
224         if (criteria)
225                 SysFreeString(criteria);
226         return 3;
227 }