source: nscp/trunk/modules/CheckDisk/CheckDisk.cpp @ 107bd0f

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

Added ChekDIsk plugin (and some utility functions)

  • Property mode set to 100644
File size: 3.3 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                        NSC_LOG_ERROR_STD(wfd.cFileName + ": " + strEx::itos((wfd.nFileSizeHigh * ((long long)MAXDWORD+1)) + (long long)wfd.nFileSizeLow) + " --- " + strEx::itos(size));
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
85
86// CheckDirectorySize&<Directory>&<Directory>...
87// request: CheckDirectorySize&<warning>&<max>&<Directory>&<Directory>...
88// Return: <return state>&<return string>...
89// <return state>       0 - No errors
90//                                      1 - Unknown
91//                                      2 - Errors
92// <return string>      <directory> <size mb> ...
93// test: CheckDirectorySize&1024M&4096M&c:\WINDOWS\*.*
94//       CheckDirectorySize
95#define BUFFER_SIZE 1024*64
96
97std::string CheckDisk::handleCommand(const std::string command, const unsigned int argLen, char **char_args) {
98        std::string ret;
99        if (command == "CheckDirectorySize") {
100                std::list<std::string> args = NSCHelper::makelist(argLen, char_args);
101                if (args.size() < 3)
102                        return "Missing argument(s)." + strEx::itos((int)args.size());
103                int iWarn = strEx::stoi64_as_BKMG(args.front()); args.pop_front();
104                int iMax = strEx::stoi64_as_BKMG(args.front()); args.pop_front();
105
106                std::list<std::string>::const_iterator cit;
107                for (cit=args.begin();cit!=args.end();++cit) {
108                        GetSize size;
109                        RecursiveScanDirectory(*cit, size);
110                        if (!ret.empty())
111                                ret += ", ";
112                        ret += (*cit) + ": " + strEx::itos_as_BKMG(size.getSize());
113                }
114        }
115        return ret;
116}
117
118
119NSC_WRAPPERS_MAIN_DEF(gCheckDisk);
120NSC_WRAPPERS_IGNORE_MSG_DEF();
121NSC_WRAPPERS_HANDLE_CMD_DEF(gCheckDisk);
Note: See TracBrowser for help on using the repository browser.