GetSysInfoStr() implemented for POSIX conforming systems
[public/netxms.git] / src / server / core / np.cpp
CommitLineData
282349dd 1/*
7257eb7d
VK
2** NetXMS - Network Management System
3** Copyright (C) 2003, 2004 Victor Kirhenshtein
282349dd
VK
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;
a7b85168 33 char szIpAddr1[32], szIpAddr2[32];
282349dd 34
a7b85168
VK
35 DbgPrintf(AF_DEBUG_DISCOVERY, "PollNode(%s,%s)\n",
36 IpToStr(dwIpAddr, szIpAddr1), IpToStr(dwNetMask, szIpAddr2));
282349dd 37 // Check for node existence
eefe7d68
VK
38 if ((FindNodeByIP(dwIpAddr) != NULL) ||
39 (FindSubnetByIP(dwIpAddr) != NULL))
282349dd 40 {
a7b85168
VK
41 DbgPrintf(AF_DEBUG_DISCOVERY, "PollNode: Node %s already exist in database\n",
42 IpToStr(dwIpAddr, szIpAddr1));
282349dd
VK
43 return;
44 }
45
469b937c 46 pNode = new Node(dwIpAddr, 0, dwFlags);
f9b7e653 47 NetObjInsert(pNode, TRUE);
469b937c 48 if (!pNode->NewNodePoll(dwNetMask))
eefe7d68 49 {
a7b85168
VK
50 DbgPrintf(AF_DEBUG_DISCOVERY, "Node::NewNodePoll(%s)failed\n",
51 IpToStr(dwIpAddr, szIpAddr1));
be0a5a53 52 ObjectsGlobalLock();
f9b7e653 53 NetObjDelete(pNode);
be0a5a53 54 ObjectsGlobalUnlock();
469b937c 55 delete pNode; // Node poll failed, delete it
e6b958c4 56 return;
469b937c 57 }
7fdc8a45
VK
58
59 // DEBUG
3ea35b38 60 pNode->AddItem(new DCItem(CreateUniqueId(IDG_ITEM), "Status", DS_INTERNAL, DTYPE_INTEGER, 60, 30, pNode));
282349dd
VK
61}
62
63
64//
65// Node poller thread (poll new nodes and put them into the database)
66//
67
68void NodePoller(void *arg)
69{
70 DB_RESULT hResult;
2da1357c 71 int iPollInterval;
f91fa4c2 72 DWORD dwWatchdogId;
2da1357c 73
f91fa4c2 74 // Read configuration and initialize
2da1357c 75 iPollInterval = ConfigReadInt("NewNodePollingInterval", 60);
f91fa4c2 76 dwWatchdogId = WatchdogAddThread("Node Poller");
a7b85168 77 DbgPrintf(AF_DEBUG_DISCOVERY, "Node poller started with poll interval %d seconds\n", iPollInterval);
282349dd
VK
78
79 while(!ShutdownInProgress())
80 {
d627ae40
VK
81 if (SleepAndCheckForShutdown(iPollInterval))
82 break; // Shutdown has arrived
f91fa4c2 83 WatchdogNotify(dwWatchdogId);
282349dd 84
fa1d3757 85 hResult = DBSelect(g_hCoreDB, "SELECT id,ip_addr,ip_netmask,discovery_flags FROM new_nodes");
282349dd
VK
86 if (hResult != 0)
87 {
88 int i, iNumRows;
89 DWORD dwId, dwIpAddr, dwNetMask, dwFlags;
90 char szQuery[256];
91
92 iNumRows = DBGetNumRows(hResult);
93 for(i = 0; i < iNumRows; i++)
94 {
be0a5a53
VK
95 WatchdogNotify(dwWatchdogId);
96
282349dd
VK
97 dwId = DBGetFieldULong(hResult, i, 0);
98 dwIpAddr = DBGetFieldULong(hResult, i, 1);
99 dwNetMask = DBGetFieldULong(hResult, i, 2);
100 dwFlags = DBGetFieldULong(hResult, i, 3);
101
102 PollNode(dwIpAddr, dwNetMask, dwFlags);
103
104 // Delete processed node
fa1d3757 105 sprintf(szQuery, "DELETE FROM new_nodes WHERE id=%ld", dwId);
282349dd
VK
106 DBQuery(g_hCoreDB, szQuery);
107 }
108 DBFreeResult(hResult);
109 }
110 }
a7b85168 111 DbgPrintf(AF_DEBUG_DISCOVERY, "Node poller thread terminated\n");
282349dd 112}