Fixed incorrect data type constants usage; confusing DT_xxx and DTYPE_xxx constants...
[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
917aa2e6 31NetObj *PollNewNode(DWORD dwIpAddr, DWORD dwNetMask, DWORD dwFlags)
282349dd
VK
32{
33 Node *pNode;
a7b85168 34 char szIpAddr1[32], szIpAddr2[32];
282349dd 35
a7b85168
VK
36 DbgPrintf(AF_DEBUG_DISCOVERY, "PollNode(%s,%s)\n",
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 {
a7b85168
VK
42 DbgPrintf(AF_DEBUG_DISCOVERY, "PollNode: Node %s already exist in database\n",
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 {
a7b85168
VK
51 DbgPrintf(AF_DEBUG_DISCOVERY, "Node::NewNodePoll(%s)failed\n",
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 }
7fdc8a45
VK
59
60 // DEBUG
42d7ed00 61 pNode->AddItem(new DCItem(CreateUniqueId(IDG_ITEM), "Status", DS_INTERNAL, DCI_DT_INTEGER, 60, 30, pNode));
917aa2e6 62 return pNode;
282349dd
VK
63}
64
65
66//
67// Node poller thread (poll new nodes and put them into the database)
68//
69
70void NodePoller(void *arg)
71{
72 DB_RESULT hResult;
2da1357c 73 int iPollInterval;
f91fa4c2 74 DWORD dwWatchdogId;
2da1357c 75
f91fa4c2 76 // Read configuration and initialize
2da1357c 77 iPollInterval = ConfigReadInt("NewNodePollingInterval", 60);
f91fa4c2 78 dwWatchdogId = WatchdogAddThread("Node Poller");
a7b85168 79 DbgPrintf(AF_DEBUG_DISCOVERY, "Node poller started with poll interval %d seconds\n", iPollInterval);
282349dd
VK
80
81 while(!ShutdownInProgress())
82 {
d627ae40
VK
83 if (SleepAndCheckForShutdown(iPollInterval))
84 break; // Shutdown has arrived
f91fa4c2 85 WatchdogNotify(dwWatchdogId);
282349dd 86
fa1d3757 87 hResult = DBSelect(g_hCoreDB, "SELECT id,ip_addr,ip_netmask,discovery_flags FROM new_nodes");
282349dd
VK
88 if (hResult != 0)
89 {
90 int i, iNumRows;
91 DWORD dwId, dwIpAddr, dwNetMask, dwFlags;
92 char szQuery[256];
93
94 iNumRows = DBGetNumRows(hResult);
95 for(i = 0; i < iNumRows; i++)
96 {
be0a5a53
VK
97 WatchdogNotify(dwWatchdogId);
98
282349dd
VK
99 dwId = DBGetFieldULong(hResult, i, 0);
100 dwIpAddr = DBGetFieldULong(hResult, i, 1);
101 dwNetMask = DBGetFieldULong(hResult, i, 2);
102 dwFlags = DBGetFieldULong(hResult, i, 3);
103
917aa2e6 104 PollNewNode(dwIpAddr, dwNetMask, dwFlags);
282349dd
VK
105
106 // Delete processed node
fa1d3757 107 sprintf(szQuery, "DELETE FROM new_nodes WHERE id=%ld", dwId);
282349dd
VK
108 DBQuery(g_hCoreDB, szQuery);
109 }
110 DBFreeResult(hResult);
111 }
112 }
a7b85168 113 DbgPrintf(AF_DEBUG_DISCOVERY, "Node poller thread terminated\n");
282349dd 114}