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