implemented SNMP raw value transformation on input (before delta calculation and...
[public/netxms.git] / src / server / core / main.cpp
CommitLineData
5039dede
AK
1/*
2** NetXMS - Network Management System
48c0079d 3** Copyright (C) 2003-2011 NetXMS Team
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 useful,
11** but WITHOUT 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: main.cpp
20**
21**/
22
23#include "nxcore.h"
24#include <netxmsdb.h>
25
0322eed7
VK
26#if !defined(_WIN32) && HAVE_READLINE_READLINE_H && HAVE_READLINE
27#include <readline/readline.h>
28#include <readline/history.h>
29#define USE_READLINE 1
5039dede
AK
30#endif
31
32#ifdef _WIN32
a23d8e0d
VK
33#include <errno.h>
34#include <psapi.h>
d717b69c
VK
35#define open _open
36#define write _write
37#define close _close
5039dede 38#else
a23d8e0d
VK
39#include <signal.h>
40#include <sys/wait.h>
5039dede
AK
41#endif
42
43
44//
45// Messages generated by mc.pl (for UNIX version only)
46//
47
48#ifndef _WIN32
49extern unsigned int g_dwNumMessages;
50extern const TCHAR *g_szMessages[];
51#endif
52
53
54//
55// Shutdown reasons
56//
57
58#define SHUTDOWN_DEFAULT 0
59#define SHUTDOWN_FROM_CONSOLE 1
60#define SHUTDOWN_BY_SIGNAL 2
61
62
63//
64// Externals
65//
66
67extern Queue g_statusPollQueue;
68extern Queue g_configPollQueue;
040c45fa 69extern Queue g_topologyPollQueue;
5039dede
AK
70extern Queue g_routePollQueue;
71extern Queue g_discoveryPollQueue;
72extern Queue g_nodePollerQueue;
73extern Queue g_conditionPollerQueue;
74extern Queue *g_pItemQueue;
75
36e44abe
VK
76void InitClientListeners();
77void InitCertificates();
ab185583
VK
78void InitUsers();
79void CleanupUsers();
5039dede
AK
80
81
82//
83// Thread functions
84//
85
86THREAD_RESULT THREAD_CALL HouseKeeper(void *pArg);
87THREAD_RESULT THREAD_CALL Syncer(void *pArg);
88THREAD_RESULT THREAD_CALL NodePoller(void *pArg);
89THREAD_RESULT THREAD_CALL PollManager(void *pArg);
90THREAD_RESULT THREAD_CALL EventProcessor(void *pArg);
91THREAD_RESULT THREAD_CALL WatchdogThread(void *pArg);
92THREAD_RESULT THREAD_CALL ClientListener(void *pArg);
36e44abe 93THREAD_RESULT THREAD_CALL ClientListenerIPv6(void *arg);
5039dede
AK
94THREAD_RESULT THREAD_CALL ISCListener(void *pArg);
95THREAD_RESULT THREAD_CALL LocalAdminListener(void *pArg);
96THREAD_RESULT THREAD_CALL SNMPTrapReceiver(void *pArg);
97THREAD_RESULT THREAD_CALL SyslogDaemon(void *pArg);
98THREAD_RESULT THREAD_CALL BeaconPoller(void *pArg);
3929b1ca 99THREAD_RESULT THREAD_CALL JobManagerThread(void *arg);
86457857 100THREAD_RESULT THREAD_CALL UptimeCalculator(void *arg);
5039dede
AK
101
102
103//
104// Global variables
105//
106
107TCHAR NXCORE_EXPORTABLE g_szConfigFile[MAX_PATH] = DEFAULT_CONFIG_FILE;
108TCHAR NXCORE_EXPORTABLE g_szLogFile[MAX_PATH] = DEFAULT_LOG_FILE;
109TCHAR NXCORE_EXPORTABLE g_szDumpDir[MAX_PATH] = DEFAULT_DUMP_DIR;
110TCHAR g_szCodePage[256] = ICONV_DEFAULT_CODEPAGE;
111TCHAR NXCORE_EXPORTABLE g_szListenAddress[MAX_PATH] = _T("0.0.0.0");
112#ifndef _WIN32
35f836fe 113char NXCORE_EXPORTABLE g_szPIDFile[MAX_PATH] = _T("/var/run/netxmsd.pid");
5039dede
AK
114#endif
115DB_HANDLE g_hCoreDB = 0;
116DWORD g_dwDiscoveryPollingInterval;
117DWORD g_dwStatusPollingInterval;
118DWORD g_dwConfigurationPollingInterval;
119DWORD g_dwRoutingTableUpdateInterval;
040c45fa 120DWORD g_dwTopologyPollingInterval;
5039dede
AK
121DWORD g_dwConditionPollingInterval;
122DWORD g_dwPingSize;
123DWORD g_dwAuditFlags;
52c75f5c 124DWORD g_dwSlmPollingInterval;
d96bd4c7 125TCHAR g_szDataDir[MAX_PATH] = _T("");
9796ce45 126TCHAR g_szLibDir[MAX_PATH] = DEFAULT_LIBDIR;
28f5b9a4 127int g_nDBSyntax = DB_SYNTAX_UNKNOWN;
73869331 128DWORD NXCORE_EXPORTABLE g_processAffinityMask = DEFAULT_AFFINITY_MASK;
5039dede
AK
129QWORD g_qwServerId;
130RSA *g_pServerKey = NULL;
131time_t g_tServerStartTime = 0;
132DWORD g_dwLockTimeout = 60000; // Default timeout for acquiring mutex
5039dede
AK
133DWORD g_dwAgentCommandTimeout = 2000; // Default timeout for requests to agent
134DWORD g_dwThresholdRepeatInterval = 0; // Disabled by default
135int g_nRequiredPolls = 1;
b8c1ec69 136DB_DRIVER g_dbDriver = NULL;
5039dede
AK
137
138
139//
140// Static data
141//
142
143static CONDITION m_condShutdown = INVALID_CONDITION_HANDLE;
144static THREAD m_thPollManager = INVALID_THREAD_HANDLE;
145static THREAD m_thHouseKeeper = INVALID_THREAD_HANDLE;
146static THREAD m_thSyncer = INVALID_THREAD_HANDLE;
147static THREAD m_thSyslogDaemon = INVALID_THREAD_HANDLE;
148static int m_nShutdownReason = SHUTDOWN_DEFAULT;
149
150#ifndef _WIN32
151static pthread_t m_signalHandlerThread;
152#endif
153
154
155//
156// Sleep for specified number of seconds or until system shutdown arrives
157// Function will return TRUE if shutdown event occurs
158//
159
160BOOL NXCORE_EXPORTABLE SleepAndCheckForShutdown(int iSeconds)
161{
162 return ConditionWait(m_condShutdown, iSeconds * 1000);
163}
164
165
166//
167// Disconnect from database (exportable function for startup module)
168//
169
73869331 170void NXCORE_EXPORTABLE ShutdownDB()
5039dede
AK
171{
172 if (g_hCoreDB != NULL)
173 DBDisconnect(g_hCoreDB);
b8c1ec69 174 DBUnloadDriver(g_dbDriver);
5039dede
AK
175}
176
177
178//
179// Check data directory for existence
180//
181
35f836fe 182static BOOL CheckDataDir()
5039dede 183{
35f836fe 184 TCHAR szBuffer[MAX_PATH];
5039dede 185
35f836fe 186 if (_tchdir(g_szDataDir) == -1)
5039dede
AK
187 {
188 nxlog_write(MSG_INVALID_DATA_DIR, EVENTLOG_ERROR_TYPE, "s", g_szDataDir);
189 return FALSE;
190 }
191
192#ifdef _WIN32
35f836fe 193#define MKDIR(name) _tmkdir(name)
5039dede 194#else
35f836fe 195#define MKDIR(name) _tmkdir(name, 0700)
5039dede
AK
196#endif
197
198 // Create directory for mib files if it doesn't exist
35f836fe
VK
199 _tcscpy(szBuffer, g_szDataDir);
200 _tcscat(szBuffer, DDIR_MIBS);
5039dede
AK
201 if (MKDIR(szBuffer) == -1)
202 if (errno != EEXIST)
203 {
204 nxlog_write(MSG_ERROR_CREATING_DATA_DIR, EVENTLOG_ERROR_TYPE, "s", szBuffer);
205 return FALSE;
206 }
207
5039dede 208 // Create directory for package files if it doesn't exist
35f836fe
VK
209 _tcscpy(szBuffer, g_szDataDir);
210 _tcscat(szBuffer, DDIR_PACKAGES);
5039dede
AK
211 if (MKDIR(szBuffer) == -1)
212 if (errno != EEXIST)
213 {
214 nxlog_write(MSG_ERROR_CREATING_DATA_DIR, EVENTLOG_ERROR_TYPE, "s", szBuffer);
215 return FALSE;
216 }
217
4d0c32f3 218 // Create directory for map background images if it doesn't exist
35f836fe
VK
219 _tcscpy(szBuffer, g_szDataDir);
220 _tcscat(szBuffer, DDIR_BACKGROUNDS);
4d0c32f3
VK
221 if (MKDIR(szBuffer) == -1)
222 if (errno != EEXIST)
223 {
224 nxlog_write(MSG_ERROR_CREATING_DATA_DIR, EVENTLOG_ERROR_TYPE, "s", szBuffer);
225 return FALSE;
226 }
227
e6b9439a
AK
228 // Create directory for image library is if does't exists
229 _tcscpy(szBuffer, g_szDataDir);
230 _tcscat(szBuffer, DDIR_IMAGES);
231 if (MKDIR(szBuffer) == -1)
232 {
233 if (errno != EEXIST)
234 {
235 nxlog_write(MSG_ERROR_CREATING_DATA_DIR, EVENTLOG_ERROR_TYPE, "s", szBuffer);
236 return FALSE;
237 }
238 }
239
6f16f12d
VK
240 // Create directory for shared file store if does't exists
241 _tcscpy(szBuffer, g_szDataDir);
242 _tcscat(szBuffer, DDIR_SHARED_FILES);
243 if (MKDIR(szBuffer) == -1)
244 {
245 if (errno != EEXIST)
246 {
247 nxlog_write(MSG_ERROR_CREATING_DATA_DIR, EVENTLOG_ERROR_TYPE, "s", szBuffer);
248 return FALSE;
249 }
250 }
251
619e5c9b
VK
252 // Create directory for file store if does't exists
253 _tcscpy(szBuffer, g_szDataDir);
254 _tcscat(szBuffer, DDIR_FILES);
255 if (MKDIR(szBuffer) == -1)
256 {
257 if (errno != EEXIST)
258 {
259 nxlog_write(MSG_ERROR_CREATING_DATA_DIR, EVENTLOG_ERROR_TYPE, "s", szBuffer);
57fef75c
VK
260 return FALSE;
261 }
262 }
263
264 // Create directory for reports if does't exists
265 _tcscpy(szBuffer, g_szDataDir);
266 _tcscat(szBuffer, DDIR_REPORTS);
267 if (MKDIR(szBuffer) == -1)
268 {
269 if (errno != EEXIST)
270 {
271 nxlog_write(MSG_ERROR_CREATING_DATA_DIR, EVENTLOG_ERROR_TYPE, "s", szBuffer);
619e5c9b
VK
272 return FALSE;
273 }
274 }
275
5039dede
AK
276#undef MKDIR
277
278 return TRUE;
279}
280
281
282//
283// Load global configuration parameters
284//
285
286static void LoadGlobalConfig()
287{
35f836fe
VK
288 g_dwDiscoveryPollingInterval = ConfigReadInt(_T("DiscoveryPollingInterval"), 900);
289 g_dwStatusPollingInterval = ConfigReadInt(_T("StatusPollingInterval"), 60);
290 g_dwConfigurationPollingInterval = ConfigReadInt(_T("ConfigurationPollingInterval"), 3600);
291 g_dwRoutingTableUpdateInterval = ConfigReadInt(_T("RoutingTableUpdateInterval"), 300);
040c45fa 292 g_dwTopologyPollingInterval = ConfigReadInt(_T("TopologyPollingInterval"), 1800);
35f836fe 293 g_dwConditionPollingInterval = ConfigReadInt(_T("ConditionPollingInterval"), 60);
52c75f5c 294 g_dwSlmPollingInterval = ConfigReadInt(_T("SlmPollingInterval"), 60);
35f836fe 295 if (ConfigReadInt(_T("DeleteEmptySubnets"), 1))
5039dede 296 g_dwFlags |= AF_DELETE_EMPTY_SUBNETS;
35f836fe 297 if (ConfigReadInt(_T("EnableSNMPTraps"), 1))
5039dede 298 g_dwFlags |= AF_ENABLE_SNMP_TRAPD;
35f836fe 299 if (ConfigReadInt(_T("EnableZoning"), 0))
5039dede 300 g_dwFlags |= AF_ENABLE_ZONING;
35f836fe 301 if (ConfigReadInt(_T("EnableMultipleDBConnections"), 1))
2a8e6a7b
VK
302 {
303 // SQLite has troubles with multiple connections to the same database
304 // from different threads, and it does not speed up database access
305 // anyway, so we will not enable multiple connections for SQLite
306 if (g_nDBSyntax != DB_SYNTAX_SQLITE)
307 {
308 g_dwFlags |= AF_ENABLE_MULTIPLE_DB_CONN;
309 }
310 else
311 {
312 DbgPrintf(1, _T("Configuration parameter EnableMultipleDBConnections ignored because database engine is SQLite"));
313 }
314 }
35f836fe 315 if (ConfigReadInt(_T("RunNetworkDiscovery"), 0))
5039dede 316 g_dwFlags |= AF_ENABLE_NETWORK_DISCOVERY;
35f836fe 317 if (ConfigReadInt(_T("ActiveNetworkDiscovery"), 0))
5039dede 318 g_dwFlags |= AF_ACTIVE_NETWORK_DISCOVERY;
35f836fe 319 if (ConfigReadInt(_T("ResolveNodeNames"), 1))
5039dede 320 g_dwFlags |= AF_RESOLVE_NODE_NAMES;
35f836fe 321 if (ConfigReadInt(_T("SyncNodeNamesWithDNS"), 0))
5039dede 322 g_dwFlags |= AF_SYNC_NODE_NAMES_WITH_DNS;
35f836fe 323 if (ConfigReadInt(_T("InternalCA"), 0))
5039dede 324 g_dwFlags |= AF_INTERNAL_CA;
35f836fe 325 if (ConfigReadInt(_T("CheckTrustedNodes"), 1))
5039dede 326 g_dwFlags |= AF_CHECK_TRUSTED_NODES;
fc958888 327
d96bd4c7
VK
328 if (g_szDataDir[0] == 0)
329 {
35f836fe 330 ConfigReadStr(_T("DataDirectory"), g_szDataDir, MAX_PATH, DEFAULT_DATA_DIR);
d96bd4c7
VK
331 DbgPrintf(1, _T("Data directory set to %s from server configuration variable"), g_szDataDir);
332 }
333 else
334 {
335 DbgPrintf(1, _T("Using data directory %s"), g_szDataDir);
336 }
337
35f836fe
VK
338 g_dwPingSize = ConfigReadInt(_T("IcmpPingSize"), 46);
339 g_dwLockTimeout = ConfigReadInt(_T("LockTimeout"), 60000);
340 g_dwSNMPTimeout = ConfigReadInt(_T("SNMPRequestTimeout"), 2000);
341 g_dwAgentCommandTimeout = ConfigReadInt(_T("AgentCommandTimeout"), 2000);
342 g_dwThresholdRepeatInterval = ConfigReadInt(_T("ThresholdRepeatInterval"), 0);
343 g_nRequiredPolls = ConfigReadInt(_T("PollCountForStatusChange"), 1);
5039dede
AK
344}
345
346
347//
348// Initialize cryptografic functions
349//
350
73869331 351static BOOL InitCryptografy()
5039dede
AK
352{
353#ifdef _WITH_ENCRYPTION
35f836fe 354 TCHAR szKeyFile[MAX_PATH];
5039dede
AK
355 BOOL bResult = FALSE;
356 int fd, iPolicy;
357 DWORD dwLen;
358 BYTE *pBufPos, *pKeyBuffer, hash[SHA1_DIGEST_SIZE];
359
35f836fe 360 if (!InitCryptoLib(ConfigReadULong(_T("AllowedCiphers"), 15)))
5039dede
AK
361 return FALSE;
362
35f836fe
VK
363 _tcscpy(szKeyFile, g_szDataDir);
364 _tcscat(szKeyFile, DFILE_KEYS);
365 fd = _topen(szKeyFile, O_RDONLY | O_BINARY);
5039dede
AK
366 g_pServerKey = LoadRSAKeys(szKeyFile);
367 if (g_pServerKey == NULL)
368 {
35f836fe 369 DbgPrintf(1, _T("Generating RSA key pair..."));
5039dede
AK
370 g_pServerKey = RSA_generate_key(NETXMS_RSA_KEYLEN, 17, NULL, 0);
371 if (g_pServerKey != NULL)
372 {
35f836fe 373 fd = _topen(szKeyFile, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC, 0600);
5039dede
AK
374 if (fd != -1)
375 {
376 dwLen = i2d_RSAPublicKey(g_pServerKey, NULL);
377 dwLen += i2d_RSAPrivateKey(g_pServerKey, NULL);
378 pKeyBuffer = (BYTE *)malloc(dwLen);
379
380 pBufPos = pKeyBuffer;
381 i2d_RSAPublicKey(g_pServerKey, &pBufPos);
382 i2d_RSAPrivateKey(g_pServerKey, &pBufPos);
383 write(fd, &dwLen, sizeof(DWORD));
384 write(fd, pKeyBuffer, dwLen);
385
386 CalculateSHA1Hash(pKeyBuffer, dwLen, hash);
387 write(fd, hash, SHA1_DIGEST_SIZE);
388
389 close(fd);
390 free(pKeyBuffer);
391 bResult = TRUE;
392 }
393 }
394 }
395 else
396 {
397 bResult = TRUE;
398 }
399
35f836fe 400 iPolicy = ConfigReadInt(_T("DefaultEncryptionPolicy"), 1);
5039dede
AK
401 if ((iPolicy < 0) || (iPolicy > 3))
402 iPolicy = 1;
403 SetAgentDEP(iPolicy);
404
405 return bResult;
406#else
407 return TRUE;
408#endif
409}
410
411
412//
413// Check if process with given PID exists and is a NetXMS server process
414//
415
416static BOOL IsNetxmsdProcess(DWORD dwPID)
417{
418#ifdef _WIN32
419 HANDLE hProcess;
420 TCHAR szExtModule[MAX_PATH], szIntModule[MAX_PATH];
421 BOOL bRet = FALSE;
422
423 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwPID);
424 if (hProcess != NULL)
425 {
426 if ((GetModuleBaseName(hProcess, NULL, szExtModule, MAX_PATH) > 0) &&
427 (GetModuleBaseName(GetCurrentProcess(), NULL, szIntModule, MAX_PATH) > 0))
428 {
429 bRet = !_tcsicmp(szExtModule, szIntModule);
430 }
431 else
432 {
433 // Cannot read process name, for safety assume that it's a server process
434 bRet = TRUE;
435 }
436 CloseHandle(hProcess);
437 }
438 return bRet;
439#else
440 return (kill((pid_t)dwPID, 0) != -1);
441#endif
442}
443
444
445//
446// Database event handler
447//
448
35f836fe 449static void DBEventHandler(DWORD dwEvent, const WCHAR *pszArg1, const WCHAR *pszArg2, void *userArg)
5039dede
AK
450{
451 if (!(g_dwFlags & AF_SERVER_INITIALIZED))
452 return; // Don't try to do anything if server is not ready yet
453
454 switch(dwEvent)
455 {
456 case DBEVENT_CONNECTION_LOST:
457 PostEvent(EVENT_DB_CONNECTION_LOST, g_dwMgmtNode, NULL);
458 g_dwFlags |= AF_DB_CONNECTION_LOST;
459 NotifyClientSessions(NX_NOTIFY_DBCONN_STATUS, FALSE);
460 break;
461 case DBEVENT_CONNECTION_RESTORED:
462 PostEvent(EVENT_DB_CONNECTION_RESTORED, g_dwMgmtNode, NULL);
463 g_dwFlags &= ~AF_DB_CONNECTION_LOST;
464 NotifyClientSessions(NX_NOTIFY_DBCONN_STATUS, TRUE);
465 break;
4d0c32f3
VK
466 case DBEVENT_QUERY_FAILED:
467 PostEvent(EVENT_DB_QUERY_FAILED, g_dwMgmtNode, "ss", pszArg1, pszArg2);
468 break;
5039dede
AK
469 default:
470 break;
471 }
472}
473
474
475//
f669df41
VK
476// Send console message to session with open console
477//
478
479static void SendConsoleMessage(ClientSession *session, void *arg)
480{
481 if (session->isConsoleOpen())
482 {
483 CSCPMessage msg;
484
485 msg.SetCode(CMD_ADM_MESSAGE);
486 msg.SetVariable(VID_MESSAGE, (TCHAR *)arg);
d3a7cf4c 487 session->postMessage(&msg);
f669df41
VK
488 }
489}
490
491
492//
493// Console writer
494//
495
496static void LogConsoleWriter(const TCHAR *format, ...)
497{
498 TCHAR buffer[8192];
499 va_list args;
500
501 va_start(args, format);
502 _vsntprintf(buffer, 8192, format, args);
503 buffer[8191] = 0;
504 va_end(args);
505
506 WriteToTerminal(buffer);
507
508 EnumerateClientSessions(SendConsoleMessage, buffer);
509}
510
511
512//
5039dede
AK
513// Server initialization
514//
515
bc980b27 516BOOL NXCORE_EXPORTABLE Initialize()
5039dede
AK
517{
518 int i, iDBVersion;
519 DWORD dwAddr;
35f836fe 520 TCHAR szInfo[256];
5039dede
AK
521
522 g_tServerStartTime = time(NULL);
523 srand((unsigned int)g_tServerStartTime);
524 nxlog_open((g_dwFlags & AF_USE_SYSLOG) ? NETXMSD_SYSLOG_NAME : g_szLogFile,
525 ((g_dwFlags & AF_USE_SYSLOG) ? NXLOG_USE_SYSLOG : 0) |
526 ((g_dwFlags & AF_DAEMON) ? 0 : NXLOG_PRINT_TO_STDOUT),
527 _T("LIBNXSRV.DLL"),
528#ifdef _WIN32
529 0, NULL);
530#else
531 g_dwNumMessages, g_szMessages);
532#endif
f669df41 533 nxlog_set_console_writer(LogConsoleWriter);
5039dede
AK
534
535 // Set code page
536#ifndef _WIN32
537 if (SetDefaultCodepage(g_szCodePage))
538 {
35f836fe 539 DbgPrintf(1, _T("Code page set to %s"), g_szCodePage);
5039dede
AK
540 }
541 else
542 {
543 nxlog_write(MSG_CODEPAGE_ERROR, EVENTLOG_WARNING_TYPE, "s", g_szCodePage);
544 }
545#endif
546
73869331
VK
547 // Set process affinity mask
548 if (g_processAffinityMask != DEFAULT_AFFINITY_MASK)
549 {
550#ifdef _WIN32
551 if (SetProcessAffinityMask(GetCurrentProcess(), g_processAffinityMask))
552 DbgPrintf(1, _T("Process affinity mask set to 0x%08X"), g_processAffinityMask);
553#else
554 nxlog_write(MSG_SET_PROCESS_AFFINITY_NOT_SUPPORTED, EVENTLOG_WARNING_TYPE, NULL);
555#endif
556 }
557
5039dede
AK
558#ifdef _WIN32
559 WSADATA wsaData;
1ddf3f0c
VK
560 int wrc = WSAStartup(MAKEWORD(2, 2), &wsaData);
561 if (wrc != 0)
562 {
563 nxlog_write(MSG_WSASTARTUP_FAILED, EVENTLOG_ERROR_TYPE, "e", wrc);
564 return FALSE;
565 }
5039dede
AK
566#endif
567
568 InitLocalNetInfo();
569
570 // Create queue for delayed SQL queries
73869331 571 g_pLazyRequestQueue = new Queue(256, 64);
5039dede
AK
572
573 // Initialize database driver and connect to database
9d88cdc9 574 DBSetDebugPrintCallback(DbgPrintf2);
b8c1ec69
VK
575 if (!DBInit(MSG_OTHER, (g_dwFlags & AF_LOG_SQL_ERRORS) ? MSG_SQL_ERROR : 0))
576 return FALSE;
577 g_dbDriver = DBLoadDriver(g_szDbDriver, g_szDbDrvParams, (g_nDebugLevel >= 9), DBEventHandler, NULL);
578 if (g_dbDriver == NULL)
5039dede
AK
579 return FALSE;
580
581 // Connect to database
465b3f2d 582 TCHAR errorText[DBDRV_MAX_ERROR_TEXT];
5039dede
AK
583 for(i = 0; ; i++)
584 {
f3c30cf5 585 g_hCoreDB = DBConnect(g_dbDriver, g_szDbServer, g_szDbName, g_szDbLogin, g_szDbPassword, g_szDbSchema, errorText);
5039dede
AK
586 if ((g_hCoreDB != NULL) || (i == 5))
587 break;
588 ThreadSleep(5);
589 }
590 if (g_hCoreDB == NULL)
591 {
465b3f2d 592 nxlog_write(MSG_DB_CONNFAIL, EVENTLOG_ERROR_TYPE, "s", errorText);
5039dede
AK
593 return FALSE;
594 }
35f836fe 595 DbgPrintf(1, _T("Successfully connected to database %s@%s"), g_szDbName, g_szDbServer);
5039dede
AK
596
597 // Check database version
28f5b9a4 598 iDBVersion = DBGetSchemaVersion(g_hCoreDB);
5039dede
AK
599 if (iDBVersion != DB_FORMAT_VERSION)
600 {
601 nxlog_write(MSG_WRONG_DB_VERSION, EVENTLOG_ERROR_TYPE, "dd", iDBVersion, DB_FORMAT_VERSION);
602 return FALSE;
603 }
604
35f836fe
VK
605 int baseSize = ConfigReadInt(_T("ConnectionPoolBaseSize"), 5);
606 int maxSize = ConfigReadInt(_T("ConnectionPoolMaxSize"), 20);
607 int cooldownTime = ConfigReadInt(_T("ConnectionPoolCooldownTime"), 300);
f3c30cf5 608 DBConnectionPoolStartup(g_dbDriver, g_szDbServer, g_szDbName, g_szDbLogin, g_szDbPassword, g_szDbSchema, baseSize, maxSize, cooldownTime, g_hCoreDB);
f727d089 609
5039dede 610 // Read database syntax
28f5b9a4 611 g_nDBSyntax = DBGetSyntax(g_hCoreDB);
5039dede
AK
612
613 // Read server ID
35f836fe 614 ConfigReadStr(_T("ServerID"), szInfo, 256, _T(""));
5039dede
AK
615 StrStrip(szInfo);
616 if (szInfo[0] != 0)
617 {
618 StrToBin(szInfo, (BYTE *)&g_qwServerId, sizeof(QWORD));
619 }
620 else
621 {
622 // Generate new ID
623 g_qwServerId = (((QWORD)time(NULL)) << 32) | rand();
624 BinToStr((BYTE *)&g_qwServerId, sizeof(QWORD), szInfo);
35f836fe 625 ConfigWriteStr(_T("ServerID"), szInfo, TRUE);
5039dede
AK
626 }
627
628 // Initialize locks
629retry_db_lock:
630 if (!InitLocks(&dwAddr, szInfo))
631 {
632 if (dwAddr == UNLOCKED) // Some SQL problems
633 {
634 nxlog_write(MSG_INIT_LOCKS_FAILED, EVENTLOG_ERROR_TYPE, NULL);
635 }
636 else // Database already locked by another server instance
637 {
638 // Check for lock from crashed/terminated local process
639 if (dwAddr == GetLocalIpAddr())
640 {
641 DWORD dwPID;
642
35f836fe 643 dwPID = ConfigReadULong(_T("DBLockPID"), 0);
5039dede
AK
644 if (!IsNetxmsdProcess(dwPID) || (dwPID == GetCurrentProcessId()))
645 {
646 UnlockDB();
647 nxlog_write(MSG_DB_LOCK_REMOVED, EVENTLOG_INFORMATION_TYPE, NULL);
648 goto retry_db_lock;
649 }
650 }
651 nxlog_write(MSG_DB_LOCKED, EVENTLOG_ERROR_TYPE, "as", dwAddr, szInfo);
652 }
653 return FALSE;
654 }
655 g_dwFlags |= AF_DB_LOCKED;
656
657 // Load global configuration parameters
658 LoadGlobalConfig();
35f836fe 659 DbgPrintf(1, _T("Global configuration loaded"));
5039dede
AK
660
661 // Check data directory
662 if (!CheckDataDir())
663 return FALSE;
664
665 // Initialize cryptografy
666 if (!InitCryptografy())
667 {
668 nxlog_write(MSG_INIT_CRYPTO_FAILED, EVENTLOG_ERROR_TYPE, NULL);
669 return FALSE;
670 }
671
672 // Initialize certificate store and CA
673 InitCertificates();
674
5039dede
AK
675 // Create synchronization stuff
676 m_condShutdown = ConditionCreate(TRUE);
677
678 // Setup unique identifiers table
679 if (!InitIdTable())
680 return FALSE;
35f836fe 681 DbgPrintf(2, _T("ID table created"));
5039dede 682
24dc5346
VK
683 // Update status for unfinished jobs in job history
684 DBQuery(g_hCoreDB, _T("UPDATE job_history SET status=4,failure_message='Aborted due to server shutdown or crash' WHERE status NOT IN (3,4,5)"));
685
5039dede
AK
686 // Load and compile scripts
687 LoadScripts();
688
689 // Initialize mailer and SMS sender
690 InitMailer();
691 InitSMSSender();
692
693 // Load users from database
ab185583 694 InitUsers();
5039dede
AK
695 if (!LoadUsers())
696 {
697 nxlog_write(MSG_ERROR_LOADING_USERS, EVENTLOG_ERROR_TYPE, NULL);
698 return FALSE;
699 }
35f836fe 700 DbgPrintf(2, _T("User accounts loaded"));
5039dede
AK
701
702 // Initialize audit
703 InitAuditLog();
704
705 // Initialize objects infrastructure and load objects from database
9796ce45 706 LoadNetworkDeviceDrivers();
5039dede
AK
707 ObjectsInit();
708 if (!LoadObjects())
709 return FALSE;
35f836fe 710 DbgPrintf(1, _T("Objects loaded and initialized"));
5039dede
AK
711
712 // Initialize situations
713 if (!SituationsInit())
714 return FALSE;
35f836fe 715 DbgPrintf(1, _T("Situations loaded and initialized"));
5039dede
AK
716
717 // Initialize and load event actions
718 if (!InitActions())
719 {
720 nxlog_write(MSG_ACTION_INIT_ERROR, EVENTLOG_ERROR_TYPE, NULL);
721 return FALSE;
722 }
723
724 // Initialize event handling subsystem
725 if (!InitEventSubsystem())
726 return FALSE;
727
728 // Initialize alarms
729 if (!g_alarmMgr.Init())
730 return FALSE;
731
732 // Initialize data collection subsystem
733 if (!InitDataCollector())
734 return FALSE;
735
e05b1945
VK
736 InitLogAccess();
737
5039dede
AK
738 // Initialize watchdog
739 WatchdogInit();
740
741 // Check if management node object presented in database
742 CheckForMgmtNode();
743 if (g_dwMgmtNode == 0)
744 {
745 nxlog_write(MSG_CANNOT_FIND_SELF, EVENTLOG_ERROR_TYPE, NULL);
746 return FALSE;
747 }
748
749 // Start threads
750 ThreadCreate(WatchdogThread, 0, NULL);
751 ThreadCreate(NodePoller, 0, NULL);
3929b1ca 752 ThreadCreate(JobManagerThread, 0, NULL);
5039dede
AK
753 m_thSyncer = ThreadCreateEx(Syncer, 0, NULL);
754 m_thHouseKeeper = ThreadCreateEx(HouseKeeper, 0, NULL);
755 m_thPollManager = ThreadCreateEx(PollManager, 0, NULL);
756
757 // Start event processor
758 ThreadCreate(EventProcessor, 0, NULL);
759
760 // Start SNMP trapper
761 InitTraps();
35f836fe 762 if (ConfigReadInt(_T("EnableSNMPTraps"), 1))
5039dede
AK
763 ThreadCreate(SNMPTrapReceiver, 0, NULL);
764
765 // Start built-in syslog daemon
35f836fe 766 if (ConfigReadInt(_T("EnableSyslogDaemon"), 0))
5039dede
AK
767 m_thSyslogDaemon = ThreadCreateEx(SyslogDaemon, 0, NULL);
768
35f836fe 769 // Start database _T("lazy") write thread
5039dede
AK
770 StartDBWriter();
771
772 // Start local administartive interface listener if required
35f836fe 773 if (ConfigReadInt(_T("EnableAdminInterface"), 1))
5039dede
AK
774 ThreadCreate(LocalAdminListener, 0, NULL);
775
776 // Load modules
777 LoadNetXMSModules();
778
779 // Start beacon host poller
780 ThreadCreate(BeaconPoller, 0, NULL);
781
782 // Start inter-server communication listener
35f836fe 783 if (ConfigReadInt(_T("EnableISCListener"), 0))
5039dede
AK
784 ThreadCreate(ISCListener, 0, NULL);
785
786 // Allow clients to connect
36e44abe 787 InitClientListeners();
5039dede 788 ThreadCreate(ClientListener, 0, NULL);
36e44abe
VK
789#ifdef WITH_IPV6
790 ThreadCreate(ClientListenerIPv6, 0, NULL);
791#endif
5039dede 792
86457857
AK
793 // Start uptime calculator for SLM
794 ThreadCreate(UptimeCalculator, 0, NULL);
795
5039dede 796 g_dwFlags |= AF_SERVER_INITIALIZED;
35f836fe 797 DbgPrintf(1, _T("Server initialization completed"));
5039dede
AK
798 return TRUE;
799}
800
801
802//
803// Server shutdown
804//
805
035a4d73 806void NXCORE_EXPORTABLE Shutdown()
5039dede
AK
807{
808 DWORD i, dwNumThreads;
809
810 // Notify clients
811 NotifyClientSessions(NX_NOTIFY_SHUTDOWN, 0);
812
813 nxlog_write(MSG_SERVER_STOPPED, EVENTLOG_INFORMATION_TYPE, NULL);
814 g_dwFlags |= AF_SHUTDOWN; // Set shutdown flag
815 ConditionSet(m_condShutdown);
816
817#ifndef _WIN32
818 if (IsStandalone() && (m_nShutdownReason != SHUTDOWN_BY_SIGNAL))
819 {
820 pthread_kill(m_signalHandlerThread, SIGUSR1); // Terminate signal handler
821 }
822#endif
823
824 // Stop event processor(s)
825 g_pEventQueue->Clear();
35f836fe 826 dwNumThreads = ConfigReadInt(_T("NumberOfEventProcessors"), 1);
5039dede
AK
827 for(i = 0; i < dwNumThreads; i++)
828 g_pEventQueue->Put(INVALID_POINTER_VALUE);
829
830 ShutdownMailer();
831 ShutdownSMSSender();
832
833 ThreadSleep(1); // Give other threads a chance to terminate in a safe way
35f836fe 834 DbgPrintf(2, _T("All threads was notified, continue with shutdown"));
5039dede
AK
835
836 // Wait for critical threads
837 ThreadJoin(m_thHouseKeeper);
838 ThreadJoin(m_thPollManager);
839 ThreadJoin(m_thSyncer);
840 ThreadJoin(m_thSyslogDaemon);
841
842 SaveObjects(g_hCoreDB);
35f836fe 843 DbgPrintf(2, _T("All objects saved to database"));
5039dede 844 SaveUsers(g_hCoreDB);
35f836fe 845 DbgPrintf(2, _T("All users saved to database"));
5039dede 846 StopDBWriter();
35f836fe 847 DbgPrintf(1, _T("Database writer stopped"));
5039dede 848
ab185583
VK
849 CleanupUsers();
850
5039dede
AK
851 // Remove database lock
852 UnlockDB();
853
854 // Disconnect from database and unload driver
855 if (g_hCoreDB != NULL)
856 DBDisconnect(g_hCoreDB);
c20b2798
AK
857
858 DBConnectionPoolShutdown();
859
b8c1ec69 860 DBUnloadDriver(g_dbDriver);
35f836fe 861 DbgPrintf(1, _T("Database driver unloaded"));
5039dede
AK
862
863 CleanupActions();
864 ShutdownEventSubsystem();
35f836fe 865 DbgPrintf(1, _T("Event processing stopped"));
5039dede
AK
866
867 // Delete all objects
868 //for(i = 0; i < g_dwIdIndexSize; i++)
869 // delete g_pIndexById[i].pObject;
870
871 delete g_pScriptLibrary;
872
873 nxlog_close();
874
875 // Remove PID file
876#ifndef _WIN32
877 remove(g_szPIDFile);
878#endif
879
880 // Terminate process
881#ifdef _WIN32
882 if (!(g_dwFlags & AF_DAEMON))
883 ExitProcess(0);
884#else
885 exit(0);
886#endif
887}
888
889
890//
891// Fast server shutdown - normally called only by Windows service on system shutdown
892//
893
f669df41 894void NXCORE_EXPORTABLE FastShutdown()
5039dede
AK
895{
896 g_dwFlags |= AF_SHUTDOWN; // Set shutdown flag
897 ConditionSet(m_condShutdown);
898
899 SaveObjects(g_hCoreDB);
35f836fe 900 DbgPrintf(2, _T("All objects saved to database"));
5039dede 901 SaveUsers(g_hCoreDB);
35f836fe 902 DbgPrintf(2, _T("All users saved to database"));
5039dede
AK
903
904 // Remove database lock first, because we have a chance to loose DB connection
905 UnlockDB();
906
907 // Stop database writers
908 StopDBWriter();
35f836fe 909 DbgPrintf(1, _T("Database writer stopped"));
5039dede
AK
910
911 nxlog_close();
912}
913
914
915//
916// Compare given string to command template with abbreviation possibility
917//
918
35f836fe 919static BOOL IsCommand(const TCHAR *pszTemplate, TCHAR *pszString, int iMinChars)
5039dede
AK
920{
921 int i;
922
923 // Convert given string to uppercase
35f836fe 924 _tcsupr(pszString);
5039dede
AK
925
926 for(i = 0; pszString[i] != 0; i++)
927 if (pszString[i] != pszTemplate[i])
928 return FALSE;
929 if (i < iMinChars)
930 return FALSE;
931 return TRUE;
932}
933
934
935//
936// Dump index
937//
938
d5e19c61 939struct __dump_index_data
5039dede 940{
d5e19c61
VK
941 CONSOLE_CTX console;
942 bool indexByIP;
943};
5039dede 944
d5e19c61
VK
945static void DumpIndexCallback(NetObj *object, void *data)
946{
947 struct __dump_index_data *d = (struct __dump_index_data *)data;
948 if (d->indexByIP)
5039dede 949 {
d5e19c61
VK
950 TCHAR buffer[16];
951 ConsolePrintf(d->console, _T("%08X [%-15s] %p %s\n"), object->IpAddr(),
952 IpToStr(object->IpAddr(), buffer),
953 object, object->Name());
5039dede 954 }
d5e19c61
VK
955 else
956 {
957 ConsolePrintf(d->console, _T("%08X %p %s\n"), object->Id(), object, object->Name());
958 }
959}
5039dede 960
d5e19c61
VK
961static void DumpIndex(CONSOLE_CTX pCtx, ObjectIndex *index, bool indexByIp)
962{
963 struct __dump_index_data data;
964 data.console = pCtx;
965 data.indexByIP = indexByIp;
966 index->forEach(DumpIndexCallback, &data);
5039dede
AK
967}
968
969
970//
971// Process command entered from command line in standalone mode
35f836fe 972// Return TRUE if command was _T("down")
5039dede
AK
973//
974
35f836fe 975int ProcessConsoleCommand(const TCHAR *pszCmdLine, CONSOLE_CTX pCtx)
5039dede 976{
35f836fe 977 const TCHAR *pArg;
48c0079d 978 TCHAR szBuffer[256], *eptr;
5039dede
AK
979 int nExitCode = CMD_EXIT_CONTINUE;
980
981 // Get command
982 pArg = ExtractWord(pszCmdLine, szBuffer);
983
35f836fe 984 if (IsCommand(_T("DEBUG"), szBuffer, 2))
5039dede
AK
985 {
986 // Get argument
987 pArg = ExtractWord(pArg, szBuffer);
48c0079d
VK
988 int level = (int)_tcstol(szBuffer, &eptr, 0);
989 if ((*eptr == 0) && (level >= 0) && (level <= 9))
5039dede 990 {
48c0079d
VK
991 g_nDebugLevel = level;
992 ConsolePrintf(pCtx, (level == 0) ? _T("Debug mode turned off\n") : _T("Debug level set to %d\n"), level);
5039dede 993 }
35f836fe 994 else if (IsCommand(_T("OFF"), szBuffer, 2))
5039dede
AK
995 {
996 g_nDebugLevel = 0;
35f836fe 997 ConsolePrintf(pCtx, _T("Debug mode turned off\n"));
5039dede
AK
998 }
999 else
1000 {
1001 if (szBuffer[0] == 0)
35f836fe 1002 ConsolePrintf(pCtx, _T("ERROR: Missing argument\n\n"));
5039dede 1003 else
48c0079d 1004 ConsolePrintf(pCtx, _T("ERROR: Invalid debug level\n\n"));
5039dede
AK
1005 }
1006 }
35f836fe 1007 else if (IsCommand(_T("DOWN"), szBuffer, 4))
5039dede 1008 {
35f836fe 1009 ConsolePrintf(pCtx, _T("Proceeding with server shutdown...\n"));
5039dede
AK
1010 nExitCode = CMD_EXIT_SHUTDOWN;
1011 }
35f836fe 1012 else if (IsCommand(_T("DUMP"), szBuffer, 4))
5039dede
AK
1013 {
1014 DumpProcess(pCtx);
1015 }
35f836fe 1016 else if (IsCommand(_T("RAISE"), szBuffer, 5))
5039dede
AK
1017 {
1018 // Get argument
1019 pArg = ExtractWord(pArg, szBuffer);
1020
35f836fe 1021 if (IsCommand(_T("ACCESS"), szBuffer, 6))
5039dede 1022 {
35f836fe 1023 ConsolePrintf(pCtx, _T("Raising exception...\n"));
5039dede
AK
1024 char *p = NULL;
1025 *p = 0;
1026 }
35f836fe 1027 else if (IsCommand(_T("BREAKPOINT"), szBuffer, 5))
5039dede
AK
1028 {
1029#ifdef _WIN32
35f836fe 1030 ConsolePrintf(pCtx, _T("Raising exception...\n"));
5039dede
AK
1031 RaiseException(EXCEPTION_BREAKPOINT, 0, 0, NULL);
1032#else
35f836fe 1033 ConsolePrintf(pCtx, _T("ERROR: Not supported on current platform\n"));
5039dede
AK
1034#endif
1035 }
1036 else
1037 {
35f836fe 1038 ConsolePrintf(pCtx, _T("Invalid exception name; possible names are:\nACCESS BREAKPOINT\n"));
5039dede
AK
1039 }
1040 }
35f836fe 1041 else if (IsCommand(_T("EXIT"), szBuffer, 4))
5039dede 1042 {
f669df41 1043 if ((pCtx->hSocket != -1) || (pCtx->session != NULL))
5039dede 1044 {
35f836fe 1045 ConsolePrintf(pCtx, _T("Closing session...\n"));
5039dede
AK
1046 nExitCode = CMD_EXIT_CLOSE_SESSION;
1047 }
1048 else
1049 {
35f836fe 1050 ConsolePrintf(pCtx, _T("Cannot exit from local server console\n"));
5039dede
AK
1051 }
1052 }
35f836fe 1053 else if (IsCommand(_T("SHOW"), szBuffer, 2))
5039dede
AK
1054 {
1055 // Get argument
1056 pArg = ExtractWord(pArg, szBuffer);
1057
35f836fe 1058 if (IsCommand(_T("FLAGS"), szBuffer, 1))
5039dede 1059 {
35f836fe 1060 ConsolePrintf(pCtx, _T("Flags: 0x%08X\n"), g_dwFlags);
5039dede
AK
1061 ConsolePrintf(pCtx, SHOW_FLAG_VALUE(AF_DAEMON));
1062 ConsolePrintf(pCtx, SHOW_FLAG_VALUE(AF_USE_SYSLOG));
1063 ConsolePrintf(pCtx, SHOW_FLAG_VALUE(AF_ENABLE_NETWORK_DISCOVERY));
1064 ConsolePrintf(pCtx, SHOW_FLAG_VALUE(AF_ACTIVE_NETWORK_DISCOVERY));
1065 ConsolePrintf(pCtx, SHOW_FLAG_VALUE(AF_LOG_SQL_ERRORS));
1066 ConsolePrintf(pCtx, SHOW_FLAG_VALUE(AF_DELETE_EMPTY_SUBNETS));
1067 ConsolePrintf(pCtx, SHOW_FLAG_VALUE(AF_ENABLE_SNMP_TRAPD));
1068 ConsolePrintf(pCtx, SHOW_FLAG_VALUE(AF_ENABLE_ZONING));
5039dede 1069 ConsolePrintf(pCtx, SHOW_FLAG_VALUE(AF_SYNC_NODE_NAMES_WITH_DNS));
cbc777ee
VK
1070 ConsolePrintf(pCtx, SHOW_FLAG_VALUE(AF_CHECK_TRUSTED_NODES));
1071 ConsolePrintf(pCtx, SHOW_FLAG_VALUE(AF_WRITE_FULL_DUMP));
1072 ConsolePrintf(pCtx, SHOW_FLAG_VALUE(AF_RESOLVE_NODE_NAMES));
5039dede 1073 ConsolePrintf(pCtx, SHOW_FLAG_VALUE(AF_CATCH_EXCEPTIONS));
5039dede
AK
1074 ConsolePrintf(pCtx, SHOW_FLAG_VALUE(AF_INTERNAL_CA));
1075 ConsolePrintf(pCtx, SHOW_FLAG_VALUE(AF_DB_LOCKED));
cbc777ee 1076 ConsolePrintf(pCtx, SHOW_FLAG_VALUE(AF_ENABLE_MULTIPLE_DB_CONN));
5039dede 1077 ConsolePrintf(pCtx, SHOW_FLAG_VALUE(AF_DB_CONNECTION_LOST));
cbc777ee 1078 ConsolePrintf(pCtx, SHOW_FLAG_VALUE(AF_NO_NETWORK_CONNECTIVITY));
5039dede
AK
1079 ConsolePrintf(pCtx, SHOW_FLAG_VALUE(AF_EVENT_STORM_DETECTED));
1080 ConsolePrintf(pCtx, SHOW_FLAG_VALUE(AF_SERVER_INITIALIZED));
1081 ConsolePrintf(pCtx, SHOW_FLAG_VALUE(AF_SHUTDOWN));
35f836fe 1082 ConsolePrintf(pCtx, _T("\n"));
5039dede 1083 }
35f836fe 1084 else if (IsCommand(_T("INDEX"), szBuffer, 1))
5039dede
AK
1085 {
1086 // Get argument
1087 pArg = ExtractWord(pArg, szBuffer);
1088
35f836fe 1089 if (IsCommand(_T("CONDITION"), szBuffer, 1))
5039dede 1090 {
d5e19c61 1091 DumpIndex(pCtx, &g_idxConditionById, false);
5039dede 1092 }
35f836fe 1093 else if (IsCommand(_T("ID"), szBuffer, 2))
5039dede 1094 {
d5e19c61 1095 DumpIndex(pCtx, &g_idxObjectById, false);
5039dede 1096 }
35f836fe 1097 else if (IsCommand(_T("INTERFACE"), szBuffer, 2))
5039dede 1098 {
d5e19c61 1099 DumpIndex(pCtx, &g_idxInterfaceByAddr, true);
6aba3998
VK
1100 }
1101 else if (IsCommand(_T("NODE"), szBuffer, 2))
1102 {
d5e19c61 1103 DumpIndex(pCtx, &g_idxNodeById, false);
5039dede 1104 }
35f836fe 1105 else if (IsCommand(_T("SUBNET"), szBuffer, 1))
5039dede 1106 {
d5e19c61 1107 DumpIndex(pCtx, &g_idxSubnetByAddr, true);
5039dede
AK
1108 }
1109 else
1110 {
1111 if (szBuffer[0] == 0)
35f836fe 1112 ConsolePrintf(pCtx, _T("ERROR: Missing index name\n")
d5e19c61 1113 _T("Valid names are: CONDITION, ID, INTERFACE, NODE, SUBNET\n\n"));
5039dede 1114 else
35f836fe 1115 ConsolePrintf(pCtx, _T("ERROR: Invalid index name\n\n"));
5039dede
AK
1116 }
1117 }
35f836fe 1118 else if (IsCommand(_T("MEMORY"), szBuffer, 2))
5039dede
AK
1119 {
1120#ifdef NETXMS_MEMORY_DEBUG
1121 PrintMemoryBlocks();
1122#else
35f836fe 1123 ConsolePrintf(pCtx, _T("ERROR: Server was compiled without memory debugger\n\n"));
5039dede
AK
1124#endif
1125 }
35f836fe 1126 else if (IsCommand(_T("OBJECTS"), szBuffer, 1))
5039dede
AK
1127 {
1128 DumpObjects(pCtx);
1129 }
35f836fe 1130 else if (IsCommand(_T("POLLERS"), szBuffer, 1))
5039dede
AK
1131 {
1132 ShowPollerState(pCtx);
1133 }
35f836fe 1134 else if (IsCommand(_T("QUEUES"), szBuffer, 1))
5039dede 1135 {
35f836fe
VK
1136 ShowQueueStats(pCtx, &g_conditionPollerQueue, _T("Condition poller"));
1137 ShowQueueStats(pCtx, &g_configPollQueue, _T("Configuration poller"));
040c45fa 1138 ShowQueueStats(pCtx, &g_topologyPollQueue, _T("Topology poller"));
35f836fe
VK
1139 ShowQueueStats(pCtx, g_pItemQueue, _T("Data collector"));
1140 ShowQueueStats(pCtx, g_pLazyRequestQueue, _T("Database writer"));
1141 ShowQueueStats(pCtx, g_pEventQueue, _T("Event processor"));
1142 ShowQueueStats(pCtx, &g_discoveryPollQueue, _T("Network discovery poller"));
1143 ShowQueueStats(pCtx, &g_nodePollerQueue, _T("Node poller"));
1144 ShowQueueStats(pCtx, &g_routePollQueue, _T("Routing table poller"));
1145 ShowQueueStats(pCtx, &g_statusPollQueue, _T("Status poller"));
1146 ConsolePrintf(pCtx, _T("\n"));
5039dede 1147 }
35f836fe 1148 else if (IsCommand(_T("ROUTING-TABLE"), szBuffer, 1))
5039dede
AK
1149 {
1150 DWORD dwNode;
1151 NetObj *pObject;
1152
1153 pArg = ExtractWord(pArg, szBuffer);
35f836fe 1154 dwNode = _tcstoul(szBuffer, NULL, 0);
5039dede
AK
1155 if (dwNode != 0)
1156 {
1157 pObject = FindObjectById(dwNode);
1158 if (pObject != NULL)
1159 {
1160 if (pObject->Type() == OBJECT_NODE)
1161 {
1162 ROUTING_TABLE *pRT;
35f836fe 1163 TCHAR szIpAddr[16];
5039dede
AK
1164 int i;
1165
35f836fe 1166 ConsolePrintf(pCtx, _T("Routing table for node %s:\n\n"), pObject->Name());
58b3e451 1167 pRT = ((Node *)pObject)->getCachedRoutingTable();
5039dede
AK
1168 if (pRT != NULL)
1169 {
1170 for(i = 0; i < pRT->iNumEntries; i++)
1171 {
35f836fe
VK
1172 _sntprintf(szBuffer, 256, _T("%s/%d"), IpToStr(pRT->pRoutes[i].dwDestAddr, szIpAddr),
1173 BitsInMask(pRT->pRoutes[i].dwDestMask));
1174 ConsolePrintf(pCtx, _T("%-18s %-15s %-6d %d\n"), szBuffer,
1175 IpToStr(pRT->pRoutes[i].dwNextHop, szIpAddr),
1176 pRT->pRoutes[i].dwIfIndex, pRT->pRoutes[i].dwRouteType);
5039dede 1177 }
35f836fe 1178 ConsolePrintf(pCtx, _T("\n"));
5039dede
AK
1179 }
1180 else
1181 {
35f836fe 1182 ConsolePrintf(pCtx, _T("Node doesn't have cached routing table\n\n"));
5039dede
AK
1183 }
1184 }
1185 else
1186 {
35f836fe 1187 ConsolePrintf(pCtx, _T("ERROR: Object is not a node\n\n"));
5039dede
AK
1188 }
1189 }
1190 else
1191 {
35f836fe 1192 ConsolePrintf(pCtx, _T("ERROR: Object with ID %d does not exist\n\n"), dwNode);
5039dede
AK
1193 }
1194 }
1195 else
1196 {
35f836fe 1197 ConsolePrintf(pCtx, _T("ERROR: Invalid or missing node ID\n\n"));
5039dede
AK
1198 }
1199 }
35f836fe 1200 else if (IsCommand(_T("SESSIONS"), szBuffer, 2))
5039dede
AK
1201 {
1202 DumpSessions(pCtx);
1203 }
35f836fe 1204 else if (IsCommand(_T("STATS"), szBuffer, 2))
5039dede
AK
1205 {
1206 ShowServerStats(pCtx);
1207 }
35f836fe 1208 else if (IsCommand(_T("USERS"), szBuffer, 1))
5039dede
AK
1209 {
1210 DumpUsers(pCtx);
1211 }
b0ff1179
VK
1212 else if (IsCommand(_T("VLANS"), szBuffer, 1))
1213 {
1214 DWORD dwNode;
1215 NetObj *pObject;
1216
1217 pArg = ExtractWord(pArg, szBuffer);
1218 dwNode = _tcstoul(szBuffer, NULL, 0);
1219 if (dwNode != 0)
1220 {
1221 pObject = FindObjectById(dwNode);
1222 if (pObject != NULL)
1223 {
1224 if (pObject->Type() == OBJECT_NODE)
1225 {
1226 VlanList *vlans = ((Node *)pObject)->getVlans();
1227 if (vlans != NULL)
1228 {
1229 ConsolePrintf(pCtx, _T("\x1b[1mVLAN\x1b[0m | \x1b[1mName\x1b[0m | \x1b[1mPorts\x1b[0m\n")
1230 _T("-----+------------------+-----------------------------------------------------------------\n"));
1231 for(int i = 0; i < vlans->getSize(); i++)
1232 {
1233 VlanInfo *vlan = vlans->get(i);
1234 ConsolePrintf(pCtx, _T("%4d | %-16s |"), vlan->getVlanId(), vlan->getName());
1235 for(int j = 0; j < vlan->getNumPorts(); j++)
1236 ConsolePrintf(pCtx, _T(" %d.%d"), (int)(vlan->getPorts()[j] >> 16), (int)(vlan->getPorts()[j] & 0xFFFF));
1237 ConsolePrintf(pCtx, _T("\n"));
1238 }
1239 ConsolePrintf(pCtx, _T("\n"));
1240 vlans->decRefCount();
1241 }
1242 else
1243 {
1244 ConsolePrintf(pCtx, _T("\x1b[31mNode doesn't have VLAN information\x1b[0m\n\n"));
1245 }
1246 }
1247 else
1248 {
1249 ConsolePrintf(pCtx, _T("\x1b[31mERROR: Object is not a node\x1b[0m\n\n"));
1250 }
1251 }
1252 else
1253 {
1254 ConsolePrintf(pCtx, _T("\x1b[31mERROR: Object with ID %d does not exist\x1b[0m\n\n"), dwNode);
1255 }
1256 }
1257 else
1258 {
1259 ConsolePrintf(pCtx, _T("\x1b[31mERROR: Invalid or missing node ID\x1b[0m\n\n"));
1260 }
1261 }
35f836fe 1262 else if (IsCommand(_T("WATCHDOG"), szBuffer, 1))
5039dede
AK
1263 {
1264 WatchdogPrintStatus(pCtx);
35f836fe 1265 ConsolePrintf(pCtx, _T("\n"));
5039dede
AK
1266 }
1267 else
1268 {
1269 if (szBuffer[0] == 0)
35f836fe 1270 ConsolePrintf(pCtx, _T("ERROR: Missing subcommand\n\n"));
5039dede 1271 else
35f836fe 1272 ConsolePrintf(pCtx, _T("ERROR: Invalid SHOW subcommand\n\n"));
5039dede
AK
1273 }
1274 }
35f836fe 1275 else if (IsCommand(_T("TRACE"), szBuffer, 1))
5039dede
AK
1276 {
1277 DWORD dwNode1, dwNode2;
1278 NetObj *pObject1, *pObject2;
1279 NETWORK_PATH_TRACE *pTrace;
35f836fe 1280 TCHAR szNextHop[16];
5039dede
AK
1281 int i;
1282
1283 // Get arguments
1284 pArg = ExtractWord(pArg, szBuffer);
35f836fe 1285 dwNode1 = _tcstoul(szBuffer, NULL, 0);
5039dede
AK
1286
1287 pArg = ExtractWord(pArg, szBuffer);
35f836fe 1288 dwNode2 = _tcstoul(szBuffer, NULL, 0);
5039dede
AK
1289
1290 if ((dwNode1 != 0) && (dwNode2 != 0))
1291 {
1292 pObject1 = FindObjectById(dwNode1);
1293 if (pObject1 == NULL)
1294 {
35f836fe 1295 ConsolePrintf(pCtx, _T("ERROR: Object with ID %d does not exist\n\n"), dwNode1);
5039dede
AK
1296 }
1297 else
1298 {
1299 pObject2 = FindObjectById(dwNode2);
1300 if (pObject2 == NULL)
1301 {
35f836fe 1302 ConsolePrintf(pCtx, _T("ERROR: Object with ID %d does not exist\n\n"), dwNode2);
5039dede
AK
1303 }
1304 else
1305 {
1306 if ((pObject1->Type() == OBJECT_NODE) &&
1307 (pObject2->Type() == OBJECT_NODE))
1308 {
1309 pTrace = TraceRoute((Node *)pObject1, (Node *)pObject2);
1310 if (pTrace != NULL)
1311 {
35f836fe 1312 ConsolePrintf(pCtx, _T("Trace from %s to %s (%d hops):\n"),
5039dede
AK
1313 pObject1->Name(), pObject2->Name(), pTrace->iNumHops);
1314 for(i = 0; i < pTrace->iNumHops; i++)
35f836fe 1315 ConsolePrintf(pCtx, _T("[%d] %s %s %s %d\n"),
5039dede
AK
1316 pTrace->pHopList[i].pObject->Id(),
1317 pTrace->pHopList[i].pObject->Name(),
1318 IpToStr(pTrace->pHopList[i].dwNextHop, szNextHop),
35f836fe 1319 pTrace->pHopList[i].bIsVPN ? _T("VPN Connector ID:") : _T("Interface Index: "),
5039dede
AK
1320 pTrace->pHopList[i].dwIfIndex);
1321 DestroyTraceData(pTrace);
35f836fe 1322 ConsolePrintf(pCtx, _T("\n"));
5039dede
AK
1323 }
1324 else
1325 {
35f836fe 1326 ConsolePrintf(pCtx, _T("ERROR: Call to TraceRoute() failed\n\n"));
5039dede
AK
1327 }
1328 }
1329 else
1330 {
35f836fe 1331 ConsolePrintf(pCtx, _T("ERROR: Object is not a node\n\n"));
5039dede
AK
1332 }
1333 }
1334 }
1335 }
1336 else
1337 {
35f836fe 1338 ConsolePrintf(pCtx, _T("ERROR: Invalid or missing node id(s)\n\n"));
5039dede
AK
1339 }
1340 }
35f836fe 1341 else if (IsCommand(_T("HELP"), szBuffer, 2) || IsCommand(_T("?"), szBuffer, 1))
5039dede 1342 {
35f836fe 1343 ConsolePrintf(pCtx, _T("Valid commands are:\n")
48c0079d 1344 _T(" debug [<level>|off] - Set debug level (valid range is 0..9)\n")
35f836fe
VK
1345 _T(" down - Down NetXMS server\n")
1346 _T(" exit - Exit from remote session\n")
1347 _T(" help - Display this help\n")
1348 _T(" raise <exception> - Raise exception\n")
1349 _T(" show flags - Show internal server flags\n")
1350 _T(" show index <index> - Show internal index\n")
35f836fe
VK
1351 _T(" show objects - Dump network objects to screen\n")
1352 _T(" show pollers - Show poller threads state information\n")
1353 _T(" show queues - Show internal queues statistics\n")
1354 _T(" show routing-table <node> - Show cached routing table for node\n")
1355 _T(" show sessions - Show active client sessions\n")
1356 _T(" show stats - Show server statistics\n")
1357 _T(" show users - Show users\n")
b0ff1179 1358 _T(" show vlans <node> - Show cached VLAN information for node\n")
35f836fe
VK
1359 _T(" show watchdog - Display watchdog information\n")
1360 _T(" trace <node1> <node2> - Show network path trace between two nodes\n")
1361 _T("\nAlmost all commands can be abbreviated to 2 or 3 characters\n")
1362 _T("\n"));
5039dede
AK
1363 }
1364 else
1365 {
35f836fe 1366 ConsolePrintf(pCtx, _T("UNKNOWN COMMAND\n\n"));
5039dede
AK
1367 }
1368
1369 return nExitCode;
1370}
1371
1372
1373//
1374// Signal handler for UNIX platforms
1375//
1376
1377#ifndef _WIN32
1378
1379void SignalHandlerStub(int nSignal)
1380{
1381 // should be unused, but JIC...
1382 if (nSignal == SIGCHLD)
1383 {
1384 while (waitpid(-1, NULL, WNOHANG) > 0)
1385 ;
1386 }
1387}
1388
1389THREAD_RESULT NXCORE_EXPORTABLE THREAD_CALL SignalHandler(void *pArg)
1390{
1391 sigset_t signals;
1392 int nSignal;
1393 BOOL bCallShutdown = FALSE;
1394
1395 m_signalHandlerThread = pthread_self();
1396
1397 // default for SIGCHLD: ignore
1398 signal(SIGCHLD, &SignalHandlerStub);
1399
1400 sigemptyset(&signals);
1401 sigaddset(&signals, SIGTERM);
1402 sigaddset(&signals, SIGINT);
1403 sigaddset(&signals, SIGPIPE);
1404 sigaddset(&signals, SIGSEGV);
1405 sigaddset(&signals, SIGCHLD);
1406 sigaddset(&signals, SIGHUP);
1407 sigaddset(&signals, SIGUSR1);
1408 sigaddset(&signals, SIGUSR2);
1409
1410 sigprocmask(SIG_BLOCK, &signals, NULL);
1411
1412 while(1)
1413 {
1414 if (sigwait(&signals, &nSignal) == 0)
1415 {
1416 switch(nSignal)
1417 {
1418 case SIGTERM:
1419 case SIGINT:
1420 m_nShutdownReason = SHUTDOWN_BY_SIGNAL;
1421 if (IsStandalone())
1422 bCallShutdown = TRUE;
1423 ConditionSet(m_condShutdown);
1424 goto stop_handler;
1425 case SIGSEGV:
1426 abort();
1427 break;
1428 case SIGCHLD:
1429 while (waitpid(-1, NULL, WNOHANG) > 0)
1430 ;
1431 break;
1432 case SIGUSR1:
1433 if (g_dwFlags & AF_SHUTDOWN)
1434 goto stop_handler;
1435 break;
1436 default:
1437 break;
1438 }
1439 }
1440 else
1441 {
1442 ThreadSleepMs(100);
1443 }
1444 }
1445
1446stop_handler:
1447 sigprocmask(SIG_UNBLOCK, &signals, NULL);
1448 if (bCallShutdown)
1449 Shutdown();
1450 return THREAD_OK;
1451}
1452
1453#endif
1454
1455
1456//
1457// Common main()
1458//
1459
1460THREAD_RESULT NXCORE_EXPORTABLE THREAD_CALL Main(void *pArg)
1461{
1462 nxlog_write(MSG_SERVER_STARTED, EVENTLOG_INFORMATION_TYPE, NULL);
1463
1464 if (IsStandalone())
1465 {
1466 char *ptr, szCommand[256];
1467 struct __console_ctx ctx;
35f836fe
VK
1468#ifdef UNICODE
1469 WCHAR wcCommand[256];
1470#endif
5039dede
AK
1471
1472 ctx.hSocket = -1;
d3a7cf4c 1473 ctx.socketMutex = INVALID_MUTEX_HANDLE;
5039dede 1474 ctx.pMsg = NULL;
200d662d 1475 ctx.session = NULL;
f669df41
VK
1476 WriteToTerminal(_T("\nNetXMS Server V") NETXMS_VERSION_STRING _T(" Ready\n")
1477 _T("Enter \"\x1b[1mhelp\x1b[0m\" for command list or \"\x1b[1mdown\x1b[0m\" for server shutdown\n")
1478 _T("System Console\n\n"));
5039dede
AK
1479
1480#if USE_READLINE
1481 // Initialize readline library if we use it
0322eed7 1482 rl_bind_key('\t', RL_INSERT_CAST rl_insert);
5039dede
AK
1483#endif
1484
1485 while(1)
1486 {
1487#if USE_READLINE
f669df41 1488 ptr = readline("\x1b[33mnetxmsd:\x1b[0m ");
5039dede 1489#else
f669df41 1490 WriteToTerminal(_T("\x1b[33mnetxmsd:\x1b[0m "));
5039dede
AK
1491 fflush(stdout);
1492 if (fgets(szCommand, 255, stdin) == NULL)
1493 break; // Error reading stdin
1494 ptr = strchr(szCommand, '\n');
1495 if (ptr != NULL)
1496 *ptr = 0;
1497 ptr = szCommand;
1498#endif
1499
1500 if (ptr != NULL)
1501 {
35f836fe
VK
1502#ifdef UNICODE
1503 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, ptr, -1, wcCommand, 256);
1504 wcCommand[255] = 0;
1505 StrStrip(wcCommand);
1506 if (wcCommand[0] != 0)
1507 {
1508 if (ProcessConsoleCommand(wcCommand, &ctx) == CMD_EXIT_SHUTDOWN)
1509#else
5039dede
AK
1510 StrStrip(ptr);
1511 if (*ptr != 0)
1512 {
1513 if (ProcessConsoleCommand(ptr, &ctx) == CMD_EXIT_SHUTDOWN)
35f836fe 1514#endif
5039dede
AK
1515 break;
1516#if USE_READLINE
1517 add_history(ptr);
1518#endif
1519 }
1520#if USE_READLINE
1521 free(ptr);
1522#endif
1523 }
1524 else
1525 {
1526 printf("\n");
1527 }
1528 }
1529
1530#if USE_READLINE
1531 free(ptr);
1532#endif
1533 m_nShutdownReason = SHUTDOWN_FROM_CONSOLE;
1534 Shutdown();
1535 }
1536 else
1537 {
1538 ConditionWait(m_condShutdown, INFINITE);
1539 // On Win32, Shutdown() will be called by service control handler
1540#ifndef _WIN32
1541 Shutdown();
1542#endif
1543 }
1544 return THREAD_OK;
1545}
1546
1547
1548//
1549// Initiate server shutdown
1550//
1551
035a4d73 1552void InitiateShutdown()
5039dede
AK
1553{
1554#ifdef _WIN32
1555 Shutdown();
1556#else
1557 if (IsStandalone())
1558 {
1559 Shutdown();
1560 }
1561 else
1562 {
1563 pthread_kill(m_signalHandlerThread, SIGTERM);
1564 }
1565#endif
1566}
1567
1568
1569//
1570// DLL Entry point
1571//
1572
1573#ifdef _WIN32
1574
1575BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
1576{
1577 if (dwReason == DLL_PROCESS_ATTACH)
1578 DisableThreadLibraryCalls(hInstance);
1579 return TRUE;
1580}
1581
1582#endif