4d4aff767b97296a04badc690ee4aaf7a99c343e
[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 <psapi.h>
37 #endif
38
39 #define LIBNXCL_NO_DECLARATIONS
40 #include <nxclapi.h>
41
42
43 //
44 // Version
45 //
46
47 #ifdef _DEBUG
48 #define DEBUG_SUFFIX "-debug"
49 #else
50 #define DEBUG_SUFFIX ""
51 #endif
52 #define AGENT_VERSION_STRING NETXMS_VERSION_STRING DEBUG_SUFFIX
53
54
55 //
56 // Default files
57 //
58
59 #if defined(_WIN32)
60 #define AGENT_DEFAULT_CONFIG "C:\\nxagentd.conf"
61 #define AGENT_DEFAULT_LOG "C:\\nxagentd.log"
62 #define AGENT_DEFAULT_FILE_STORE "C:\\"
63 #elif defined(_NETWARE)
64 #define AGENT_DEFAULT_CONFIG "SYS:ETC/nxagentd.conf"
65 #define AGENT_DEFAULT_LOG "SYS:ETC/nxagentd.log"
66 #define AGENT_DEFAULT_FILE_STORE "SYS:\\"
67 #else
68 #define AGENT_DEFAULT_CONFIG "/etc/nxagentd.conf"
69 #define AGENT_DEFAULT_LOG "/var/log/nxagentd"
70 #define AGENT_DEFAULT_FILE_STORE "/tmp"
71 #endif
72
73
74 //
75 // Constants
76 //
77
78 #ifdef _WIN32
79 #define AGENT_SERVICE_NAME "NetXMSAgentdW32"
80 #define AGENT_EVENT_SOURCE "NetXMS Win32 Agent"
81 #endif
82
83 #define MAX_PSUFFIX_LENGTH 32
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 DWORD Upgrade(CSCPMessage *pRequest);
243
244 void ReadThread(void);
245 void WriteThread(void);
246 void ProcessingThread(void);
247
248 static THREAD_RESULT THREAD_CALL ReadThreadStarter(void *);
249 static THREAD_RESULT THREAD_CALL WriteThreadStarter(void *);
250 static THREAD_RESULT THREAD_CALL ProcessingThreadStarter(void *);
251
252 public:
253 CommSession(SOCKET hSocket, DWORD dwHostAddr, BOOL bInstallServer);
254 ~CommSession();
255
256 void Run(void);
257
258 void SendMessage(CSCPMessage *pMsg) { m_pSendQueue->Put(pMsg->CreateMessage()); }
259
260 DWORD GetIndex(void) { return m_dwIndex; }
261 void SetIndex(DWORD dwIndex) { if (m_dwIndex == INVALID_INDEX) m_dwIndex = dwIndex; }
262 };
263
264
265 //
266 // Functions
267 //
268
269 BOOL Initialize(void);
270 void Shutdown(void);
271 void Main(void);
272
273 void WriteLog(DWORD msg, WORD wType, char *format, ...);
274 void InitLog(void);
275 void CloseLog(void);
276
277 void ConsolePrintf(char *pszFormat, ...);
278 void DebugPrintf(char *pszFormat, ...);
279
280 void BuildFullPath(TCHAR *pszFileName, TCHAR *pszFullPath);
281
282 BOOL InitParameterList(void);
283 void AddParameter(char *szName, LONG (* fpHandler)(char *,char *,char *), char *pArg);
284 void AddEnum(char *szName, LONG (* fpHandler)(char *,char *,NETXMS_VALUES_LIST *), char *pArg);
285 BOOL AddExternalParameter(char *pszCfgLine);
286 DWORD GetParameterValue(char *pszParam, char *pszValue);
287 DWORD GetEnumValue(char *pszParam, NETXMS_VALUES_LIST *pValue);
288
289 BOOL LoadSubAgent(char *szModuleName);
290 void UnloadAllSubAgents(void);
291
292 BOOL AddAction(char *pszName, int iType, char *pszCmdLine);
293 BOOL AddActionFromConfig(char *pszLine);
294 DWORD ExecAction(char *pszAction, NETXMS_VALUES_LIST *pArgs);
295
296 DWORD ExecuteCommand(char *pszCommand, NETXMS_VALUES_LIST *pArgs);
297
298 DWORD UpgradeAgent(TCHAR *pszPkgFile);
299
300 #ifdef _WIN32
301
302 void InitService(void);
303 void InstallService(char *execName, char *confFile);
304 void RemoveService(void);
305 void StartAgentService(void);
306 void StopAgentService(void);
307 void InstallEventSource(char *path);
308 void RemoveEventSource(void);
309
310 char *GetPdhErrorText(DWORD dwError, char *pszBuffer, int iBufSize);
311
312 #endif
313
314
315 //
316 // Global variables
317 //
318
319 extern DWORD g_dwFlags;
320 extern char g_szLogFile[];
321 extern char g_szSharedSecret[];
322 extern char g_szConfigFile[];
323 extern char g_szFileStore[];
324 extern WORD g_wListenPort;
325 extern SERVER_INFO g_pServerList[];
326 extern DWORD g_dwServerCount;
327 extern time_t g_dwAgentStartTime;
328 extern char g_szPlatformSuffix[];
329
330 extern DWORD g_dwAcceptErrors;
331 extern DWORD g_dwAcceptedConnections;
332 extern DWORD g_dwRejectedConnections;
333
334 #ifdef _WIN32
335 extern DWORD (__stdcall *imp_GetGuiResources)(HANDLE, DWORD);
336 extern BOOL (__stdcall *imp_GetProcessIoCounters)(HANDLE, PIO_COUNTERS);
337 extern BOOL (__stdcall *imp_GetPerformanceInfo)(PPERFORMANCE_INFORMATION, DWORD);
338 extern BOOL (__stdcall *imp_GlobalMemoryStatusEx)(LPMEMORYSTATUSEX);
339 extern DWORD (__stdcall *imp_HrLanConnectionNameFromGuidOrPath)(LPWSTR, LPWSTR, LPWSTR, LPDWORD);
340 #endif /* _WIN32 */
341
342 #endif /* _nxagentd_h_ */