+ sockets reuse (*nix only)
[public/netxms.git] / src / agent / core / nxagentd.h
CommitLineData
e7474978
VK
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>
b50f1100 28#include <nms_threads.h>
e7474978 29#include <nms_agent.h>
e9580fef 30#include <nms_cscp.h>
e7474978 31#include <stdio.h>
e9580fef 32#include <nxqueue.h>
e7474978
VK
33#include "messages.h"
34
dc0e93b4
VK
35#ifdef _WIN32
36#include <psapi.h>
37#endif
38
7c205c0c
VK
39#define LIBNXCL_NO_DECLARATIONS
40#include <nxclapi.h>
41
e7474978 42
9f881a19
AK
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
e7474978
VK
54//
55// Version
56//
57
58#ifdef _DEBUG
59#define DEBUG_SUFFIX "-debug"
60#else
1980ebfc 61#define DEBUG_SUFFIX ""
e7474978 62#endif
b50f1100 63#define AGENT_VERSION_STRING NETXMS_VERSION_STRING DEBUG_SUFFIX
e7474978
VK
64
65
66//
67// Default files
68//
69
3b1f0a0a 70#if defined(_WIN32)
d096bcdd
VK
71#define AGENT_DEFAULT_CONFIG "C:\\nxagentd.conf"
72#define AGENT_DEFAULT_LOG "C:\\nxagentd.log"
73#define AGENT_DEFAULT_FILE_STORE "C:\\"
3b1f0a0a 74#elif defined(_NETWARE)
d096bcdd
VK
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:\\"
e7474978 78#else
d096bcdd
VK
79#define AGENT_DEFAULT_CONFIG "/etc/nxagentd.conf"
80#define AGENT_DEFAULT_LOG "/var/log/nxagentd"
81#define AGENT_DEFAULT_FILE_STORE "/tmp"
e7474978
VK
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
9383147c 94#define MAX_PSUFFIX_LENGTH 32
e9580fef 95#define MAX_SERVERS 32
e7474978 96
dc0e93b4
VK
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
3c774461 102#define AF_ENABLE_ACTIONS 0x0020
dc0e93b4
VK
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//
6849d9be
VK
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//
dc0e93b4
VK
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 */
e7474978
VK
171
172
173//
3c774461
VK
174// Action types
175//
176
6849d9be
VK
177#define AGENT_ACTION_EXEC 1
178#define AGENT_ACTION_SUBAGENT 2
3c774461
VK
179
180
181//
182// Action definition structure
183//
184
185typedef struct
186{
187 char szName[MAX_PARAM_NAME];
188 int iType;
189 char *pszCmdLine;
190} ACTION;
191
192
193//
e7474978
VK
194// Parameter definition structure
195//
196
197struct 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
209struct 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//
d096bcdd
VK
218// Server information
219//
220
221struct SERVER_INFO
222{
223 DWORD dwIpAddr;
224 BOOL bInstallationServer;
225};
226
227
228//
e9580fef
VK
229// Communication session
230//
231
232class CommSession
233{
234private:
235 SOCKET m_hSocket;
236 Queue *m_pSendQueue;
237 Queue *m_pMessageQueue;
238 CSCP_BUFFER *m_pMsgBuffer;
ccdbbb52
VK
239 THREAD m_hWriteThread;
240 THREAD m_hProcessingThread;
e9580fef
VK
241 DWORD m_dwHostAddr; // IP address of connected host (network byte order)
242 DWORD m_dwIndex;
7c205c0c 243 BOOL m_bIsAuthenticated;
d096bcdd
VK
244 BOOL m_bInstallationServer;
245 int m_hCurrFile;
246 DWORD m_dwFileRqId;
7c205c0c
VK
247
248 void Authenticate(CSCPMessage *pRequest, CSCPMessage *pMsg);
249 void GetParameter(CSCPMessage *pRequest, CSCPMessage *pMsg);
901c96c7 250 void GetList(CSCPMessage *pRequest, CSCPMessage *pMsg);
3c774461 251 void Action(CSCPMessage *pRequest, CSCPMessage *pMsg);
d096bcdd 252 void RecvFile(CSCPMessage *pRequest, CSCPMessage *pMsg);
e925a5fc 253 DWORD Upgrade(CSCPMessage *pRequest);
e9580fef 254
ccdbbb52
VK
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
e9580fef 263public:
d096bcdd 264 CommSession(SOCKET hSocket, DWORD dwHostAddr, BOOL bInstallServer);
e9580fef
VK
265 ~CommSession();
266
ccdbbb52 267 void Run(void);
e9580fef
VK
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//
e7474978
VK
277// Functions
278//
279
280BOOL Initialize(void);
281void Shutdown(void);
282void Main(void);
283
284void WriteLog(DWORD msg, WORD wType, char *format, ...);
285void InitLog(void);
286void CloseLog(void);
287
e7474978 288void ConsolePrintf(char *pszFormat, ...);
0a147f4b 289void DebugPrintf(char *pszFormat, ...);
e7474978 290
e925a5fc
VK
291void BuildFullPath(TCHAR *pszFileName, TCHAR *pszFullPath);
292
e7474978
VK
293BOOL InitParameterList(void);
294void AddParameter(char *szName, LONG (* fpHandler)(char *,char *,char *), char *pArg);
962b3915 295void AddEnum(char *szName, LONG (* fpHandler)(char *,char *,NETXMS_VALUES_LIST *), char *pArg);
21a2454a 296BOOL AddExternalParameter(char *pszCfgLine);
7c205c0c 297DWORD GetParameterValue(char *pszParam, char *pszValue);
901c96c7 298DWORD GetEnumValue(char *pszParam, NETXMS_VALUES_LIST *pValue);
e7474978
VK
299
300BOOL LoadSubAgent(char *szModuleName);
47e0446b 301void UnloadAllSubAgents(void);
e7474978 302
3c774461
VK
303BOOL AddAction(char *pszName, int iType, char *pszCmdLine);
304BOOL AddActionFromConfig(char *pszLine);
305DWORD ExecAction(char *pszAction, NETXMS_VALUES_LIST *pArgs);
306
307DWORD ExecuteCommand(char *pszCommand, NETXMS_VALUES_LIST *pArgs);
308
1980ebfc
VK
309DWORD UpgradeAgent(TCHAR *pszPkgFile);
310
e7474978
VK
311#ifdef _WIN32
312
313void InitService(void);
314void InstallService(char *execName, char *confFile);
315void RemoveService(void);
316void StartAgentService(void);
317void StopAgentService(void);
318void InstallEventSource(char *path);
319void RemoveEventSource(void);
320
e9580fef 321char *GetPdhErrorText(DWORD dwError, char *pszBuffer, int iBufSize);
e7474978
VK
322
323#endif
324
325
326//
327// Global variables
328//
329
330extern DWORD g_dwFlags;
331extern char g_szLogFile[];
332extern char g_szSharedSecret[];
ae9cddaf 333extern char g_szConfigFile[];
d096bcdd 334extern char g_szFileStore[];
e7474978 335extern WORD g_wListenPort;
d096bcdd 336extern SERVER_INFO g_pServerList[];
e7474978
VK
337extern DWORD g_dwServerCount;
338extern time_t g_dwAgentStartTime;
9383147c 339extern char g_szPlatformSuffix[];
e7474978 340
e9580fef
VK
341extern DWORD g_dwAcceptErrors;
342extern DWORD g_dwAcceptedConnections;
343extern DWORD g_dwRejectedConnections;
344
dc0e93b4
VK
345#ifdef _WIN32
346extern DWORD (__stdcall *imp_GetGuiResources)(HANDLE, DWORD);
347extern BOOL (__stdcall *imp_GetProcessIoCounters)(HANDLE, PIO_COUNTERS);
348extern BOOL (__stdcall *imp_GetPerformanceInfo)(PPERFORMANCE_INFORMATION, DWORD);
349extern BOOL (__stdcall *imp_GlobalMemoryStatusEx)(LPMEMORYSTATUSEX);
6849d9be 350extern DWORD (__stdcall *imp_HrLanConnectionNameFromGuidOrPath)(LPWSTR, LPWSTR, LPWSTR, LPDWORD);
dc0e93b4
VK
351#endif /* _WIN32 */
352
e7474978 353#endif /* _nxagentd_h_ */