Internal watchdog 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 61 int iPollInterval;
f91fa4c2 62 DWORD dwWatchdogId;
2da1357c 63
f91fa4c2 64 // Read configuration and initialize
2da1357c 65 iPollInterval = ConfigReadInt("NewNodePollingInterval", 60);
f91fa4c2 66 dwWatchdogId = WatchdogAddThread("Node Poller");
282349dd
VK
67
68 while(!ShutdownInProgress())
69 {
d627ae40
VK
70 if (SleepAndCheckForShutdown(iPollInterval))
71 break; // Shutdown has arrived
f91fa4c2 72 WatchdogNotify(dwWatchdogId);
282349dd
VK
73
74 hResult = DBSelect(g_hCoreDB, "SELECT id,ip_addr,ip_netmask,discovery_flags FROM NewNodes");
75 if (hResult != 0)
76 {
77 int i, iNumRows;
78 DWORD dwId, dwIpAddr, dwNetMask, dwFlags;
79 char szQuery[256];
80
81 iNumRows = DBGetNumRows(hResult);
82 for(i = 0; i < iNumRows; i++)
83 {
84 dwId = DBGetFieldULong(hResult, i, 0);
85 dwIpAddr = DBGetFieldULong(hResult, i, 1);
86 dwNetMask = DBGetFieldULong(hResult, i, 2);
87 dwFlags = DBGetFieldULong(hResult, i, 3);
88
89 PollNode(dwIpAddr, dwNetMask, dwFlags);
90
91 // Delete processed node
92 sprintf(szQuery, "DELETE FROM NewNodes WHERE id=%ld", dwId);
93 DBQuery(g_hCoreDB, szQuery);
94 }
95 DBFreeResult(hResult);
96 }
97 }
98}