+ sockets reuse (*nix only)
[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 // Parameter definition structure
195 //
196
197 struct AGENT_PARAM
198 {
199 char name[MAX_PARAM_NAME]; // Parameter's name (wildcard)
200 LONG (* handler)(char *,char *,char *); // Handler
201 char *arg; // Optional argument passsed to the handler function
202 };
203
204
205 //
206 // Loaded subagent information
207 //
208
209 struct SUBAGENT
210 {
211 HMODULE hModule; // Subagent's module handle
212 NETXMS_SUBAGENT_INFO *pInfo; // Information provided by subagent
213 char szName[MAX_PATH]; // Name of the module
214 };
215
216
217 //
218 // Server information
219 //
220
221 struct SERVER_INFO
222 {
223 DWORD dwIpAddr;
224 BOOL bInstallationServer;
225 };
226
227
228 //
229 // Communication session
230 //
231
232 class CommSession
233 {
234 private:
235 SOCKET m_hSocket;
236 Queue *m_pSendQueue;
237 Queue *m_pMessageQueue;
238 CSCP_BUFFER *m_pMsgBuffer;
239 THREAD m_hWriteThread;
240 THREAD m_hProcessingThread;
241 DWORD m_dwHostAddr; // IP address of connected host (network byte order)
242 DWORD m_dwIndex;
243 BOOL m_bIsAuthenticated;
244 BOOL m_bInstallationServer;
245 int m_hCurrFile;
246 DWORD m_dwFileRqId;
247
248 void Authenticate(CSCPMessage *pRequest, CSCPMessage *pMsg);
249 void GetParameter(CSCPMessage *pRequest, CSCPMessage *pMsg);
250 void GetList(CSCPMessage *pRequest, CSCPMessage *pMsg);
251 void Action(CSCPMessage *pRequest, CSCPMessage *pMsg);
252 void RecvFile(CSCPMessage *pRequest, CSCPMessage *pMsg);
253 DWORD Upgrade(CSCPMessage *pRequest);
254
255 void ReadThread(void);
256 void WriteThread(void);
257 void ProcessingThread(void);
258
259 static THREAD_RESULT THREAD_CALL ReadThreadStarter(void *);
260 static THREAD_RESULT THREAD_CALL WriteThreadStarter(void *);
261 static THREAD_RESULT THREAD_CALL ProcessingThreadStarter(void *);
262
263 public:
264 CommSession(SOCKET hSocket, DWORD dwHostAddr, BOOL bInstallServer);
265 ~CommSession();
266
267 void Run(void);
268
269 void SendMessage(CSCPMessage *pMsg) { m_pSendQueue->Put(pMsg->CreateMessage()); }
270
271 DWORD GetIndex(void) { return m_dwIndex; }
272 void SetIndex(DWORD dwIndex) { if (m_dwIndex == INVALID_INDEX) m_dwIndex = dwIndex; }
273 };
274
275
276 //
277 // Functions
278 //
279
280 BOOL Initialize(void);
281 void Shutdown(void);
282 void Main(void);
283
284 void WriteLog(DWORD msg, WORD wType, char *format, ...);
285 void InitLog(void);
286 void CloseLog(void);
287
288 void ConsolePrintf(char *pszFormat, ...);
289 void DebugPrintf(char *pszFormat, ...);
290
291 void BuildFullPath(TCHAR *pszFileName, TCHAR *pszFullPath);
292
293 BOOL InitParameterList(void);
294 void AddParameter(char *szName, LONG (* fpHandler)(char *,char *,char *), char *pArg);
295 void AddEnum(char *szName, LONG (* fpHandler)(char *,char *,NETXMS_VALUES_LIST *), char *pArg);
296 BOOL AddExternalParameter(char *pszCfgLine);
297 DWORD GetParameterValue(char *pszParam, char *pszValue);
298 DWORD GetEnumValue(char *pszParam, NETXMS_VALUES_LIST *pValue);
299
300 BOOL LoadSubAgent(char *szModuleName);
301 void UnloadAllSubAgents(void);
302
303 BOOL AddAction(char *pszName, int iType, char *pszCmdLine);
304 BOOL AddActionFromConfig(char *pszLine);
305 DWORD ExecAction(char *pszAction, NETXMS_VALUES_LIST *pArgs);
306
307 DWORD ExecuteCommand(char *pszCommand, NETXMS_VALUES_LIST *pArgs);
308
309 DWORD UpgradeAgent(TCHAR *pszPkgFile);
310
311 #ifdef _WIN32
312
313 void InitService(void);
314 void InstallService(char *execName, char *confFile);
315 void RemoveService(void);
316 void StartAgentService(void);
317 void StopAgentService(void);
318 void InstallEventSource(char *path);
319 void RemoveEventSource(void);
320
321 char *GetPdhErrorText(DWORD dwError, char *pszBuffer, int iBufSize);
322
323 #endif
324
325
326 //
327 // Global variables
328 //
329
330 extern DWORD g_dwFlags;
331 extern char g_szLogFile[];
332 extern char g_szSharedSecret[];
333 extern char g_szConfigFile[];
334 extern char g_szFileStore[];
335 extern WORD g_wListenPort;
336 extern SERVER_INFO g_pServerList[];
337 extern DWORD g_dwServerCount;
338 extern time_t g_dwAgentStartTime;
339 extern char g_szPlatformSuffix[];
340
341 extern DWORD g_dwAcceptErrors;
342 extern DWORD g_dwAcceptedConnections;
343 extern DWORD g_dwRejectedConnections;
344
345 #ifdef _WIN32
346 extern DWORD (__stdcall *imp_GetGuiResources)(HANDLE, DWORD);
347 extern BOOL (__stdcall *imp_GetProcessIoCounters)(HANDLE, PIO_COUNTERS);
348 extern BOOL (__stdcall *imp_GetPerformanceInfo)(PPERFORMANCE_INFORMATION, DWORD);
349 extern BOOL (__stdcall *imp_GlobalMemoryStatusEx)(LPMEMORYSTATUSEX);
350 extern DWORD (__stdcall *imp_HrLanConnectionNameFromGuidOrPath)(LPWSTR, LPWSTR, LPWSTR, LPDWORD);
351 #endif /* _WIN32 */
352
353 #endif /* _nxagentd_h_ */