Implemented file upload to agents
[public/netxms.git] / src / agent / core / nxagentd.h
1 /*
2 ** NetXMS multiplatform core agent
3 ** Copyright (C) 2003, 2004 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be usefu,,
11 ** but ITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** $module: nxagentd.h
20 **
21 **/
22
23 #ifndef _nxagentd_h_
24 #define _nxagentd_h_
25
26 #include <nms_common.h>
27 #include <nms_util.h>
28 #include <nms_threads.h>
29 #include <nms_agent.h>
30 #include <nms_cscp.h>
31 #include <stdio.h>
32 #include <nxqueue.h>
33 #include "messages.h"
34
35 #ifdef _WIN32
36 #include <io.h>
37 #include <psapi.h>
38 #endif
39
40 #define LIBNXCL_NO_DECLARATIONS
41 #include <nxclapi.h>
42
43
44 //
45 // Version
46 //
47
48 #ifdef _DEBUG
49 #define DEBUG_SUFFIX "-debug"
50 #else
51 #define DEBUG_SUFFIX
52 #endif
53 #define AGENT_VERSION_STRING NETXMS_VERSION_STRING DEBUG_SUFFIX
54
55
56 //
57 // Default files
58 //
59
60 #if defined(_WIN32)
61 #define AGENT_DEFAULT_CONFIG "C:\\nxagentd.conf"
62 #define AGENT_DEFAULT_LOG "C:\\nxagentd.log"
63 #define AGENT_DEFAULT_FILE_STORE "C:\\"
64 #elif defined(_NETWARE)
65 #define AGENT_DEFAULT_CONFIG "SYS:ETC/nxagentd.conf"
66 #define AGENT_DEFAULT_LOG "SYS:ETC/nxagentd.log"
67 #define AGENT_DEFAULT_FILE_STORE "SYS:\\"
68 #else
69 #define AGENT_DEFAULT_CONFIG "/etc/nxagentd.conf"
70 #define AGENT_DEFAULT_LOG "/var/log/nxagentd"
71 #define AGENT_DEFAULT_FILE_STORE "/tmp"
72 #endif
73
74
75 //
76 // Constants
77 //
78
79 #ifdef _WIN32
80 #define AGENT_SERVICE_NAME "NetXMSAgentdW32"
81 #define AGENT_EVENT_SOURCE "NetXMS Win32 Agent"
82 #endif
83
84 #define MAX_SERVERS 32
85
86 #define AF_DAEMON 0x0001
87 #define AF_USE_SYSLOG 0x0002
88 #define AF_DEBUG 0x0004
89 #define AF_REQUIRE_AUTH 0x0008
90 #define AF_LOG_UNRESOLVED_SYMBOLS 0x0010
91 #define AF_ENABLE_ACTIONS 0x0020
92 #define AF_SHUTDOWN 0x1000
93
94
95 #ifdef _WIN32
96
97 #define MAX_PROCESSES 4096
98 #define MAX_MODULES 512
99
100
101 //
102 // Attributes for H_ProcInfo
103 //
104
105 #define PROCINFO_GDI_OBJ 1
106 #define PROCINFO_IO_OTHER_B 2
107 #define PROCINFO_IO_OTHER_OP 3
108 #define PROCINFO_IO_READ_B 4
109 #define PROCINFO_IO_READ_OP 5
110 #define PROCINFO_IO_WRITE_B 6
111 #define PROCINFO_IO_WRITE_OP 7
112 #define PROCINFO_KTIME 8
113 #define PROCINFO_PF 9
114 #define PROCINFO_USER_OBJ 10
115 #define PROCINFO_UTIME 11
116 #define PROCINFO_VMSIZE 12
117 #define PROCINFO_WKSET 13
118
119 #define INFOTYPE_MIN 0
120 #define INFOTYPE_MAX 1
121 #define INFOTYPE_AVG 2
122 #define INFOTYPE_SUM 3
123
124
125 //
126 // Attributes for H_NetIPStats and H_NetInterfacStats
127 //
128
129 #ifdef _WIN32
130 #define NET_IP_FORWARDING 1
131
132 #define NET_IF_BYTES_IN 1
133 #define NET_IF_BYTES_OUT 2
134 #define NET_IF_DESCR 3
135 #define NET_IF_IN_ERRORS 4
136 #define NET_IF_LINK 5
137 #define NET_IF_OUT_ERRORS 6
138 #define NET_IF_PACKETS_IN 7
139 #define NET_IF_PACKETS_OUT 8
140 #define NET_IF_SPEED 9
141 #define NET_IF_ADMIN_STATUS 10
142 #endif
143
144
145 //
146 // Request types for H_MemoryInfo
147 //
148
149 #define MEMINFO_PHYSICAL_FREE 1
150 #define MEMINFO_PHYSICAL_TOTAL 2
151 #define MEMINFO_PHYSICAL_USED 3
152 #define MEMINFO_SWAP_FREE 4
153 #define MEMINFO_SWAP_TOTAL 5
154 #define MEMINFO_SWAP_USED 6
155 #define MEMINFO_VIRTUAL_FREE 7
156 #define MEMINFO_VIRTUAL_TOTAL 8
157 #define MEMINFO_VIRTUAL_USED 9
158
159 #endif /* _WIN32 */
160
161
162 //
163 // Action types
164 //
165
166 #define AGENT_ACTION_EXEC 1
167 #define AGENT_ACTION_SUBAGENT 2
168
169
170 //
171 // Action definition structure
172 //
173
174 typedef struct
175 {
176 char szName[MAX_PARAM_NAME];
177 int iType;
178 char *pszCmdLine;
179 } ACTION;
180
181
182 //
183 // Parameter definition structure
184 //
185
186 struct AGENT_PARAM
187 {
188 char name[MAX_PARAM_NAME]; // Parameter's name (wildcard)
189 LONG (* handler)(char *,char *,char *); // Handler
190 char *arg; // Optional argument passsed to the handler function
191 };
192
193
194 //
195 // Loaded subagent information
196 //
197
198 struct SUBAGENT
199 {
200 HMODULE hModule; // Subagent's module handle
201 NETXMS_SUBAGENT_INFO *pInfo; // Information provided by subagent
202 char szName[MAX_PATH]; // Name of the module
203 };
204
205
206 //
207 // Server information
208 //
209
210 struct SERVER_INFO
211 {
212 DWORD dwIpAddr;
213 BOOL bInstallationServer;
214 };
215
216
217 //
218 // Communication session
219 //
220
221 class CommSession
222 {
223 private:
224 SOCKET m_hSocket;
225 Queue *m_pSendQueue;
226 Queue *m_pMessageQueue;
227 CSCP_BUFFER *m_pMsgBuffer;
228 THREAD m_hWriteThread;
229 THREAD m_hProcessingThread;
230 DWORD m_dwHostAddr; // IP address of connected host (network byte order)
231 DWORD m_dwIndex;
232 BOOL m_bIsAuthenticated;
233 BOOL m_bInstallationServer;
234 int m_hCurrFile;
235 DWORD m_dwFileRqId;
236
237 void Authenticate(CSCPMessage *pRequest, CSCPMessage *pMsg);
238 void GetParameter(CSCPMessage *pRequest, CSCPMessage *pMsg);
239 void GetList(CSCPMessage *pRequest, CSCPMessage *pMsg);
240 void Action(CSCPMessage *pRequest, CSCPMessage *pMsg);
241 void RecvFile(CSCPMessage *pRequest, CSCPMessage *pMsg);
242
243 void ReadThread(void);
244 void WriteThread(void);
245 void ProcessingThread(void);
246
247 static THREAD_RESULT THREAD_CALL ReadThreadStarter(void *);
248 static THREAD_RESULT THREAD_CALL WriteThreadStarter(void *);
249 static THREAD_RESULT THREAD_CALL ProcessingThreadStarter(void *);
250
251 public:
252 CommSession(SOCKET hSocket, DWORD dwHostAddr, BOOL bInstallServer);
253 ~CommSession();
254
255 void Run(void);
256
257 void SendMessage(CSCPMessage *pMsg) { m_pSendQueue->Put(pMsg->CreateMessage()); }
258
259 DWORD GetIndex(void) { return m_dwIndex; }
260 void SetIndex(DWORD dwIndex) { if (m_dwIndex == INVALID_INDEX) m_dwIndex = dwIndex; }
261 };
262
263
264 //
265 // Functions
266 //
267
268 BOOL Initialize(void);
269 void Shutdown(void);
270 void Main(void);
271
272 void WriteLog(DWORD msg, WORD wType, char *format, ...);
273 void InitLog(void);
274 void CloseLog(void);
275
276 void ConsolePrintf(char *pszFormat, ...);
277 void DebugPrintf(char *pszFormat, ...);
278
279 BOOL InitParameterList(void);
280 void AddParameter(char *szName, LONG (* fpHandler)(char *,char *,char *), char *pArg);
281 void AddEnum(char *szName, LONG (* fpHandler)(char *,char *,NETXMS_VALUES_LIST *), char *pArg);
282 BOOL AddExternalParameter(char *pszCfgLine);
283 DWORD GetParameterValue(char *pszParam, char *pszValue);
284 DWORD GetEnumValue(char *pszParam, NETXMS_VALUES_LIST *pValue);
285
286 BOOL LoadSubAgent(char *szModuleName);
287 void UnloadAllSubAgents(void);
288
289 BOOL AddAction(char *pszName, int iType, char *pszCmdLine);
290 BOOL AddActionFromConfig(char *pszLine);
291 DWORD ExecAction(char *pszAction, NETXMS_VALUES_LIST *pArgs);
292
293 DWORD ExecuteCommand(char *pszCommand, NETXMS_VALUES_LIST *pArgs);
294
295 #ifdef _WIN32
296
297 void InitService(void);
298 void InstallService(char *execName, char *confFile);
299 void RemoveService(void);
300 void StartAgentService(void);
301 void StopAgentService(void);
302 void InstallEventSource(char *path);
303 void RemoveEventSource(void);
304
305 char *GetPdhErrorText(DWORD dwError, char *pszBuffer, int iBufSize);
306
307 #endif
308
309
310 //
311 // Global variables
312 //
313
314 extern DWORD g_dwFlags;
315 extern char g_szLogFile[];
316 extern char g_szSharedSecret[];
317 extern char g_szConfigFile[];
318 extern char g_szFileStore[];
319 extern WORD g_wListenPort;
320 extern SERVER_INFO g_pServerList[];
321 extern DWORD g_dwServerCount;
322 extern time_t g_dwAgentStartTime;
323
324 extern DWORD g_dwAcceptErrors;
325 extern DWORD g_dwAcceptedConnections;
326 extern DWORD g_dwRejectedConnections;
327
328 #ifdef _WIN32
329 extern DWORD (__stdcall *imp_GetGuiResources)(HANDLE, DWORD);
330 extern BOOL (__stdcall *imp_GetProcessIoCounters)(HANDLE, PIO_COUNTERS);
331 extern BOOL (__stdcall *imp_GetPerformanceInfo)(PPERFORMANCE_INFORMATION, DWORD);
332 extern BOOL (__stdcall *imp_GlobalMemoryStatusEx)(LPMEMORYSTATUSEX);
333 extern DWORD (__stdcall *imp_HrLanConnectionNameFromGuidOrPath)(LPWSTR, LPWSTR, LPWSTR, LPDWORD);
334 #endif /* _WIN32 */
335
336 #endif /* _nxagentd_h_ */