- Added function SnmpEnumerate()
[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;
eefe7d68
VK
33 Interface *pInterface;
34 Subnet *pSubnet;
cc140cce 35 DWORD dwSubnetAddr, dwNodeFlags = 0;
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 52 pSubnet = new Subnet(dwSubnetAddr, dwNetMask);
cc140cce 53 NetObjInsert(pSubnet, TRUE);
2da1357c 54 g_pEntireNet->AddSubnet(pSubnet);
eefe7d68
VK
55 }
56
eefe7d68 57 pInterface = new Interface(dwIpAddr, dwNetMask);
cc140cce
VK
58 NetObjInsert(pInterface, TRUE);
59 pNode = new Node(dwIpAddr, 0, dwFlags);
eefe7d68 60 pNode->AddInterface(pInterface);
cc140cce 61 NetObjInsert(pNode, TRUE);
eefe7d68
VK
62
63 // Link node to subnet
64 pSubnet->AddNode(pNode);
282349dd
VK
65}
66
67
68//
69// Node poller thread (poll new nodes and put them into the database)
70//
71
72void NodePoller(void *arg)
73{
74 DB_RESULT hResult;
2da1357c
VK
75 int iPollInterval;
76
77 // Read configuration
78 iPollInterval = ConfigReadInt("NewNodePollingInterval", 60);
282349dd
VK
79
80 while(!ShutdownInProgress())
81 {
2da1357c 82 ThreadSleep(iPollInterval);
282349dd
VK
83 if (ShutdownInProgress())
84 break;
85
86 hResult = DBSelect(g_hCoreDB, "SELECT id,ip_addr,ip_netmask,discovery_flags FROM NewNodes");
87 if (hResult != 0)
88 {
89 int i, iNumRows;
90 DWORD dwId, dwIpAddr, dwNetMask, dwFlags;
91 char szQuery[256];
92
93 iNumRows = DBGetNumRows(hResult);
94 for(i = 0; i < iNumRows; i++)
95 {
96 dwId = DBGetFieldULong(hResult, i, 0);
97 dwIpAddr = DBGetFieldULong(hResult, i, 1);
98 dwNetMask = DBGetFieldULong(hResult, i, 2);
99 dwFlags = DBGetFieldULong(hResult, i, 3);
100
101 PollNode(dwIpAddr, dwNetMask, dwFlags);
102
103 // Delete processed node
104 sprintf(szQuery, "DELETE FROM NewNodes WHERE id=%ld", dwId);
105 DBQuery(g_hCoreDB, szQuery);
106 }
107 DBFreeResult(hResult);
108 }
109 }
110}