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

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

Multiple fixes in various places.

  • Added threadding blocks "core"
  • Added new Module (CheckDisk)
  • Added new option [log] / debug=1 to enable debug logs.
  • Added more error messages
  • other minor tweaks and fixes
  • Property mode set to 100644
File size: 6.4 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
84std::string CheckDisk::CheckFileSize(const unsigned int argLen, char **char_args) {
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        std::string perfData;
111        std::string ret;
112        NSCAPI::returnCodes returnCode = NSCAPI::returnOK;
113        std::list<std::string> args = NSCHelper::makelist(argLen, char_args);
114        if (args.empty())
115                return "Missing argument(s).";
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                                return "Unknown command: " + p.first;
145                        }
146                } else {
147                        return "Unknown command: " + p.first;
148                }
149        }
150        NSC_DEBUG_MSG_STD("Bounds: critical " + strEx::itos(minCrit) + " > siez > " + strEx::itos(maxCrit));
151        NSC_DEBUG_MSG_STD("Bounds: warning " + strEx::itos(minWarn) + " > size > " + strEx::itos(maxWarn));
152        NSC_DEBUG_MSG_STD("Showall: " + ((bShowAll)?"yeap":"noop"));
153
154        std::list<std::pair<std::string,std::string> >::const_iterator pit;
155        for (pit = paths.begin(); pit != paths.end(); ++pit) {
156                std::string tstr;
157                GetSize sizeFinder;
158                std::string sName = (*pit).first;
159                if (sName.empty())
160                        sName = (*pit).second;
161                RecursiveScanDirectory((*pit).second, sizeFinder);
162
163                if ((maxCrit!=0) && (sizeFinder.getSize() > maxCrit)) {
164                        tstr = sName + ": " + strEx::itos_as_BKMG(sizeFinder.getSize());
165                        returnCode = NSCAPI::returnCRIT;
166                } else if (sizeFinder.getSize() < minCrit) {
167                        tstr = sName + ": " + strEx::itos_as_BKMG(sizeFinder.getSize());
168                        NSCHelper::escalteReturnCodeToCRIT(returnCode);
169                } else if ((maxWarn!=0)&&(sizeFinder.getSize() > maxWarn)) {
170                        tstr = sName +  ": " + strEx::itos_as_BKMG(sizeFinder.getSize());
171                        NSCHelper::escalteReturnCodeToWARN(returnCode);
172                } else if (sizeFinder.getSize() < minWarn) {
173                        tstr = sName +  ": " + strEx::itos_as_BKMG(sizeFinder.getSize());
174                        NSCHelper::escalteReturnCodeToWARN(returnCode);
175                } else if (bShowAll) {
176                        tstr = sName +  ": " + strEx::itos_as_BKMG(sizeFinder.getSize());
177                }
178                if (!(*pit).first.empty())
179                        perfData += (*pit).first + "=" + strEx::itos(sizeFinder.getSize()) + ";" + strEx::itos(maxWarn) + ";" + strEx::itos(maxCrit) + " ";
180                if (!ret.empty() && !tstr.empty())
181                        ret += ", ";
182                if (!tstr.empty())
183                        ret += tstr;
184        }
185        if (ret.empty())
186                ret = "OK all file sizes are within bounds.";
187        if (!perfData.empty())
188                ret += "|" + perfData;
189        return NSCHelper::returnNSCP(returnCode, ret);
190}
191
192
193#define BUFFER_SIZE 1024*64
194
195std::string CheckDisk::handleCommand(const std::string command, const unsigned int argLen, char **char_args) {
196        if (command == "CheckFileSize") {
197                return CheckFileSize(argLen, char_args);
198//      } else if (command == "CheckFileDate") {
199        }       
200        return "";
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.