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