agent restart issues restart command to all connected external subagents
[public/netxms.git] / src / agent / core / nxagentd.h
CommitLineData
05e3ba32 1/*
5039dede 2** NetXMS multiplatform core agent
3f635e1b 3** Copyright (C) 2003-2016 Victor Kirhenshtein
5039dede
AK
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>
bf6fb6c3 30#include <nxcpapi.h>
5039dede
AK
31#include <nxqueue.h>
32#include <nxlog.h>
bf6fb6c3 33#include <nxcldefs.h>
c72b812a 34#include <nxdbapi.h>
5039dede 35#include "messages.h"
489b117b 36#include "nxsnmp.h"
7f6ecb6d 37#include "localdb.h"
5039dede 38
5039dede
AK
39#ifdef _NETWARE
40#undef SEVERITY_CRITICAL
41#endif
42
a2a24e9d
VK
43#ifdef _WIN32
44#include <aclapi.h>
a2a24e9d
VK
45#endif
46
1a93e64a
VK
47/**
48 * Default files
49 */
5039dede 50#if defined(_WIN32)
1039d7ee
VK
51#define AGENT_DEFAULT_CONFIG _T("{search}")
52#define AGENT_DEFAULT_CONFIG_D _T("{search}")
bf3b7f79
VK
53#define AGENT_DEFAULT_LOG _T("C:\\nxagentd.log")
54#define AGENT_DEFAULT_FILE_STORE _T("C:\\")
1039d7ee 55#define AGENT_DEFAULT_DATA_DIR _T("{default}")
5039dede
AK
56#elif defined(_NETWARE)
57#define AGENT_DEFAULT_CONFIG "SYS:ETC/nxagentd.conf"
abf35d58 58#define AGENT_DEFAULT_CONFIG_D "SYS:ETC/nxagentd.conf.d"
5039dede
AK
59#define AGENT_DEFAULT_LOG "SYS:ETC/nxagentd.log"
60#define AGENT_DEFAULT_FILE_STORE "SYS:\\"
203e9d8a 61#define AGENT_DEFAULT_DATA_DIR "SYS:ETC"
5039dede 62#elif defined(_IPSO)
f3387429
VK
63#define AGENT_DEFAULT_CONFIG _T("/opt/netxms/etc/nxagentd.conf")
64#define AGENT_DEFAULT_CONFIG_D _T("/opt/netxms/etc/nxagentd.conf.d")
65#define AGENT_DEFAULT_LOG _T("/opt/netxms/log/nxagentd.log")
66#define AGENT_DEFAULT_FILE_STORE _T("/opt/netxms/store")
67#define AGENT_DEFAULT_DATA_DIR _T("/opt/netxms/data")
5039dede 68#else
f3387429
VK
69#define AGENT_DEFAULT_CONFIG _T("{search}")
70#define AGENT_DEFAULT_CONFIG_D _T("{search}")
71#define AGENT_DEFAULT_LOG _T("/var/log/nxagentd")
72#define AGENT_DEFAULT_FILE_STORE _T("/tmp")
f31c22b3 73#define AGENT_DEFAULT_DATA_DIR _T("{default}")
5039dede
AK
74#endif
75
489b117b 76/**
77 * Constants
78 */
5039dede 79#ifdef _WIN32
9f96fc88
VK
80#define DEFAULT_AGENT_SERVICE_NAME _T("NetXMSAgentdW32")
81#define DEFAULT_AGENT_EVENT_SOURCE _T("NetXMS Win32 Agent")
82#define NXAGENTD_SYSLOG_NAME g_windowsEventSourceName
5039dede 83#else
9f96fc88 84#define NXAGENTD_SYSLOG_NAME _T("nxagentd")
5039dede
AK
85#endif
86
87#define MAX_PSUFFIX_LENGTH 32
88#define MAX_SERVERS 32
e464b7dc 89#define MAX_ESA_USER_NAME 64
842378a4
VK
90#define MAX_AGENT_MSG_SIZE 4194304
91
9fcdfda7
VK
92#define AF_DAEMON 0x00000001
93#define AF_USE_SYSLOG 0x00000002
0df58041 94#define AF_SUBAGENT_LOADER 0x00000004
9fcdfda7
VK
95#define AF_REQUIRE_AUTH 0x00000008
96#define AF_LOG_UNRESOLVED_SYMBOLS 0x00000010
97#define AF_ENABLE_ACTIONS 0x00000020
98#define AF_REQUIRE_ENCRYPTION 0x00000040
99#define AF_HIDE_WINDOW 0x00000080
100#define AF_ENABLE_AUTOLOAD 0x00000100
101#define AF_ENABLE_PROXY 0x00000200
102#define AF_CENTRAL_CONFIG 0x00000400
175bd003 103#define AF_ENABLE_SNMP_PROXY 0x00000800
9fcdfda7 104#define AF_SHUTDOWN 0x00001000
175bd003 105#define AF_INTERACTIVE_SERVICE 0x00002000
9fcdfda7
VK
106#define AF_REGISTER 0x00004000
107#define AF_ENABLE_WATCHDOG 0x00008000
108#define AF_CATCH_EXCEPTIONS 0x00010000
cbc777ee 109#define AF_WRITE_FULL_DUMP 0x00020000
c90749f1 110#define AF_ENABLE_CONTROL_CONNECTOR 0x00040000
6239e6a4
VK
111#define AF_DISABLE_IPV4 0x00080000
112#define AF_DISABLE_IPV6 0x00100000
489b117b 113#define AF_ENABLE_SNMP_TRAP_PROXY 0x00200000
4e0e77e6 114#define AF_BACKGROUND_LOG_WRITER 0x00400000
685508a7 115#define AF_ENABLE_SYSLOG_PROXY 0x00800000
5039dede 116
c5459490 117// Flags for component failures
b123ecb3 118#define FAIL_OPEN_LOG 0x00000001
119#define FAIL_OPEN_DATABASE 0x00000002
c5459490 120#define FAIL_UPGRADE_DATABASE 0x00000004
5039dede
AK
121
122#ifdef _WIN32
123
489b117b 124/**
125 * Request types for H_MemoryInfo
126 */
5039dede
AK
127#define MEMINFO_PHYSICAL_FREE 1
128#define MEMINFO_PHYSICAL_FREE_PCT 2
129#define MEMINFO_PHYSICAL_TOTAL 3
130#define MEMINFO_PHYSICAL_USED 4
131#define MEMINFO_PHYSICAL_USED_PCT 5
132#define MEMINFO_VIRTUAL_FREE 6
133#define MEMINFO_VIRTUAL_FREE_PCT 7
134#define MEMINFO_VIRTUAL_TOTAL 8
135#define MEMINFO_VIRTUAL_USED 9
136#define MEMINFO_VIRTUAL_USED_PCT 10
137
489b117b 138/**
139 * Request types for H_DiskInfo
140 */
5039dede
AK
141#define DISKINFO_FREE_BYTES 1
142#define DISKINFO_USED_BYTES 2
143#define DISKINFO_TOTAL_BYTES 3
144#define DISKINFO_FREE_SPACE_PCT 4
145#define DISKINFO_USED_SPACE_PCT 5
146
147#endif /* _WIN32 */
148
374afd7b
VK
149/**
150 * Thread pool stats
151 */
152enum ThreadPoolStat
153{
154 THREAD_POOL_CURR_SIZE,
155 THREAD_POOL_MIN_SIZE,
156 THREAD_POOL_MAX_SIZE,
157 THREAD_POOL_REQUESTS,
158 THREAD_POOL_LOAD,
159 THREAD_POOL_USAGE,
160 THREAD_POOL_LOADAVG_1,
161 THREAD_POOL_LOADAVG_5,
162 THREAD_POOL_LOADAVG_15
163};
5039dede 164
489b117b 165/**
166 * Request types for H_DirInfo
167 */
5039dede
AK
168#define DIRINFO_FILE_COUNT 1
169#define DIRINFO_FILE_SIZE 2
5ad4ed06 170#define DIRINFO_FOLDER_COUNT 3
5039dede 171
489b117b 172/**
173 * Request types for H_FileTime
174 */
5039dede
AK
175#define FILETIME_ATIME 1
176#define FILETIME_MTIME 2
177#define FILETIME_CTIME 3
178
489b117b 179/**
180 * Action types
181 */
5039dede
AK
182#define AGENT_ACTION_EXEC 1
183#define AGENT_ACTION_SUBAGENT 2
84d1bd22
VK
184#define AGENT_ACTION_SHELLEXEC 3
185
8e334368
VK
186/**
187 * Action definition structure
188 */
5039dede
AK
189typedef struct
190{
191 TCHAR szName[MAX_PARAM_NAME];
192 int iType;
193 union
194 {
05e3ba32 195 TCHAR *pszCmdLine; // to TCHAR
5039dede
AK
196 struct __subagentAction
197 {
060c5a11 198 LONG (*fpHandler)(const TCHAR *, StringList *, const TCHAR *, AbstractCommSession *);
5039dede
AK
199 const TCHAR *pArg;
200 TCHAR szSubagentName[MAX_PATH];
201 } sa;
202 } handler;
203 TCHAR szDescription[MAX_DB_STRING];
204} ACTION;
205
8e334368
VK
206/**
207 * Loaded subagent information
208 */
5039dede
AK
209struct SUBAGENT
210{
211 HMODULE hModule; // Subagent's module handle
212 NETXMS_SUBAGENT_INFO *pInfo; // Information provided by subagent
bf3b7f79 213 TCHAR szName[MAX_PATH]; // Name of the module // to TCHAR by LWX
5039dede
AK
214#ifdef _NETWARE
215 char szEntryPoint[256]; // Name of agent's entry point
216#endif
217};
218
0f506caa
VK
219/**
220 * External subagent information
221 */
204ace22
VK
222class ExternalSubagent
223{
224private:
225 TCHAR m_name[MAX_SUBAGENT_NAME];
e464b7dc 226 TCHAR m_user[MAX_ESA_USER_NAME];
84d1bd22 227 HPIPE m_pipe;
204ace22 228 bool m_connected;
0df58041 229 MsgWaitQueue *m_msgQueue;
967893bb 230 UINT32 m_requestId;
0df58041
VK
231 MUTEX m_mutexPipeWrite;
232
b368969c
VK
233 bool sendMessage(NXCPMessage *msg);
234 NXCPMessage *waitForMessage(WORD code, UINT32 id);
967893bb
VK
235 UINT32 waitForRCC(UINT32 id);
236 NETXMS_SUBAGENT_PARAM *getSupportedParameters(UINT32 *count);
237 NETXMS_SUBAGENT_LIST *getSupportedLists(UINT32 *count);
238 NETXMS_SUBAGENT_TABLE *getSupportedTables(UINT32 *count);
204ace22
VK
239
240public:
e464b7dc 241 ExternalSubagent(const TCHAR *name, const TCHAR *user);
204ace22
VK
242 ~ExternalSubagent();
243
84d1bd22 244 void connect(HPIPE hPipe);
204ace22
VK
245
246 bool isConnected() { return m_connected; }
247 const TCHAR *getName() { return m_name; }
e464b7dc 248 const TCHAR *getUserName() { return m_user; }
0df58041 249
967893bb
VK
250 UINT32 getParameter(const TCHAR *name, TCHAR *buffer);
251 UINT32 getTable(const TCHAR *name, Table *value);
252 UINT32 getList(const TCHAR *name, StringList *value);
b368969c 253 void listParameters(NXCPMessage *msg, UINT32 *baseId, UINT32 *count);
0df58041 254 void listParameters(StringList *list);
b368969c 255 void listLists(NXCPMessage *msg, UINT32 *baseId, UINT32 *count);
0f506caa 256 void listLists(StringList *list);
b368969c 257 void listTables(NXCPMessage *msg, UINT32 *baseId, UINT32 *count);
0f506caa 258 void listTables(StringList *list);
f918b160 259 void shutdown();
eb17778f 260 void restart();
204ace22
VK
261};
262
0f506caa
VK
263/**
264 * Server information
265 */
9319c166 266class ServerInfo
5039dede 267{
9319c166
VK
268private:
269 char *m_name;
8c75ad41 270 InetAddress m_address;
9319c166
VK
271 bool m_control;
272 bool m_master;
273 time_t m_lastResolveTime;
274 bool m_redoResolve;
275 MUTEX m_mutex;
276
277 void resolve();
278
279public:
280 ServerInfo(const TCHAR *name, bool control, bool master);
281 ~ServerInfo();
282
8c75ad41 283 bool match(const InetAddress &addr);
9319c166
VK
284
285 bool isMaster() { return m_master; }
286 bool isControl() { return m_control; }
5039dede
AK
287};
288
6fbaa926
VK
289/**
290 * Pending request information
291 */
292struct PendingRequest
293{
294 UINT32 id;
295 INT64 expirationTime;
296 bool completed;
297
298 PendingRequest(NXCPMessage *msg, UINT32 timeout)
299 {
300 id = msg->getId();
301 expirationTime = GetCurrentTimeMs() + (INT64)timeout;
302 completed = false;
303 }
304};
305
0f506caa
VK
306/**
307 * Communication session
308 */
7cf549ad 309class CommSession : public AbstractCommSession
5039dede
AK
310{
311private:
da8453a5
VK
312 UINT32 m_id;
313 UINT32 m_index;
eae56922 314 AbstractCommChannel *m_channel;
6fbaa926
VK
315 Queue *m_sendQueue;
316 Queue *m_processingQueue;
5039dede
AK
317 THREAD m_hWriteThread;
318 THREAD m_hProcessingThread;
319 THREAD m_hProxyReadThread;
e9902466 320 UINT64 m_serverId;
8c75ad41 321 InetAddress m_serverAddr; // IP address of connected host
da8453a5 322 bool m_disconnected;
9319c166
VK
323 bool m_authenticated;
324 bool m_masterServer;
325 bool m_controlServer;
326 bool m_proxyConnection;
d1dfba1a 327 bool m_acceptData;
9319c166 328 bool m_acceptTraps;
e13420c1 329 bool m_acceptFileUpdates;
ea3993c8 330 bool m_ipv6Aware;
1a5ddd2a 331 bool m_bulkReconciliationSupported;
7b45b90d 332 HashMap<UINT32, DownloadFileInfo> m_downloadFileMap;
9674aefa 333 bool m_allowCompression; // allow compression for structured messages
98abc9f1 334 NXCPEncryptionContext *m_pCtx;
e4a64da2 335 time_t m_ts; // Last activity timestamp
5039dede 336 SOCKET m_hProxySocket; // Socket for proxy connection
7b8b337e 337 MUTEX m_socketWriteMutex;
6fbaa926
VK
338 VolatileCounter m_requestId;
339 MsgWaitQueue *m_responseQueue;
5039dede 340
da8453a5 341 bool sendRawMessage(NXCP_MESSAGE *msg, NXCPEncryptionContext *ctx);
b368969c
VK
342 void authenticate(NXCPMessage *pRequest, NXCPMessage *pMsg);
343 void getConfig(NXCPMessage *pMsg);
344 void updateConfig(NXCPMessage *pRequest, NXCPMessage *pMsg);
345 void getParameter(NXCPMessage *pRequest, NXCPMessage *pMsg);
346 void getList(NXCPMessage *pRequest, NXCPMessage *pMsg);
347 void getTable(NXCPMessage *pRequest, NXCPMessage *pMsg);
348 void action(NXCPMessage *pRequest, NXCPMessage *pMsg);
349 void recvFile(NXCPMessage *pRequest, NXCPMessage *pMsg);
350 void getLocalFile(NXCPMessage *pRequest, NXCPMessage *pMsg);
351 void cancelFileMonitoring(NXCPMessage *pRequest, NXCPMessage *pMsg);
eae56922 352 UINT32 upgrade(NXCPMessage *request);
b368969c 353 UINT32 setupProxyConnection(NXCPMessage *pRequest);
e4a64da2 354
4685a2ad
VK
355 void readThread();
356 void writeThread();
357 void processingThread();
358 void proxyReadThread();
83191808 359 void proxySnmpRequest(NXCPMessage *request);
e4a64da2
VK
360
361 static THREAD_RESULT THREAD_CALL readThreadStarter(void *);
362 static THREAD_RESULT THREAD_CALL writeThreadStarter(void *);
363 static THREAD_RESULT THREAD_CALL processingThreadStarter(void *);
364 static THREAD_RESULT THREAD_CALL proxyReadThreadStarter(void *);
5039dede
AK
365
366public:
eae56922 367 CommSession(AbstractCommChannel *channel, const InetAddress &serverAddr, bool masterServer, bool controlServer);
6fbaa926 368 virtual ~CommSession();
5039dede 369
4685a2ad
VK
370 void run();
371 void disconnect();
5039dede 372
da8453a5 373 virtual bool sendMessage(NXCPMessage *msg);
9674aefa 374 virtual void postMessage(NXCPMessage *msg) { if (!m_disconnected) m_sendQueue->put(msg->createMessage(m_allowCompression)); }
da8453a5
VK
375 virtual bool sendRawMessage(NXCP_MESSAGE *msg);
376 virtual void postRawMessage(NXCP_MESSAGE *msg) { if (!m_disconnected) m_sendQueue->put(nx_memdup(msg, ntohl(msg->size))); }
b4879817 377 virtual bool sendFile(UINT32 requestId, const TCHAR *file, long offset, bool allowCompression);
63ff3c9d 378 virtual UINT32 doRequest(NXCPMessage *msg, UINT32 timeout);
a1273b42 379 virtual NXCPMessage *doRequestEx(NXCPMessage *msg, UINT32 timeout);
6fbaa926 380 virtual UINT32 generateRequestId();
5039dede 381
da8453a5
VK
382 virtual UINT32 getId() { return m_id; };
383
e9902466 384 virtual UINT64 getServerId() { return m_serverId; }
8c75ad41 385 virtual const InetAddress& getServerAddress() { return m_serverAddr; }
203e9d8a 386
da8453a5
VK
387 virtual bool isMasterServer() { return m_masterServer; }
388 virtual bool isControlServer() { return m_controlServer; }
389 virtual bool canAcceptData() { return m_acceptData; }
390 virtual bool canAcceptTraps() { return m_acceptTraps; }
391 virtual bool canAcceptFileUpdates() { return m_acceptFileUpdates; }
392 virtual bool isBulkReconciliationSupported() { return m_bulkReconciliationSupported; }
ea3993c8
VK
393 virtual bool isIPv6Aware() { return m_ipv6Aware; }
394
7b45b90d 395 virtual UINT32 openFile(TCHAR *nameOfFile, UINT32 requestId, time_t fileModTime = 0);
da8453a5
VK
396
397 virtual void debugPrintf(int level, const TCHAR *format, ...);
398
399 UINT32 getIndex() { return m_index; }
400 void setIndex(UINT32 index) { if (m_index == INVALID_INDEX) m_index = index; }
5039dede 401
4685a2ad
VK
402 time_t getTimeStamp() { return m_ts; }
403 void updateTimeStamp() { m_ts = time(NULL); }
da8453a5 404};
5039dede 405
da8453a5
VK
406/**
407 * Pseudo-session for cached data collection
408 */
409class VirtualSession : public AbstractCommSession
410{
411private:
412 UINT32 m_id;
413 UINT64 m_serverId;
489b117b 414
da8453a5
VK
415public:
416 VirtualSession(UINT64 serverId);
417
418 virtual UINT32 getId() { return m_id; }
419
420 virtual UINT64 getServerId() { return m_serverId; };
421 virtual const InetAddress& getServerAddress() { return InetAddress::LOOPBACK; }
422
423 virtual bool isMasterServer() { return false; }
424 virtual bool isControlServer() { return false; }
425 virtual bool canAcceptData() { return true; }
426 virtual bool canAcceptTraps() { return true; }
427 virtual bool canAcceptFileUpdates() { return false; }
428 virtual bool isBulkReconciliationSupported() { return false; }
429 virtual bool isIPv6Aware() { return true; }
430
431 virtual bool sendMessage(NXCPMessage *pMsg) { return false; }
432 virtual void postMessage(NXCPMessage *pMsg) { }
433 virtual bool sendRawMessage(NXCP_MESSAGE *pMsg) { return false; }
434 virtual void postRawMessage(NXCP_MESSAGE *pMsg) { }
b4879817 435 virtual bool sendFile(UINT32 requestId, const TCHAR *file, long offset, bool allowCompression) { return false; }
da8453a5
VK
436 virtual UINT32 doRequest(NXCPMessage *msg, UINT32 timeout) { return RCC_NOT_IMPLEMENTED; }
437 virtual NXCPMessage *doRequestEx(NXCPMessage *msg, UINT32 timeout) { return NULL; }
438 virtual UINT32 generateRequestId() { return 0; }
7b45b90d 439 virtual UINT32 openFile(TCHAR *fileName, UINT32 requestId, time_t fileModTime = 0) { return ERR_INTERNAL_ERROR; }
da8453a5 440 virtual void debugPrintf(int level, const TCHAR *format, ...);
5039dede
AK
441};
442
9c786c0f
VK
443/**
444 * Session agent connector
445 */
446class SessionAgentConnector : public RefCountObject
447{
448private:
449 UINT32 m_id;
450 SOCKET m_socket;
451 MUTEX m_mutex;
b368969c 452 NXCP_BUFFER m_msgBuffer;
9c786c0f
VK
453 MsgWaitQueue m_msgQueue;
454 UINT32 m_sessionId;
455 TCHAR *m_sessionName;
74540dab
VK
456 INT16 m_sessionState;
457 TCHAR *m_userName;
9c786c0f
VK
458 VolatileCounter m_requestId;
459
460 void readThread();
b368969c 461 bool sendMessage(NXCPMessage *msg);
9c786c0f
VK
462 UINT32 nextRequestId() { return InterlockedIncrement(&m_requestId); }
463
464 static THREAD_RESULT THREAD_CALL readThreadStarter(void *);
465
466public:
467 SessionAgentConnector(UINT32 id, SOCKET s);
5e254c4e 468 virtual ~SessionAgentConnector();
9c786c0f
VK
469
470 void run();
471 void disconnect();
472
473 UINT32 getId() { return m_id; }
474 UINT32 getSessionId() { return m_sessionId; }
74540dab 475 INT16 getSessionState() { return m_sessionState; }
9c786c0f 476 const TCHAR *getSessionName() { return CHECK_NULL(m_sessionName); }
74540dab 477 const TCHAR *getUserName() { return CHECK_NULL(m_userName); }
9c786c0f
VK
478
479 bool testConnection();
b368969c 480 void takeScreenshot(NXCPMessage *msg);
9c786c0f
VK
481};
482
489b117b 483/**
484 * Class to reciever traps
485 */
486class SNMP_TrapProxyTransport : public SNMP_UDPTransport
487{
488public:
489 SNMP_TrapProxyTransport(SOCKET hSocket);
e58d58bd
VK
490
491 int readRawMessage(BYTE **rawData, UINT32 timeout, struct sockaddr *sender, socklen_t *addrSize);
489b117b 492};
493
296ae03d
VK
494/**
495 * SNMP target
496 */
c1bc623e 497class SNMPTarget : public RefCountObject
296ae03d
VK
498{
499private:
de4af576 500 uuid m_guid;
296ae03d
VK
501 UINT64 m_serverId;
502 InetAddress m_ipAddress;
503 UINT16 m_port;
504 BYTE m_snmpVersion;
505 BYTE m_authType;
506 BYTE m_encType;
507 char *m_authName;
508 char *m_authPassword;
509 char *m_encPassword;
510 SNMP_Transport *m_transport;
511
512public:
513 SNMPTarget(UINT64 serverId, NXCPMessage *msg, UINT32 baseId);
514 SNMPTarget(DB_RESULT hResult, int row);
5e254c4e 515 virtual ~SNMPTarget();
296ae03d 516
de4af576 517 const uuid& getGuid() const { return m_guid; }
296ae03d
VK
518 SNMP_Transport *getTransport(UINT16 port);
519
520 bool saveToDatabase();
521};
522
0f506caa
VK
523/**
524 * Functions
525 */
203e9d8a
VK
526BOOL Initialize();
527void Shutdown();
528void Main();
640b6719 529
da8453a5 530void ConsolePrintf(const TCHAR *format, ...)
6e383343 531#if !defined(UNICODE) && (defined(__GNUC__) || defined(__clang__))
640b6719
VK
532 __attribute__ ((format(printf, 1, 2)))
533#endif
534;
535
da8453a5 536void DebugPrintf(int level, const TCHAR *format, ...)
6e383343 537#if !defined(UNICODE) && (defined(__GNUC__) || defined(__clang__))
da8453a5 538 __attribute__ ((format(printf, 2, 3)))
640b6719
VK
539#endif
540;
541
5039dede
AK
542void BuildFullPath(TCHAR *pszFileName, TCHAR *pszFullPath);
543
544BOOL DownloadConfig(TCHAR *pszServer);
545
203e9d8a 546BOOL InitParameterList();
bf3b7f79 547
060c5a11 548void AddParameter(const TCHAR *szName, LONG (* fpHandler)(const TCHAR *, const TCHAR *, TCHAR *, AbstractCommSession *), const TCHAR *pArg,
bf3b7f79 549 int iDataType, const TCHAR *pszDescription);
f480bdd4 550void AddPushParameter(const TCHAR *name, int dataType, const TCHAR *description);
060c5a11 551void AddList(const TCHAR *name, LONG (* handler)(const TCHAR *, const TCHAR *, StringList *, AbstractCommSession *), const TCHAR *arg);
e13420c1 552void AddTable(const TCHAR *name, LONG (* handler)(const TCHAR *, const TCHAR *, Table *, AbstractCommSession *),
cb97f4dd
VK
553 const TCHAR *arg, const TCHAR *instanceColumns, const TCHAR *description,
554 int numColumns, NETXMS_SUBAGENT_TABLE_COLUMN *columns);
a85cab20
VK
555bool AddExternalParameter(TCHAR *config, bool shellExec, bool isList);
556bool AddExternalTable(TCHAR *config, bool shellExec);
da8453a5
VK
557UINT32 GetParameterValue(const TCHAR *param, TCHAR *value, AbstractCommSession *session);
558UINT32 GetListValue(const TCHAR *param, StringList *value, AbstractCommSession *session);
559UINT32 GetTableValue(const TCHAR *param, Table *value, AbstractCommSession *session);
b368969c
VK
560void GetParameterList(NXCPMessage *pMsg);
561void GetEnumList(NXCPMessage *pMsg);
562void GetTableList(NXCPMessage *pMsg);
bf3b7f79 563BOOL LoadSubAgent(TCHAR *szModuleName);
203e9d8a 564void UnloadAllSubAgents();
c1165d33
VK
565BOOL InitSubAgent(HMODULE hModule, const TCHAR *pszModuleName,
566 BOOL (* SubAgentInit)(NETXMS_SUBAGENT_INFO **, Config *),
567 const TCHAR *pszEntryPoint);
b368969c 568BOOL ProcessCmdBySubAgent(UINT32 dwCommand, NXCPMessage *pRequest, NXCPMessage *pResponse, AbstractCommSession *session);
05e3ba32 569BOOL AddAction(const TCHAR *pszName, int iType, const TCHAR *pArg,
060c5a11 570 LONG (*fpHandler)(const TCHAR *, StringList *, const TCHAR *, AbstractCommSession *session),
bf3b7f79
VK
571 const TCHAR *pszSubAgent, const TCHAR *pszDescription);
572BOOL AddActionFromConfig(TCHAR *pszLine, BOOL bShellExec);
060c5a11 573UINT32 ExecAction(const TCHAR *action, StringList *args, AbstractCommSession *session);
908d71bd 574UINT32 ExecActionWithOutput(CommSession *session, UINT32 requestId, const TCHAR *action, StringList *args);
967893bb
VK
575UINT32 ExecuteCommand(TCHAR *pszCommand, StringList *pArgs, pid_t *pid);
576UINT32 ExecuteShellCommand(TCHAR *pszCommand, StringList *pArgs);
5039dede 577
9795113c
VK
578void StartParamProvidersPoller();
579bool AddParametersProvider(const TCHAR *line);
0df58041 580LONG GetParameterValueFromExtProvider(const TCHAR *name, TCHAR *buffer);
b368969c 581void ListParametersFromExtProviders(NXCPMessage *msg, UINT32 *baseId, UINT32 *count);
9795113c
VK
582void ListParametersFromExtProviders(StringList *list);
583
204ace22 584bool AddExternalSubagent(const TCHAR *config);
967893bb
VK
585UINT32 GetParameterValueFromExtSubagent(const TCHAR *name, TCHAR *buffer);
586UINT32 GetTableValueFromExtSubagent(const TCHAR *name, Table *value);
587UINT32 GetListValueFromExtSubagent(const TCHAR *name, StringList *value);
b368969c 588void ListParametersFromExtSubagents(NXCPMessage *msg, UINT32 *baseId, UINT32 *count);
0df58041 589void ListParametersFromExtSubagents(StringList *list);
b368969c 590void ListListsFromExtSubagents(NXCPMessage *msg, UINT32 *baseId, UINT32 *count);
0f506caa 591void ListListsFromExtSubagents(StringList *list);
b368969c 592void ListTablesFromExtSubagents(NXCPMessage *msg, UINT32 *baseId, UINT32 *count);
0f506caa 593void ListTablesFromExtSubagents(StringList *list);
b368969c
VK
594bool SendMessageToPipe(HPIPE hPipe, NXCP_MESSAGE *msg);
595bool SendMessageToMasterAgent(NXCPMessage *msg);
596bool SendRawMessageToMasterAgent(NXCP_MESSAGE *msg);
f918b160 597void ShutdownExtSubagents();
eb17778f 598void RestartExtSubagents();
204ace22 599
83b20bab
VK
600void RegisterApplicationAgent(const TCHAR *name);
601UINT32 GetParameterValueFromAppAgent(const TCHAR *name, TCHAR *buffer);
602
da8453a5 603bool WaitForProcess(const TCHAR *name);
5039dede 604
967893bb 605UINT32 UpgradeAgent(TCHAR *pszPkgFile);
5039dede 606
967893bb
VK
607void SendTrap(UINT32 dwEventCode, const TCHAR *eventName, int iNumArgs, TCHAR **ppArgList);
608void SendTrap(UINT32 dwEventCode, const TCHAR *eventName, const char *pszFormat, ...);
609void SendTrap(UINT32 dwEventCode, const TCHAR *eventName, const char *pszFormat, va_list args);
b368969c 610void ForwardTrap(NXCPMessage *msg);
5039dede 611
0bd64860 612void StartPushConnector();
b60584cf 613bool PushData(const TCHAR *parameter, const TCHAR *value, UINT32 objectId, time_t timestamp);
0bd64860 614
a2a24e9d
VK
615void StartStorageDiscoveryConnector();
616
f918b160 617void StartControlConnector();
b368969c 618bool SendControlMessage(NXCPMessage *msg);
0bd64860 619
9c786c0f
VK
620void StartSessionAgentConnector();
621SessionAgentConnector *AcquireSessionAgentConnector(const TCHAR *sessionName);
09ecddac
VK
622
623UINT32 GenerateMessageId();
9c786c0f 624
87fff547
VK
625void ConfigureDataCollection(UINT64 serverId, NXCPMessage *msg);
626
34b9a3e7 627bool EnumerateSessions(EnumerationCallbackResult (* callback)(AbstractCommSession *, void* ), void *data);
6fbaa926 628AbstractCommSession *FindServerSession(UINT64 serverId);
a1273b42 629AbstractCommSession *FindServerSession(bool (*comparator)(AbstractCommSession *, void *), void *userData);
34b9a3e7 630
5039dede
AK
631#ifdef _WIN32
632
203e9d8a 633void InitService();
56ea99c3 634void InstallService(TCHAR *execName, TCHAR *confFile, int debugLevel);
203e9d8a
VK
635void RemoveService();
636void StartAgentService();
637void StopAgentService();
0137b9f7 638bool WaitForService(DWORD dwDesiredState);
bf3b7f79 639void InstallEventSource(const TCHAR *path);
203e9d8a 640void RemoveEventSource();
5039dede 641
967893bb 642TCHAR *GetPdhErrorText(UINT32 dwError, TCHAR *pszBuffer, int iBufSize);
5039dede
AK
643
644#endif
645
646
489b117b 647/**
648 * Global variables
649 */
967893bb 650extern UINT32 g_dwFlags;
b123ecb3 651extern UINT32 g_failFlags;
bf3b7f79 652extern TCHAR g_szLogFile[];
05e3ba32 653extern TCHAR g_szSharedSecret[];
654extern TCHAR g_szConfigFile[];
655extern TCHAR g_szFileStore[];
656extern TCHAR g_szConfigServer[];
657extern TCHAR g_szRegistrar[];
658extern TCHAR g_szListenAddress[];
6ca3b41c 659extern TCHAR g_szConfigIncludeDir[];
b123ecb3 660extern TCHAR g_szConfigPolicyDir[];
3df8bccd 661extern TCHAR g_szLogParserDirectory[];
3b89a4c1 662extern TCHAR g_certificateDirectory[];
c72b812a 663extern TCHAR g_szDataDirectory[];
0df58041 664extern TCHAR g_masterAgent[];
489b117b 665extern TCHAR g_szSNMPTrapListenAddress[];
9c786c0f 666extern UINT16 g_wListenPort;
cce82c3a 667extern TCHAR g_systemName[];
9319c166 668extern ObjectArray<ServerInfo> g_serverList;
5039dede 669extern time_t g_tmAgentStartTime;
05e3ba32 670extern TCHAR g_szPlatformSuffix[];
967893bb
VK
671extern UINT32 g_dwStartupDelay;
672extern UINT32 g_dwIdleTimeout;
673extern UINT32 g_dwMaxSessions;
2f943f78
VK
674extern UINT32 g_execTimeout;
675extern UINT32 g_snmpTimeout;
ee3d42df 676extern UINT16 g_snmpTrapPort;
89d79e7b 677extern UINT32 g_longRunningQueryThreshold;
9c786c0f 678extern UINT16 g_sessionAgentPort;
3f635e1b 679extern UINT32 g_zoneId;
685508a7 680extern UINT16 g_syslogListenPort;
5039dede 681
e6c91aac
VK
682extern Config *g_config;
683
da8453a5
VK
684extern UINT32 g_acceptErrors;
685extern UINT32 g_acceptedConnections;
686extern UINT32 g_rejectedConnections;
5039dede
AK
687
688extern CommSession **g_pSessionList;
689extern MUTEX g_hSessionListAccess;
83191808 690extern ThreadPool *g_snmpProxyThreadPool;
3b89a4c1 691extern ThreadPool *g_commThreadPool;
5039dede
AK
692
693#ifdef _WIN32
9f96fc88 694extern TCHAR g_windowsEventSourceName[];
5039dede
AK
695#endif /* _WIN32 */
696
697#endif /* _nxagentd_h_ */