adf5dd35104418f811e543233dcc697336072b5a
[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 #ifdef _WIN32
44 # define SetSocketReuseFlag(sd)
45 #else
46 # define SetSocketReuseFlag(sd) { \
47 int nVal = 1; \
48 setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (const void *)&nVal, \
49 (socklen_t)sizeof(nVal)); \
50 }
51 #endif
52
53
54 //
55 // Version
56 //
57
58 #ifdef _DEBUG
59 #define DEBUG_SUFFIX "-debug"
60 #else
61 #define DEBUG_SUFFIX ""
62 #endif
63 #define AGENT_VERSION_STRING NETXMS_VERSION_STRING DEBUG_SUFFIX
64
65
66 //
67 // Default files
68 //
69
70 #if defined(_WIN32)
71 #define AGENT_DEFAULT_CONFIG "C:\\nxagentd.conf"
72 #define AGENT_DEFAULT_LOG "C:\\nxagentd.log"
73 #define AGENT_DEFAULT_FILE_STORE "C:\\"
74 #elif defined(_NETWARE)
75 #define AGENT_DEFAULT_CONFIG "SYS:ETC/nxagentd.conf"
76 #define AGENT_DEFAULT_LOG "SYS:ETC/nxagentd.log"
77 #define AGENT_DEFAULT_FILE_STORE "SYS:\\"
78 #else
79 #define AGENT_DEFAULT_CONFIG "/etc/nxagentd.conf"
80 #define AGENT_DEFAULT_LOG "/var/log/nxagentd"
81 #define AGENT_DEFAULT_FILE_STORE "/tmp"
82 #endif
83
84
85 //
86 // Constants
87 //
88
89 #ifdef _WIN32
90 #define AGENT_SERVICE_NAME "NetXMSAgentdW32"
91 #define AGENT_EVENT_SOURCE "NetXMS Win32 Agent"
92 #endif
93
94 #define MAX_PSUFFIX_LENGTH 32
95 #define MAX_SERVERS 32
96
97 #define AF_DAEMON 0x0001
98 #define AF_USE_SYSLOG 0x0002
99 #define AF_DEBUG 0x0004
100 #define AF_REQUIRE_AUTH 0x0008
101 #define AF_LOG_UNRESOLVED_SYMBOLS 0x0010
102 #define AF_ENABLE_ACTIONS 0x0020
103 #define AF_SHUTDOWN 0x1000
104
105
106 #ifdef _WIN32
107
108 #define MAX_PROCESSES 4096
109 #define MAX_MODULES 512
110
111
112 //
113 // Attributes for H_ProcInfo
114 //
115
116 #define PROCINFO_GDI_OBJ 1
117 #define PROCINFO_IO_OTHER_B 2
118 #define PROCINFO_IO_OTHER_OP 3
119 #define PROCINFO_IO_READ_B 4
120 #define PROCINFO_IO_READ_OP 5
121 #define PROCINFO_IO_WRITE_B 6
122 #define PROCINFO_IO_WRITE_OP 7
123 #define PROCINFO_KTIME 8
124 #define PROCINFO_PF 9
125 #define PROCINFO_USER_OBJ 10
126 #define PROCINFO_UTIME 11
127 #define PROCINFO_VMSIZE 12
128 #define PROCINFO_WKSET 13
129
130 #define INFOTYPE_MIN 0
131 #define INFOTYPE_MAX 1
132 #define INFOTYPE_AVG 2
133 #define INFOTYPE_SUM 3
134
135
136 //
137 // Attributes for H_NetIPStats and H_NetInterfacStats
138 //
139
140 #ifdef _WIN32
141 #define NET_IP_FORWARDING 1
142
143 #define NET_IF_BYTES_IN 1
144 #define NET_IF_BYTES_OUT 2
145 #define NET_IF_DESCR 3
146 #define NET_IF_IN_ERRORS 4
147 #define NET_IF_LINK 5
148 #define NET_IF_OUT_ERRORS 6
149 #define NET_IF_PACKETS_IN 7
150 #define NET_IF_PACKETS_OUT 8
151 #define NET_IF_SPEED 9
152 #define NET_IF_ADMIN_STATUS 10
153 #endif
154
155
156 //
157 // Request types for H_MemoryInfo
158 //
159
160 #define MEMINFO_PHYSICAL_FREE 1
161 #define MEMINFO_PHYSICAL_TOTAL 2
162 #define MEMINFO_PHYSICAL_USED 3
163 #define MEMINFO_SWAP_FREE 4
164 #define MEMINFO_SWAP_TOTAL 5
165 #define MEMINFO_SWAP_USED 6
166 #define MEMINFO_VIRTUAL_FREE 7
167 #define MEMINFO_VIRTUAL_TOTAL 8
168 #define MEMINFO_VIRTUAL_USED 9
169
170 #endif /* _WIN32 */
171
172
173 //
174 // Action types
175 //
176
177 #define AGENT_ACTION_EXEC 1
178 #define AGENT_ACTION_SUBAGENT 2
179
180
181 //
182 // Action definition structure
183 //
184
185 typedef struct
186 {
187 char szName[MAX_PARAM_NAME];
188 int iType;
189 char *pszCmdLine;
190 } ACTION;
191
192
193 //
194 // Loaded subagent information
195 //
196
197 struct SUBAGENT
198 {
199 HMODULE hModule; // Subagent's module handle
200 NETXMS_SUBAGENT_INFO *pInfo; // Information provided by subagent
201 char szName[MAX_PATH]; // Name of the module
202 };
203
204
205 //
206 // Server information
207 //
208
209 struct SERVER_INFO
210 {
211 DWORD dwIpAddr;
212 BOOL bInstallationServer;
213 };
214
215
216 //
217 // Communication session
218 //
219
220 class CommSession
221 {
222 private:
223 SOCKET m_hSocket;
224 Queue *m_pSendQueue;
225 Queue *m_pMessageQueue;
226 CSCP_BUFFER *m_pMsgBuffer;
227 THREAD m_hWriteThread;
228 THREAD m_hProcessingThread;
229 DWORD m_dwHostAddr; // IP address of connected host (network byte order)
230 DWORD m_dwIndex;
231 BOOL m_bIsAuthenticated;
232 BOOL m_bInstallationServer;
233 int m_hCurrFile;
234 DWORD m_dwFileRqId;
235
236 void Authenticate(CSCPMessage *pRequest, CSCPMessage *pMsg);
237 void GetParameter(CSCPMessage *pRequest, CSCPMessage *pMsg);
238 void GetList(CSCPMessage *pRequest, CSCPMessage *pMsg);
239 void Action(CSCPMessage *pRequest, CSCPMessage *pMsg);
240 void RecvFile(CSCPMessage *pRequest, CSCPMessage *pMsg);
241 DWORD Upgrade(CSCPMessage *pRequest);
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 void BuildFullPath(TCHAR *pszFileName, TCHAR *pszFullPath);
280
281 BOOL InitParameterList(void);
282 void AddParameter(char *szName, LONG (* fpHandler)(char *,char *,char *), char *pArg,
283 int iDataType, char *pszDescription);
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 void GetParameterList(CSCPMessage *pMsg);
289
290 BOOL LoadSubAgent(char *szModuleName);
291 void UnloadAllSubAgents(void);
292 BOOL ProcessCmdBySubAgent(DWORD dwCommand, CSCPMessage *pRequest, CSCPMessage *pResponce);
293
294 BOOL AddAction(char *pszName, int iType, char *pszCmdLine);
295 BOOL AddActionFromConfig(char *pszLine);
296 DWORD ExecAction(char *pszAction, NETXMS_VALUES_LIST *pArgs);
297
298 DWORD ExecuteCommand(char *pszCommand, NETXMS_VALUES_LIST *pArgs);
299
300 DWORD UpgradeAgent(TCHAR *pszPkgFile);
301
302 #ifdef _WIN32
303
304 void InitService(void);
305 void InstallService(char *execName, char *confFile);
306 void RemoveService(void);
307 void StartAgentService(void);
308 void StopAgentService(void);
309 void InstallEventSource(char *path);
310 void RemoveEventSource(void);
311
312 char *GetPdhErrorText(DWORD dwError, char *pszBuffer, int iBufSize);
313
314 #endif
315
316
317 //
318 // Global variables
319 //
320
321 extern DWORD g_dwFlags;
322 extern char g_szLogFile[];
323 extern char g_szSharedSecret[];
324 extern char g_szConfigFile[];
325 extern char g_szFileStore[];
326 extern WORD g_wListenPort;
327 extern SERVER_INFO g_pServerList[];
328 extern DWORD g_dwServerCount;
329 extern time_t g_dwAgentStartTime;
330 extern char g_szPlatformSuffix[];
331
332 extern DWORD g_dwAcceptErrors;
333 extern DWORD g_dwAcceptedConnections;
334 extern DWORD g_dwRejectedConnections;
335
336 #ifdef _WIN32
337 extern DWORD (__stdcall *imp_GetGuiResources)(HANDLE, DWORD);
338 extern BOOL (__stdcall *imp_GetProcessIoCounters)(HANDLE, PIO_COUNTERS);
339 extern BOOL (__stdcall *imp_GetPerformanceInfo)(PPERFORMANCE_INFORMATION, DWORD);
340 extern BOOL (__stdcall *imp_GlobalMemoryStatusEx)(LPMEMORYSTATUSEX);
341 extern DWORD (__stdcall *imp_HrLanConnectionNameFromGuidOrPath)(LPWSTR, LPWSTR, LPWSTR, LPDWORD);
342 #endif /* _WIN32 */
343
344 #endif /* _nxagentd_h_ */