- New event "EVENT_THREAD_HANGS" added
[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**
cc140cce 19** $module: np.cpp
282349dd
VK
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;
282349dd 33
282349dd 34 // Check for node existence
eefe7d68
VK
35 if ((FindNodeByIP(dwIpAddr) != NULL) ||
36 (FindSubnetByIP(dwIpAddr) != NULL))
282349dd 37 {
45ac7a2e 38char buffer[32];
c1263934 39printf("Node %s already exist in database\n", IpToStr(dwIpAddr,buffer));
282349dd
VK
40 return;
41 }
42
469b937c 43 pNode = new Node(dwIpAddr, 0, dwFlags);
f9b7e653 44 NetObjInsert(pNode, TRUE);
469b937c 45 if (!pNode->NewNodePoll(dwNetMask))
eefe7d68 46 {
48b1c0ac 47printf("New node %s cannot be added\n",pNode->Name());
f9b7e653 48 NetObjDelete(pNode);
469b937c
VK
49 delete pNode; // Node poll failed, delete it
50 }
282349dd
VK
51}
52
53
54//
55// Node poller thread (poll new nodes and put them into the database)
56//
57
58void NodePoller(void *arg)
59{
60 DB_RESULT hResult;
2da1357c
VK
61 int iPollInterval;
62
63 // Read configuration
64 iPollInterval = ConfigReadInt("NewNodePollingInterval", 60);
282349dd
VK
65
66 while(!ShutdownInProgress())
67 {
d627ae40
VK
68 if (SleepAndCheckForShutdown(iPollInterval))
69 break; // Shutdown has arrived
282349dd
VK
70
71 hResult = DBSelect(g_hCoreDB, "SELECT id,ip_addr,ip_netmask,discovery_flags FROM NewNodes");
72 if (hResult != 0)
73 {
74 int i, iNumRows;
75 DWORD dwId, dwIpAddr, dwNetMask, dwFlags;
76 char szQuery[256];
77
78 iNumRows = DBGetNumRows(hResult);
79 for(i = 0; i < iNumRows; i++)
80 {
81 dwId = DBGetFieldULong(hResult, i, 0);
82 dwIpAddr = DBGetFieldULong(hResult, i, 1);
83 dwNetMask = DBGetFieldULong(hResult, i, 2);
84 dwFlags = DBGetFieldULong(hResult, i, 3);
85
86 PollNode(dwIpAddr, dwNetMask, dwFlags);
87
88 // Delete processed node
89 sprintf(szQuery, "DELETE FROM NewNodes WHERE id=%ld", dwId);
90 DBQuery(g_hCoreDB, szQuery);
91 }
92 DBFreeResult(hResult);
93 }
94 }
95}