- Changes in logging and debug output
[public/netxms.git] / src / server / core / np.cpp
CommitLineData
282349dd 1/*
7257eb7d
VK
2** NetXMS - Network Management System
3** Copyright (C) 2003, 2004 Victor Kirhenshtein
282349dd
VK
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**
cc140cce 19** $module: np.cpp
282349dd
VK
20**
21**/
22
23#include "nms_core.h"
24
25
26//
27// Poll new node for configuration
917aa2e6 28// Returns pointer to new node object on success or NULL on failure
282349dd
VK
29//
30
b900a78b 31NetObj *PollNewNode(DWORD dwIpAddr, DWORD dwNetMask, DWORD dwFlags, TCHAR *pszName)
282349dd
VK
32{
33 Node *pNode;
a7b85168 34 char szIpAddr1[32], szIpAddr2[32];
282349dd 35
b1dd534d 36 DbgPrintf(AF_DEBUG_DISCOVERY, "PollNode(%s,%s)",
a7b85168 37 IpToStr(dwIpAddr, szIpAddr1), IpToStr(dwNetMask, szIpAddr2));
282349dd 38 // Check for node existence
eefe7d68
VK
39 if ((FindNodeByIP(dwIpAddr) != NULL) ||
40 (FindSubnetByIP(dwIpAddr) != NULL))
282349dd 41 {
b1dd534d 42 DbgPrintf(AF_DEBUG_DISCOVERY, "PollNode: Node %s already exist in database",
a7b85168 43 IpToStr(dwIpAddr, szIpAddr1));
917aa2e6 44 return NULL;
282349dd
VK
45 }
46
469b937c 47 pNode = new Node(dwIpAddr, 0, dwFlags);
f9b7e653 48 NetObjInsert(pNode, TRUE);
469b937c 49 if (!pNode->NewNodePoll(dwNetMask))
eefe7d68 50 {
b1dd534d 51 DbgPrintf(AF_DEBUG_DISCOVERY, "Node::NewNodePoll(%s)failed",
a7b85168 52 IpToStr(dwIpAddr, szIpAddr1));
be0a5a53 53 ObjectsGlobalLock();
f9b7e653 54 NetObjDelete(pNode);
be0a5a53 55 ObjectsGlobalUnlock();
469b937c 56 delete pNode; // Node poll failed, delete it
917aa2e6 57 return NULL;
469b937c 58 }
b900a78b
VK
59 if (pszName != NULL)
60 pNode->SetName(pszName);
7fdc8a45
VK
61
62 // DEBUG
0ae741b9 63 pNode->AddItem(new DCItem(CreateUniqueId(IDG_ITEM), "Status", DS_INTERNAL, DCI_DT_INT, 60, 30, pNode));
917aa2e6 64 return pNode;
282349dd
VK
65}
66
67
68//
69// Node poller thread (poll new nodes and put them into the database)
70//
71
ccdbbb52 72THREAD_RESULT THREAD_CALL NodePoller(void *arg)
282349dd
VK
73{
74 DB_RESULT hResult;
2da1357c 75 int iPollInterval;
f91fa4c2 76 DWORD dwWatchdogId;
2da1357c 77
f91fa4c2 78 // Read configuration and initialize
2da1357c 79 iPollInterval = ConfigReadInt("NewNodePollingInterval", 60);
a97797f8 80 dwWatchdogId = WatchdogAddThread("Node Poller", iPollInterval * 2 + 10);
b1dd534d 81 DbgPrintf(AF_DEBUG_DISCOVERY, "Node poller started with poll interval %d seconds", iPollInterval);
282349dd
VK
82
83 while(!ShutdownInProgress())
84 {
d627ae40
VK
85 if (SleepAndCheckForShutdown(iPollInterval))
86 break; // Shutdown has arrived
f91fa4c2 87 WatchdogNotify(dwWatchdogId);
282349dd 88
fa1d3757 89 hResult = DBSelect(g_hCoreDB, "SELECT id,ip_addr,ip_netmask,discovery_flags FROM new_nodes");
282349dd
VK
90 if (hResult != 0)
91 {
92 int i, iNumRows;
93 DWORD dwId, dwIpAddr, dwNetMask, dwFlags;
94 char szQuery[256];
95
96 iNumRows = DBGetNumRows(hResult);
97 for(i = 0; i < iNumRows; i++)
98 {
be0a5a53
VK
99 WatchdogNotify(dwWatchdogId);
100
282349dd
VK
101 dwId = DBGetFieldULong(hResult, i, 0);
102 dwIpAddr = DBGetFieldULong(hResult, i, 1);
103 dwNetMask = DBGetFieldULong(hResult, i, 2);
104 dwFlags = DBGetFieldULong(hResult, i, 3);
105
b900a78b 106 PollNewNode(dwIpAddr, dwNetMask, dwFlags, NULL);
282349dd
VK
107
108 // Delete processed node
fa1d3757 109 sprintf(szQuery, "DELETE FROM new_nodes WHERE id=%ld", dwId);
282349dd
VK
110 DBQuery(g_hCoreDB, szQuery);
111 }
112 DBFreeResult(hResult);
113 }
114 }
b1dd534d 115 DbgPrintf(AF_DEBUG_DISCOVERY, "Node poller thread terminated");
ccdbbb52 116 return THREAD_OK;
282349dd 117}