source: nscp/trunk/modules/NSClientCompat/NSCommands.cpp @ 079055f

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

Verified NSCLient compatiblity for many commands and some minor tweaks (now most NSClient commands are OK)

  • Property mode set to 100644
File size: 5.9 KB
Line 
1#include "StdAfx.h"
2#include ".\nscommands.h"
3#include <strEx.h>
4#include <tlhelp32.h>
5
6
7
8typedef std::hash_map<std::string,DWORD> NSPROCLST;
9
10/**
11 * Get a hash_map with all running processes.
12 * @return a hash_map with all running processes
13 */
14NSPROCLST GetProcessList(void)
15{
16        HANDLE hProcessSnap;
17        PROCESSENTRY32 pe32;
18        NSPROCLST ret;
19
20        // Take a snapshot of all processes in the system.
21        hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
22        if( hProcessSnap == INVALID_HANDLE_VALUE )
23                throw "CreateToolhelp32Snapshot (of processes) failed";
24
25        // Set the size of the structure before using it.
26        pe32.dwSize = sizeof( PROCESSENTRY32 );
27
28        // Retrieve information about the first process,
29        // and exit if unsuccessful
30        if( !Process32First( hProcessSnap, &pe32 ) ) {
31                CloseHandle( hProcessSnap );     // Must clean up the snapshot object!
32                throw "Process32First failed!";
33        }
34
35        // Now walk the snapshot of processes, and
36        // display information about each process in turn
37        do {
38                ret[pe32.szExeFile] = pe32.th32ProcessID;
39        } while( Process32Next( hProcessSnap, &pe32 ) );
40
41        // Don't forget to clean up the snapshot object!
42        CloseHandle( hProcessSnap );
43        return ret;
44}
45
46/**
47 * Retrieve the process state of one or more processes (by name).
48 * Parse a list with a process names and verify that all names processes are running.
49 * <pre>
50 * Syntax:
51 * request: 5&<showAll|showFail>&<service1>&<service2>&...
52 * Return: <return state>&<service1> : <state1> - <service2> : <state2> - ...
53 * <return state>       0 - All services OK
54 *                                      1 - Unknown
55 *                                      2 - At least one service is stopped
56 *</pre>
57 * @param args
58 * @return
59 * @todo Is this correct ? (has never been used so is most likely broken)
60 */
61std::string NSCommands::procState(std::list<std::string> args) {
62        if (args.empty())
63                return "ERROR: Missing argument exception.";
64        NSPROCLST procs;
65        std::string ret;
66        ServiceState state = ok;
67        bool showAll = args.front() == "ShowAll";
68        args.pop_front();
69       
70        try {
71                procs = GetProcessList();
72        } catch (char *c) {
73                NSC_LOG_ERROR_STD("ERROR: " + c);
74                return (std::string)("ERROR: ") + c;
75        }
76        for (std::list<std::string>::iterator it = args.begin();it!=args.end();it++) {
77                std::string exe = (*it);
78                NSPROCLST::iterator proc = procs.find(exe);
79
80                if (proc == procs.end()) {
81                        if (state == ok)
82                                state = error;
83                        if (!ret.empty()) ret += " - ";
84                        ret += exe + " : not running";
85                } else if (showAll) {
86                        if (!ret.empty()) ret += " - ";
87                        ret += exe + " : Running";
88                }
89        }
90        if (ret.empty())
91                ret ="All processes are running.";
92        return strEx::itos(state) + "&" + ret;
93}
94
95
96/**
97 * Helper function for serviceState.
98 * Checks if a service is running and prepares a string representing the information we want to retrive
99 *
100 * @param bShowAll Show all running processes
101 * @param &nState State of the current process
102 * @param &info Service info struct
103 * @return A string with service the current state
104 */
105std::string NSCommands::cmdServiceStateCheckItem(bool bShowAll, int &nState, TNtServiceInfo &info) {
106        if (info.m_dwCurrentState == SERVICE_RUNNING) {
107                if (bShowAll)
108                        return info.m_strServiceName + " : Started";
109        }
110        else if (info.m_dwCurrentState == SERVICE_STOPPED) {
111                nState = 2;
112                return info.m_strServiceName + " : Stopped";
113        } else {
114                if (nState < 1)
115                        nState = 1;
116                return info.m_strServiceName + " : Unknown("+strEx::itos(nState)+")";
117        }
118        return "";
119}
120/**
121 * Retrieve the service state of one or more services (by name).
122 * Parse a list with a service names and verify that all named services are running.
123 * <pre>
124 * Syntax:
125 * request: 5&<showAll|showFail>&<service1|_all_>&<service2>&...
126 * Return: <return state>&<service1> : <state1> - <service2> : <state2> - ...
127 * <return state>       0 - All services OK
128 *                                      1 - Unknown
129 *                                      2 - At least one service is stopped
130 *</pre>
131 *
132 * @param args
133 * @return
134 */
135std::string NSCommands::serviceState(std::list<std::string> args) {
136        if (args.empty())
137                return "ERROR: Missing argument exception.";
138        else {
139                std::string ret;
140                int nState = 0;
141                bool bShowAll = args.front() == "ShowAll";
142                args.pop_front();
143
144                if ((args.empty())||(args.front() == "_all_")) {
145                        TNtServiceInfoList list;
146                        TNtServiceInfo::EnumServices(SERVICE_WIN32, SERVICE_ACTIVE|SERVICE_INACTIVE, &list);
147                        for (TNtServiceInfoList::iterator it = list.begin(); it != list.end(); it ++) {
148                                std::string s = cmdServiceStateCheckItem(bShowAll, nState, *it);
149                                if (!s.empty() && !ret.empty())
150                                        ret += " - ";
151                                ret += s;
152                        }
153                } else {
154                        for (std::list<std::string>::iterator it = args.begin();it!=args.end();it++) {
155                                TNtServiceInfo info = TNtServiceInfo::GetService((*it).c_str());
156                                std::string s = cmdServiceStateCheckItem(bShowAll, nState, info);
157                                if (!s.empty() && !ret.empty())
158                                        ret += " - ";
159                                ret += s;
160                        }
161                }
162                if (ret.empty())
163                        ret ="All services are running.";
164                return strEx::itos(nState) + "&" + ret;
165        }
166}
167/**
168 * Check the used space of one ore more drives.
169 * <pre>
170 * Syntax:
171 * request: <passwd>&4&<drive 1>&<drive 2>&...
172 * Return: <free space 1>&<total space 1>&<free space 2>&<total space 2>&...
173 *</pre>
174 *
175 * @param args A list of drives
176 * @return A string with a list of drive usage stats
177 */
178std::string NSCommands::usedDiskSpace(std::list<std::string> args) {
179        if (args.empty())
180                return "ERROR: Missing argument exception.";
181        std::string ret;
182        for (std::list<std::string>::iterator it = args.begin();it!=args.end();it++) {
183                std::string path = (*it);
184                if (path.length() == 1)
185                        path += ":";
186                if (GetDriveType(path.c_str()) != DRIVE_FIXED)
187                        return "ERROR: Drive is not a fixed drive." + path;
188                ULARGE_INTEGER freeBytesAvailableToCaller;
189                ULARGE_INTEGER totalNumberOfBytes;
190                ULARGE_INTEGER totalNumberOfFreeBytes;
191                if (!GetDiskFreeSpaceEx(path.c_str(), &freeBytesAvailableToCaller, &totalNumberOfBytes, &totalNumberOfFreeBytes))
192                        return "ERROR: Could not get freespace." + path;
193                ret += strEx::itos(static_cast<__int64>(totalNumberOfFreeBytes.QuadPart)) + "&";
194                ret += strEx::itos(static_cast<__int64>(totalNumberOfBytes.QuadPart)) + "&";
195        }
196        return ret;
197}
Note: See TracBrowser for help on using the repository browser.