source: nscp/trunk/modules/CheckDisk/CheckDisk.cpp @ c515660

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

*OBS* This might no longer work! (expect updated code in the next few days if things are broken)

  • Fundamental API changes (due to NRPE compatibility)
    • HandleCommand? has changed
    • Inject has changed
    • Most API calls have new "return codes" (typedef:ed INT to allow for return code compiler checks)
    • A lot of the old return codes have changed
  • Preliminary NRPE support (can parse and execute incoming requests, cant return data yet, and no encryption)
  • New SimpleSocket? in include/ will be used as base class for Listeners
  • A lot of rewrite to the NSC API
  • Property mode set to 100644
File size: 6.6 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
9CheckDisk gCheckDisk;
10
11BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
12{
13        NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call);
14        return TRUE;
15}
16
17CheckDisk::CheckDisk() {
18}
19CheckDisk::~CheckDisk() {
20}
21
22
23bool CheckDisk::loadModule() {
24        return true;
25}
26bool CheckDisk::unloadModule() {
27        return true;
28}
29
30std::string CheckDisk::getModuleName() {
31        return "CheckDisk Various Disk related checks.";
32}
33NSCModuleWrapper::module_version CheckDisk::getModuleVersion() {
34        NSCModuleWrapper::module_version version = {0, 0, 1 };
35        return version;
36}
37
38bool CheckDisk::hasCommandHandler() {
39        return true;
40}
41bool CheckDisk::hasMessageHandler() {
42        return false;
43}
44
45typedef std::unary_function<const WIN32_FIND_DATA&, bool> baseClass;
46struct GetSize : public baseClass
47{
48        GetSize() : size(0) { }
49        result_type operator()(argument_type wfd) {
50                if (!(wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)) {
51                        size += (wfd.nFileSizeHigh * ((long long)MAXDWORD+1)) + (long long)wfd.nFileSizeLow;
52                }
53                return true;
54        }
55        inline long long getSize() {
56                return size;
57        }
58private: 
59        long long size;
60};
61
62void RecursiveScanDirectory(std::string dir, GetSize & f) {
63        std::string baseDir;
64        std::string::size_type pos = dir.find_last_of('\\');
65        if (pos == std::string::npos)
66                return;
67        baseDir = dir.substr(0, pos);
68
69        WIN32_FIND_DATA wfd;
70        HANDLE hFind = FindFirstFile(dir.c_str(), &wfd);
71        if (hFind != INVALID_HANDLE_VALUE) {
72                do {
73                        if (!f(wfd))
74                                break;
75                        if ((wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) {
76                                if ( (strcmp(wfd.cFileName, ".") != 0) && (strcmp(wfd.cFileName, "..") != 0) )
77                                        RecursiveScanDirectory(baseDir + "\\" + wfd.cFileName + "\\*.*", f);
78                        }
79                } while (FindNextFile(hFind, &wfd));
80        }
81        FindClose(hFind);
82}
83
84NSCAPI::nagiosReturn CheckDisk::CheckFileSize(const unsigned int argLen, char **char_args, std::string &message, std::string &perf) {
85        // CheckFileSize
86        // request: CheckFileSize&<option>&<option>...
87        // <option>                     MaxWarn=<size gmkb>
88        //                                      MaxCrit=<size gmkb>
89        //                                      MinWarn=<size gmkb>
90        //                                      MinCrit=<size gmkb>
91        //                                      ShowAll
92        //                                      File=<path>
93        //                                      File:<shortname>=<path>
94        //
95        // Return: <return state>&<return string>...
96        // <return state>       0 - No errors
97        //                                      1 - Unknown
98        //                                      2 - Errors
99        // <size gmkb> is a size with a possible modifier letter (such as G for gigabyte, M for Megabyte, K for kilobyte etc)
100        // Examples:
101        // <return string>      <directory> <size gmkb> ... |<shortname>=<size>:<warn>:<crit>
102        // test: CheckFileSize&ShowAll&MaxWarn=1024M&MaxCrit=4096M&File:WIN=c:\WINDOWS\*.*
103        //       CheckFileSize
104        //
105        // check_nscp -H <ip> -p <port> -s <passwd> -c <commandstring>
106        //
107        // ./check_nscp -H 192.168.0.167 -p 1234 -s pwd -c 'CheckFileSize&ShowAll&MaxWarn=1024M&MaxCrit=4096M&File:WIN=c:\WINDOWS\*.*'
108        // WIN: 1G (2110962363B)|WIN:2110962363:1073741824:4294967296
109        NSC_DEBUG_MSG("CheckFileSize");
110        NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK;
111        std::list<std::string> args = NSCHelper::arrayBuffer2list(argLen, char_args);
112        if (args.empty()) {
113                message = "Missing argument(s).";
114                return NSCAPI::returnCRIT;
115        }
116        long long maxWarn = 0;
117        long long maxCrit = 0;
118        long long minWarn = 0;
119        long long minCrit = 0;
120        bool bShowAll = false;
121        std::list<std::pair<std::string,std::string> > paths;
122
123        std::list<std::string>::const_iterator cit;
124        for (cit=args.begin();cit!=args.end();++cit) {
125                std::string arg = *cit;
126                std::pair<std::string,std::string> p = strEx::split(arg,"=");
127                if (p.first == "File") {
128                        paths.push_back(std::pair<std::string,std::string>("",p.second));
129                } else if (p.first == "MaxWarn") {
130                        maxWarn = strEx::stoi64_as_BKMG(p.second);
131                } else if (p.first == "MinWarn") {
132                        minWarn = strEx::stoi64_as_BKMG(p.second);
133                } else if (p.first == "MaxCrit") {
134                        maxCrit = strEx::stoi64_as_BKMG(p.second);
135                } else if (p.first == "MinCrit") {
136                        minCrit = strEx::stoi64_as_BKMG(p.second);
137                } else if (p.first == "ShowAll") {
138                        bShowAll = true;
139                } else if (p.first.find(":") != std::string::npos) {
140                        std::pair<std::string,std::string> p2 = strEx::split(p.first,":");
141                        if (p2.first == "File") {
142                                paths.push_back(std::pair<std::string,std::string>(p2.second,p.second));
143                        } else {
144                                message = "Unknown command: " + p.first;
145                                return NSCAPI::returnCRIT;
146                        }
147                } else {
148                        message = "Unknown command: " + p.first;
149                        return NSCAPI::returnCRIT;
150                }
151        }
152        NSC_DEBUG_MSG_STD("Bounds: critical " + strEx::itos(minCrit) + " > siez > " + strEx::itos(maxCrit));
153        NSC_DEBUG_MSG_STD("Bounds: warning " + strEx::itos(minWarn) + " > size > " + strEx::itos(maxWarn));
154        NSC_DEBUG_MSG_STD("Showall: " + ((bShowAll)?"yeap":"noop"));
155
156        std::list<std::pair<std::string,std::string> >::const_iterator pit;
157        for (pit = paths.begin(); pit != paths.end(); ++pit) {
158                std::string tstr;
159                GetSize sizeFinder;
160                std::string sName = (*pit).first;
161                if (sName.empty())
162                        sName = (*pit).second;
163                RecursiveScanDirectory((*pit).second, sizeFinder);
164
165                if ((maxCrit!=0) && (sizeFinder.getSize() > maxCrit)) {
166                        tstr = sName + ": " + strEx::itos_as_BKMG(sizeFinder.getSize());
167                        returnCode = NSCAPI::returnCRIT;
168                } else if (sizeFinder.getSize() < minCrit) {
169                        tstr = sName + ": " + strEx::itos_as_BKMG(sizeFinder.getSize());
170                        NSCHelper::escalteReturnCodeToCRIT(returnCode);
171                } else if ((maxWarn!=0)&&(sizeFinder.getSize() > maxWarn)) {
172                        tstr = sName +  ": " + strEx::itos_as_BKMG(sizeFinder.getSize());
173                        NSCHelper::escalteReturnCodeToWARN(returnCode);
174                } else if (sizeFinder.getSize() < minWarn) {
175                        tstr = sName +  ": " + strEx::itos_as_BKMG(sizeFinder.getSize());
176                        NSCHelper::escalteReturnCodeToWARN(returnCode);
177                } else if (bShowAll) {
178                        tstr = sName +  ": " + strEx::itos_as_BKMG(sizeFinder.getSize());
179                }
180                if (!(*pit).first.empty())
181                        perf += (*pit).first + "=" + strEx::itos(sizeFinder.getSize()) + ";" + strEx::itos(maxWarn) + ";" + strEx::itos(maxCrit) + " ";
182                if (!message.empty() && !tstr.empty())
183                        message += ", ";
184                if (!tstr.empty())
185                        message += tstr;
186        }
187        if (message.empty())
188                message = "OK all file sizes are within bounds.";
189        return returnCode;
190}
191
192
193#define BUFFER_SIZE 1024*64
194
195NSCAPI::nagiosReturn CheckDisk::handleCommand(const std::string command, const unsigned int argLen, char **char_args, std::string &msg, std::string &perf) {
196        if (command == "CheckFileSize") {
197                return CheckFileSize(argLen, char_args, msg, perf);
198//      } else if (command == "CheckFileDate") {
199        }       
200        return NSCAPI::returnIgnored;
201}
202
203
204NSC_WRAPPERS_MAIN_DEF(gCheckDisk);
205NSC_WRAPPERS_IGNORE_MSG_DEF();
206NSC_WRAPPERS_HANDLE_CMD_DEF(gCheckDisk);
Note: See TracBrowser for help on using the repository browser.