source: nscp/trunk/modules/CheckDisk/CheckDisk.cpp @ 5d8e0b5

Last change on this file since 5d8e0b5 was 5d8e0b5, checked in by Michael Medin <michael@…>, 8 years ago

MAJOR CHANGES
2004-04-19 MickeM

+ Added SSL support
+ Added alot of new options
+ Added documentation

  • Renamed and restructured NSCLientCompat to CheckSystem
  • *ALOT* of fixes all over : We are now starting to get to something that is "stable" : This means that soon you might actually be able to use this.
  • Property mode set to 100644
File size: 11.2 KB
Line 
1// CheckEventLog.cpp : Defines the entry point for the DLL application.
2//
3
4#include "stdafx.h"
5#include "CheckDisk.h"
6#include <strEx.h>
7#include <time.h>
8#include <utils.h>
9
10CheckDisk gCheckDisk;
11
12BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
13{
14        NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call);
15        return TRUE;
16}
17
18CheckDisk::CheckDisk() {
19}
20CheckDisk::~CheckDisk() {
21}
22
23
24bool CheckDisk::loadModule() {
25        return true;
26}
27bool CheckDisk::unloadModule() {
28        return true;
29}
30
31std::string CheckDisk::getModuleName() {
32        return "CheckDisk Various Disk related checks.";
33}
34NSCModuleWrapper::module_version CheckDisk::getModuleVersion() {
35        NSCModuleWrapper::module_version version = {0, 0, 1 };
36        return version;
37}
38
39bool CheckDisk::hasCommandHandler() {
40        return true;
41}
42bool CheckDisk::hasMessageHandler() {
43        return false;
44}
45
46typedef std::unary_function<const WIN32_FIND_DATA&, bool> baseClass;
47struct GetSize : public baseClass
48{
49        GetSize() : size(0) { }
50        result_type operator()(argument_type wfd) {
51                if (!(wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)) {
52                        size += (wfd.nFileSizeHigh * ((long long)MAXDWORD+1)) + (long long)wfd.nFileSizeLow;
53                }
54                return true;
55        }
56        inline long long getSize() {
57                return size;
58        }
59private: 
60        long long size;
61};
62
63void RecursiveScanDirectory(std::string dir, GetSize & f) {
64        std::string baseDir;
65        std::string::size_type pos = dir.find_last_of('\\');
66        if (pos == std::string::npos)
67                return;
68        baseDir = dir.substr(0, pos);
69
70        WIN32_FIND_DATA wfd;
71        HANDLE hFind = FindFirstFile(dir.c_str(), &wfd);
72        if (hFind != INVALID_HANDLE_VALUE) {
73                do {
74                        if (!f(wfd))
75                                break;
76                        if ((wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) {
77                                if ( (strcmp(wfd.cFileName, ".") != 0) && (strcmp(wfd.cFileName, "..") != 0) )
78                                        RecursiveScanDirectory(baseDir + "\\" + wfd.cFileName + "\\*.*", f);
79                        }
80                } while (FindNextFile(hFind, &wfd));
81        }
82        FindClose(hFind);
83}
84
85NSCAPI::nagiosReturn CheckDisk::CheckDriveSize(const unsigned int argLen, char **char_args, std::string &message, std::string &perf) {
86        // CheckFileSize
87        // request: CheckFileSize <option> <option>...
88        // <option>                     MaxWarn=<size gmkb>
89        //                                      MaxCrit=<size gmkb>
90        //                                      MinWarn=<size gmkb>
91        //                                      MinCrit=<size gmkb>
92        //                                      ShowAll
93        //                                      File=<path>
94        //                                      File:<shortname>=<path>
95        //
96        // Return: <return state>&<return string>...
97        // <return state>       0 - No errors
98        //                                      1 - Unknown
99        //                                      2 - Errors
100        // <size gmkb> is a size with a possible modifier letter (such as G for gigabyte, M for Megabyte, K for kilobyte etc)
101        // Examples:
102        // <return string>      <directory> <size gmkb> ... |<shortname>=<size>:<warn>:<crit>
103        // test: CheckFileSize ShowAll MaxWarn=1024M MaxCrit=4096M File:WIN=c:\WINDOWS\*.*
104        //       CheckFileSize
105        //
106        // check_nscp -H <ip> -p <port> -s <passwd> -c <commandstring>
107        //
108        // ./check_nscp -H 192.168.0.167 -p 1234 -s pwd -c 'CheckFileSize&ShowAll&MaxWarn=1024M&MaxCrit=4096M&File:WIN=c:\WINDOWS\*.*'
109        // WIN: 1G (2110962363B)|WIN:2110962363:1073741824:4294967296
110        NSC_DEBUG_MSG("CheckDriveSize");
111        NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK;
112        std::list<std::string> args = arrayBuffer::arrayBuffer2list(argLen, char_args);
113        if (args.empty()) {
114                message = "Missing argument(s).";
115                return NSCAPI::returnCRIT;
116        }
117
118        checkHolders::SizeMaxMin warn;
119        checkHolders::SizeMaxMin crit;
120        bool bShowAll = false;
121        bool bNSClient = false;
122        std::list<std::string> drives;
123
124        std::list<std::string>::const_iterator cit;
125        for (cit=args.begin();cit!=args.end();++cit) {
126                std::string arg = *cit;
127                std::pair<std::string,std::string> p = strEx::split(arg,"=");
128                if (p.first == "Drive") {
129                        drives.push_back(p.second);
130                } else if (p.first == "MaxWarn") {
131                        warn.max.set(p.second);
132                } else if (p.first == "MinWarn") {
133                        warn.min.set(p.second);
134                } else if (p.first == "MaxCrit") {
135                        crit.max.set(p.second);
136                } else if (p.first == "MinCrit") {
137                        crit.min.set(p.second);
138                } else if (p.first == "ShowAll") {
139                        bShowAll = true;
140                } else if (p.first == "nsclient") {
141                        bNSClient = true;
142                } else {
143                        drives.push_back(p.first);
144                }
145        }
146
147        NSC_DEBUG_MSG_STD("Bounds: critical " + crit.min.toString() + " > size > " + crit.max.toString());
148        NSC_DEBUG_MSG_STD("Bounds: warning " + warn.min.toString() + " > size > " + warn.max.toString());
149        NSC_DEBUG_MSG_STD("Showall: " + ((bShowAll)?"yeap":"noop"));
150        NSC_DEBUG_MSG_STD("nsclient: " + ((bNSClient)?"yeap":"noop"));
151
152        for (std::list<std::string>::iterator it = drives.begin();it!=drives.end();it++) {
153                std::string drive = (*it);
154                if (drive.length() == 1)
155                        drive += ":";
156                if (GetDriveType(drive.c_str()) != DRIVE_FIXED){
157                        message = "ERROR: Drive is not a fixed drive: " + drive;
158                        return NSCAPI::returnUNKNOWN;
159                }
160                ULARGE_INTEGER freeBytesAvailableToCaller;
161                ULARGE_INTEGER totalNumberOfBytes;
162                ULARGE_INTEGER totalNumberOfFreeBytes;
163                if (!GetDiskFreeSpaceEx(drive.c_str(), &freeBytesAvailableToCaller, &totalNumberOfBytes, &totalNumberOfFreeBytes)) {
164                        message = "ERROR: Could not get free space for" + drive;
165                        return NSCAPI::returnUNKNOWN;
166                }
167
168                if (bNSClient) {
169                        message += strEx::itos(totalNumberOfFreeBytes.QuadPart) + "&";
170                        message += strEx::itos(totalNumberOfBytes.QuadPart) + "&";
171                } else {
172                        std::string tStr;
173                        long long usedSpace = totalNumberOfBytes.QuadPart-totalNumberOfFreeBytes.QuadPart;
174                        long long totalSpace = totalNumberOfBytes.QuadPart;
175                        if (crit.max.hasBounds() && crit.max.checkMAX(usedSpace, totalSpace)) {
176                                message += crit.max.prettyPrint(drive, usedSpace, totalSpace);
177                                NSCHelper::escalteReturnCodeToCRIT(returnCode);
178                        } else if (crit.min.hasBounds() && crit.min.checkMIN(usedSpace, totalSpace)) {
179                                tStr = crit.min.prettyPrint(drive, usedSpace, totalSpace);
180                                NSCHelper::escalteReturnCodeToCRIT(returnCode);
181                        } else if (warn.max.hasBounds() && warn.max.checkMAX(usedSpace, totalSpace)) {
182                                tStr = warn.max.prettyPrint(drive, usedSpace, totalSpace);
183                                NSCHelper::escalteReturnCodeToWARN(returnCode);
184                        } else if (warn.min.hasBounds() && warn.min.checkMIN(usedSpace, totalSpace)) {
185                                tStr = warn.min.prettyPrint(drive, usedSpace, totalSpace);
186                                NSCHelper::escalteReturnCodeToWARN(returnCode);
187                        } else if (bShowAll) {
188                                tStr = drive + ": " + strEx::itos_as_BKMG(usedSpace);
189                        }
190                        perf += checkHolders::SizeMaxMin::printPerf(drive, usedSpace, totalSpace, warn, crit);
191                        if (!message.empty() && !tStr.empty())
192                                message += ", ";
193                        if (!tStr.empty())
194                                message += tStr;
195                }
196        }
197        if (message.empty())
198                message = "All drive sizes are within bounds.";
199        return returnCode;
200}
201
202NSCAPI::nagiosReturn CheckDisk::CheckFileSize(const unsigned int argLen, char **char_args, std::string &message, std::string &perf) {
203        // CheckFileSize
204        // request: CheckFileSize <option> <option>...
205        // <option>                     MaxWarn=<size gmkb>
206        //                                      MaxCrit=<size gmkb>
207        //                                      MinWarn=<size gmkb>
208        //                                      MinCrit=<size gmkb>
209        //                                      ShowAll
210        //                                      File=<path>
211        //                                      File:<shortname>=<path>
212        //
213        // Return: <return state>&<return string>...
214        // <return state>       0 - No errors
215        //                                      1 - Unknown
216        //                                      2 - Errors
217        // <size gmkb> is a size with a possible modifier letter (such as G for gigabyte, M for Megabyte, K for kilobyte etc)
218        // Examples:
219        // <return string>      <directory> <size gmkb> ... |<shortname>=<size>:<warn>:<crit>
220        // test: CheckFileSize ShowAll MaxWarn=1024M MaxCrit=4096M File:WIN=c:\WINDOWS\*.*
221        //       CheckFileSize
222        //
223        // check_nscp -H <ip> -p <port> -s <passwd> -c <commandstring>
224        //
225        // ./check_nscp -H 192.168.0.167 -p 1234 -s pwd -c 'CheckFileSize&ShowAll&MaxWarn=1024M&MaxCrit=4096M&File:WIN=c:\WINDOWS\*.*'
226        // WIN: 1G (2110962363B)|WIN:2110962363:1073741824:4294967296
227        NSC_DEBUG_MSG("CheckFileSize");
228        NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK;
229        std::list<std::string> args = arrayBuffer::arrayBuffer2list(argLen, char_args);
230        if (args.empty()) {
231                message = "Missing argument(s).";
232                return NSCAPI::returnCRIT;
233        }
234        long long maxWarn = 0;
235        long long maxCrit = 0;
236        long long minWarn = 0;
237        long long minCrit = 0;
238        bool bShowAll = false;
239        std::list<std::pair<std::string,std::string> > paths;
240
241        std::list<std::string>::const_iterator cit;
242        for (cit=args.begin();cit!=args.end();++cit) {
243                std::string arg = *cit;
244                std::pair<std::string,std::string> p = strEx::split(arg,"=");
245                if (p.first == "File") {
246                        paths.push_back(std::pair<std::string,std::string>("",p.second));
247                } else if (p.first == "MaxWarn") {
248                        maxWarn = strEx::stoi64_as_BKMG(p.second);
249                } else if (p.first == "MinWarn") {
250                        minWarn = strEx::stoi64_as_BKMG(p.second);
251                } else if (p.first == "MaxCrit") {
252                        maxCrit = strEx::stoi64_as_BKMG(p.second);
253                } else if (p.first == "MinCrit") {
254                        minCrit = strEx::stoi64_as_BKMG(p.second);
255                } else if (p.first == "ShowAll") {
256                        bShowAll = true;
257                } else if (p.first.find(":") != std::string::npos) {
258                        std::pair<std::string,std::string> p2 = strEx::split(p.first,":");
259                        if (p2.first == "File") {
260                                paths.push_back(std::pair<std::string,std::string>(p2.second,p.second));
261                        } else {
262                                message = "Unknown command: " + p.first;
263                                return NSCAPI::returnCRIT;
264                        }
265                } else {
266                        message = "Unknown command: " + p.first;
267                        return NSCAPI::returnCRIT;
268                }
269        }
270        NSC_DEBUG_MSG_STD("Bounds: critical " + strEx::itos(minCrit) + " > siez > " + strEx::itos(maxCrit));
271        NSC_DEBUG_MSG_STD("Bounds: warning " + strEx::itos(minWarn) + " > size > " + strEx::itos(maxWarn));
272        NSC_DEBUG_MSG_STD("Showall: " + ((bShowAll)?"yeap":"noop"));
273
274        std::list<std::pair<std::string,std::string> >::const_iterator pit;
275        for (pit = paths.begin(); pit != paths.end(); ++pit) {
276                std::string tstr;
277                GetSize sizeFinder;
278                std::string sName = (*pit).first;
279                if (sName.empty())
280                        sName = (*pit).second;
281                RecursiveScanDirectory((*pit).second, sizeFinder);
282
283                if ((maxCrit!=0) && (sizeFinder.getSize() > maxCrit)) {
284                        tstr = sName + ": " + strEx::itos_as_BKMG(sizeFinder.getSize());
285                        returnCode = NSCAPI::returnCRIT;
286                } else if (sizeFinder.getSize() < minCrit) {
287                        tstr = sName + ": " + strEx::itos_as_BKMG(sizeFinder.getSize());
288                        NSCHelper::escalteReturnCodeToCRIT(returnCode);
289                } else if ((maxWarn!=0)&&(sizeFinder.getSize() > maxWarn)) {
290                        tstr = sName +  ": " + strEx::itos_as_BKMG(sizeFinder.getSize());
291                        NSCHelper::escalteReturnCodeToWARN(returnCode);
292                } else if (sizeFinder.getSize() < minWarn) {
293                        tstr = sName +  ": " + strEx::itos_as_BKMG(sizeFinder.getSize());
294                        NSCHelper::escalteReturnCodeToWARN(returnCode);
295                } else if (bShowAll) {
296                        tstr = sName +  ": " + strEx::itos_as_BKMG(sizeFinder.getSize());
297                }
298                if (!(*pit).first.empty())
299                        perf += (*pit).first + "=" + strEx::itos(sizeFinder.getSize()) + ";" + strEx::itos(maxWarn) + ";" + strEx::itos(maxCrit) + " ";
300                if (!message.empty() && !tstr.empty())
301                        message += ", ";
302                if (!tstr.empty())
303                        message += tstr;
304        }
305        if (message.empty())
306                message = "OK all file sizes are within bounds.";
307        return returnCode;
308}
309
310
311#define BUFFER_SIZE 1024*64
312
313NSCAPI::nagiosReturn CheckDisk::handleCommand(const std::string command, const unsigned int argLen, char **char_args, std::string &msg, std::string &perf) {
314        if (command == "CheckFileSize") {
315                return CheckFileSize(argLen, char_args, msg, perf);
316        } else if (command == "CheckDriveSize") {
317                return CheckDriveSize(argLen, char_args, msg, perf);
318
319//      } else if (command == "CheckFileDate") {
320        }       
321        return NSCAPI::returnIgnored;
322}
323
324
325NSC_WRAPPERS_MAIN_DEF(gCheckDisk);
326NSC_WRAPPERS_IGNORE_MSG_DEF();
327NSC_WRAPPERS_HANDLE_CMD_DEF(gCheckDisk);
Note: See TracBrowser for help on using the repository browser.