added persistent storage in agent; added agent policy registration in persistent...
[public/netxms.git] / src / agent / core / nxagentd.h
CommitLineData
5039dede
AK
1/*
2** NetXMS multiplatform core agent
3** Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 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** File: 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 <stdarg.h>
33#include <nxqueue.h>
34#include <nxlog.h>
35#include "messages.h"
36
37#define LIBNXCL_NO_DECLARATIONS
38#include <nxclapi.h>
39
40#ifdef _NETWARE
41#undef SEVERITY_CRITICAL
42#endif
43
44
45//
46// Version
47//
48
49#ifdef _DEBUG
50#define DEBUG_SUFFIX "-debug"
51#else
52#define DEBUG_SUFFIX ""
53#endif
54#define AGENT_VERSION_STRING NETXMS_VERSION_STRING DEBUG_SUFFIX
55
56
57//
58// Default files
59//
60
61#if defined(_WIN32)
62#define AGENT_DEFAULT_CONFIG "C:\\nxagentd.conf"
abf35d58 63#define AGENT_DEFAULT_CONFIG_D "C:\\nxagentd.conf.d"
5039dede
AK
64#define AGENT_DEFAULT_LOG "C:\\nxagentd.log"
65#define AGENT_DEFAULT_FILE_STORE "C:\\"
203e9d8a 66#define AGENT_DEFAULT_DATA_DIR "C:\\"
5039dede
AK
67#elif defined(_NETWARE)
68#define AGENT_DEFAULT_CONFIG "SYS:ETC/nxagentd.conf"
abf35d58 69#define AGENT_DEFAULT_CONFIG_D "SYS:ETC/nxagentd.conf.d"
5039dede
AK
70#define AGENT_DEFAULT_LOG "SYS:ETC/nxagentd.log"
71#define AGENT_DEFAULT_FILE_STORE "SYS:\\"
203e9d8a 72#define AGENT_DEFAULT_DATA_DIR "SYS:ETC"
5039dede
AK
73#elif defined(_IPSO)
74#define AGENT_DEFAULT_CONFIG "/opt/netxms/etc/nxagentd.conf"
abf35d58 75#define AGENT_DEFAULT_CONFIG_D "/opt/netxms/etc/nxagentd.conf.d"
5039dede
AK
76#define AGENT_DEFAULT_LOG "/opt/netxms/log/nxagentd.log"
77#define AGENT_DEFAULT_FILE_STORE "/opt/netxms/store"
203e9d8a 78#define AGENT_DEFAULT_DATA_DIR "/opt/netxms/data"
5039dede
AK
79#else
80#define AGENT_DEFAULT_CONFIG "{search}"
abf35d58 81#define AGENT_DEFAULT_CONFIG_D "{search}"
5039dede
AK
82#define AGENT_DEFAULT_LOG "/var/log/nxagentd"
83#define AGENT_DEFAULT_FILE_STORE "/tmp"
203e9d8a 84#define AGENT_DEFAULT_DATA_DIR "/var/opt/netxms/agent"
5039dede
AK
85#endif
86
203e9d8a
VK
87#define REGISTRY_FILE_NAME "registry.dat"
88
5039dede
AK
89
90//
91// Constants
92//
93
94#ifdef _WIN32
9f96fc88
VK
95#define DEFAULT_AGENT_SERVICE_NAME _T("NetXMSAgentdW32")
96#define DEFAULT_AGENT_EVENT_SOURCE _T("NetXMS Win32 Agent")
97#define NXAGENTD_SYSLOG_NAME g_windowsEventSourceName
5039dede 98#else
9f96fc88 99#define NXAGENTD_SYSLOG_NAME _T("nxagentd")
5039dede
AK
100#endif
101
102#define MAX_PSUFFIX_LENGTH 32
103#define MAX_SERVERS 32
104
9fcdfda7
VK
105#define AF_DAEMON 0x00000001
106#define AF_USE_SYSLOG 0x00000002
c303351c 107//obsolete: #define AF_DEBUG 0x00000004
9fcdfda7
VK
108#define AF_REQUIRE_AUTH 0x00000008
109#define AF_LOG_UNRESOLVED_SYMBOLS 0x00000010
110#define AF_ENABLE_ACTIONS 0x00000020
111#define AF_REQUIRE_ENCRYPTION 0x00000040
112#define AF_HIDE_WINDOW 0x00000080
113#define AF_ENABLE_AUTOLOAD 0x00000100
114#define AF_ENABLE_PROXY 0x00000200
115#define AF_CENTRAL_CONFIG 0x00000400
116#define AF_ENABLE_SNMP_PROXY 0x00000800
117#define AF_SHUTDOWN 0x00001000
118#define AF_RUNNING_ON_NT4 0x00002000
119#define AF_REGISTER 0x00004000
120#define AF_ENABLE_WATCHDOG 0x00008000
121#define AF_CATCH_EXCEPTIONS 0x00010000
cbc777ee 122#define AF_WRITE_FULL_DUMP 0x00020000
5039dede
AK
123
124
125#ifdef _WIN32
126
127
128//
129// Attributes for H_NetIPStats and H_NetInterfacStats
130//
131
d4440ca8
VK
132#define NETINFO_IP_FORWARDING 1
133
134#define NETINFO_IF_BYTES_IN 1
135#define NETINFO_IF_BYTES_OUT 2
136#define NETINFO_IF_DESCR 3
137#define NETINFO_IF_IN_ERRORS 4
9f4da706 138#define NETINFO_IF_OPER_STATUS 5
d4440ca8
VK
139#define NETINFO_IF_OUT_ERRORS 6
140#define NETINFO_IF_PACKETS_IN 7
141#define NETINFO_IF_PACKETS_OUT 8
142#define NETINFO_IF_SPEED 9
143#define NETINFO_IF_ADMIN_STATUS 10
9f4da706 144#define NETINFO_IF_MTU 11
5039dede
AK
145
146
147//
148// Request types for H_MemoryInfo
149//
150
151#define MEMINFO_PHYSICAL_FREE 1
152#define MEMINFO_PHYSICAL_FREE_PCT 2
153#define MEMINFO_PHYSICAL_TOTAL 3
154#define MEMINFO_PHYSICAL_USED 4
155#define MEMINFO_PHYSICAL_USED_PCT 5
156#define MEMINFO_VIRTUAL_FREE 6
157#define MEMINFO_VIRTUAL_FREE_PCT 7
158#define MEMINFO_VIRTUAL_TOTAL 8
159#define MEMINFO_VIRTUAL_USED 9
160#define MEMINFO_VIRTUAL_USED_PCT 10
161
162
163//
164// Request types for H_DiskInfo
165//
166
167#define DISKINFO_FREE_BYTES 1
168#define DISKINFO_USED_BYTES 2
169#define DISKINFO_TOTAL_BYTES 3
170#define DISKINFO_FREE_SPACE_PCT 4
171#define DISKINFO_USED_SPACE_PCT 5
172
173#endif /* _WIN32 */
174
175
176//
177// Request types for H_DirInfo
178//
179
180#define DIRINFO_FILE_COUNT 1
181#define DIRINFO_FILE_SIZE 2
182
183
184//
185// Request types for H_FileTime
186//
187
188#define FILETIME_ATIME 1
189#define FILETIME_MTIME 2
190#define FILETIME_CTIME 3
191
192
193//
194// Action types
195//
196
197#define AGENT_ACTION_EXEC 1
198#define AGENT_ACTION_SUBAGENT 2
199#define AGENT_ACTION_SHELLEXEC 3
200
201
202//
203// Action definition structure
204//
205
206typedef struct
207{
208 TCHAR szName[MAX_PARAM_NAME];
209 int iType;
210 union
211 {
212 char *pszCmdLine;
213 struct __subagentAction
214 {
6173bea8 215 LONG (*fpHandler)(const TCHAR *, StringList *, const TCHAR *);
5039dede
AK
216 const TCHAR *pArg;
217 TCHAR szSubagentName[MAX_PATH];
218 } sa;
219 } handler;
220 TCHAR szDescription[MAX_DB_STRING];
221} ACTION;
222
223
224//
225// Loaded subagent information
226//
227
228struct SUBAGENT
229{
230 HMODULE hModule; // Subagent's module handle
231 NETXMS_SUBAGENT_INFO *pInfo; // Information provided by subagent
232 char szName[MAX_PATH]; // Name of the module
233#ifdef _NETWARE
234 char szEntryPoint[256]; // Name of agent's entry point
235#endif
236};
237
238
239//
240// Server information
241//
242
243struct SERVER_INFO
244{
245 DWORD dwIpAddr;
246 BOOL bMasterServer;
247 BOOL bControlServer;
248};
249
250
251//
252// Communication session
253//
254
255class CommSession
256{
257private:
258 SOCKET m_hSocket;
259 Queue *m_pSendQueue;
260 Queue *m_pMessageQueue;
261 CSCP_BUFFER *m_pMsgBuffer;
262 THREAD m_hWriteThread;
263 THREAD m_hProcessingThread;
264 THREAD m_hProxyReadThread;
265 DWORD m_dwHostAddr; // IP address of connected host (network byte order)
266 DWORD m_dwIndex;
267 BOOL m_bIsAuthenticated;
268 BOOL m_bMasterServer;
269 BOOL m_bControlServer;
270 BOOL m_bProxyConnection;
271 BOOL m_bAcceptTraps;
272 int m_hCurrFile;
273 DWORD m_dwFileRqId;
274 CSCP_ENCRYPTION_CONTEXT *m_pCtx;
e4a64da2 275 time_t m_ts; // Last activity timestamp
5039dede
AK
276 SOCKET m_hProxySocket; // Socket for proxy connection
277
e4a64da2
VK
278 BOOL sendRawMessage(CSCP_MESSAGE *pMsg, CSCP_ENCRYPTION_CONTEXT *pCtx);
279 void authenticate(CSCPMessage *pRequest, CSCPMessage *pMsg);
280 void getConfig(CSCPMessage *pMsg);
281 void updateConfig(CSCPMessage *pRequest, CSCPMessage *pMsg);
282 void getParameter(CSCPMessage *pRequest, CSCPMessage *pMsg);
283 void getList(CSCPMessage *pRequest, CSCPMessage *pMsg);
284 void action(CSCPMessage *pRequest, CSCPMessage *pMsg);
285 void recvFile(CSCPMessage *pRequest, CSCPMessage *pMsg);
286 DWORD upgrade(CSCPMessage *pRequest);
287 DWORD setupProxyConnection(CSCPMessage *pRequest);
288
4685a2ad
VK
289 void readThread();
290 void writeThread();
291 void processingThread();
292 void proxyReadThread();
e4a64da2
VK
293
294 static THREAD_RESULT THREAD_CALL readThreadStarter(void *);
295 static THREAD_RESULT THREAD_CALL writeThreadStarter(void *);
296 static THREAD_RESULT THREAD_CALL processingThreadStarter(void *);
297 static THREAD_RESULT THREAD_CALL proxyReadThreadStarter(void *);
5039dede
AK
298
299public:
300 CommSession(SOCKET hSocket, DWORD dwHostAddr, BOOL bMasterServer, BOOL bControlServer);
301 ~CommSession();
302
4685a2ad
VK
303 void run();
304 void disconnect();
5039dede 305
e4a64da2
VK
306 void sendMessage(CSCPMessage *pMsg) { m_pSendQueue->Put(pMsg->CreateMessage()); }
307 void sendRawMessage(CSCP_MESSAGE *pMsg) { m_pSendQueue->Put(nx_memdup(pMsg, ntohl(pMsg->dwSize))); }
4685a2ad 308 bool sendFile(DWORD requestId, const TCHAR *file, long offset);
5039dede 309
203e9d8a
VK
310 DWORD getServerAddress() { return m_dwHostAddr; }
311
4685a2ad 312 DWORD getIndex() { return m_dwIndex; }
e4a64da2 313 void setIndex(DWORD dwIndex) { if (m_dwIndex == INVALID_INDEX) m_dwIndex = dwIndex; }
5039dede 314
4685a2ad
VK
315 time_t getTimeStamp() { return m_ts; }
316 void updateTimeStamp() { m_ts = time(NULL); }
5039dede 317
4685a2ad 318 BOOL canAcceptTraps() { return m_bAcceptTraps; }
5039dede
AK
319};
320
321
322//
323// Functions
324//
325
203e9d8a
VK
326BOOL Initialize();
327void Shutdown();
328void Main();
5039dede
AK
329
330void ConsolePrintf(const char *pszFormat, ...);
c303351c 331void DebugPrintf(DWORD dwSessionId, int level, const char *pszFormat, ...);
5039dede
AK
332
333void BuildFullPath(TCHAR *pszFileName, TCHAR *pszFullPath);
334
335BOOL DownloadConfig(TCHAR *pszServer);
336
203e9d8a 337BOOL InitParameterList();
5039dede
AK
338void AddParameter(const char *szName, LONG (* fpHandler)(const char *, const char *, char *), const char *pArg,
339 int iDataType, const char *pszDescription);
f480bdd4 340void AddPushParameter(const TCHAR *name, int dataType, const TCHAR *description);
6173bea8 341void AddEnum(const char *szName, LONG (* fpHandler)(const char *, const char *, StringList *), const char *pArg);
5039dede
AK
342BOOL AddExternalParameter(char *pszCfgLine, BOOL bShellExec);
343DWORD GetParameterValue(DWORD dwSessionId, char *pszParam, char *pszValue);
6173bea8 344DWORD GetEnumValue(DWORD dwSessionId, char *pszParam, StringList *pValue);
5039dede
AK
345void GetParameterList(CSCPMessage *pMsg);
346
347BOOL LoadSubAgent(char *szModuleName);
203e9d8a 348void UnloadAllSubAgents();
5039dede
AK
349BOOL InitSubAgent(HMODULE hModule, TCHAR *pszModuleName,
350 BOOL (* SubAgentInit)(NETXMS_SUBAGENT_INFO **, TCHAR *),
351 TCHAR *pszEntryPoint);
6173bea8 352BOOL ProcessCmdBySubAgent(DWORD dwCommand, CSCPMessage *pRequest, CSCPMessage *pResponse, void *session);
5039dede
AK
353
354BOOL AddAction(const char *pszName, int iType, const char *pArg,
6173bea8 355 LONG (*fpHandler)(const TCHAR *, StringList *, const TCHAR *),
5039dede
AK
356 const char *pszSubAgent, const char *pszDescription);
357BOOL AddActionFromConfig(char *pszLine, BOOL bShellExec);
6173bea8 358DWORD ExecAction(char *pszAction, StringList *pArgs);
5039dede 359
6173bea8
VK
360DWORD ExecuteCommand(char *pszCommand, StringList *pArgs, pid_t *pid);
361DWORD ExecuteShellCommand(char *pszCommand, StringList *pArgs);
5039dede
AK
362
363BOOL WaitForProcess(const TCHAR *name);
364
365DWORD UpgradeAgent(TCHAR *pszPkgFile);
366
367void SendTrap(DWORD dwEventCode, int iNumArgs, TCHAR **ppArgList);
368void SendTrap(DWORD dwEventCode, const char *pszFormat, ...);
369void SendTrap(DWORD dwEventCode, const char *pszFormat, va_list args);
370
203e9d8a
VK
371Config *OpenRegistry();
372void CloseRegistry(bool modified);
373
5039dede
AK
374#ifdef _WIN32
375
203e9d8a 376void InitService();
5039dede 377void InstallService(char *execName, char *confFile);
203e9d8a
VK
378void RemoveService();
379void StartAgentService();
380void StopAgentService();
5039dede
AK
381BOOL WaitForService(DWORD dwDesiredState);
382void InstallEventSource(char *path);
203e9d8a 383void RemoveEventSource();
5039dede
AK
384
385char *GetPdhErrorText(DWORD dwError, char *pszBuffer, int iBufSize);
386
387#endif
388
389
390//
391// Global variables
392//
393
394extern DWORD g_dwFlags;
395extern char g_szLogFile[];
396extern char g_szSharedSecret[];
397extern char g_szConfigFile[];
398extern char g_szFileStore[];
399extern char g_szConfigServer[];
400extern char g_szRegistrar[];
401extern char g_szListenAddress[];
6ca3b41c 402extern TCHAR g_szConfigIncludeDir[];
5039dede
AK
403extern WORD g_wListenPort;
404extern SERVER_INFO g_pServerList[];
405extern DWORD g_dwServerCount;
406extern time_t g_tmAgentStartTime;
407extern char g_szPlatformSuffix[];
408extern DWORD g_dwStartupDelay;
409extern DWORD g_dwIdleTimeout;
410extern DWORD g_dwMaxSessions;
411extern DWORD g_dwExecTimeout;
412extern DWORD g_dwSNMPTimeout;
15e11d1b 413extern DWORD g_debugLevel;
5039dede 414
e6c91aac
VK
415extern Config *g_config;
416
5039dede
AK
417extern DWORD g_dwAcceptErrors;
418extern DWORD g_dwAcceptedConnections;
419extern DWORD g_dwRejectedConnections;
420
421extern CommSession **g_pSessionList;
422extern MUTEX g_hSessionListAccess;
423
424#ifdef _WIN32
425extern BOOL (__stdcall *imp_GlobalMemoryStatusEx)(LPMEMORYSTATUSEX);
426extern DWORD (__stdcall *imp_HrLanConnectionNameFromGuidOrPath)(LPWSTR, LPWSTR, LPWSTR, LPDWORD);
9f96fc88
VK
427
428extern TCHAR g_windowsEventSourceName[];
5039dede
AK
429#endif /* _WIN32 */
430
431#endif /* _nxagentd_h_ */