- New fields added to NODES table
[public/netxms.git] / src / server / core / np.cpp
CommitLineData
282349dd
VK
1/*
2** Project X - Network Management System
3** Copyright (C) 2003 Victor Kirhenshtein
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** $module: discovery.cpp
20**
21**/
22
23#include "nms_core.h"
24
25
26//
27// Poll new node for configuration
28//
29
30static void PollNode(DWORD dwIpAddr, DWORD dwNetMask, DWORD dwFlags)
31{
32 Node *pNode;
eefe7d68
VK
33 Interface *pInterface;
34 Subnet *pSubnet;
35 DWORD dwSubnetAddr;
282349dd 36
282349dd 37 // Check for node existence
eefe7d68
VK
38 if ((FindNodeByIP(dwIpAddr) != NULL) ||
39 (FindSubnetByIP(dwIpAddr) != NULL))
282349dd
VK
40 {
41 char szBuffer[32];
42
43 WriteLog(MSG_NODE_EXIST, EVENTLOG_WARNING_TYPE, "s", IpToStr(dwIpAddr, szBuffer));
44 return;
45 }
46
eefe7d68
VK
47 // Find appropriate subnet
48 dwSubnetAddr = dwIpAddr & dwNetMask;
eefe7d68 49 pSubnet = FindSubnetByIP(dwSubnetAddr);
eefe7d68
VK
50 if (pSubnet == NULL)
51 {
eefe7d68
VK
52 pSubnet = new Subnet(dwSubnetAddr, dwNetMask);
53 NetObjInsert(pSubnet);
eefe7d68
VK
54 }
55
eefe7d68
VK
56 pInterface = new Interface(dwIpAddr, dwNetMask);
57 NetObjInsert(pInterface);
eefe7d68
VK
58 pNode = new Node(dwIpAddr, dwFlags, 0);
59 pNode->AddInterface(pInterface);
60 NetObjInsert(pNode);
eefe7d68
VK
61
62 // Link node to subnet
63 pSubnet->AddNode(pNode);
282349dd
VK
64}
65
66
67//
68// Node poller thread (poll new nodes and put them into the database)
69//
70
71void NodePoller(void *arg)
72{
73 DB_RESULT hResult;
74
75 while(!ShutdownInProgress())
76 {
eefe7d68
VK
77// ThreadSleep(30);
78 ThreadSleep(5);
282349dd
VK
79 if (ShutdownInProgress())
80 break;
81
82 hResult = DBSelect(g_hCoreDB, "SELECT id,ip_addr,ip_netmask,discovery_flags FROM NewNodes");
83 if (hResult != 0)
84 {
85 int i, iNumRows;
86 DWORD dwId, dwIpAddr, dwNetMask, dwFlags;
87 char szQuery[256];
88
89 iNumRows = DBGetNumRows(hResult);
90 for(i = 0; i < iNumRows; i++)
91 {
92 dwId = DBGetFieldULong(hResult, i, 0);
93 dwIpAddr = DBGetFieldULong(hResult, i, 1);
94 dwNetMask = DBGetFieldULong(hResult, i, 2);
95 dwFlags = DBGetFieldULong(hResult, i, 3);
96
97 PollNode(dwIpAddr, dwNetMask, dwFlags);
98
99 // Delete processed node
100 sprintf(szQuery, "DELETE FROM NewNodes WHERE id=%ld", dwId);
101 DBQuery(g_hCoreDB, szQuery);
102 }
103 DBFreeResult(hResult);
104 }
105 }
106}