added method InetAddress::isLinkLocal(); added method InetAddressList::get(); fixed...
[public/netxms.git] / src / server / core / node.cpp
CommitLineData
4d2c3a54 1/*
5039dede 2** NetXMS - Network Management System
9a1f7c45 3** Copyright (C) 2003-2014 Victor Kirhenshtein
5039dede
AK
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**
19** File: node.cpp
20**
21**/
22
23#include "nxcore.h"
24
7aad6641
VK
25/**
26 * Externals
27 */
46117060
VK
28extern Queue g_statusPollQueue;
29extern Queue g_configPollQueue;
805171de 30extern Queue g_instancePollQueue;
46117060
VK
31extern Queue g_topologyPollQueue;
32extern Queue g_routePollQueue;
33extern Queue g_discoveryPollQueue;
34
c50f77f7
VK
35/**
36 * Node class default constructor
37 */
6fd6de0a 38Node::Node() : DataCollectionTarget()
5039dede 39{
5ad2167d 40 m_primaryName[0] = 0;
a65c1819 41 m_iStatus = STATUS_UNKNOWN;
5039dede
AK
42 m_dwFlags = 0;
43 m_dwDynamicFlags = 0;
52c31148 44 m_zoneId = 0;
c42b4551
VK
45 m_agentPort = AGENT_LISTEN_PORT;
46 m_agentAuthMethod = AUTH_NONE;
5039dede
AK
47 m_szSharedSecret[0] = 0;
48 m_iStatusPollType = POLL_ICMP_PING;
5d2c5741 49 m_snmpVersion = SNMP_VERSION_1;
5039dede 50 m_wSNMPPort = SNMP_DEFAULT_PORT;
35f836fe
VK
51 char community[MAX_COMMUNITY_LENGTH];
52 ConfigReadStrA(_T("DefaultCommunityString"), community, MAX_COMMUNITY_LENGTH, "public");
5d2c5741 53 m_snmpSecurity = new SNMP_SecurityContext(community);
5039dede 54 m_szObjectId[0] = 0;
8573e935
VK
55 m_lastDiscoveryPoll = 0;
56 m_lastStatusPoll = 0;
57 m_lastConfigurationPoll = 0;
805171de 58 m_lastInstancePoll = 0;
8573e935
VK
59 m_lastTopologyPoll = 0;
60 m_lastRTUpdate = 0;
61 m_downSince = 0;
71e4ed3a 62 m_bootTime = 0;
0ab347c0 63 m_agentUpTime = 0;
5039dede
AK
64 m_hPollerMutex = MutexCreate();
65 m_hAgentAccessMutex = MutexCreate();
1d0d82b3 66 m_hSmclpAccessMutex = MutexCreate();
5039dede
AK
67 m_mutexRTAccess = MutexCreate();
68 m_mutexTopoAccess = MutexCreate();
69 m_pAgentConnection = NULL;
1d0d82b3 70 m_smclpConnection = NULL;
fed33789 71 m_lastAgentTrapId = 0;
489b117b 72 m_lastSNMPTrapId = 0;
42a3be4f 73 m_lastAgentPushRequestId = 0;
5039dede
AK
74 m_szAgentVersion[0] = 0;
75 m_szPlatformName[0] = 0;
0ecc2200
VK
76 m_sysDescription = NULL;
77 m_sysName = NULL;
78 m_lldpNodeId = NULL;
3a82d5ae 79 m_lldpLocalPortInfo = NULL;
cc8ce218
VK
80 m_paramList = NULL;
81 m_tableList = NULL;
9208c84b
VK
82 m_pollerNode = 0;
83 m_agentProxy = 0;
84 m_snmpProxy = 0;
85 m_icmpProxy = 0;
5039dede
AK
86 memset(m_qwLastEvents, 0, sizeof(QWORD) * MAX_LAST_EVENTS);
87 m_pRoutingTable = NULL;
8573e935
VK
88 m_failTimeSNMP = 0;
89 m_failTimeAgent = 0;
040c45fa 90 m_linkLayerNeighbors = NULL;
0d75ea88 91 m_vrrpInfo = NULL;
040c45fa
VK
92 m_pTopology = NULL;
93 m_topologyRebuildTimestamp = 0;
5039dede
AK
94 m_iPendingStatus = -1;
95 m_iPollCount = 0;
96 m_iRequiredPollCount = 0; // Use system default
97 m_nUseIfXTable = IFXTABLE_DEFAULT; // Use system default
ab621f39 98 m_jobQueue = new ServerJobQueue();
f42b8099 99 m_fdb = NULL;
7f632dfe 100 m_vlans = NULL;
d5de1d1d 101 m_wirelessStations = NULL;
f1989a3a
VK
102 m_adoptedApCount = 0;
103 m_totalApCount = 0;
9796ce45 104 m_driver = NULL;
ae32341d 105 m_driverData = NULL;
50d0de67 106 m_components = NULL;
caa04e26 107 m_softwarePackages = NULL;
46ee6286 108 m_winPerfObjects = NULL;
3eab63f2 109 memset(m_baseBridgeAddress, 0, MAC_ADDR_LENGTH);
5039dede
AK
110}
111
c50f77f7
VK
112/**
113 * Constructor for new node object
114 */
c75e9ee4 115Node::Node(const InetAddress& addr, UINT32 dwFlags, UINT32 agentProxy, UINT32 snmpProxy, UINT32 dwZone) : DataCollectionTarget()
5039dede 116{
c75e9ee4 117 addr.toString(m_primaryName);
a65c1819 118 m_iStatus = STATUS_UNKNOWN;
c75e9ee4 119 m_ipAddress = addr;
5039dede
AK
120 m_dwFlags = dwFlags;
121 m_dwDynamicFlags = 0;
52c31148 122 m_zoneId = dwZone;
c42b4551
VK
123 m_agentPort = AGENT_LISTEN_PORT;
124 m_agentAuthMethod = AUTH_NONE;
5039dede
AK
125 m_szSharedSecret[0] = 0;
126 m_iStatusPollType = POLL_ICMP_PING;
5d2c5741 127 m_snmpVersion = SNMP_VERSION_1;
5039dede 128 m_wSNMPPort = SNMP_DEFAULT_PORT;
35f836fe
VK
129 char community[MAX_COMMUNITY_LENGTH];
130 ConfigReadStrA(_T("DefaultCommunityString"), community, MAX_COMMUNITY_LENGTH, "public");
5d2c5741 131 m_snmpSecurity = new SNMP_SecurityContext(community);
c75e9ee4 132 addr.toString(m_name); // Make default name from IP address
5039dede 133 m_szObjectId[0] = 0;
8573e935
VK
134 m_lastDiscoveryPoll = 0;
135 m_lastStatusPoll = 0;
136 m_lastConfigurationPoll = 0;
805171de 137 m_lastInstancePoll = 0;
8573e935
VK
138 m_lastTopologyPoll = 0;
139 m_lastRTUpdate = 0;
140 m_downSince = 0;
71e4ed3a 141 m_bootTime = 0;
0ab347c0 142 m_agentUpTime = 0;
5039dede
AK
143 m_hPollerMutex = MutexCreate();
144 m_hAgentAccessMutex = MutexCreate();
1d0d82b3 145 m_hSmclpAccessMutex = MutexCreate();
5039dede
AK
146 m_mutexRTAccess = MutexCreate();
147 m_mutexTopoAccess = MutexCreate();
148 m_pAgentConnection = NULL;
1d0d82b3 149 m_smclpConnection = NULL;
fed33789 150 m_lastAgentTrapId = 0;
489b117b 151 m_lastSNMPTrapId = 0;
42a3be4f 152 m_lastAgentPushRequestId = 0;
5039dede
AK
153 m_szAgentVersion[0] = 0;
154 m_szPlatformName[0] = 0;
0ecc2200
VK
155 m_sysDescription = NULL;
156 m_sysName = NULL;
157 m_lldpNodeId = NULL;
3a82d5ae 158 m_lldpLocalPortInfo = NULL;
cc8ce218
VK
159 m_paramList = NULL;
160 m_tableList = NULL;
9208c84b
VK
161 m_pollerNode = 0;
162 m_agentProxy = agentProxy;
163 m_snmpProxy = snmpProxy;
164 m_icmpProxy = 0;
5039dede 165 memset(m_qwLastEvents, 0, sizeof(QWORD) * MAX_LAST_EVENTS);
01152a54 166 m_isHidden = true;
5039dede 167 m_pRoutingTable = NULL;
8573e935
VK
168 m_failTimeSNMP = 0;
169 m_failTimeAgent = 0;
040c45fa 170 m_linkLayerNeighbors = NULL;
0d75ea88 171 m_vrrpInfo = NULL;
040c45fa
VK
172 m_pTopology = NULL;
173 m_topologyRebuildTimestamp = 0;
5039dede
AK
174 m_iPendingStatus = -1;
175 m_iPollCount = 0;
176 m_iRequiredPollCount = 0; // Use system default
177 m_nUseIfXTable = IFXTABLE_DEFAULT; // Use system default
ab621f39 178 m_jobQueue = new ServerJobQueue();
f42b8099 179 m_fdb = NULL;
7f632dfe 180 m_vlans = NULL;
d5de1d1d 181 m_wirelessStations = NULL;
f1989a3a
VK
182 m_adoptedApCount = 0;
183 m_totalApCount = 0;
9796ce45 184 m_driver = NULL;
ae32341d 185 m_driverData = NULL;
50d0de67 186 m_components = NULL;
caa04e26 187 m_softwarePackages = NULL;
46ee6286 188 m_winPerfObjects = NULL;
3eab63f2 189 memset(m_baseBridgeAddress, 0, MAC_ADDR_LENGTH);
5039dede
AK
190}
191
c50f77f7
VK
192/**
193 * Node destructor
194 */
5039dede
AK
195Node::~Node()
196{
225b92bc 197 delete m_driverData;
5039dede
AK
198 MutexDestroy(m_hPollerMutex);
199 MutexDestroy(m_hAgentAccessMutex);
1d0d82b3 200 MutexDestroy(m_hSmclpAccessMutex);
5039dede
AK
201 MutexDestroy(m_mutexRTAccess);
202 MutexDestroy(m_mutexTopoAccess);
203 delete m_pAgentConnection;
1d0d82b3 204 delete m_smclpConnection;
cc8ce218
VK
205 delete m_paramList;
206 delete m_tableList;
bb151059 207 safe_free(m_sysDescription);
5039dede 208 DestroyRoutingTable(m_pRoutingTable);
a9ad9f61
VK
209 if (m_linkLayerNeighbors != NULL)
210 m_linkLayerNeighbors->decRefCount();
0d75ea88 211 delete m_vrrpInfo;
5039dede 212 delete m_pTopology;
ab621f39 213 delete m_jobQueue;
5d2c5741 214 delete m_snmpSecurity;
f42b8099
VK
215 if (m_fdb != NULL)
216 m_fdb->decRefCount();
7f632dfe
VK
217 if (m_vlans != NULL)
218 m_vlans->decRefCount();
d5de1d1d 219 delete m_wirelessStations;
33dfe57e
VK
220 if (m_components != NULL)
221 m_components->decRefCount();
3a82d5ae 222 delete m_lldpLocalPortInfo;
caa04e26 223 delete m_softwarePackages;
46ee6286 224 delete m_winPerfObjects;
4671afeb 225 safe_free(m_sysName);
5039dede
AK
226}
227
c50f77f7
VK
228/**
229 * Create object from database data
230 */
c42b4551 231BOOL Node::loadFromDatabase(UINT32 dwId)
5039dede 232{
5039dede 233 int i, iNumRows;
967893bb 234 UINT32 dwSubnetId;
5039dede
AK
235 NetObj *pObject;
236 BOOL bResult = FALSE;
237
c42b4551 238 m_id = dwId;
5039dede 239
89135050 240 if (!loadCommonProperties())
5039dede 241 {
35f836fe 242 DbgPrintf(2, _T("Cannot load common properties for node object %d"), dwId);
5039dede
AK
243 return FALSE;
244 }
245
4d2c3a54 246 DB_STATEMENT hStmt = DBPrepare(g_hCoreDB,
4866d57b
VK
247 _T("SELECT primary_name,primary_ip,node_flags,")
248 _T("snmp_version,auth_method,secret,")
249 _T("agent_port,status_poll_type,snmp_oid,agent_version,")
250 _T("platform_name,poller_node_id,zone_guid,")
251 _T("proxy_node,snmp_proxy,required_polls,uname,")
252 _T("use_ifxtable,snmp_port,community,usm_auth_password,")
253 _T("usm_priv_password,usm_methods,snmp_sys_name,bridge_base_addr,")
9208c84b 254 _T("runtime_flags,down_since,boot_time,driver_name,icmp_proxy FROM nodes WHERE id=?"));
4866d57b
VK
255 if (hStmt == NULL)
256 return FALSE;
257
258 DBBind(hStmt, 1, DB_SQLTYPE_INTEGER, dwId);
92c51b1d 259 DB_RESULT hResult = DBSelectPrepared(hStmt);
e2babedf 260 if (hResult == NULL)
b1789b80
VK
261 {
262 DBFreeStatement(hStmt);
5039dede 263 return FALSE; // Query failed
b1789b80 264 }
5039dede
AK
265
266 if (DBGetNumRows(hResult) == 0)
267 {
268 DBFreeResult(hResult);
b1789b80 269 DBFreeStatement(hStmt);
5d2c5741 270 DbgPrintf(2, _T("Missing record in \"nodes\" table for node object %d"), dwId);
5039dede
AK
271 return FALSE;
272 }
273
5ad2167d 274 DBGetField(hResult, 0, 0, m_primaryName, MAX_DNS_NAME);
c75e9ee4 275 m_ipAddress = DBGetFieldInetAddr(hResult, 0, 1);
5ad2167d
VK
276 m_dwFlags = DBGetFieldULong(hResult, 0, 2);
277 m_snmpVersion = DBGetFieldLong(hResult, 0, 3);
c42b4551 278 m_agentAuthMethod = (WORD)DBGetFieldLong(hResult, 0, 4);
5ad2167d 279 DBGetField(hResult, 0, 5, m_szSharedSecret, MAX_SECRET_LENGTH);
c42b4551 280 m_agentPort = (WORD)DBGetFieldLong(hResult, 0, 6);
5ad2167d
VK
281 m_iStatusPollType = DBGetFieldLong(hResult, 0, 7);
282 DBGetField(hResult, 0, 8, m_szObjectId, MAX_OID_LEN * 4);
5d2c5741 283 DBGetField(hResult, 0, 9, m_szAgentVersion, MAX_AGENT_VERSION_LEN);
5d2c5741 284 DBGetField(hResult, 0, 10, m_szPlatformName, MAX_PLATFORM_NAME_LEN);
9208c84b 285 m_pollerNode = DBGetFieldULong(hResult, 0, 11);
52c31148 286 m_zoneId = DBGetFieldULong(hResult, 0, 12);
9208c84b
VK
287 m_agentProxy = DBGetFieldULong(hResult, 0, 13);
288 m_snmpProxy = DBGetFieldULong(hResult, 0, 14);
5d2c5741 289 m_iRequiredPollCount = DBGetFieldLong(hResult, 0, 15);
0ecc2200 290 m_sysDescription = DBGetField(hResult, 0, 16, NULL, 0);
5d2c5741 291 m_nUseIfXTable = (BYTE)DBGetFieldLong(hResult, 0, 17);
65e2005b 292 m_wSNMPPort = (WORD)DBGetFieldLong(hResult, 0, 18);
5039dede 293
5d2c5741 294 // SNMP authentication parameters
35f836fe
VK
295 char snmpAuthObject[256], snmpAuthPassword[256], snmpPrivPassword[256];
296 DBGetFieldA(hResult, 0, 19, snmpAuthObject, 256);
297 DBGetFieldA(hResult, 0, 20, snmpAuthPassword, 256);
298 DBGetFieldA(hResult, 0, 21, snmpPrivPassword, 256);
3408b115 299 int snmpMethods = DBGetFieldLong(hResult, 0, 22);
5d2c5741
VK
300 delete m_snmpSecurity;
301 m_snmpSecurity = new SNMP_SecurityContext(snmpAuthObject, snmpAuthPassword, snmpPrivPassword, snmpMethods & 0xFF, snmpMethods >> 8);
14b98b3a 302 m_snmpSecurity->setSecurityModel((m_snmpVersion == SNMP_VERSION_3) ? SNMP_SECURITY_MODEL_USM : SNMP_SECURITY_MODEL_V2C);
5d2c5741 303
88ac8e88 304 m_sysName = DBGetField(hResult, 0, 23, NULL, 0);
d74c80ea 305
3eab63f2
VK
306 TCHAR baseAddr[16];
307 TCHAR *value = DBGetField(hResult, 0, 24, baseAddr, 16);
308 if (value != NULL)
309 StrToBin(value, m_baseBridgeAddress, MAC_ADDR_LENGTH);
310
4866d57b 311 m_dwDynamicFlags = DBGetFieldULong(hResult, 0, 25);
b6ead716 312 m_dwDynamicFlags &= NDF_PERSISTENT; // Clear out all non-persistent runtime flags
4866d57b 313
8573e935 314 m_downSince = DBGetFieldLong(hResult, 0, 26);
71e4ed3a 315 m_bootTime = DBGetFieldLong(hResult, 0, 27);
dd42ad0a 316
28e1575f
VK
317 // Setup driver
318 TCHAR driverName[34];
71e4ed3a 319 DBGetField(hResult, 0, 28, driverName, 34);
28e1575f
VK
320 StrStrip(driverName);
321 if (driverName[0] != 0)
322 m_driver = FindDriverByName(driverName);
323
9208c84b
VK
324 m_icmpProxy = DBGetFieldULong(hResult, 0, 29);
325
5039dede 326 DBFreeResult(hResult);
b1789b80 327 DBFreeStatement(hStmt);
5039dede 328
01152a54 329 if (!m_isDeleted)
5039dede
AK
330 {
331 // Link node to subnets
92c51b1d
VK
332 hStmt = DBPrepare(g_hCoreDB, _T("SELECT subnet_id FROM nsmap WHERE node_id=?"));
333 if (hStmt == NULL)
334 return FALSE;
335
c42b4551 336 DBBind(hStmt, 1, DB_SQLTYPE_INTEGER, m_id);
92c51b1d 337 hResult = DBSelectPrepared(hStmt);
5039dede 338 if (hResult == NULL)
92c51b1d
VK
339 {
340 DBFreeStatement(hStmt);
5039dede 341 return FALSE; // Query failed
92c51b1d 342 }
5039dede
AK
343
344 iNumRows = DBGetNumRows(hResult);
5039dede
AK
345 for(i = 0; i < iNumRows; i++)
346 {
347 dwSubnetId = DBGetFieldULong(hResult, i, 0);
348 pObject = FindObjectById(dwSubnetId);
349 if (pObject == NULL)
350 {
351 nxlog_write(MSG_INVALID_SUBNET_ID, EVENTLOG_ERROR_TYPE, "dd", dwId, dwSubnetId);
352 break;
353 }
c42b4551 354 else if (pObject->getObjectClass() != OBJECT_SUBNET)
5039dede
AK
355 {
356 nxlog_write(MSG_SUBNET_NOT_SUBNET, EVENTLOG_ERROR_TYPE, "dd", dwId, dwSubnetId);
357 break;
358 }
359 else
360 {
361 pObject->AddChild(this);
362 AddParent(pObject);
5039dede
AK
363 }
364 }
365
366 DBFreeResult(hResult);
92c51b1d
VK
367 DBFreeStatement(hStmt);
368
369 loadItemsFromDB();
89135050 370 loadACLFromDB();
5039dede
AK
371
372 // Walk through all items in the node and load appropriate thresholds
a6c1f07f 373 bResult = TRUE;
16d6f798
VK
374 for(i = 0; i < m_dcObjects->size(); i++)
375 if (!m_dcObjects->get(i)->loadThresholdsFromDB())
5039dede 376 {
5d2c5741 377 DbgPrintf(3, _T("Cannot load thresholds for DCI %d of node %d (%s)"),
c42b4551 378 m_dcObjects->get(i)->getId(), dwId, m_name);
5039dede
AK
379 bResult = FALSE;
380 }
381 }
382 else
383 {
384 bResult = TRUE;
385 }
386
387 return bResult;
388}
389
c50f77f7
VK
390/**
391 * Save object to database
392 */
c42b4551 393BOOL Node::saveToDatabase(DB_HANDLE hdb)
5039dede 394{
5039dede 395 // Lock object's access
c42b4551 396 lockProperties();
5039dede 397
28e1575f
VK
398 if (!saveCommonProperties(hdb))
399 {
c42b4551 400 unlockProperties();
28e1575f
VK
401 return FALSE;
402 }
5039dede 403
5039dede 404 // Form and execute INSERT or UPDATE query
4b7d8903 405 int snmpMethods = m_snmpSecurity->getAuthMethod() | (m_snmpSecurity->getPrivMethod() << 8);
28e1575f 406 DB_STATEMENT hStmt;
c42b4551 407 if (IsDatabaseRecordExist(hdb, _T("nodes"), _T("id"), m_id))
ba89fed2 408 {
28e1575f
VK
409 hStmt = DBPrepare(hdb,
410 _T("UPDATE nodes SET primary_ip=?,primary_name=?,snmp_port=?,node_flags=?,snmp_version=?,community=?,")
411 _T("status_poll_type=?,agent_port=?,auth_method=?,secret=?,snmp_oid=?,uname=?,agent_version=?,")
9208c84b 412 _T("platform_name=?,poller_node_id=?,zone_guid=?,proxy_node=?,snmp_proxy=?,icmp_proxy=?,required_polls=?,")
28e1575f 413 _T("use_ifxtable=?,usm_auth_password=?,usm_priv_password=?,usm_methods=?,snmp_sys_name=?,bridge_base_addr=?,")
71e4ed3a 414 _T("runtime_flags=?,down_since=?,driver_name=?,rack_image=?,rack_position=?,rack_id=?,boot_time=? WHERE id=?"));
ba89fed2
VK
415 }
416 else
65e2005b 417 {
28e1575f
VK
418 hStmt = DBPrepare(hdb,
419 _T("INSERT INTO nodes (primary_ip,primary_name,snmp_port,node_flags,snmp_version,community,status_poll_type,")
420 _T("agent_port,auth_method,secret,snmp_oid,uname,agent_version,platform_name,poller_node_id,zone_guid,")
9208c84b 421 _T("proxy_node,snmp_proxy,icmp_proxy,required_polls,use_ifxtable,usm_auth_password,usm_priv_password,usm_methods,")
71e4ed3a 422 _T("snmp_sys_name,bridge_base_addr,runtime_flags,down_since,driver_name,rack_image,rack_position,rack_id,boot_time,id) ")
9208c84b 423 _T("VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"));
65e2005b 424 }
28e1575f
VK
425 if (hStmt == NULL)
426 {
c42b4551 427 unlockProperties();
28e1575f
VK
428 return FALSE;
429 }
430
c75e9ee4 431 TCHAR ipAddr[64], baseAddress[16];
28e1575f 432
c75e9ee4 433 DBBind(hStmt, 1, DB_SQLTYPE_VARCHAR, m_ipAddress.toString(ipAddr), DB_BIND_STATIC);
28e1575f
VK
434 DBBind(hStmt, 2, DB_SQLTYPE_VARCHAR, m_primaryName, DB_BIND_STATIC);
435 DBBind(hStmt, 3, DB_SQLTYPE_INTEGER, (LONG)m_wSNMPPort);
436 DBBind(hStmt, 4, DB_SQLTYPE_INTEGER, m_dwFlags);
437 DBBind(hStmt, 5, DB_SQLTYPE_INTEGER, (LONG)m_snmpVersion);
438#ifdef UNICODE
439 DBBind(hStmt, 6, DB_SQLTYPE_VARCHAR, WideStringFromMBString(m_snmpSecurity->getCommunity()), DB_BIND_DYNAMIC);
440#else
441 DBBind(hStmt, 6, DB_SQLTYPE_VARCHAR, m_snmpSecurity->getCommunity(), DB_BIND_STATIC);
442#endif
443 DBBind(hStmt, 7, DB_SQLTYPE_INTEGER, (LONG)m_iStatusPollType);
c42b4551
VK
444 DBBind(hStmt, 8, DB_SQLTYPE_INTEGER, (LONG)m_agentPort);
445 DBBind(hStmt, 9, DB_SQLTYPE_INTEGER, (LONG)m_agentAuthMethod);
28e1575f
VK
446 DBBind(hStmt, 10, DB_SQLTYPE_VARCHAR, m_szSharedSecret, DB_BIND_STATIC);
447 DBBind(hStmt, 11, DB_SQLTYPE_VARCHAR, m_szObjectId, DB_BIND_STATIC);
448 DBBind(hStmt, 12, DB_SQLTYPE_VARCHAR, m_sysDescription, DB_BIND_STATIC);
449 DBBind(hStmt, 13, DB_SQLTYPE_VARCHAR, m_szAgentVersion, DB_BIND_STATIC);
450 DBBind(hStmt, 14, DB_SQLTYPE_VARCHAR, m_szPlatformName, DB_BIND_STATIC);
9208c84b 451 DBBind(hStmt, 15, DB_SQLTYPE_INTEGER, m_pollerNode);
28e1575f 452 DBBind(hStmt, 16, DB_SQLTYPE_INTEGER, m_zoneId);
9208c84b
VK
453 DBBind(hStmt, 17, DB_SQLTYPE_INTEGER, m_agentProxy);
454 DBBind(hStmt, 18, DB_SQLTYPE_INTEGER, m_snmpProxy);
455 DBBind(hStmt, 19, DB_SQLTYPE_INTEGER, m_icmpProxy);
456 DBBind(hStmt, 20, DB_SQLTYPE_INTEGER, (LONG)m_iRequiredPollCount);
457 DBBind(hStmt, 21, DB_SQLTYPE_INTEGER, (LONG)m_nUseIfXTable);
28e1575f 458#ifdef UNICODE
9208c84b
VK
459 DBBind(hStmt, 22, DB_SQLTYPE_VARCHAR, WideStringFromMBString(m_snmpSecurity->getAuthPassword()), DB_BIND_DYNAMIC);
460 DBBind(hStmt, 23, DB_SQLTYPE_VARCHAR, WideStringFromMBString(m_snmpSecurity->getPrivPassword()), DB_BIND_DYNAMIC);
28e1575f 461#else
9208c84b
VK
462 DBBind(hStmt, 22, DB_SQLTYPE_VARCHAR, m_snmpSecurity->getAuthPassword(), DB_BIND_STATIC);
463 DBBind(hStmt, 23, DB_SQLTYPE_VARCHAR, m_snmpSecurity->getPrivPassword(), DB_BIND_STATIC);
28e1575f 464#endif
9208c84b
VK
465 DBBind(hStmt, 24, DB_SQLTYPE_INTEGER, (LONG)snmpMethods);
466 DBBind(hStmt, 25, DB_SQLTYPE_VARCHAR, m_sysName, DB_BIND_STATIC);
467 DBBind(hStmt, 26, DB_SQLTYPE_VARCHAR, BinToStr(m_baseBridgeAddress, MAC_ADDR_LENGTH, baseAddress), DB_BIND_STATIC);
468 DBBind(hStmt, 27, DB_SQLTYPE_INTEGER, m_dwDynamicFlags);
469 DBBind(hStmt, 28, DB_SQLTYPE_INTEGER, (LONG)m_downSince);
470 DBBind(hStmt, 29, DB_SQLTYPE_VARCHAR, (m_driver != NULL) ? m_driver->getName() : _T(""), DB_BIND_STATIC);
471 DBBind(hStmt, 30, DB_SQLTYPE_VARCHAR, _T("00000000-0000-0000-0000-000000000000"), DB_BIND_STATIC); // rack image
472 DBBind(hStmt, 31, DB_SQLTYPE_INTEGER, (LONG)0); // rack position
473 DBBind(hStmt, 32, DB_SQLTYPE_INTEGER, (LONG)0); // rack ID
474 DBBind(hStmt, 33, DB_SQLTYPE_INTEGER, (LONG)m_bootTime); // rack ID
c42b4551 475 DBBind(hStmt, 34, DB_SQLTYPE_INTEGER, m_id);
28e1575f
VK
476
477 BOOL bResult = DBExecute(hStmt);
478 DBFreeStatement(hStmt);
5039dede 479
7c521895 480 // Save access list
89135050 481 saveACLToDB(hdb);
7c521895 482
c42b4551 483 unlockProperties();
7c521895 484
5039dede
AK
485 // Save data collection items
486 if (bResult)
487 {
b06436f4 488 lockDciAccess(false);
16d6f798
VK
489 for(int i = 0; i < m_dcObjects->size(); i++)
490 m_dcObjects->get(i)->saveToDB(hdb);
7c521895 491 unlockDciAccess();
5039dede
AK
492 }
493
7c521895 494 // Clear modifications flag
c42b4551 495 lockProperties();
01152a54 496 m_isModified = false;
c42b4551 497 unlockProperties();
5039dede 498
7c521895 499 return bResult;
5039dede
AK
500}
501
171c2fd6
VK
502/**
503 * Delete object from database
504 */
c42b4551 505bool Node::deleteFromDatabase(DB_HANDLE hdb)
5039dede 506{
c42b4551 507 bool success = DataCollectionTarget::deleteFromDatabase(hdb);
22ee6d97
VK
508 if (success)
509 success = executeQueryOnObject(hdb, _T("DELETE FROM nodes WHERE id=?"));
510 if (success)
511 success = executeQueryOnObject(hdb, _T("DELETE FROM nsmap WHERE node_id=?"));
512 return success;
5039dede
AK
513}
514
80e0db05
VK
515/**
516 * Get ARP cache from node
517 */
7c521895 518ARP_CACHE *Node::getArpCache()
5039dede
AK
519{
520 ARP_CACHE *pArpCache = NULL;
521
522 if (m_dwFlags & NF_IS_LOCAL_MGMT)
523 {
524 pArpCache = GetLocalArpCache();
525 }
526 else if (m_dwFlags & NF_IS_NATIVE_AGENT)
527 {
7c521895
VK
528 agentLock();
529 if (connectToAgent())
4687826e 530 pArpCache = m_pAgentConnection->getArpCache();
7c521895 531 agentUnlock();
5039dede
AK
532 }
533 else if (m_dwFlags & NF_IS_SNMP)
534 {
535 SNMP_Transport *pTransport;
536
cd9f247e 537 pTransport = createSnmpTransport();
803d47be
VK
538 if (pTransport != NULL)
539 {
540 pArpCache = SnmpGetArpCache(m_snmpVersion, pTransport);
541 delete pTransport;
542 }
5039dede
AK
543 }
544
545 return pArpCache;
546}
547
80e0db05
VK
548/**
549 * Get list of interfaces from node
550 */
98762401 551InterfaceList *Node::getInterfaceList()
5039dede 552{
98762401 553 InterfaceList *pIfList = NULL;
5039dede 554
b741f151 555 if ((m_dwFlags & NF_IS_NATIVE_AGENT) && (!(m_dwFlags & NF_DISABLE_NXCP)))
5039dede 556 {
7c521895
VK
557 agentLock();
558 if (connectToAgent())
5039dede 559 {
4687826e 560 pIfList = m_pAgentConnection->getInterfaceList();
5039dede 561 }
7c521895 562 agentUnlock();
5039dede 563 }
b741f151
VK
564 if ((pIfList == NULL) && (m_dwFlags & NF_IS_LOCAL_MGMT))
565 {
566 pIfList = GetLocalInterfaceList();
567 }
5039dede 568 if ((pIfList == NULL) && (m_dwFlags & NF_IS_SNMP) &&
9796ce45 569 (!(m_dwFlags & NF_DISABLE_SNMP)) && (m_driver != NULL))
5039dede 570 {
2fbf7e1c 571 SNMP_Transport *pTransport = createSnmpTransport();
803d47be 572 if (pTransport != NULL)
5039dede 573 {
2fbf7e1c 574 bool useIfXTable;
803d47be
VK
575 if (m_nUseIfXTable == IFXTABLE_DEFAULT)
576 {
9796ce45 577 useIfXTable = (ConfigReadInt(_T("UseIfXTable"), 1) != 0) ? true : false;
803d47be
VK
578 }
579 else
580 {
9796ce45 581 useIfXTable = (m_nUseIfXTable == IFXTABLE_ENABLED) ? true : false;
803d47be 582 }
9796ce45
VK
583
584 int useAliases = ConfigReadInt(_T("UseInterfaceAliases"), 0);
ae32341d 585 pIfList = m_driver->getInterfaces(pTransport, &m_customAttributes, m_driverData, useAliases, useIfXTable);
eec253a8 586
8792fe27 587 if ((pIfList != NULL) && (m_dwFlags & NF_IS_BRIDGE))
eec253a8
VK
588 {
589 BridgeMapPorts(m_snmpVersion, pTransport, pIfList);
590 }
803d47be 591 delete pTransport;
5039dede 592 }
5039dede
AK
593 }
594
595 if (pIfList != NULL)
0d75ea88 596 {
024c3faf 597 checkInterfaceNames(pIfList);
0d75ea88
VK
598 addVrrpInterfaces(pIfList);
599 }
5039dede
AK
600
601 return pIfList;
602}
603
6fd6de0a
VK
604/**
605 * Add VRRP interfaces to interface list
606 */
98762401 607void Node::addVrrpInterfaces(InterfaceList *ifList)
0d75ea88
VK
608{
609 int i, j, k;
610 TCHAR buffer[32];
611
c42b4551 612 lockProperties();
0d75ea88
VK
613 if (m_vrrpInfo != NULL)
614 {
c42b4551 615 DbgPrintf(6, _T("Node::addVrrpInterfaces(node=%s [%d]): m_vrrpInfo->size()=%d"), m_name, (int)m_id, m_vrrpInfo->size());
0d75ea88 616
a6312bd6 617 for(i = 0; i < m_vrrpInfo->size(); i++)
0d75ea88
VK
618 {
619 VrrpRouter *router = m_vrrpInfo->getRouter(i);
c42b4551 620 DbgPrintf(6, _T("Node::addVrrpInterfaces(node=%s [%d]): vrouter %d state=%d"), m_name, (int)m_id, i, router->getState());
0d75ea88
VK
621 if (router->getState() != VRRP_STATE_MASTER)
622 continue; // Do not add interfaces if router is not in master state
623
624 // Get netmask for this VR
967893bb 625 UINT32 netmask = 0;
a6312bd6 626 for(j = 0; j < ifList->size(); j++)
e95680e5 627 if (ifList->get(j)->index == router->getIfIndex())
0d75ea88 628 {
e95680e5 629 netmask = ifList->get(j)->ipNetMask;
0d75ea88
VK
630 break;
631 }
632
633 // Walk through all VR virtual IPs
634 for(j = 0; j < router->getVipCount(); j++)
635 {
967893bb 636 UINT32 vip = router->getVip(j);
c42b4551 637 DbgPrintf(6, _T("Node::addVrrpInterfaces(node=%s [%d]): checking VIP %s@%d"), m_name, (int)m_id, IpToStr(vip, buffer), i);
0d75ea88
VK
638 if (vip != 0)
639 {
a6312bd6 640 for(k = 0; k < ifList->size(); k++)
e95680e5 641 if (ifList->get(k)->ipAddr == vip)
0d75ea88 642 break;
a6312bd6 643 if (k == ifList->size())
0d75ea88 644 {
60557d06
VK
645 NX_INTERFACE_INFO iface;
646 memset(&iface, 0, sizeof(NX_INTERFACE_INFO));
e95680e5
VK
647 _sntprintf(iface.name, MAX_DB_STRING, _T("vrrp.%u.%u.%d"), router->getId(), router->getIfIndex(), j);
648 memcpy(iface.macAddr, router->getVirtualMacAddr(), MAC_ADDR_LENGTH);
649 iface.ipAddr = vip;
650 iface.ipNetMask = netmask;
98762401 651 ifList->add(&iface);
e95680e5 652 DbgPrintf(6, _T("Node::addVrrpInterfaces(node=%s [%d]): added interface %s"), m_name, (int)m_id, iface.name);
0d75ea88
VK
653 }
654 }
655 }
656 }
657 }
c42b4551 658 unlockProperties();
0d75ea88
VK
659}
660
7946bd7c 661/**
71ea7674
VK
662 * Find interface by index and/or IP subnet. Interface IP considered matching
663 * if it is from same IP subnet as hostAddr or if hostAddr set to INADDR_ANY.
664 *
665 * @param ifIndex interface index to match or INVALID_INDEX to select first matching interface
666 * @param hostAddr IP address to match or INADDR_ANY to select first matching interface
667 * @return pointer to interface object or NULL if appropriate interface couldn't be found
7946bd7c 668 */
c75e9ee4 669Interface *Node::findInterfaceByIndex(UINT32 ifIndex)
5039dede 670{
967893bb 671 UINT32 i;
5039dede
AK
672 Interface *pInterface;
673
674 LockChildList(FALSE);
675 for(i = 0; i < m_dwChildCount; i++)
c42b4551 676 if (m_pChildList[i]->getObjectClass() == OBJECT_INTERFACE)
5039dede
AK
677 {
678 pInterface = (Interface *)m_pChildList[i];
c75e9ee4 679 if (pInterface->getIfIndex() == ifIndex)
5039dede 680 {
c75e9ee4
VK
681 UnlockChildList();
682 return pInterface;
5039dede
AK
683 }
684 }
685 UnlockChildList();
686 return NULL;
687}
688
7946bd7c
VK
689/**
690 * Find interface by name or description
691 * Returns pointer to interface object or NULL if appropriate interface couldn't be found
692 */
c75e9ee4 693Interface *Node::findInterfaceByName(const TCHAR *name)
630e15d6 694{
967893bb 695 UINT32 i;
630e15d6
VK
696 Interface *pInterface;
697
698 LockChildList(FALSE);
699 for(i = 0; i < m_dwChildCount; i++)
c42b4551 700 if (m_pChildList[i]->getObjectClass() == OBJECT_INTERFACE)
630e15d6
VK
701 {
702 pInterface = (Interface *)m_pChildList[i];
c42b4551 703 if (!_tcsicmp(pInterface->getName(), name) || !_tcsicmp(pInterface->getDescription(), name))
630e15d6
VK
704 {
705 UnlockChildList();
706 return pInterface;
707 }
708 }
709 UnlockChildList();
710 return NULL;
711}
712
7946bd7c
VK
713/**
714 * Find interface by slot/port pair
715 * Returns pointer to interface object or NULL if appropriate interface couldn't be found
716 */
967893bb 717Interface *Node::findInterfaceBySlotAndPort(UINT32 slot, UINT32 port)
3f8c54e7 718{
967893bb 719 UINT32 i;
3f8c54e7
VK
720 Interface *pInterface;
721
3f8c54e7
VK
722 LockChildList(FALSE);
723 for(i = 0; i < m_dwChildCount; i++)
c42b4551 724 if (m_pChildList[i]->getObjectClass() == OBJECT_INTERFACE)
3f8c54e7
VK
725 {
726 pInterface = (Interface *)m_pChildList[i];
4c16cdc7 727 if (pInterface->isPhysicalPort() && (pInterface->getSlotNumber() == slot) && (pInterface->getPortNumber() == port))
3f8c54e7
VK
728 {
729 UnlockChildList();
730 return pInterface;
731 }
732 }
733 UnlockChildList();
734 return NULL;
735}
736
7946bd7c
VK
737/**
738 * Find interface by MAC address
739 * Returns pointer to interface object or NULL if appropriate interface couldn't be found
740 */
eec253a8
VK
741Interface *Node::findInterfaceByMAC(const BYTE *macAddr)
742{
8f26db67 743 Interface *iface = NULL;
eec253a8 744 LockChildList(FALSE);
8f26db67 745 for(UINT32 i = 0; i < m_dwChildCount; i++)
c42b4551 746 if (m_pChildList[i]->getObjectClass() == OBJECT_INTERFACE)
eec253a8 747 {
8f26db67 748 if (!memcmp(((Interface *)m_pChildList[i])->getMacAddr(), macAddr, MAC_ADDR_LENGTH))
eec253a8 749 {
8f26db67
VK
750 iface = (Interface *)m_pChildList[i];
751 break;
eec253a8
VK
752 }
753 }
754 UnlockChildList();
8f26db67 755 return iface;
eec253a8
VK
756}
757
7946bd7c
VK
758/**
759 * Find interface by IP address
760 * Returns pointer to interface object or NULL if appropriate interface couldn't be found
761 */
c75e9ee4 762Interface *Node::findInterfaceByIP(const InetAddress& addr)
eec253a8 763{
967893bb 764 UINT32 i;
eec253a8
VK
765 Interface *pInterface;
766
c75e9ee4 767 if (!addr.isValid())
eec253a8
VK
768 return NULL;
769
770 LockChildList(FALSE);
771 for(i = 0; i < m_dwChildCount; i++)
c42b4551 772 if (m_pChildList[i]->getObjectClass() == OBJECT_INTERFACE)
eec253a8
VK
773 {
774 pInterface = (Interface *)m_pChildList[i];
c75e9ee4 775 if (pInterface->getIpAddress().equals(addr))
eec253a8
VK
776 {
777 UnlockChildList();
778 return pInterface;
779 }
780 }
781 UnlockChildList();
782 return NULL;
783}
784
7946bd7c
VK
785/**
786 * Find interface by bridge port number
787 */
967893bb 788Interface *Node::findBridgePort(UINT32 bridgePortNumber)
eec253a8 789{
967893bb 790 UINT32 i;
eec253a8
VK
791 Interface *pInterface;
792
793 LockChildList(FALSE);
794 for(i = 0; i < m_dwChildCount; i++)
c42b4551 795 if (m_pChildList[i]->getObjectClass() == OBJECT_INTERFACE)
eec253a8
VK
796 {
797 pInterface = (Interface *)m_pChildList[i];
798 if (pInterface->getBridgePortNumber() == bridgePortNumber)
799 {
800 UnlockChildList();
801 return pInterface;
802 }
803 }
804 UnlockChildList();
805 return NULL;
806}
807
7946bd7c
VK
808/**
809 * Find connection point for node
810 */
75ebb063 811NetObj *Node::findConnectionPoint(UINT32 *localIfId, BYTE *localMacAddr, int *type)
f42b8099 812{
75ebb063 813 NetObj *cp = NULL;
f42b8099 814 LockChildList(FALSE);
967893bb 815 for(UINT32 i = 0; i < m_dwChildCount; i++)
c42b4551 816 if (m_pChildList[i]->getObjectClass() == OBJECT_INTERFACE)
f42b8099
VK
817 {
818 Interface *iface = (Interface *)m_pChildList[i];
75ebb063 819 cp = FindInterfaceConnectionPoint(iface->getMacAddr(), type);
f42b8099 820 if (cp != NULL)
06a93345 821 {
c42b4551 822 *localIfId = iface->getId();
eec253a8 823 memcpy(localMacAddr, iface->getMacAddr(), MAC_ADDR_LENGTH);
f42b8099 824 break;
06a93345 825 }
f42b8099
VK
826 }
827 UnlockChildList();
828 return cp;
829}
830
a3050773 831/**
8f26db67
VK
832 * Find attached access point by MAC address
833 */
834AccessPoint *Node::findAccessPointByMAC(const BYTE *macAddr)
835{
836 AccessPoint *ap = NULL;
837 LockChildList(FALSE);
838 for(UINT32 i = 0; i < m_dwChildCount; i++)
c42b4551 839 if (m_pChildList[i]->getObjectClass() == OBJECT_ACCESSPOINT)
8f26db67
VK
840 {
841 if (!memcmp(((AccessPoint *)m_pChildList[i])->getMacAddr(), macAddr, MAC_ADDR_LENGTH))
842 {
843 ap = (AccessPoint *)m_pChildList[i];
844 break;
845 }
846 }
847 UnlockChildList();
848 return ap;
849}
850
851/**
852 * Find access point by radio ID (radio interface index)
853 */
854AccessPoint *Node::findAccessPointByRadioId(int rfIndex)
855{
856 AccessPoint *ap = NULL;
857 LockChildList(FALSE);
858 for(UINT32 i = 0; i < m_dwChildCount; i++)
c42b4551 859 if (m_pChildList[i]->getObjectClass() == OBJECT_ACCESSPOINT)
8f26db67
VK
860 {
861 if (((AccessPoint *)m_pChildList[i])->isMyRadio(rfIndex))
862 {
863 ap = (AccessPoint *)m_pChildList[i];
864 break;
865 }
866 }
867 UnlockChildList();
868 return ap;
869}
870
871/**
386f88e3
VK
872 * Find attached access point by BSSID
873 */
874AccessPoint *Node::findAccessPointByBSSID(const BYTE *bssid)
875{
876 AccessPoint *ap = NULL;
877 LockChildList(FALSE);
878 for(UINT32 i = 0; i < m_dwChildCount; i++)
c42b4551 879 if (m_pChildList[i]->getObjectClass() == OBJECT_ACCESSPOINT)
386f88e3
VK
880 {
881 if (!memcmp(((AccessPoint *)m_pChildList[i])->getMacAddr(), bssid, MAC_ADDR_LENGTH) ||
882 ((AccessPoint *)m_pChildList[i])->isMyRadio(bssid))
883 {
884 ap = (AccessPoint *)m_pChildList[i];
885 break;
886 }
887 }
888 UnlockChildList();
889 return ap;
890}
891
892/**
a3050773
VK
893 * Check if given IP address is one of node's interfaces
894 */
c75e9ee4 895bool Node::isMyIP(const InetAddress& addr)
5039dede 896{
967893bb 897 UINT32 i;
5039dede
AK
898
899 LockChildList(FALSE);
900 for(i = 0; i < m_dwChildCount; i++)
c42b4551 901 if (m_pChildList[i]->getObjectClass() == OBJECT_INTERFACE)
5039dede 902 {
c75e9ee4 903 if (((Interface *)m_pChildList[i])->getIpAddress().equals(addr))
5039dede
AK
904 {
905 UnlockChildList();
c75e9ee4 906 return true;
5039dede
AK
907 }
908 }
909 UnlockChildList();
c75e9ee4 910 return false;
5039dede
AK
911}
912
a3050773 913/**
e95680e5
VK
914 * Create new interface - convenience wrapper
915 */
bf2ad753 916Interface *Node::createNewInterface(const InetAddress& ipAddr, BYTE *macAddr)
e95680e5
VK
917{
918 NX_INTERFACE_INFO info;
919 memset(&info, 0, sizeof(info));
bf2ad753
VK
920 info.ipAddr = ipAddr.getAddressV4();
921 info.ipNetMask = 0xFFFFFFFF << (32 - ipAddr.getMaskBits());
e95680e5
VK
922 if (macAddr != NULL)
923 memcpy(info.macAddr, macAddr, MAC_ADDR_LENGTH);
924 return createNewInterface(&info, false);
925}
926
927/**
a3050773
VK
928 * Create new interface
929 */
e95680e5 930Interface *Node::createNewInterface(NX_INTERFACE_INFO *info, bool manuallyCreated)
5039dede
AK
931{
932 Interface *pInterface;
933 Subnet *pSubnet = NULL;
934 Cluster *pCluster;
e95680e5 935 bool bSyntheticMask = false;
5039dede 936
bf2ad753
VK
937 DbgPrintf(5, _T("Node::createNewInterface(%08X/%08X, %s, %d, %d, bp=%d, slot=%d, port=%d) called for node %s [%d]"),
938// (const TCHAR *)info->ipAddr.toString(), info->ipAddr.getMaskBits(), info->name, info->index, info->type, info->bridgePort, info->slot, info->port, m_name, m_id);
939 info->ipAddr, info->ipNetMask, info->name, info->index, info->type, info->bridgePort, info->slot, info->port, m_name, m_id);
5039dede
AK
940
941 // Find subnet to place interface object to
e95680e5 942 if ((info->ipAddr != 0) && (info->type != IFTYPE_SOFTWARE_LOOPBACK) && ((info->ipAddr & 0xFF000000) != 0x7F000000))
5039dede 943 {
7c521895 944 pCluster = getMyCluster();
e95680e5
VK
945 bool addToSubnet = (pCluster != NULL) ? !pCluster->isSyncAddr(info->ipAddr) : true;
946 DbgPrintf(5, _T("Node::createNewInterface: node=%s [%d] cluster=%s [%d] add=%s"),
c42b4551 947 m_name, m_id, (pCluster != NULL) ? pCluster->getName() : _T("(null)"),
e95680e5
VK
948 (pCluster != NULL) ? pCluster->getId() : 0, addToSubnet ? _T("yes") : _T("no"));
949 if (addToSubnet)
5039dede 950 {
e95680e5 951 pSubnet = FindSubnetForNode(m_zoneId, info->ipAddr);
5039dede
AK
952 if (pSubnet == NULL)
953 {
954 // Check if netmask is 0 (detect), and if yes, create
955 // new subnet with class mask
e95680e5 956 if (info->ipNetMask == 0)
5039dede 957 {
53edf4dd 958 bSyntheticMask = true;
e95680e5 959 if (info->ipAddr < 0xE0000000)
5039dede 960 {
e95680e5 961 info->ipNetMask = 0xFFFFFF00; // Class A, B or C
5039dede
AK
962 }
963 else
964 {
965 TCHAR szBuffer[16];
966
967 // Multicast address??
4d2c3a54 968 DbgPrintf(2, _T("Attempt to create interface object with multicast address %s"),
e95680e5 969 IpToStr(info->ipAddr, szBuffer));
5039dede
AK
970 }
971 }
972
973 // Create new subnet object
6449475d 974 // Ignore mask 255.255.255.255 - some point-to-point interfaces can have such mask
674164eb 975 // Ignore mask 255.255.255.254 - it's invalid
e95680e5 976 if ((info->ipAddr < 0xE0000000) && (info->ipNetMask != 0xFFFFFFFF) && (info->ipNetMask != 0xFFFFFFFE))
5039dede 977 {
c75e9ee4 978 pSubnet = createSubnet(InetAddress(info->ipAddr, info->ipNetMask), bSyntheticMask);
5039dede
AK
979 }
980 }
981 else
982 {
983 // Set correct netmask if we was asked for it
e95680e5 984 if (info->ipNetMask == 0)
5039dede 985 {
c75e9ee4 986 info->ipNetMask = 0xFFFFFFFF << (32 - pSubnet->getIpAddress().getMaskBits());
eec253a8 987 bSyntheticMask = pSubnet->isSyntheticMask();
5039dede
AK
988 }
989 }
990 }
991 }
992
993 // Create interface object
e95680e5 994 if (info->name[0] != 0)
d66236c0 995 pInterface = new Interface(info->name, (info->description[0] != 0) ? info->description : info->name,
c75e9ee4 996 info->index, InetAddress(info->ipAddr, info->ipNetMask), info->type, m_zoneId);
5039dede 997 else
c75e9ee4 998 pInterface = new Interface(InetAddress(info->ipAddr, info->ipNetMask), m_zoneId, bSyntheticMask);
e95680e5
VK
999 pInterface->setMacAddr(info->macAddr);
1000 pInterface->setBridgePortNumber(info->bridgePort);
1001 pInterface->setSlotNumber(info->slot);
1002 pInterface->setPortNumber(info->port);
1003 pInterface->setPhysicalPortFlag(info->isPhysicalPort);
9214177b 1004 pInterface->setManualCreationFlag(manuallyCreated);
e95680e5 1005 pInterface->setSystemFlag(info->isSystem);
5039dede
AK
1006
1007 // Insert to objects' list and generate event
1008 NetObjInsert(pInterface, TRUE);
eec253a8 1009 addInterface(pInterface);
01152a54 1010 if (!m_isHidden)
478d4ff4 1011 pInterface->unhide();
01152a54 1012 if (!pInterface->isSystem())
c75e9ee4
VK
1013 PostEvent(EVENT_INTERFACE_ADDED, m_id, "dsAdd", pInterface->getId(),
1014 pInterface->getName(), &pInterface->getIpAddress(),
1015 pInterface->getIpAddress().getMaskBits(), pInterface->getIfIndex());
5039dede
AK
1016
1017 // Bind node to appropriate subnet
1018 if (pSubnet != NULL)
1019 {
386d7c46 1020 pSubnet->addNode(this);
4d2c3a54 1021
5039dede 1022 // Check if subnet mask is correct on interface
c75e9ee4 1023 if ((pSubnet->getIpAddress().getMaskBits() != pInterface->getIpAddress().getMaskBits()) && !pSubnet->isSyntheticMask() && (info->ipNetMask != 0xFFFFFFFF))
5039dede 1024 {
c75e9ee4 1025 PostEvent(EVENT_INCORRECT_NETMASK, m_id, "idsdd", pInterface->getId(),
c42b4551 1026 pInterface->getIfIndex(), pInterface->getName(),
c75e9ee4 1027 pInterface->getIpAddress().getMaskBits(), pSubnet->getIpAddress().getMaskBits());
5039dede
AK
1028 }
1029 }
9214177b
VK
1030
1031 return pInterface;
5039dede
AK
1032}
1033
a3050773
VK
1034/**
1035 * Delete interface from node
1036 */
eec253a8 1037void Node::deleteInterface(Interface *pInterface)
5039dede 1038{
967893bb 1039 UINT32 i;
5039dede 1040
c42b4551 1041 DbgPrintf(5, _T("Node::deleteInterface(node=%s [%d], interface=%s [%d])"), m_name, m_id, pInterface->getName(), pInterface->getId());
dfb38baf 1042
5039dede 1043 // Check if we should unlink node from interface's subnet
c75e9ee4 1044 if (pInterface->getIpAddress().isValidUnicast() && !pInterface->isExcludedFromTopology())
5039dede
AK
1045 {
1046 BOOL bUnlink = TRUE;
1047
1048 LockChildList(FALSE);
1049 for(i = 0; i < m_dwChildCount; i++)
c42b4551 1050 if (m_pChildList[i]->getObjectClass() == OBJECT_INTERFACE)
5039dede 1051 if (m_pChildList[i] != pInterface)
c75e9ee4 1052 if (((Interface *)m_pChildList[i])->getIpAddress().getSubnetAddress().equals(pInterface->getIpAddress().getSubnetAddress()))
5039dede
AK
1053 {
1054 bUnlink = FALSE;
1055 break;
1056 }
1057 UnlockChildList();
4d2c3a54 1058
5039dede
AK
1059 if (bUnlink)
1060 {
1061 // Last interface in subnet, should unlink node
c75e9ee4 1062 Subnet *pSubnet = FindSubnetByIP(m_zoneId, pInterface->getIpAddress().getSubnetAddress());
5039dede
AK
1063 if (pSubnet != NULL)
1064 {
1065 DeleteParent(pSubnet);
1066 pSubnet->DeleteChild(this);
1067 }
dfb38baf 1068 DbgPrintf(5, _T("Node::deleteInterface(node=%s [%d], interface=%s [%d]): unlinked from subnet %s [%d]"),
c42b4551
VK
1069 m_name, m_id, pInterface->getName(), pInterface->getId(),
1070 (pSubnet != NULL) ? pSubnet->getName() : _T("(null)"),
1071 (pSubnet != NULL) ? pSubnet->getId() : 0);
5039dede
AK
1072 }
1073 }
27de5dab 1074 pInterface->deleteObject();
5039dede
AK
1075}
1076
7946bd7c
VK
1077/**
1078 * Calculate node status based on child objects status
1079 */
27f9598d 1080void Node::calculateCompoundStatus(BOOL bForcedRecalc)
5039dede
AK
1081{
1082 int iOldStatus = m_iStatus;
967893bb 1083 static UINT32 dwEventCodes[] = { EVENT_NODE_NORMAL, EVENT_NODE_WARNING,
67c45b4d 1084 EVENT_NODE_MINOR, EVENT_NODE_MAJOR, EVENT_NODE_CRITICAL,
5039dede
AK
1085 EVENT_NODE_UNKNOWN, EVENT_NODE_UNMANAGED };
1086
f94d5259 1087 DataCollectionTarget::calculateCompoundStatus(bForcedRecalc);
5039dede 1088 if (m_iStatus != iOldStatus)
c42b4551 1089 PostEvent(dwEventCodes[m_iStatus], m_id, "d", iOldStatus);
5039dede
AK
1090}
1091
a5ee7b3b
VK
1092/**
1093 * Perform status poll on node
1094 */
967893bb 1095void Node::statusPoll(ClientSession *pSession, UINT32 dwRqId, int nPoller)
5039dede 1096{
46117060
VK
1097 if (m_dwDynamicFlags & NDF_DELETE_IN_PROGRESS)
1098 {
1099 if (dwRqId == 0)
1100 m_dwDynamicFlags &= ~NDF_QUEUED_FOR_STATUS_POLL;
1101 return;
1102 }
1103
967893bb 1104 UINT32 i, dwPollListSize, dwOldFlags = m_dwFlags;
5039dede
AK
1105 NetObj *pPollerNode = NULL, **ppPollList;
1106 BOOL bAllDown;
5039dede
AK
1107 SNMP_Transport *pTransport;
1108 Cluster *pCluster;
1109 time_t tNow, tExpire;
1110
c1482463 1111 Queue *pQueue = new Queue; // Delayed event queue
35f836fe 1112 SetPollerInfo(nPoller, _T("wait for lock"));
7c521895 1113 pollerLock();
c59466d2 1114 m_pollRequestor = pSession;
c42b4551
VK
1115 sendPollerMsg(dwRqId, _T("Starting status poll for node %s\r\n"), m_name);
1116 DbgPrintf(5, _T("Starting status poll for node %s (ID: %d)"), m_name, m_id);
5039dede
AK
1117
1118 // Read capability expiration time and current time
1119 tExpire = (time_t)ConfigReadULong(_T("CapabilityExpirationTime"), 604800);
1120 tNow = time(NULL);
1121
5039dede 1122restart_agent_check:
69a4eaa6 1123 if (g_flags & AF_RESOLVE_IP_FOR_EACH_STATUS_POLL)
385b1f20 1124 {
1125 updatePrimaryIpAddr();
1126 }
1127
1128 // Check SNMP agent connectivity
c75e9ee4 1129 if ((m_dwFlags & NF_IS_SNMP) && (!(m_dwFlags & NF_DISABLE_SNMP)) && m_ipAddress.isValidUnicast())
5039dede
AK
1130 {
1131 TCHAR szBuffer[256];
967893bb 1132 UINT32 dwResult;
5039dede 1133
c42b4551 1134 DbgPrintf(6, _T("StatusPoll(%s): check SNMP"), m_name);
cd9f247e 1135 pTransport = createSnmpTransport();
61b359bc 1136 if (pTransport != NULL)
5039dede 1137 {
61b359bc
VK
1138 SetPollerInfo(nPoller, _T("check SNMP"));
1139 sendPollerMsg(dwRqId, _T("Checking SNMP agent connectivity\r\n"));
9eeed592
VK
1140 const TCHAR *testOid = m_customAttributes.get(_T("snmp.testoid"));
1141 if (testOid == NULL)
1142 {
1143 testOid = _T(".1.3.6.1.2.1.1.2.0");
1144 }
1145 dwResult = SnmpGet(m_snmpVersion, pTransport, testOid, NULL, 0, szBuffer, sizeof(szBuffer), 0);
61b359bc 1146 if ((dwResult == SNMP_ERR_SUCCESS) || (dwResult == SNMP_ERR_NO_OBJECT))
5039dede 1147 {
61b359bc 1148 if (m_dwDynamicFlags & NDF_SNMP_UNREACHABLE)
5039dede 1149 {
5039dede 1150 m_dwDynamicFlags &= ~NDF_SNMP_UNREACHABLE;
c42b4551 1151 PostEventEx(pQueue, EVENT_SNMP_OK, m_id, NULL);
61b359bc 1152 sendPollerMsg(dwRqId, POLLER_INFO _T("Connectivity with SNMP agent restored\r\n"));
5039dede 1153 }
aa379fa0
VK
1154
1155 // Update authoritative engine data for SNMPv3
1156 if ((pTransport->getSnmpVersion() == SNMP_VERSION_3) && (pTransport->getAuthoritativeEngine() != NULL))
1157 {
1158 lockProperties();
1159 m_snmpSecurity->setAuthoritativeEngine(*pTransport->getAuthoritativeEngine());
1160 unlockProperties();
1161 }
5039dede
AK
1162 }
1163 else
1164 {
61b359bc
VK
1165 sendPollerMsg(dwRqId, POLLER_ERROR _T("SNMP agent unreachable\r\n"));
1166 if (m_dwDynamicFlags & NDF_SNMP_UNREACHABLE)
1167 {
1168 if ((tNow > m_failTimeSNMP + tExpire) &&
1169 (!(m_dwDynamicFlags & NDF_UNREACHABLE)))
1170 {
1171 m_dwFlags &= ~NF_IS_SNMP;
1172 m_dwDynamicFlags &= ~NDF_SNMP_UNREACHABLE;
1173 m_szObjectId[0] = 0;
1174 sendPollerMsg(dwRqId, POLLER_WARNING _T("Attribute isSNMP set to FALSE\r\n"));
1175 }
1176 }
1177 else
1178 {
1179 m_dwDynamicFlags |= NDF_SNMP_UNREACHABLE;
c42b4551 1180 PostEventEx(pQueue, EVENT_SNMP_FAIL, m_id, NULL);
61b359bc
VK
1181 m_failTimeSNMP = tNow;
1182 }
5039dede 1183 }
61b359bc
VK
1184 delete pTransport;
1185 }
1186 else
1187 {
c42b4551 1188 DbgPrintf(6, _T("StatusPoll(%s): cannot create SNMP transport"), m_name);
5039dede 1189 }
c42b4551 1190 DbgPrintf(6, _T("StatusPoll(%s): SNMP check finished"), m_name);
5039dede
AK
1191 }
1192
1193 // Check native agent connectivity
c75e9ee4 1194 if ((m_dwFlags & NF_IS_NATIVE_AGENT) && (!(m_dwFlags & NF_DISABLE_NXCP)) && m_ipAddress.isValidUnicast())
5039dede 1195 {
c42b4551 1196 DbgPrintf(6, _T("StatusPoll(%s): checking agent"), m_name);
35f836fe 1197 SetPollerInfo(nPoller, _T("check agent"));
21c9acce 1198 sendPollerMsg(dwRqId, _T("Checking NetXMS agent connectivity\r\n"));
c3acd0f6 1199
967893bb 1200 UINT32 error, socketError;
a4569c4d
VK
1201 agentLock();
1202 if (connectToAgent(&error, &socketError))
5039dede 1203 {
c42b4551 1204 DbgPrintf(7, _T("StatusPoll(%s): connected to agent"), m_name);
5039dede
AK
1205 if (m_dwDynamicFlags & NDF_AGENT_UNREACHABLE)
1206 {
1207 m_dwDynamicFlags &= ~NDF_AGENT_UNREACHABLE;
c42b4551 1208 PostEventEx(pQueue, EVENT_AGENT_OK, m_id, NULL);
21c9acce 1209 sendPollerMsg(dwRqId, POLLER_INFO _T("Connectivity with NetXMS agent restored\r\n"));
5039dede 1210 }
5039dede
AK
1211 }
1212 else
1213 {
c42b4551 1214 DbgPrintf(6, _T("StatusPoll(%s): agent unreachable, error=%d, socketError=%d"), m_name, (int)error, (int)socketError);
21c9acce 1215 sendPollerMsg(dwRqId, POLLER_ERROR _T("NetXMS agent unreachable\r\n"));
5039dede
AK
1216 if (m_dwDynamicFlags & NDF_AGENT_UNREACHABLE)
1217 {
71e4ed3a 1218 if ((tNow > m_failTimeAgent + tExpire) && !(m_dwDynamicFlags & NDF_UNREACHABLE))
5039dede
AK
1219 {
1220 m_dwFlags &= ~NF_IS_NATIVE_AGENT;
1221 m_dwDynamicFlags &= ~NDF_AGENT_UNREACHABLE;
1222 m_szPlatformName[0] = 0;
1223 m_szAgentVersion[0] = 0;
21c9acce 1224 sendPollerMsg(dwRqId, POLLER_WARNING _T("Attribute isNetXMSAgent set to FALSE\r\n"));
5039dede
AK
1225 }
1226 }
1227 else
1228 {
1229 m_dwDynamicFlags |= NDF_AGENT_UNREACHABLE;
c42b4551 1230 PostEventEx(pQueue, EVENT_AGENT_FAIL, m_id, NULL);
8573e935 1231 m_failTimeAgent = tNow;
0ab347c0 1232 //cancel file monitoring locally(on agent it is canceled if agent have fallen)
c42b4551 1233 g_monitoringList.removeDisconectedNode(m_id);
5039dede
AK
1234 }
1235 }
a4569c4d 1236 agentUnlock();
c42b4551 1237 DbgPrintf(7, _T("StatusPoll(%s): agent check finished"), m_name);
5039dede
AK
1238 }
1239
35f836fe 1240 SetPollerInfo(nPoller, _T("prepare polling list"));
5039dede
AK
1241
1242 // Find service poller node object
c42b4551 1243 lockProperties();
9208c84b 1244 if (m_pollerNode != 0)
5039dede 1245 {
9208c84b 1246 UINT32 id = m_pollerNode;
c42b4551 1247 unlockProperties();
f3218755 1248 pPollerNode = FindObjectById(id);
5039dede
AK
1249 if (pPollerNode != NULL)
1250 {
c42b4551 1251 if (pPollerNode->getObjectClass() != OBJECT_NODE)
5039dede
AK
1252 pPollerNode = NULL;
1253 }
1254 }
f3218755
VK
1255 else
1256 {
c42b4551 1257 unlockProperties();
f3218755 1258 }
5039dede
AK
1259
1260 // If nothing found, use management server
1261 if (pPollerNode == NULL)
1262 {
1263 pPollerNode = FindObjectById(g_dwMgmtNode);
1264 if (pPollerNode != NULL)
21c9acce 1265 pPollerNode->incRefCount();
5039dede
AK
1266 }
1267 else
1268 {
21c9acce 1269 pPollerNode->incRefCount();
5039dede
AK
1270 }
1271
1272 // Create polling list
1273 ppPollList = (NetObj **)malloc(sizeof(NetObj *) * m_dwChildCount);
1274 LockChildList(FALSE);
1275 for(i = 0, dwPollListSize = 0; i < m_dwChildCount; i++)
1276 if (m_pChildList[i]->Status() != STATUS_UNMANAGED)
1277 {
21c9acce 1278 m_pChildList[i]->incRefCount();
5039dede
AK
1279 ppPollList[dwPollListSize++] = m_pChildList[i];
1280 }
1281 UnlockChildList();
1282
1283 // Poll interfaces and services
35f836fe 1284 SetPollerInfo(nPoller, _T("child poll"));
c42b4551 1285 DbgPrintf(7, _T("StatusPoll(%s): starting child object poll"), m_name);
7c521895 1286 pCluster = getMyCluster();
cd9f247e 1287 pTransport = createSnmpTransport();
5039dede
AK
1288 for(i = 0; i < dwPollListSize; i++)
1289 {
c42b4551 1290 switch(ppPollList[i]->getObjectClass())
5039dede
AK
1291 {
1292 case OBJECT_INTERFACE:
c42b4551 1293 DbgPrintf(7, _T("StatusPoll(%s): polling interface %d [%s]"), m_name, ppPollList[i]->getId(), ppPollList[i]->getName());
c59466d2 1294 ((Interface *)ppPollList[i])->statusPoll(pSession, dwRqId, pQueue,
c75e9ee4 1295 (pCluster != NULL) ? pCluster->isSyncAddr(((Interface *)ppPollList[i])->getIpAddress()) : FALSE,
9208c84b 1296 pTransport, m_icmpProxy);
5039dede
AK
1297 break;
1298 case OBJECT_NETWORKSERVICE:
c42b4551 1299 DbgPrintf(7, _T("StatusPoll(%s): polling network service %d [%s]"), m_name, ppPollList[i]->getId(), ppPollList[i]->getName());
c59466d2 1300 ((NetworkService *)ppPollList[i])->statusPoll(pSession, dwRqId,
5039dede
AK
1301 (Node *)pPollerNode, pQueue);
1302 break;
56d5289b 1303 case OBJECT_ACCESSPOINT:
c42b4551 1304 DbgPrintf(7, _T("StatusPoll(%s): polling access point %d [%s]"), m_name, ppPollList[i]->getId(), ppPollList[i]->getName());
56d5289b
VK
1305 ((AccessPoint *)ppPollList[i])->statusPoll(pSession, dwRqId, pQueue, this);
1306 break;
5039dede 1307 default:
c42b4551 1308 DbgPrintf(7, _T("StatusPoll(%s): skipping object %d [%s] class %d"), m_name, ppPollList[i]->getId(), ppPollList[i]->getName(), ppPollList[i]->getObjectClass());
5039dede
AK
1309 break;
1310 }
21c9acce 1311 ppPollList[i]->decRefCount();
5039dede
AK
1312 }
1313 delete pTransport;
1314 safe_free(ppPollList);
c42b4551 1315 DbgPrintf(7, _T("StatusPoll(%s): finished child object poll"), m_name);
5039dede
AK
1316
1317 // Check if entire node is down
15d6f8c9 1318 // This check is disabled for nodes without IP address
c75e9ee4 1319 if (m_ipAddress.isValidUnicast())
5039dede 1320 {
15d6f8c9
VK
1321 LockChildList(FALSE);
1322 if (m_dwChildCount > 0)
1323 {
1324 for(i = 0, bAllDown = TRUE; i < m_dwChildCount; i++)
c42b4551 1325 if ((m_pChildList[i]->getObjectClass() == OBJECT_INTERFACE) &&
15d6f8c9
VK
1326 (m_pChildList[i]->Status() != STATUS_CRITICAL) &&
1327 (m_pChildList[i]->Status() != STATUS_UNKNOWN) &&
1328 (m_pChildList[i]->Status() != STATUS_UNMANAGED) &&
1329 (m_pChildList[i]->Status() != STATUS_DISABLED))
1330 {
1331 bAllDown = FALSE;
1332 break;
1333 }
1334 }
1335 else
1336 {
1337 bAllDown = FALSE;
1338 }
1339 UnlockChildList();
1340 if (bAllDown && (m_dwFlags & NF_IS_NATIVE_AGENT) &&
1341 (!(m_dwFlags & NF_DISABLE_NXCP)))
1342 if (!(m_dwDynamicFlags & NDF_AGENT_UNREACHABLE))
1343 bAllDown = FALSE;
1344 if (bAllDown && (m_dwFlags & NF_IS_SNMP) &&
1345 (!(m_dwFlags & NF_DISABLE_SNMP)))
1346 if (!(m_dwDynamicFlags & NDF_SNMP_UNREACHABLE))
1347 bAllDown = FALSE;
c1482463 1348
c42b4551 1349 DbgPrintf(6, _T("StatusPoll(%s): bAllDown=%s, dynFlags=0x%08X"), m_name, bAllDown ? _T("true") : _T("false"), m_dwDynamicFlags);
15d6f8c9
VK
1350 if (bAllDown)
1351 {
1352 if (!(m_dwDynamicFlags & NDF_UNREACHABLE))
1353 {
1354 m_dwDynamicFlags |= NDF_UNREACHABLE;
8573e935 1355 m_downSince = time(NULL);
7946bd7c 1356 SetPollerInfo(nPoller, _T("check network path"));
c1482463
VK
1357 if (checkNetworkPath(dwRqId))
1358 {
1359 m_dwDynamicFlags |= NDF_NETWORK_PATH_PROBLEM;
1360
1361 // Set interfaces and network services to UNKNOWN state
1362 LockChildList(FALSE);
1363 for(i = 0, bAllDown = TRUE; i < m_dwChildCount; i++)
c42b4551 1364 if (((m_pChildList[i]->getObjectClass() == OBJECT_INTERFACE) || (m_pChildList[i]->getObjectClass() == OBJECT_NETWORKSERVICE)) &&
c1482463
VK
1365 (m_pChildList[i]->Status() == STATUS_CRITICAL))
1366 {
1367 m_pChildList[i]->resetStatus();
1368 }
1369 UnlockChildList();
1370
1371 // Clear delayed event queue
1372 while(1)
1373 {
1374 Event *pEvent = (Event *)pQueue->Get();
1375 if (pEvent == NULL)
1376 break;
1377 delete pEvent;
1378 }
1379 delete_and_null(pQueue);
1380
c42b4551 1381 PostEvent(EVENT_NODE_UNREACHABLE, m_id, NULL);
c1482463
VK
1382 }
1383 else
1384 {
c42b4551 1385 PostEvent(EVENT_NODE_DOWN, m_id, NULL);
c1482463 1386 }
21c9acce 1387 sendPollerMsg(dwRqId, POLLER_ERROR _T("Node is unreachable\r\n"));
15d6f8c9
VK
1388 }
1389 else
1390 {
21c9acce 1391 sendPollerMsg(dwRqId, POLLER_WARNING _T("Node is still unreachable\r\n"));
15d6f8c9
VK
1392 }
1393 }
1394 else
1395 {
8573e935 1396 m_downSince = 0;
15d6f8c9
VK
1397 if (m_dwDynamicFlags & NDF_UNREACHABLE)
1398 {
a26861ad 1399 int reason = (m_dwDynamicFlags & NDF_NETWORK_PATH_PROBLEM) ? 1 : 0;
c1482463 1400 m_dwDynamicFlags &= ~(NDF_UNREACHABLE | NDF_SNMP_UNREACHABLE | NDF_AGENT_UNREACHABLE | NDF_NETWORK_PATH_PROBLEM);
c42b4551 1401 PostEvent(EVENT_NODE_UP, m_id, "d", reason);
21c9acce 1402 sendPollerMsg(dwRqId, POLLER_INFO _T("Node recovered from unreachable state\r\n"));
15d6f8c9
VK
1403 goto restart_agent_check;
1404 }
1405 else
1406 {
21c9acce 1407 sendPollerMsg(dwRqId, POLLER_INFO _T("Node is connected\r\n"));
15d6f8c9
VK
1408 }
1409 }
5039dede 1410 }
5039dede 1411
71e4ed3a
VK
1412 // Get uptime and update boot time
1413 if (!(m_dwDynamicFlags & NDF_UNREACHABLE))
1414 {
1415 TCHAR buffer[MAX_RESULT_LENGTH];
1416 if (getItemFromAgent(_T("System.Uptime"), MAX_RESULT_LENGTH, buffer) == DCE_SUCCESS)
1417 {
1418 m_bootTime = time(NULL) - _tcstol(buffer, NULL, 0);
c42b4551 1419 DbgPrintf(5, _T("StatusPoll(%s [%d]): boot time set to %u from agent"), m_name, m_id, (UINT32)m_bootTime);
71e4ed3a
VK
1420 }
1421 else if (getItemFromSNMP(m_wSNMPPort, _T(".1.3.6.1.2.1.1.3.0"), MAX_RESULT_LENGTH, buffer, SNMP_RAWTYPE_NONE) == DCE_SUCCESS)
1422 {
1423 m_bootTime = time(NULL) - _tcstol(buffer, NULL, 0) / 100; // sysUpTime is in hundredths of a second
c42b4551 1424 DbgPrintf(5, _T("StatusPoll(%s [%d]): boot time set to %u from SNMP"), m_name, m_id, (UINT32)m_bootTime);
71e4ed3a
VK
1425 }
1426 else
1427 {
c42b4551 1428 DbgPrintf(5, _T("StatusPoll(%s [%d]): unable to get system uptime"), m_name, m_id);
71e4ed3a
VK
1429 }
1430 }
1431 else
1432 {
1433 m_bootTime = 0;
1434 }
1435
0ab347c0 1436 // Get agent uptime to check if it was restared
1437 if (!(m_dwDynamicFlags & NDF_UNREACHABLE))
1438 {
1439 TCHAR buffer[MAX_RESULT_LENGTH];
1440 if (getItemFromAgent(_T("Agent.Uptime"), MAX_RESULT_LENGTH, buffer) == DCE_SUCCESS)
1441 {
1442 time_t oldAgentuptime = m_agentUpTime;
1443 m_agentUpTime = _tcstol(buffer, NULL, 0);
1444 if((UINT32)oldAgentuptime > (UINT32)m_agentUpTime)
1445 {
1446 //cancel file monitoring locally(on agent it is canceled if agent have fallen)
c42b4551 1447 g_monitoringList.removeDisconectedNode(m_id);
0ab347c0 1448 }
1449 }
1450 else
1451 {
c42b4551
VK
1452 DbgPrintf(5, _T("StatusPoll(%s [%d]): unable to get agent uptime"), m_name, m_id);
1453 g_monitoringList.removeDisconectedNode(m_id);
0ab347c0 1454 m_agentUpTime = 0;
1455 }
1456 }
1457 else
1458 {
c42b4551 1459 g_monitoringList.removeDisconectedNode(m_id);
0ab347c0 1460 m_agentUpTime = 0;
1461 }
1462
5039dede 1463 // Send delayed events and destroy delayed event queue
c1482463
VK
1464 if (pQueue != NULL)
1465 {
1466 ResendEvents(pQueue);
1467 delete pQueue;
1468 }
a5ee7b3b
VK
1469
1470 // Call hooks in loaded modules
967893bb 1471 for(UINT32 i = 0; i < g_dwNumModules; i++)
a5ee7b3b
VK
1472 {
1473 if (g_pModuleList[i].pfStatusPollHook != NULL)
1474 {
c42b4551 1475 DbgPrintf(5, _T("StatusPoll(%s [%d]): calling hook in module %s"), m_name, m_id, g_pModuleList[i].szName);
a5ee7b3b
VK
1476 g_pModuleList[i].pfStatusPollHook(this, pSession, dwRqId, nPoller);
1477 }
1478 }
4d2c3a54 1479
a5ee7b3b
VK
1480 // Execute hook script
1481 SetPollerInfo(nPoller, _T("hook"));
1482 executeHookScript(_T("StatusPoll"));
1483
35f836fe 1484 SetPollerInfo(nPoller, _T("cleanup"));
5039dede 1485 if (pPollerNode != NULL)
21c9acce 1486 pPollerNode->decRefCount();
5039dede
AK
1487
1488 if (dwOldFlags != m_dwFlags)
1489 {
c42b4551
VK
1490 PostEvent(EVENT_NODE_FLAGS_CHANGED, m_id, "xx", dwOldFlags, m_dwFlags);
1491 lockProperties();
1492 setModified();
1493 unlockProperties();
5039dede
AK
1494 }
1495
27f9598d 1496 calculateCompoundStatus();
8573e935 1497 m_lastStatusPoll = time(NULL);
c42b4551 1498 sendPollerMsg(dwRqId, _T("Finished status poll for node %s\r\n"), m_name);
6fec127d 1499 sendPollerMsg(dwRqId, _T("Node status after poll is %s\r\n"), GetStatusAsText(m_iStatus, true));
c59466d2 1500 m_pollRequestor = NULL;
5039dede
AK
1501 if (dwRqId == 0)
1502 m_dwDynamicFlags &= ~NDF_QUEUED_FOR_STATUS_POLL;
7c521895 1503 pollerUnlock();
c42b4551 1504 DbgPrintf(5, _T("Finished status poll for node %s (ID: %d)"), m_name, m_id);
5039dede
AK
1505}
1506
7946bd7c 1507/**
1babf64d 1508 * Check single element of network path
beae365a 1509 */
967893bb 1510bool Node::checkNetworkPathElement(UINT32 nodeId, const TCHAR *nodeType, bool isProxy, UINT32 dwRqId)
beae365a
VK
1511{
1512 Node *node = (Node *)FindObjectById(nodeId, OBJECT_NODE);
1513 if (node == NULL)
1514 return false;
1515
c42b4551 1516 DbgPrintf(6, _T("Node::checkNetworkPathElement(%s [%d]): found %s: %s [%d]"), m_name, m_id, nodeType, node->getName(), node->getId());
beae365a
VK
1517 if (node->isDown())
1518 {
1519 DbgPrintf(5, _T("Node::checkNetworkPathElement(%s [%d]): %s %s [%d] is down"),
c42b4551
VK
1520 m_name, m_id, nodeType, node->getName(), node->getId());
1521 sendPollerMsg(dwRqId, POLLER_WARNING _T(" %s %s is down\r\n"), nodeType, node->getName());
beae365a
VK
1522 return true;
1523 }
66d15dff
VK
1524 if (isProxy && node->isNativeAgent() && (node->getRuntimeFlags() & NDF_AGENT_UNREACHABLE))
1525 {
1526 DbgPrintf(5, _T("Node::checkNetworkPathElement(%s [%d]): agent on %s %s [%d] is down"),
c42b4551
VK
1527 m_name, m_id, nodeType, node->getName(), node->getId());
1528 sendPollerMsg(dwRqId, POLLER_WARNING _T(" Agent on %s %s is down\r\n"), nodeType, node->getName());
66d15dff
VK
1529 return true;
1530 }
8573e935 1531 if (node->m_lastStatusPoll < time(NULL) - 1)
beae365a
VK
1532 {
1533 DbgPrintf(6, _T("Node::checkNetworkPathElement(%s [%d]): forced status poll on node %s [%d]"),
c42b4551 1534 m_name, m_id, node->getName(), node->getId());
beae365a
VK
1535 node->statusPoll(NULL, 0, 0);
1536 if (node->isDown())
1537 {
1538 DbgPrintf(5, _T("Node::checkNetworkPathElement(%s [%d]): %s %s [%d] is down"),
c42b4551
VK
1539 m_name, m_id, nodeType, node->getName(), node->getId());
1540 sendPollerMsg(dwRqId, POLLER_WARNING _T(" %s %s is down\r\n"), nodeType, node->getName());
beae365a
VK
1541 return true;
1542 }
66d15dff
VK
1543 if (isProxy && node->isNativeAgent() && (node->getRuntimeFlags() & NDF_AGENT_UNREACHABLE))
1544 {
1545 DbgPrintf(5, _T("Node::checkNetworkPathElement(%s [%d]): agent on %s %s [%d] is down"),
c42b4551
VK
1546 m_name, m_id, nodeType, node->getName(), node->getId());
1547 sendPollerMsg(dwRqId, POLLER_WARNING _T(" Agent on %s %s is down\r\n"), nodeType, node->getName());
66d15dff
VK
1548 return true;
1549 }
beae365a
VK
1550 }
1551 return false;
1552}
1553
1554/**
7946bd7c 1555 * Check network path between node and management server to detect possible intermediate node failure
c1482463
VK
1556 *
1557 * @return true if network path problems found
7946bd7c 1558 */
967893bb 1559bool Node::checkNetworkPath(UINT32 dwRqId)
7946bd7c 1560{
67c45b4d
VK
1561 time_t now = time(NULL);
1562
beae365a
VK
1563 // Check proxy node(s)
1564 if (IsZoningEnabled() && (m_zoneId != 0))
1565 {
1566 Zone *zone = (Zone *)g_idxZoneByGUID.get(m_zoneId);
1567 if ((zone != NULL) && ((zone->getAgentProxy() != 0) || (zone->getSnmpProxy() != 0) || (zone->getIcmpProxy() != 0)))
1568 {
1569 bool allProxyDown = true;
1570 if (zone->getIcmpProxy() != 0)
66d15dff 1571 allProxyDown = checkNetworkPathElement(zone->getIcmpProxy(), _T("ICMP proxy"), true, dwRqId);
beae365a 1572 if (allProxyDown && (zone->getSnmpProxy() != 0) && (zone->getSnmpProxy() != zone->getIcmpProxy()))
66d15dff 1573 allProxyDown = checkNetworkPathElement(zone->getSnmpProxy(), _T("SNMP proxy"), true, dwRqId);
beae365a 1574 if (allProxyDown && (zone->getAgentProxy() != 0) && (zone->getAgentProxy() != zone->getIcmpProxy()) && (zone->getAgentProxy() != zone->getSnmpProxy()))
66d15dff 1575 allProxyDown = checkNetworkPathElement(zone->getAgentProxy(), _T("agent proxy"), true, dwRqId);
beae365a
VK
1576 if (allProxyDown)
1577 return true;
1578 }
1579 }
1580
67c45b4d
VK
1581 // Check directly connected switch
1582 sendPollerMsg(dwRqId, _T("Checking ethernet connectivity...\r\n"));
c75e9ee4 1583 Interface *iface = findInterfaceByIP(m_ipAddress);
664b42a4 1584 if ((iface != NULL) && (iface->getPeerNodeId() != 0))
67c45b4d 1585 {
c42b4551 1586 DbgPrintf(6, _T("Node::checkNetworkPath(%s [%d]): found interface object for primary IP: %s [%d]"), m_name, m_id, iface->getName(), iface->getId());
66d15dff 1587 if (checkNetworkPathElement(iface->getPeerNodeId(), _T("upstream switch"), false, dwRqId))
beae365a 1588 return true;
67c45b4d 1589 }
664b42a4
VK
1590 else
1591 {
c42b4551 1592 DbgPrintf(5, _T("Node::checkNetworkPath(%s [%d]): cannot find interface object for primary IP"), m_name, m_id);
664b42a4 1593 }
67c45b4d 1594
7946bd7c
VK
1595 Node *mgmtNode = (Node *)FindObjectById(g_dwMgmtNode);
1596 if (mgmtNode == NULL)
1597 {
c42b4551 1598 DbgPrintf(5, _T("Node::checkNetworkPath(%s [%d]): cannot find management node"), m_name, m_id);
c1482463 1599 return false;
7946bd7c 1600 }
5039dede 1601
7946bd7c
VK
1602 NetworkPath *trace = TraceRoute(mgmtNode, this);
1603 if (trace == NULL)
1604 {
c42b4551 1605 DbgPrintf(5, _T("Node::checkNetworkPath(%s [%d]): trace not available"), m_name, m_id);
c1482463 1606 return false;
7946bd7c
VK
1607 }
1608 DbgPrintf(5, _T("Node::checkNetworkPath(%s [%d]): trace available, %d hops, %s"),
c42b4551 1609 m_name, m_id, trace->getHopCount(), trace->isComplete() ? _T("complete") : _T("incomplete"));
7946bd7c
VK
1610
1611 // We will do path check in two passes
1612 // If unreachable intermediate node will be found on first pass,
1613 // then method will just return true. Otherwise, we will do
1614 // second pass, this time forcing status poll on each node in the path.
21c9acce 1615 sendPollerMsg(dwRqId, _T("Checking network path...\r\n"));
7946bd7c
VK
1616 bool secondPass = false;
1617 bool pathProblemFound = false;
1618restart:
1619 for(int i = 0; i < trace->getHopCount(); i++)
1620 {
1621 HOP_INFO *hop = trace->getHopInfo(i);
c42b4551 1622 if ((hop->object == NULL) || (hop->object == this) || (hop->object->getObjectClass() != OBJECT_NODE))
7946bd7c
VK
1623 continue;
1624
1625 DbgPrintf(6, _T("Node::checkNetworkPath(%s [%d]): checking upstream node %s [%d]"),
c42b4551 1626 m_name, m_id, hop->object->getName(), hop->object->getId());
8573e935 1627 if (secondPass && !((Node *)hop->object)->isDown() && (((Node *)hop->object)->m_lastStatusPoll < now - 1))
7946bd7c
VK
1628 {
1629 DbgPrintf(6, _T("Node::checkNetworkPath(%s [%d]): forced status poll on node %s [%d]"),
c42b4551 1630 m_name, m_id, hop->object->getName(), hop->object->getId());
7946bd7c
VK
1631 ((Node *)hop->object)->statusPoll(NULL, 0, 0);
1632 }
1f385e47 1633
7946bd7c
VK
1634 if (((Node *)hop->object)->isDown())
1635 {
1636 DbgPrintf(5, _T("Node::checkNetworkPath(%s [%d]): upstream node %s [%d] is down"),
c42b4551
VK
1637 m_name, m_id, hop->object->getName(), hop->object->getId());
1638 sendPollerMsg(dwRqId, POLLER_WARNING _T(" Upstream node %s is down\r\n"), hop->object->getName());
7946bd7c
VK
1639 pathProblemFound = true;
1640 break;
1641 }
1642 }
1643 if (!secondPass && !pathProblemFound)
1644 {
c42b4551 1645 DbgPrintf(5, _T("Node::checkNetworkPath(%s [%d]): will do second pass"), m_name, m_id);
7946bd7c
VK
1646 secondPass = true;
1647 goto restart;
1648 }
1649 delete trace;
c1482463 1650 return pathProblemFound;
7946bd7c
VK
1651}
1652
1653/**
1654 * Check agent policy binding
1655 * Intended to be called only from configuration poller
1656 */
1f385e47
VK
1657void Node::checkAgentPolicyBinding(AgentConnection *conn)
1658{
1659 AgentPolicyInfo *ap;
967893bb 1660 UINT32 rcc = conn->getPolicyInventory(&ap);
1f385e47
VK
1661 if (rcc == ERR_SUCCESS)
1662 {
1663 // Check for unbound but installed policies
a6312bd6 1664 for(int i = 0; i < ap->size(); i++)
1f385e47
VK
1665 {
1666 uuid_t guid;
1667 ap->getGuid(i, guid);
1668 NetObj *object = FindObjectByGUID(guid, -1);
c42b4551 1669 if ((object != NULL) && (!object->isChild(m_id)))
1f385e47
VK
1670 {
1671 object->AddChild(this);
1672 AddParent(object);
c42b4551 1673 DbgPrintf(5, _T("ConfPoll(%s): bound to policy object %s [%d]"), m_name, object->getName(), object->getId());
1f385e47
VK
1674 }
1675 }
1676
1677 // Check for bound but not installed policies
1678 LockParentList(FALSE);
1679 NetObj **unbindList = (NetObj **)malloc(sizeof(NetObj *) * m_dwParentCount);
1680 int unbindListSize = 0;
967893bb 1681 for(UINT32 i = 0; i < m_dwParentCount; i++)
1f385e47
VK
1682 {
1683 if (IsAgentPolicyObject(m_pParentList[i]))
1684 {
1685 uuid_t guid1, guid2;
1686 int j;
1687
1688 m_pParentList[i]->getGuid(guid1);
a6312bd6 1689 for(j = 0; j < ap->size(); j++)
1f385e47
VK
1690 {
1691 ap->getGuid(j, guid2);
1692 if (!uuid_compare(guid1, guid2))
1693 break;
1694 }
a6312bd6 1695 if (j == ap->size())
1f385e47
VK
1696 unbindList[unbindListSize++] = m_pParentList[i];
1697 }
1698 }
1699 UnlockParentList();
1700
1701 for(int i = 0; i < unbindListSize; i++)
1702 {
1703 unbindList[i]->DeleteChild(this);
1704 DeleteParent(unbindList[i]);
c42b4551 1705 DbgPrintf(5, _T("ConfPoll(%s): unbound from policy object %s [%d]"), m_name, unbindList[i]->getName(), unbindList[i]->getId());
1f385e47
VK
1706 }
1707 safe_free(unbindList);
1708
1709 delete ap;
1710 }
1711 else
1712 {
c42b4551 1713 DbgPrintf(5, _T("ConfPoll(%s): AgentConnection::getPolicyInventory() failed: rcc=%d"), m_name, rcc);
1f385e47
VK
1714 }
1715}
1716
a3050773
VK
1717/**
1718 * Update primary IP address from primary name
1719 */
5a7d6a10
VK
1720void Node::updatePrimaryIpAddr()
1721{
1722 if (m_primaryName[0] == 0)
1723 return;
1724
c75e9ee4
VK
1725 InetAddress ipAddr = InetAddress::resolveHostName(m_primaryName);
1726 if (!ipAddr.equals(m_ipAddress) && (ipAddr.isValidUnicast() || !_tcscmp(m_primaryName, _T("0.0.0.0"))))
5a7d6a10 1727 {
c75e9ee4 1728 TCHAR buffer1[64], buffer2[64];
5a7d6a10 1729
4d2c3a54 1730 DbgPrintf(4, _T("IP address for node %s [%d] changed from %s to %s"),
c75e9ee4
VK
1731 m_name, (int)m_id, m_ipAddress.toString(buffer1), ipAddr.toString(buffer2));
1732 PostEvent(EVENT_IP_ADDRESS_CHANGED, m_id, "AA", &ipAddr, &m_ipAddress);
2467ed57 1733
dbe28185
VK
1734 if (m_dwFlags & NF_REMOTE_AGENT)
1735 {
c42b4551 1736 lockProperties();
c75e9ee4 1737 m_ipAddress = ipAddr;
c42b4551
VK
1738 setModified();
1739 unlockProperties();
dbe28185
VK
1740 }
1741 else
1742 {
1743 setPrimaryIPAddress(ipAddr);
1744 }
a3050773
VK
1745
1746 agentLock();
1747 delete_and_null(m_pAgentConnection);
1748 agentUnlock();
5a7d6a10
VK
1749 }
1750}
1751
76720a09
VK
1752/**
1753 * Perform configuration poll on node
1754 */
bf2ad753 1755void Node::configurationPoll(ClientSession *pSession, UINT32 dwRqId, int nPoller, int maskBits)
5039dede 1756{
46117060
VK
1757 if (m_dwDynamicFlags & NDF_DELETE_IN_PROGRESS)
1758 {
1759 if (dwRqId == 0)
1760 m_dwDynamicFlags &= ~NDF_QUEUED_FOR_CONFIG_POLL;
1761 return;
1762 }
1763
967893bb 1764 UINT32 dwOldFlags = m_dwFlags;
35f836fe 1765 TCHAR szBuffer[4096];
76720a09 1766 bool hasChanges = false;
5039dede 1767
35f836fe 1768 SetPollerInfo(nPoller, _T("wait for lock"));
7c521895 1769 pollerLock();
c59466d2 1770 m_pollRequestor = pSession;
c42b4551
VK
1771 sendPollerMsg(dwRqId, _T("Starting configuration poll for node %s\r\n"), m_name);
1772 DbgPrintf(4, _T("Starting configuration poll for node %s (ID: %d)"), m_name, m_id);
5039dede
AK
1773
1774 // Check for forced capabilities recheck
1775 if (m_dwDynamicFlags & NDF_RECHECK_CAPABILITIES)
1776 {
f7e3eb5a 1777 sendPollerMsg(dwRqId, POLLER_WARNING _T("Capability reset\r\n"));
5039dede 1778 m_dwFlags &= ~(NF_IS_NATIVE_AGENT | NF_IS_SNMP | NF_IS_CPSNMP |
0ecc2200 1779 NF_IS_BRIDGE | NF_IS_ROUTER | NF_IS_OSPF | NF_IS_PRINTER |
76720a09 1780 NF_IS_CDP | NF_IS_LLDP | NF_IS_SONMP | NF_IS_VRRP | NF_HAS_VLANS |
46ee6286
VK
1781 NF_IS_8021X | NF_IS_STP | NF_HAS_ENTITY_MIB | NF_HAS_IFXTABLE |
1782 NF_HAS_WINPDH);
e4a64da2 1783 m_dwDynamicFlags &= ~NDF_CONFIGURATION_POLL_PASSED;
5039dede
AK
1784 m_szObjectId[0] = 0;
1785 m_szPlatformName[0] = 0;
1786 m_szAgentVersion[0] = 0;
0ecc2200
VK
1787 safe_free_and_null(m_sysDescription);
1788 safe_free_and_null(m_sysName);
1789 safe_free_and_null(m_lldpNodeId);
5039dede
AK
1790 }
1791
1792 // Check if node is marked as unreachable
1793 if ((m_dwDynamicFlags & NDF_UNREACHABLE) && !(m_dwDynamicFlags & NDF_RECHECK_CAPABILITIES))
1794 {
21c9acce 1795 sendPollerMsg(dwRqId, POLLER_WARNING _T("Node is marked as unreachable, configuration poll aborted\r\n"));
35f836fe 1796 DbgPrintf(4, _T("Node is marked as unreachable, configuration poll aborted"));
8573e935 1797 m_lastConfigurationPoll = time(NULL);
5039dede
AK
1798 }
1799 else
1800 {
5a7d6a10
VK
1801 updatePrimaryIpAddr();
1802
35f836fe 1803 SetPollerInfo(nPoller, _T("capability check"));
21c9acce 1804 sendPollerMsg(dwRqId, _T("Checking node's capabilities...\r\n"));
5039dede 1805
99058170
VK
1806 if (confPollAgent(dwRqId))
1807 hasChanges = true;
1808 if (confPollSnmp(dwRqId))
1809 hasChanges = true;
5039dede
AK
1810
1811 // Check for CheckPoint SNMP agent on port 260
9a1f7c45 1812 if (ConfigReadInt(_T("EnableCheckPointSNMP"), 0))
5039dede 1813 {
c42b4551 1814 DbgPrintf(5, _T("ConfPoll(%s): checking for CheckPoint SNMP on port 260"), m_name);
c75e9ee4 1815 if (!((m_dwFlags & NF_IS_CPSNMP) && (m_dwDynamicFlags & NDF_CPSNMP_UNREACHABLE)) && m_ipAddress.isValidUnicast())
5039dede 1816 {
2fbf7e1c 1817 SNMP_Transport *pTransport = new SNMP_UDPTransport;
c75e9ee4 1818 ((SNMP_UDPTransport *)pTransport)->createUDPTransport(m_ipAddress, CHECKPOINT_SNMP_PORT);
9a1f7c45
VK
1819 if (SnmpGet(SNMP_VERSION_1, pTransport,
1820 _T(".1.3.6.1.4.1.2620.1.1.10.0"), NULL, 0, szBuffer, sizeof(szBuffer), 0) == SNMP_ERR_SUCCESS)
1821 {
c42b4551 1822 lockProperties();
9a1f7c45
VK
1823 m_dwFlags |= NF_IS_CPSNMP | NF_IS_ROUTER;
1824 m_dwDynamicFlags &= ~NDF_CPSNMP_UNREACHABLE;
c42b4551 1825 unlockProperties();
9a1f7c45
VK
1826 sendPollerMsg(dwRqId, POLLER_INFO _T(" CheckPoint SNMP agent on port 260 is active\r\n"));
1827 }
1828 delete pTransport;
5039dede 1829 }
5039dede
AK
1830 }
1831
1832 // Generate event if node flags has been changed
1833 if (dwOldFlags != m_dwFlags)
1834 {
c42b4551 1835 PostEvent(EVENT_NODE_FLAGS_CHANGED, m_id, "xx", dwOldFlags, m_dwFlags);
76720a09 1836 hasChanges = true;
5039dede
AK
1837 }
1838
5039dede 1839 // Retrieve interface list
35f836fe 1840 SetPollerInfo(nPoller, _T("interface check"));
21c9acce 1841 sendPollerMsg(dwRqId, _T("Capability check finished\r\n"));
5039dede 1842
bf2ad753 1843 if (updateInterfaceConfiguration(dwRqId, maskBits))
76720a09 1844 hasChanges = true;
5039dede 1845
8573e935 1846 m_lastConfigurationPoll = time(NULL);
5039dede
AK
1847
1848 // Check node name
21c9acce 1849 sendPollerMsg(dwRqId, _T("Checking node name\r\n"));
c42b4551 1850 UINT32 dwAddr = ntohl(_t_inet_addr(m_name));
c8076b19 1851 if ((g_flags & AF_RESOLVE_NODE_NAMES) &&
4d2c3a54 1852 (dwAddr != INADDR_NONE) &&
5039dede 1853 (dwAddr != INADDR_ANY) &&
58b3e451 1854 isMyIP(dwAddr))
5039dede 1855 {
21c9acce 1856 sendPollerMsg(dwRqId, _T("Node name is an IP address and need to be resolved\r\n"));
35f836fe 1857 SetPollerInfo(nPoller, _T("resolving name"));
024c3faf 1858 if (resolveName(FALSE))
5039dede 1859 {
c42b4551 1860 sendPollerMsg(dwRqId, POLLER_INFO _T("Node name resolved to %s\r\n"), m_name);
76720a09 1861 hasChanges = true;
5039dede
AK
1862 }
1863 else
1864 {
21c9acce 1865 sendPollerMsg(dwRqId, POLLER_WARNING _T("Node name cannot be resolved\r\n"));
5039dede
AK
1866 }
1867 }
1868 else
1869 {
c8076b19 1870 if (g_flags & AF_SYNC_NODE_NAMES_WITH_DNS)
5039dede 1871 {
21c9acce 1872 sendPollerMsg(dwRqId, _T("Syncing node name with DNS\r\n"));
35f836fe 1873 SetPollerInfo(nPoller, _T("resolving name"));
024c3faf 1874 if (resolveName(TRUE))
5039dede 1875 {
c42b4551 1876 sendPollerMsg(dwRqId, POLLER_INFO _T("Node name resolved to %s\r\n"), m_name);
76720a09 1877 hasChanges = true;
5039dede
AK
1878 }
1879 }
1880 else
1881 {
21c9acce 1882 sendPollerMsg(dwRqId, _T("Node name is OK\r\n"));
5039dede
AK
1883 }
1884 }
1885
6fd6de0a 1886 applyUserTemplates();
7c521895 1887 updateContainerMembership();
4d0c32f3 1888
caa04e26
VK
1889 // Get list of installed products
1890 if (m_dwFlags & NF_IS_NATIVE_AGENT)
1891 {
1892 SetPollerInfo(nPoller, _T("software check"));
1893 sendPollerMsg(dwRqId, _T("Reading list of installed software packages\r\n"));
1894
1895 Table *table;
1896 if (getTableFromAgent(_T("System.InstalledProducts"), &table) == DCE_SUCCESS)
1897 {
c42b4551 1898 lockProperties();
caa04e26
VK
1899 delete m_softwarePackages;
1900 m_softwarePackages = new ObjectArray<SoftwarePackage>(table->getNumRows(), 16, true);
1901 for(int i = 0; i < table->getNumRows(); i++)
1902 m_softwarePackages->add(new SoftwarePackage(table, i));
c42b4551 1903 unlockProperties();
caa04e26
VK
1904 delete table;
1905 sendPollerMsg(dwRqId, POLLER_INFO _T("Got information about %d installed software packages\r\n"), m_softwarePackages->size());
1906 }
1907 else
1908 {
1909 delete_and_null(m_softwarePackages);
1910 sendPollerMsg(dwRqId, POLLER_WARNING _T("Unable to get information about installed software packages\r\n"));
1911 }
1912 }
1913
c42b4551 1914 sendPollerMsg(dwRqId, _T("Finished configuration poll for node %s\r\n"), m_name);
21c9acce 1915 sendPollerMsg(dwRqId, _T("Node configuration was%schanged after poll\r\n"), hasChanges ? _T(" ") : _T(" not "));
e4a64da2 1916
a3050773 1917 // Call hooks in loaded modules
967893bb 1918 for(UINT32 i = 0; i < g_dwNumModules; i++)
a5ee7b3b 1919 {
a3050773
VK
1920 if (g_pModuleList[i].pfConfPollHook != NULL)
1921 {
c42b4551 1922 DbgPrintf(5, _T("ConfigurationPoll(%s [%d]): calling hook in module %s"), m_name, m_id, g_pModuleList[i].szName);
3368b8b7
VK
1923 if (g_pModuleList[i].pfConfPollHook(this, pSession, dwRqId, nPoller))
1924 hasChanges = true;
a3050773 1925 }
a5ee7b3b 1926 }
a5ee7b3b 1927
a3050773
VK
1928 // Execute hook script
1929 SetPollerInfo(nPoller, _T("hook"));
1930 executeHookScript(_T("ConfigurationPoll"));
1931
1932 m_dwDynamicFlags |= NDF_CONFIGURATION_POLL_PASSED;
1933 }
1824629a 1934
4d0c32f3 1935 // Finish configuration poll
35f836fe 1936 SetPollerInfo(nPoller, _T("cleanup"));
4d0c32f3
VK
1937 if (dwRqId == 0)
1938 m_dwDynamicFlags &= ~NDF_QUEUED_FOR_CONFIG_POLL;
1939 m_dwDynamicFlags &= ~NDF_RECHECK_CAPABILITIES;
7c521895 1940 pollerUnlock();
c42b4551 1941 DbgPrintf(4, _T("Finished configuration poll for node %s (ID: %d)"), m_name, m_id);
4d0c32f3 1942
76720a09 1943 if (hasChanges)
4d0c32f3 1944 {
c42b4551
VK
1945 lockProperties();
1946 setModified();
1947 unlockProperties();
4d0c32f3
VK
1948 }
1949}
1950
76720a09
VK
1951/**
1952 * Configuration poll: check for NetXMS agent
1953 */
967893bb 1954bool Node::confPollAgent(UINT32 dwRqId)
76720a09 1955{
c42b4551 1956 DbgPrintf(5, _T("ConfPoll(%s): checking for NetXMS agent Flags={%08X} DynamicFlags={%08X}"), m_name, m_dwFlags, m_dwDynamicFlags);
76720a09 1957 if (((m_dwFlags & NF_IS_NATIVE_AGENT) && (m_dwDynamicFlags & NDF_AGENT_UNREACHABLE)) ||
c75e9ee4 1958 !m_ipAddress.isValidUnicast() || (m_dwFlags & NF_DISABLE_NXCP))
76720a09
VK
1959 return false;
1960
1961 bool hasChanges = false;
1962
21c9acce 1963 sendPollerMsg(dwRqId, _T(" Checking NetXMS agent...\r\n"));
c75e9ee4 1964 AgentConnection *pAgentConn = new AgentConnectionEx(m_id, m_ipAddress, m_agentPort, m_agentAuthMethod, m_szSharedSecret);
76720a09 1965 setAgentProxy(pAgentConn);
c42b4551 1966 DbgPrintf(5, _T("ConfPoll(%s): checking for NetXMS agent - connecting"), m_name);
af21affe
VK
1967
1968 // Try to connect to agent
1969 UINT32 rcc;
1970 if (!pAgentConn->connect(g_pServerKey, FALSE, &rcc))
1971 {
1972 // If there are authentication problem, try default shared secret
1973 if ((rcc == ERR_AUTH_REQUIRED) || (rcc == ERR_AUTH_FAILED))
1974 {
1975 TCHAR secret[MAX_SECRET_LENGTH];
1976 ConfigReadStr(_T("AgentDefaultSharedSecret"), secret, MAX_SECRET_LENGTH, _T("netxms"));
1977 pAgentConn->setAuthData(AUTH_SHA1_HASH, secret);
1978 if (pAgentConn->connect(g_pServerKey, FALSE, &rcc))
1979 {
c42b4551 1980 m_agentAuthMethod = AUTH_SHA1_HASH;
af21affe 1981 nx_strncpy(m_szSharedSecret, secret, MAX_SECRET_LENGTH);
c42b4551 1982 DbgPrintf(5, _T("ConfPoll(%s): checking for NetXMS agent - shared secret changed to system default"), m_name);
af21affe
VK
1983 }
1984 }
1985 }
1986
1987 if (rcc == ERR_SUCCESS)
76720a09 1988 {
c42b4551
VK
1989 DbgPrintf(5, _T("ConfPoll(%s): checking for NetXMS agent - connected"), m_name);
1990 lockProperties();
76720a09
VK
1991 m_dwFlags |= NF_IS_NATIVE_AGENT;
1992 if (m_dwDynamicFlags & NDF_AGENT_UNREACHABLE)
1993 {
1994 m_dwDynamicFlags &= ~NDF_AGENT_UNREACHABLE;
c42b4551 1995 PostEvent(EVENT_AGENT_OK, m_id, NULL);
21c9acce 1996 sendPollerMsg(dwRqId, POLLER_INFO _T(" Connectivity with NetXMS agent restored\r\n"));
76720a09
VK
1997 }
1998 else
1999 {
21c9acce 2000 sendPollerMsg(dwRqId, POLLER_INFO _T(" NetXMS native agent is active\r\n"));
76720a09 2001 }
c42b4551 2002 unlockProperties();
4d0c32f3 2003
76720a09
VK
2004 TCHAR buffer[MAX_RESULT_LENGTH];
2005 if (pAgentConn->getParameter(_T("Agent.Version"), MAX_AGENT_VERSION_LEN, buffer) == ERR_SUCCESS)
2006 {
c42b4551 2007 lockProperties();
76720a09
VK
2008 if (_tcscmp(m_szAgentVersion, buffer))
2009 {
2010 _tcscpy(m_szAgentVersion, buffer);
2011 hasChanges = true;
21c9acce 2012 sendPollerMsg(dwRqId, _T(" NetXMS agent version changed to %s\r\n"), m_szAgentVersion);
76720a09 2013 }
c42b4551 2014 unlockProperties();
76720a09
VK
2015 }
2016
2017 if (pAgentConn->getParameter(_T("System.PlatformName"), MAX_PLATFORM_NAME_LEN, buffer) == ERR_SUCCESS)
2018 {
c42b4551 2019 lockProperties();
76720a09
VK
2020 if (_tcscmp(m_szPlatformName, buffer))
2021 {
2022 _tcscpy(m_szPlatformName, buffer);
2023 hasChanges = true;
21c9acce 2024 sendPollerMsg(dwRqId, _T(" Platform name changed to %s\r\n"), m_szPlatformName);
76720a09 2025 }
c42b4551 2026 unlockProperties();
76720a09
VK
2027 }
2028
2029 // Check IP forwarding status
2030 if (pAgentConn->getParameter(_T("Net.IP.Forwarding"), 16, buffer) == ERR_SUCCESS)
2031 {
2032 if (_tcstoul(buffer, NULL, 10) != 0)
2033 m_dwFlags |= NF_IS_ROUTER;
2034 else
2035 m_dwFlags &= ~NF_IS_ROUTER;
2036 }
2037
2038 // Get uname
2039 if (pAgentConn->getParameter(_T("System.Uname"), MAX_DB_STRING, buffer) == ERR_SUCCESS)
2040 {
2041 TranslateStr(buffer, _T("\r\n"), _T(" "));
2042 TranslateStr(buffer, _T("\n"), _T(" "));
2043 TranslateStr(buffer, _T("\r"), _T(" "));
c42b4551 2044 lockProperties();
76720a09
VK
2045 if ((m_sysDescription == NULL) || _tcscmp(m_sysDescription, buffer))
2046 {
2047 safe_free(m_sysDescription);
2048 m_sysDescription = _tcsdup(buffer);
2049 hasChanges = true;
21c9acce 2050 sendPollerMsg(dwRqId, _T(" System description changed to %s\r\n"), m_sysDescription);
76720a09 2051 }
c42b4551 2052 unlockProperties();
76720a09
VK
2053 }
2054
11b75b2e 2055 // Check for 64 bit counter support.
acf01215 2056 // if Net.Interface.64BitCounters not supported by agent then use
11b75b2e
VK
2057 // only presence of 64 bit parameters as indicator
2058 bool netIf64bitCounters = true;
2059 if (pAgentConn->getParameter(_T("Net.Interface.64BitCounters"), MAX_DB_STRING, buffer) == ERR_SUCCESS)
2060 {
2061 netIf64bitCounters = _tcstol(buffer, NULL, 10) ? true : false;
2062 }
2063
86c126f5
VK
2064 ObjectArray<AgentParameterDefinition> *plist;
2065 ObjectArray<AgentTableDefinition> *tlist;
967893bb 2066 UINT32 rcc = pAgentConn->getSupportedParameters(&plist, &tlist);
76720a09
VK
2067 if (rcc == ERR_SUCCESS)
2068 {
c42b4551 2069 lockProperties();
76720a09
VK
2070 delete m_paramList;
2071 delete m_tableList;
2072 m_paramList = plist;
2073 m_tableList = tlist;
084fb4c1
VK
2074
2075 // Check for 64-bit interface counters
2076 m_dwFlags &= ~NF_HAS_AGENT_IFXCOUNTERS;
11b75b2e
VK
2077 if (netIf64bitCounters)
2078 {
2079 for(int i = 0; i < plist->size(); i++)
2080 {
2081 if (!_tcsicmp(plist->get(i)->getName(), _T("Net.Interface.BytesIn64(*)")))
2082 {
2083 m_dwFlags |= NF_HAS_AGENT_IFXCOUNTERS;
2084 break;
2085 }
2086 }
2087 }
084fb4c1 2088
c42b4551 2089 unlockProperties();
76720a09
VK
2090 }
2091 else
2092 {
c42b4551 2093 DbgPrintf(5, _T("ConfPoll(%s): AgentConnection::getSupportedParameters() failed: rcc=%d"), m_name, rcc);
76720a09
VK
2094 }
2095
46ee6286
VK
2096 // Get supported Windows Performance Counters
2097 if (!_tcsncmp(m_szPlatformName, _T("windows-"), 8))
2098 {
2099 sendPollerMsg(dwRqId, _T(" Reading list of available Windows Performance Counters...\r\n"));
2100 ObjectArray<WinPerfObject> *perfObjects = WinPerfObject::getWinPerfObjectsFromNode(this, pAgentConn);
c42b4551 2101 lockProperties();
46ee6286
VK
2102 delete m_winPerfObjects;
2103 m_winPerfObjects = perfObjects;
2104 if (m_winPerfObjects != NULL)
2105 {
2106 sendPollerMsg(dwRqId, POLLER_INFO _T(" %d counters read\r\n"), m_winPerfObjects->size());
2107 if (!(m_dwFlags & NF_HAS_WINPDH))
2108 {
2109 m_dwFlags |= NF_HAS_WINPDH;
2110 hasChanges = true;
2111 }
2112 }
2113 else
2114 {
2115 sendPollerMsg(dwRqId, POLLER_ERROR _T(" unable to get Windows Performance Counters list\r\n"));
2116 if (m_dwFlags & NF_HAS_WINPDH)
2117 {
2118 m_dwFlags &= ~NF_HAS_WINPDH;
2119 hasChanges = true;
2120 }
2121 }
c42b4551 2122 unlockProperties();
46ee6286
VK
2123 }
2124
76720a09
VK
2125 checkAgentPolicyBinding(pAgentConn);
2126
2127 pAgentConn->disconnect();
2128 }
2129 else
2130 {
c42b4551 2131 DbgPrintf(5, _T("ConfPoll(%s): checking for NetXMS agent - failed to connect (error %d)"), m_name, rcc);
76720a09
VK
2132 }
2133 delete pAgentConn;
c42b4551 2134 DbgPrintf(5, _T("ConfPoll(%s): checking for NetXMS agent - finished"), m_name);
76720a09
VK
2135 return hasChanges;
2136}
2137
2138/**
2139 * SNMP walker callback which just counts number of varbinds
2140 */
967893bb 2141static UINT32 CountingSnmpWalkerCallback(UINT32 version, SNMP_Variable *var, SNMP_Transport *transport, void *arg)
76720a09
VK
2142{
2143 (*((int *)arg))++;
2144 return SNMP_ERR_SUCCESS;
2145}
2146
2147/**
2148 * Configuration poll: check for SNMP
2149 */
967893bb 2150bool Node::confPollSnmp(UINT32 dwRqId)
76720a09
VK
2151{
2152 if (((m_dwFlags & NF_IS_SNMP) && (m_dwDynamicFlags & NDF_SNMP_UNREACHABLE)) ||
c75e9ee4 2153 !m_ipAddress.isValidUnicast() || (m_dwFlags & NF_DISABLE_SNMP))
76720a09
VK
2154 return false;
2155
2156 bool hasChanges = false;
2157
21c9acce 2158 sendPollerMsg(dwRqId, _T(" Checking SNMP...\r\n"));
c42b4551 2159 DbgPrintf(5, _T("ConfPoll(%s): calling SnmpCheckCommSettings()"), m_name);
76720a09
VK
2160 SNMP_Transport *pTransport = createSnmpTransport();
2161 if (pTransport == NULL)
2162 {
c42b4551 2163 DbgPrintf(5, _T("ConfPoll(%s): unable to create SNMP transport"), m_name);
76720a09
VK
2164 return false;
2165 }
2166
1f4c37ee
VK
2167 StringList oids;
2168 const TCHAR *customOid = m_customAttributes.get(_T("snmp.testoid"));
2169 if (customOid != NULL)
2170 oids.add(customOid);
2171 oids.add(_T(".1.3.6.1.2.1.1.2.0"));
2172 oids.add(_T(".1.3.6.1.2.1.1.1.0"));
2173 AddDriverSpecificOids(&oids);
2174 SNMP_SecurityContext *newCtx = SnmpCheckCommSettings(pTransport, &m_snmpVersion, m_snmpSecurity, &oids);
76720a09
VK
2175 if (newCtx != NULL)
2176 {
c42b4551 2177 lockProperties();
76720a09
VK
2178 delete m_snmpSecurity;
2179 m_snmpSecurity = newCtx;
2180 m_dwFlags |= NF_IS_SNMP;
2181 if (m_dwDynamicFlags & NDF_SNMP_UNREACHABLE)
2182 {
2183 m_dwDynamicFlags &= ~NDF_SNMP_UNREACHABLE;
c42b4551 2184 PostEvent(EVENT_SNMP_OK, m_id, NULL);
21c9acce 2185 sendPollerMsg(dwRqId, POLLER_INFO _T(" Connectivity with SNMP agent restored\r\n"));
76720a09 2186 }
c42b4551 2187 unlockProperties();
21c9acce 2188 sendPollerMsg(dwRqId, _T(" SNMP agent is active (version %s)\r\n"),
76720a09
VK
2189 (m_snmpVersion == SNMP_VERSION_3) ? _T("3") : ((m_snmpVersion == SNMP_VERSION_2C) ? _T("2c") : _T("1")));
2190
2191 TCHAR szBuffer[4096];
d525c9ed 2192 if (SnmpGet(m_snmpVersion, pTransport, _T(".1.3.6.1.2.1.1.2.0"), NULL, 0, szBuffer, sizeof(szBuffer), SG_STRING_RESULT) != SNMP_ERR_SUCCESS)
76720a09 2193 {
3b2d5932
VK
2194 // Set snmp object ID to .0.0 if it cannot be read
2195 _tcscpy(szBuffer, _T(".0.0"));
76720a09 2196 }
c42b4551 2197 lockProperties();
3b2d5932
VK
2198 if (_tcscmp(m_szObjectId, szBuffer))
2199 {
2200 nx_strncpy(m_szObjectId, szBuffer, MAX_OID_LEN * 4);
2201 hasChanges = true;
2202 }
c42b4551 2203 unlockProperties();
76720a09
VK
2204
2205 // Get system description
d525c9ed 2206 if (SnmpGet(m_snmpVersion, pTransport, _T(".1.3.6.1.2.1.1.1.0"), NULL, 0, szBuffer, sizeof(szBuffer), SG_STRING_RESULT) == SNMP_ERR_SUCCESS)
76720a09
VK
2207 {
2208 TranslateStr(szBuffer, _T("\r\n"), _T(" "));
2209 TranslateStr(szBuffer, _T("\n"), _T(" "));
2210 TranslateStr(szBuffer, _T("\r"), _T(" "));
c42b4551 2211 lockProperties();
76720a09
VK
2212 if ((m_sysDescription == NULL) || _tcscmp(m_sysDescription, szBuffer))
2213 {
2214 safe_free(m_sysDescription);
2215 m_sysDescription = _tcsdup(szBuffer);
2216 hasChanges = true;
21c9acce 2217 sendPollerMsg(dwRqId, _T(" System description changed to %s\r\n"), m_sysDescription);
76720a09 2218 }
c42b4551 2219 unlockProperties();
76720a09
VK
2220 }
2221
2222 // Select device driver
2223 NetworkDeviceDriver *driver = FindDriverForNode(this, pTransport);
c42b4551
VK
2224 DbgPrintf(5, _T("ConfPoll(%s): selected device driver %s"), m_name, driver->getName());
2225 lockProperties();
76720a09
VK
2226 if (driver != m_driver)
2227 {
2228 m_driver = driver;
21c9acce 2229 sendPollerMsg(dwRqId, _T(" New network device driver selected: %s\r\n"), m_driver->getName());
76720a09 2230 }
c42b4551 2231 unlockProperties();
76720a09
VK
2232
2233 // Allow driver to gather additional info
ae32341d 2234 m_driver->analyzeDevice(pTransport, m_szObjectId, &m_customAttributes, &m_driverData);
76720a09
VK
2235
2236 // Get sysName
2237 if (SnmpGet(m_snmpVersion, pTransport,
d525c9ed 2238 _T(".1.3.6.1.2.1.1.5.0"), NULL, 0, szBuffer, sizeof(szBuffer), SG_STRING_RESULT) == SNMP_ERR_SUCCESS)
76720a09 2239 {
c42b4551 2240 lockProperties();
76720a09
VK
2241 if ((m_sysName == NULL) || _tcscmp(m_sysName, szBuffer))
2242 {
2243 safe_free(m_sysName);
2244 m_sysName = _tcsdup(szBuffer);
2245 hasChanges = true;
21c9acce 2246 sendPollerMsg(dwRqId, _T(" System name changed to %s\r\n"), m_sysName);
76720a09 2247 }
c42b4551 2248 unlockProperties();
76720a09
VK
2249 }
2250
2251 // Check IP forwarding
074498ac 2252 if (checkSNMPIntegerValue(pTransport, _T(".1.3.6.1.2.1.4.1.0"), 1))
76720a09 2253 {
c42b4551 2254 lockProperties();
76720a09 2255 m_dwFlags |= NF_IS_ROUTER;
c42b4551 2256 unlockProperties();
76720a09
VK
2257 }
2258 else
2259 {
c42b4551 2260 lockProperties();
76720a09 2261 m_dwFlags &= ~NF_IS_ROUTER;
c42b4551 2262 unlockProperties();
76720a09
VK
2263 }
2264
2265 checkIfXTable(pTransport);
2266 checkBridgeMib(pTransport);
eec253a8 2267
76720a09 2268 // Check for ENTITY-MIB support
d525c9ed 2269 if (SnmpGet(m_snmpVersion, pTransport, _T(".1.3.6.1.2.1.47.1.4.1.0"), NULL, 0, szBuffer, sizeof(szBuffer), SG_RAW_RESULT) == SNMP_ERR_SUCCESS)
76720a09 2270 {
c42b4551 2271 lockProperties();
76720a09 2272 m_dwFlags |= NF_HAS_ENTITY_MIB;
c42b4551 2273 unlockProperties();
76720a09
VK
2274
2275 ComponentTree *components = BuildComponentTree(this, pTransport);
c42b4551 2276 lockProperties();
76720a09
VK
2277 if (m_components != NULL)
2278 m_components->decRefCount();
2279 m_components = components;
c42b4551 2280 unlockProperties();
76720a09
VK
2281 }
2282 else
2283 {
c42b4551 2284 lockProperties();
76720a09
VK
2285 m_dwFlags &= ~NF_HAS_ENTITY_MIB;
2286 if (m_components != NULL)
2287 {
2288 m_components->decRefCount();
2289 m_components = NULL;
2290 }
c42b4551 2291 unlockProperties();
76720a09
VK
2292 }
2293
2294 // Check for printer MIB support
2295 int count = 0;
46b7166d 2296 SnmpWalk(m_snmpVersion, pTransport, _T(".1.3.6.1.2.1.43.5.1.1.17"), CountingSnmpWalkerCallback, &count, FALSE);
76720a09
VK
2297 if (count > 0)
2298 {
c42b4551 2299 lockProperties();
76720a09 2300 m_dwFlags |= NF_IS_PRINTER;
c42b4551 2301 unlockProperties();
76720a09
VK
2302 }
2303 else
2304 {
c42b4551 2305 lockProperties();
76720a09 2306 m_dwFlags &= ~NF_IS_PRINTER;
c42b4551 2307 unlockProperties();
76720a09
VK
2308 }
2309
2310 // Check for CDP (Cisco Discovery Protocol) support
074498ac 2311 if (checkSNMPIntegerValue(pTransport, _T(".1.3.6.1.4.1.9.9.23.1.3.1.0"), 1))
76720a09 2312 {
c42b4551 2313 lockProperties();
76720a09 2314 m_dwFlags |= NF_IS_CDP;
c42b4551 2315 unlockProperties();
76720a09
VK
2316 }
2317 else
2318 {
c42b4551 2319 lockProperties();
76720a09 2320 m_dwFlags &= ~NF_IS_CDP;
c42b4551 2321 unlockProperties();
76720a09
VK
2322 }
2323
2324 // Check for NDP (Nortel Discovery Protocol) support
074498ac 2325 if (checkSNMPIntegerValue(pTransport, _T(".1.3.6.1.4.1.45.1.6.13.1.2.0"), 1))
76720a09 2326 {
c42b4551 2327 lockProperties();
76720a09 2328 m_dwFlags |= NF_IS_NDP;
c42b4551 2329 unlockProperties();
76720a09
VK
2330 }
2331 else
2332 {
c42b4551 2333 lockProperties();
76720a09 2334 m_dwFlags &= ~NF_IS_NDP;
c42b4551 2335 unlockProperties();
76720a09
VK
2336 }
2337
2338 // Check for LLDP (Link Layer Discovery Protocol) support
d525c9ed 2339 if (SnmpGet(m_snmpVersion, pTransport, _T(".1.0.8802.1.1.2.1.3.2.0"), NULL, 0, szBuffer, sizeof(szBuffer), 0) == SNMP_ERR_SUCCESS)
76720a09 2340 {
c42b4551 2341 lockProperties();
76720a09 2342 m_dwFlags |= NF_IS_LLDP;
c42b4551 2343 unlockProperties();
76720a09 2344
20c7bdf7
VK
2345 INT32 type;
2346 BYTE data[256];
2347 UINT32 dataLen;
2348 if ((SnmpGetEx(pTransport, _T(".1.0.8802.1.1.2.1.3.1.0"), NULL, 0, &type, sizeof(INT32), 0, NULL) == SNMP_ERR_SUCCESS) &&
2349 (SnmpGetEx(pTransport, _T(".1.0.8802.1.1.2.1.3.2.0"), NULL, 0, data, 256, SG_RAW_RESULT, &dataLen) == SNMP_ERR_SUCCESS))
76720a09 2350 {
20c7bdf7 2351 BuildLldpId(type, data, dataLen, szBuffer, 1024);
c42b4551 2352 lockProperties();
20c7bdf7 2353 if ((m_lldpNodeId == NULL) || _tcscmp(m_lldpNodeId, szBuffer))
76720a09 2354 {
20c7bdf7
VK
2355 safe_free(m_lldpNodeId);
2356 m_lldpNodeId = _tcsdup(szBuffer);
2357 hasChanges = true;
2358 sendPollerMsg(dwRqId, _T(" LLDP node ID changed to %s\r\n"), m_lldpNodeId);
76720a09 2359 }
c42b4551 2360 unlockProperties();
76720a09 2361 }
3a82d5ae
VK
2362
2363 ObjectArray<LLDP_LOCAL_PORT_INFO> *lldpPorts = GetLLDPLocalPortInfo(pTransport);
c42b4551 2364 lockProperties();
3a82d5ae
VK
2365 delete m_lldpLocalPortInfo;
2366 m_lldpLocalPortInfo = lldpPorts;
c42b4551 2367 unlockProperties();
76720a09
VK
2368 }
2369 else
2370 {
c42b4551 2371 lockProperties();
76720a09 2372 m_dwFlags &= ~NF_IS_LLDP;
c42b4551 2373 unlockProperties();
76720a09
VK
2374 }
2375
2376 // Check for 802.1x support
074498ac 2377 if (checkSNMPIntegerValue(pTransport, _T(".1.0.8802.1.1.1.1.1.1.0"), 1))
76720a09 2378 {
c42b4551 2379 lockProperties();
76720a09 2380 m_dwFlags |= NF_IS_8021X;
c42b4551 2381 unlockProperties();
76720a09
VK
2382 }
2383 else
2384 {
c42b4551 2385 lockProperties();
76720a09 2386 m_dwFlags &= ~NF_IS_8021X;
c42b4551 2387 unlockProperties();
76720a09
VK
2388 }
2389
074498ac 2390 checkOSPFSupport(pTransport);
76720a09
VK
2391
2392 // Get VRRP information
2393 VrrpInfo *vrrpInfo = GetVRRPInfo(this);
2394 if (vrrpInfo != NULL)
2395 {
c42b4551 2396 lockProperties();
76720a09
VK
2397 m_dwFlags |= NF_IS_VRRP;
2398 delete m_vrrpInfo;
2399 m_vrrpInfo = vrrpInfo;
c42b4551 2400 unlockProperties();
76720a09
VK
2401 }
2402 else
2403 {
c42b4551 2404 lockProperties();
76720a09 2405 m_dwFlags &= ~NF_IS_VRRP;
c42b4551 2406 unlockProperties();
76720a09 2407 }
c6afd26a
VK
2408
2409 // Get wireless controller data
2410 if ((m_driver != NULL) && m_driver->isWirelessController(pTransport, &m_customAttributes, m_driverData))
2411 {
c42b4551 2412 DbgPrintf(5, _T("ConfPoll(%s): node is wireless controller, reading access point information"), m_name);
2587b41b 2413 sendPollerMsg(dwRqId, _T(" Reading wireless access point information\r\n"));
c42b4551 2414 lockProperties();
c6afd26a 2415 m_dwFlags |= NF_IS_WIFI_CONTROLLER;
c42b4551 2416 unlockProperties();
c6afd26a 2417
8f26db67
VK
2418 int clusterMode = m_driver->getClusterMode(pTransport, &m_customAttributes, m_driverData);
2419
c6afd26a
VK
2420 ObjectArray<AccessPointInfo> *aps = m_driver->getAccessPoints(pTransport, &m_customAttributes, m_driverData);
2421 if (aps != NULL)
2422 {
2587b41b 2423 sendPollerMsg(dwRqId, POLLER_INFO _T(" %d wireless access points found\r\n"), aps->size());
c42b4551 2424 DbgPrintf(5, _T("ConfPoll(%s): got information about %d access points"), m_name, aps->size());
f1989a3a 2425 int adopted = 0;
c6afd26a
VK
2426 for(int i = 0; i < aps->size(); i++)
2427 {
2428 AccessPointInfo *info = aps->get(i);
23ed00c4
VK
2429 if (info->getState() == AP_ADOPTED)
2430 adopted++;
f1989a3a 2431
56d5289b 2432 bool newAp = false;
8f26db67 2433 AccessPoint *ap = (clusterMode == CLUSTER_MODE_STANDALONE) ? findAccessPointByMAC(info->getMacAddr()) : FindAccessPointByMAC(info->getMacAddr());
d5de1d1d 2434 if (ap == NULL)
c6afd26a 2435 {
d5de1d1d 2436 String name;
4d2c3a54 2437
ffb44442
VK
2438 if (info->getName() != NULL)
2439 {
2440 name = info->getName();
2441 }
2442 else
2443 {
2444 for(int j = 0; j < info->getRadioInterfaces()->size(); j++)
2445 {
2446 if (j > 0)
2447 name += _T("/");
2448 name += info->getRadioInterfaces()->get(j)->name;
2449 }
2450 }
f1989a3a
VK
2451 ap = new AccessPoint((const TCHAR *)name, info->getMacAddr());
2452 NetObjInsert(ap, TRUE);
c42b4551 2453 DbgPrintf(5, _T("ConfPoll(%s): created new access point object %s [%d]"), m_name, ap->getName(), ap->getId());
56d5289b 2454 newAp = true;
c6afd26a 2455 }
c42b4551 2456 ap->attachToNode(m_id);
56d5289b
VK
2457 ap->setIpAddr(info->getIpAddr());
2458 if ((info->getState() == AP_ADOPTED) || newAp)
23ed00c4
VK
2459 {
2460 ap->updateRadioInterfaces(info->getRadioInterfaces());
56d5289b 2461 ap->updateInfo(info->getVendor(), info->getModel(), info->getSerial());
23ed00c4 2462 }
f1989a3a 2463 ap->unhide();
23ed00c4 2464 ap->updateState(info->getState());
c6afd26a 2465 }
f1989a3a 2466
c42b4551 2467 lockProperties();
f1989a3a
VK
2468 m_adoptedApCount = adopted;
2469 m_totalApCount = aps->size();
c42b4551 2470 unlockProperties();
f1989a3a 2471
c6afd26a
VK
2472 delete aps;
2473 }
2587b41b
VK
2474 else
2475 {
c42b4551 2476 DbgPrintf(5, _T("ConfPoll(%s): failed to read access point information"), m_name);
2587b41b
VK
2477 sendPollerMsg(dwRqId, POLLER_ERROR _T(" Failed to read access point information\r\n"));
2478 }
c6afd26a
VK
2479 }
2480 else
2481 {
c42b4551 2482 lockProperties();
c6afd26a 2483 m_dwFlags &= ~NF_IS_WIFI_CONTROLLER;
c42b4551 2484 unlockProperties();
c6afd26a 2485 }
76720a09 2486 }
9a1f7c45 2487 else if (ConfigReadInt(_T("EnableCheckPointSNMP"), 0))
76720a09
VK
2488 {
2489 // Check for CheckPoint SNMP agent on port 161
c42b4551 2490 DbgPrintf(5, _T("ConfPoll(%s): checking for CheckPoint SNMP"), m_name);
76720a09 2491 TCHAR szBuffer[4096];
d525c9ed 2492 if (SnmpGet(SNMP_VERSION_1, pTransport, _T(".1.3.6.1.4.1.2620.1.1.10.0"), NULL, 0, szBuffer, sizeof(szBuffer), 0) == SNMP_ERR_SUCCESS)
76720a09 2493 {
c42b4551 2494 lockProperties();
76720a09
VK
2495 if (_tcscmp(m_szObjectId, _T(".1.3.6.1.4.1.2620.1.1")))
2496 {
2497 nx_strncpy(m_szObjectId, _T(".1.3.6.1.4.1.2620.1.1"), MAX_OID_LEN * 4);
2498 hasChanges = true;
2499 }
2500
2501 m_dwFlags |= NF_IS_SNMP | NF_IS_ROUTER;
2502 m_dwDynamicFlags &= ~NDF_SNMP_UNREACHABLE;
c42b4551 2503 unlockProperties();
21c9acce 2504 sendPollerMsg(dwRqId, POLLER_INFO _T(" CheckPoint SNMP agent on port 161 is active\r\n"));
76720a09
VK
2505 }
2506 }
2507 delete pTransport;
2508 return hasChanges;
2509}
2510
2511/**
2512 * Configuration poll: check for BRIDGE MIB
2513 */
2514void Node::checkBridgeMib(SNMP_Transport *pTransport)
2515{
2516 TCHAR szBuffer[4096];
d525c9ed 2517 if (SnmpGet(m_snmpVersion, pTransport, _T(".1.3.6.1.2.1.17.1.1.0"), NULL, 0, szBuffer, sizeof(szBuffer), SG_RAW_RESULT) == SNMP_ERR_SUCCESS)
76720a09 2518 {
c42b4551 2519 lockProperties();
76720a09
VK
2520 m_dwFlags |= NF_IS_BRIDGE;
2521 memcpy(m_baseBridgeAddress, szBuffer, 6);
c42b4551 2522 unlockProperties();
76720a09
VK
2523
2524 // Check for Spanning Tree (IEEE 802.1d) MIB support
074498ac 2525 if (checkSNMPIntegerValue(pTransport, _T(".1.3.6.1.2.1.17.2.1.0"), 3))
76720a09 2526 {
c42b4551 2527 lockProperties();
76720a09 2528 m_dwFlags |= NF_IS_STP;
c42b4551 2529 unlockProperties();
76720a09
VK
2530 }
2531 else
2532 {
c42b4551 2533 lockProperties();
76720a09 2534 m_dwFlags &= ~NF_IS_STP;
c42b4551 2535 unlockProperties();
76720a09
VK
2536 }
2537 }
2538 else
2539 {
c42b4551 2540 lockProperties();
76720a09 2541 m_dwFlags &= ~(NF_IS_BRIDGE | NF_IS_STP);
c42b4551 2542 unlockProperties();
76720a09
VK
2543 }
2544}
2545
2546/**
2547 * Configuration poll: check for ifXTable
2548 */
2549void Node::checkIfXTable(SNMP_Transport *pTransport)
2550{
2551 int count = 0;
46b7166d 2552 SnmpWalk(m_snmpVersion, pTransport, _T(".1.3.6.1.2.1.31.1.1.1.1"), CountingSnmpWalkerCallback, &count, FALSE);
76720a09
VK
2553 if (count > 0)
2554 {
c42b4551 2555 lockProperties();
76720a09 2556 m_dwFlags |= NF_HAS_IFXTABLE;
c42b4551 2557 unlockProperties();
76720a09
VK
2558 }
2559 else
2560 {
c42b4551 2561 lockProperties();
76720a09 2562 m_dwFlags &= ~NF_HAS_IFXTABLE;
c42b4551 2563 unlockProperties();
76720a09
VK
2564 }
2565}
2566
2567/**
2568 * Update interface configuration
2569 */
bf2ad753 2570BOOL Node::updateInterfaceConfiguration(UINT32 dwRqId, int maskBits)
eec253a8 2571{
98762401 2572 InterfaceList *pIfList;
eec253a8
VK
2573 Interface **ppDeleteList;
2574 int i, j, iDelCount;
2575 BOOL hasChanges = FALSE;
2576 Cluster *pCluster = getMyCluster();
2577
21c9acce 2578 sendPollerMsg(dwRqId, _T("Checking interface configuration...\r\n"));
eec253a8
VK
2579 pIfList = getInterfaceList();
2580 if (pIfList != NULL)
2581 {
c42b4551 2582 DbgPrintf(6, _T("Node::updateInterfaceConfiguration(%s [%u]): got %d interfaces"), m_name, m_id, pIfList->size());
eec253a8
VK
2583 // Remove cluster virtual interfaces from list
2584 if (pCluster != NULL)
2585 {
a6312bd6 2586 for(i = 0; i < pIfList->size(); i++)
eec253a8 2587 {
e95680e5 2588 if (pCluster->isVirtualAddr(pIfList->get(i)->ipAddr))
eec253a8 2589 {
98762401 2590 pIfList->remove(i);
eec253a8
VK
2591 i--;
2592 }
2593 }
2594 }
2595
2596 // Find non-existing interfaces
2597 LockChildList(FALSE);
2598 ppDeleteList = (Interface **)malloc(sizeof(Interface *) * m_dwChildCount);
2599 for(i = 0, iDelCount = 0; i < (int)m_dwChildCount; i++)
2600 {
c42b4551 2601 if (m_pChildList[i]->getObjectClass() == OBJECT_INTERFACE)
eec253a8
VK
2602 {
2603 Interface *pInterface = (Interface *)m_pChildList[i];
9214177b
VK
2604 if (!pInterface->isManuallyCreated())
2605 {
a6312bd6 2606 for(j = 0; j < pIfList->size(); j++)
9214177b 2607 {
e95680e5 2608 if ((pIfList->get(j)->index == pInterface->getIfIndex()) &&
c75e9ee4 2609 pInterface->getIpAddress().equals(pIfList->get(j)->ipAddr))
9214177b
VK
2610 break;
2611 }
eec253a8 2612
a6312bd6 2613 if (j == pIfList->size())
9214177b
VK
2614 {
2615 // No such interface in current configuration, add it to delete list
2616 ppDeleteList[iDelCount++] = pInterface;
2617 }
2618 }
eec253a8
VK
2619 }
2620 }
2621 UnlockChildList();
2622
2623 // Delete non-existent interfaces
2624 if (iDelCount > 0)
2625 {
2626 for(j = 0; j < iDelCount; j++)
2627 {
c42b4551 2628 sendPollerMsg(dwRqId, POLLER_WARNING _T(" Interface \"%s\" is no longer exist\r\n"), ppDeleteList[j]->getName());
c75e9ee4
VK
2629 PostEvent(EVENT_INTERFACE_DELETED, m_id, "dsAd", ppDeleteList[j]->getIfIndex(),
2630 ppDeleteList[j]->getName(), &ppDeleteList[j]->getIpAddress(), ppDeleteList[j]->getIpAddress().getMaskBits());
eec253a8
VK
2631 deleteInterface(ppDeleteList[j]);
2632 }
2633 hasChanges = TRUE;
2634 }
2635 safe_free(ppDeleteList);
2636
2637 // Add new interfaces and check configuration of existing
a6312bd6 2638 for(j = 0; j < pIfList->size(); j++)
eec253a8 2639 {
60557d06 2640 NX_INTERFACE_INFO *ifInfo = pIfList->get(j);
eec253a8
VK
2641 BOOL bNewInterface = TRUE;
2642
2643 LockChildList(FALSE);
2644 for(i = 0; i < (int)m_dwChildCount; i++)
2645 {
c42b4551 2646 if (m_pChildList[i]->getObjectClass() == OBJECT_INTERFACE)
eec253a8
VK
2647 {
2648 Interface *pInterface = (Interface *)m_pChildList[i];
2649
e95680e5 2650 if ((ifInfo->index == pInterface->getIfIndex()) &&
c75e9ee4 2651 pInterface->getIpAddress().equals(ifInfo->ipAddr))
eec253a8
VK
2652 {
2653 // Existing interface, check configuration
e95680e5 2654 if (memcmp(ifInfo->macAddr, "\x00\x00\x00\x00\x00\x00", MAC_ADDR_LENGTH) && memcmp(ifInfo->macAddr, pInterface->getMacAddr(), MAC_ADDR_LENGTH))
eec253a8
VK
2655 {
2656 TCHAR szOldMac[16], szNewMac[16];
2657
2658 BinToStr((BYTE *)pInterface->getMacAddr(), MAC_ADDR_LENGTH, szOldMac);
e95680e5 2659 BinToStr(ifInfo->macAddr, MAC_ADDR_LENGTH, szNewMac);
c42b4551
VK
2660 PostEvent(EVENT_MAC_ADDR_CHANGED, m_id, "idsss",
2661 pInterface->getId(), pInterface->getIfIndex(),
2662 pInterface->getName(), szOldMac, szNewMac);
e95680e5
VK
2663 pInterface->setMacAddr(ifInfo->macAddr);
2664 }
2665 if (_tcscmp(ifInfo->name, pInterface->getName()))
2666 {
2667 pInterface->setName(ifInfo->name);
eec253a8 2668 }
e95680e5 2669 if (_tcscmp(ifInfo->description, pInterface->getDescription()))
eec253a8 2670 {
e95680e5 2671 pInterface->setDescription(ifInfo->description);
eec253a8 2672 }
e95680e5 2673 if (_tcscmp(ifInfo->alias, pInterface->getAlias()))
478d4ff4 2674 {
e95680e5 2675 pInterface->setAlias(ifInfo->alias);
478d4ff4 2676 }
e95680e5 2677 if (ifInfo->bridgePort != pInterface->getBridgePortNumber())
eec253a8 2678 {
e95680e5 2679 pInterface->setBridgePortNumber(ifInfo->bridgePort);
eec253a8 2680 }
e95680e5 2681 if (ifInfo->slot != pInterface->getSlotNumber())
76f9abfd 2682 {
e95680e5 2683 pInterface->setSlotNumber(ifInfo->slot);
76f9abfd 2684 }
e95680e5 2685 if (ifInfo->port != pInterface->getPortNumber())
76f9abfd 2686 {
e95680e5 2687 pInterface->setPortNumber(ifInfo->port);
76f9abfd 2688 }
4c16cdc7
VK
2689 if (ifInfo->isPhysicalPort != pInterface->isPhysicalPort())
2690 {
2691 pInterface->setPhysicalPortFlag(ifInfo->isPhysicalPort);
2692 }
c75e9ee4 2693 if ((ifInfo->ipNetMask != 0) && (ifInfo->ipNetMask != pInterface->getIpAddress().getMaskBits()))
dfb38baf 2694 {
c75e9ee4 2695 pInterface->setIpNetMask(BitsInMask(ifInfo->ipNetMask));
dfb38baf 2696 }
eec253a8
VK
2697 bNewInterface = FALSE;
2698 break;
2699 }
2700 }
2701 }
2702 UnlockChildList();
2703
2704 if (bNewInterface)
2705 {
2706 // New interface
e95680e5
VK
2707 sendPollerMsg(dwRqId, POLLER_INFO _T(" Found new interface \"%s\"\r\n"), ifInfo->name);
2708 createNewInterface(ifInfo, false);
eec253a8
VK
2709 hasChanges = TRUE;
2710 }
2711 }
eec253a8 2712 }
dbe28185 2713 else if (!(m_dwFlags & NF_REMOTE_AGENT)) /* pIfList == NULL */
eec253a8
VK
2714 {
2715 Interface *pInterface;
967893bb 2716 UINT32 dwCount;
eec253a8 2717
21c9acce 2718 sendPollerMsg(dwRqId, POLLER_ERROR _T("Unable to get interface list from node\r\n"));
c42b4551 2719 DbgPrintf(6, _T("Node::updateInterfaceConfiguration(%s [%u]): Unable to get interface list from node"), m_name, m_id);
eec253a8
VK
2720
2721 // Delete all existing interfaces in case of forced capability recheck
2722 if (m_dwDynamicFlags & NDF_RECHECK_CAPABILITIES)
2723 {
2724 LockChildList(FALSE);
2725 ppDeleteList = (Interface **)malloc(sizeof(Interface *) * m_dwChildCount);
2726 for(i = 0, iDelCount = 0; i < (int)m_dwChildCount; i++)
2727 {
c42b4551 2728 if ((m_pChildList[i]->getObjectClass() == OBJECT_INTERFACE) && !((Interface *)m_pChildList[i])->isManuallyCreated())
eec253a8
VK
2729 ppDeleteList[iDelCount++] = (Interface *)m_pChildList[i];
2730 }
2731 UnlockChildList();
2732 for(j = 0; j < iDelCount; j++)
2733 {
21c9acce 2734 sendPollerMsg(dwRqId, POLLER_WARNING _T(" Interface \"%s\" is no longer exist\r\n"),
c42b4551 2735 ppDeleteList[j]->getName());
c75e9ee4
VK
2736 PostEvent(EVENT_INTERFACE_DELETED, m_id, "dsAd", ppDeleteList[j]->getIfIndex(),
2737 ppDeleteList[j]->getName(), &ppDeleteList[j]->getIpAddress(), ppDeleteList[j]->getIpAddress().getMaskBits());
eec253a8
VK
2738 deleteInterface(ppDeleteList[j]);
2739 }
2740 safe_free(ppDeleteList);
2741 }
2742
2743 // Check if we have pseudo-interface object
baa5324c
AK
2744 BYTE macAddr[MAC_ADDR_LENGTH];
2745 BYTE *pMacAddr;
eec253a8
VK
2746 dwCount = getInterfaceCount(&pInterface);
2747 if (dwCount == 1)
2748 {
2749 if (pInterface->isFake())
2750 {
2751 // Check if primary IP is different from interface's IP
c75e9ee4 2752 if (!pInterface->getIpAddress().equals(m_ipAddress))
eec253a8
VK
2753 {
2754 deleteInterface(pInterface);
c75e9ee4 2755 if (m_ipAddress.isValidUnicast())
baa5324c
AK
2756 {
2757 memset(macAddr, 0, MAC_ADDR_LENGTH);
c75e9ee4 2758 Subnet *pSubnet = FindSubnetForNode(m_zoneId, m_ipAddress);
baa5324c 2759 if (pSubnet != NULL)
c75e9ee4 2760 pSubnet->findMacAddress(m_ipAddress, macAddr);
22d657d2 2761 pMacAddr = !memcmp(macAddr, "\x00\x00\x00\x00\x00\x00", MAC_ADDR_LENGTH) ? NULL : macAddr;
baa5324c
AK
2762 TCHAR szMac[20];
2763 MACToStr(macAddr, szMac);
c42b4551 2764 DbgPrintf(5, _T("Node::updateInterfaceConfiguration(%s [%u]): got MAC for unknown interface: %s"), m_name, m_id, szMac);
bf2ad753 2765 createNewInterface(m_ipAddress, pMacAddr);
baa5324c 2766 }
eec253a8 2767 }
22d657d2
VK
2768 else
2769 {
2770 // check MAC address
2771 memset(macAddr, 0, MAC_ADDR_LENGTH);
c75e9ee4 2772 Subnet *pSubnet = FindSubnetForNode(m_zoneId, m_ipAddress);
22d657d2 2773 if (pSubnet != NULL)
c75e9ee4 2774 pSubnet->findMacAddress(m_ipAddress, macAddr);
22d657d2
VK
2775 if (memcmp(macAddr, "\x00\x00\x00\x00\x00\x00", MAC_ADDR_LENGTH) && memcmp(macAddr, pInterface->getMacAddr(), MAC_ADDR_LENGTH))
2776 {
2777 TCHAR szOldMac[16], szNewMac[16];
2778
2779 BinToStr((BYTE *)pInterface->getMacAddr(), MAC_ADDR_LENGTH, szOldMac);
2780 BinToStr(macAddr, MAC_ADDR_LENGTH, szNewMac);
dab4332d 2781 DbgPrintf(5, _T("Node::updateInterfaceConfiguration(%s [%u]): MAC change for unknown interface: %s to %s"),
c42b4551
VK
2782 m_name, m_id, szOldMac, szNewMac);
2783 PostEvent(EVENT_MAC_ADDR_CHANGED, m_id, "idsss",
2784 pInterface->getId(), pInterface->getIfIndex(),
2785 pInterface->getName(), szOldMac, szNewMac);
22d657d2
VK
2786 pInterface->setMacAddr(macAddr);
2787 }
2788 }
eec253a8
VK
2789 }
2790 }
2791 else if (dwCount == 0)
2792 {
2793 // No interfaces at all, create pseudo-interface
c75e9ee4 2794 if (m_ipAddress.isValidUnicast())
baa5324c
AK
2795 {
2796 memset(macAddr, 0, MAC_ADDR_LENGTH);
c75e9ee4 2797 Subnet *pSubnet = FindSubnetForNode(m_zoneId, m_ipAddress);
baa5324c 2798 if (pSubnet != NULL)
c75e9ee4 2799 pSubnet->findMacAddress(m_ipAddress, macAddr);
22d657d2 2800 pMacAddr = !memcmp(macAddr, "\x00\x00\x00\x00\x00\x00", MAC_ADDR_LENGTH) ? NULL : macAddr;
baa5324c
AK
2801 TCHAR szMac[20];
2802 MACToStr(macAddr, szMac);
c42b4551 2803 DbgPrintf(5, _T("Node::updateInterfaceConfiguration(%s [%u]): got MAC for unknown interface: %s"), m_name, m_id, szMac);
bf2ad753
VK
2804 InetAddress ifaceAddr = m_ipAddress;
2805 ifaceAddr.setMaskBits(maskBits);
2806 createNewInterface(ifaceAddr, pMacAddr);
baa5324c 2807 }
eec253a8 2808 }
c42b4551 2809 DbgPrintf(6, _T("Node::updateInterfaceConfiguration(%s [%u]): pflist == NULL, dwCount = %u"), m_name, m_id, dwCount);
eec253a8
VK
2810 }
2811
72e97d1c
VK
2812 checkSubnetBinding(pIfList);
2813 delete pIfList;
2814
21c9acce 2815 sendPollerMsg(dwRqId, _T("Interface configuration check finished\r\n"));
eec253a8
VK
2816 return hasChanges;
2817}
2818
6fd6de0a 2819/**
7aad6641 2820 * Callback: apply template to nodes
6fd6de0a 2821 */
27de5dab 2822static void ApplyTemplate(NetObj *object, void *node)
4d0c32f3 2823{
c42b4551 2824 if ((object->getObjectClass() == OBJECT_TEMPLATE) && !object->isDeleted())
4d0c32f3 2825 {
27de5dab
VK
2826 Template *pTemplate = (Template *)object;
2827 if (pTemplate->isApplicable((Node *)node))
2828 {
c42b4551 2829 if (!pTemplate->isChild(((Node *)node)->getId()))
5039dede 2830 {
27de5dab 2831 DbgPrintf(4, _T("Node::ApplyUserTemplates(): applying template %d \"%s\" to node %d \"%s\""),
c42b4551 2832 pTemplate->getId(), pTemplate->getName(), ((Node *)node)->getId(), ((Node *)node)->getName());
6fd6de0a 2833 pTemplate->applyToTarget((Node *)node);
c42b4551 2834 PostEvent(EVENT_TEMPLATE_AUTOAPPLY, g_dwMgmtNode, "isis", ((Node *)node)->getId(), ((Node *)node)->getName(), pTemplate->getId(), pTemplate->getName());
5039dede 2835 }
27de5dab
VK
2836 }
2837 else
2838 {
c42b4551 2839 if (pTemplate->isAutoRemoveEnabled() && pTemplate->isChild(((Node *)node)->getId()))
5039dede 2840 {
27de5dab 2841 DbgPrintf(4, _T("Node::ApplyUserTemplates(): removing template %d \"%s\" from node %d \"%s\""),
c42b4551 2842 pTemplate->getId(), pTemplate->getName(), ((Node *)node)->getId(), ((Node *)node)->getName());
27de5dab
VK
2843 pTemplate->DeleteChild((Node *)node);
2844 ((Node *)node)->DeleteParent(pTemplate);
c42b4551
VK
2845 pTemplate->queueRemoveFromTarget(((Node *)node)->getId(), TRUE);
2846 PostEvent(EVENT_TEMPLATE_AUTOREMOVE, g_dwMgmtNode, "isis", ((Node *)node)->getId(), ((Node *)node)->getName(), pTemplate->getId(), pTemplate->getName());
5039dede 2847 }
27de5dab 2848 }
5039dede 2849 }
27de5dab
VK
2850}
2851
7aad6641
VK
2852/**
2853 * Apply user templates
2854 */
6fd6de0a 2855void Node::applyUserTemplates()
27de5dab
VK
2856{
2857 g_idxObjectById.forEach(ApplyTemplate, this);
4d0c32f3 2858}
5039dede 2859
6fd6de0a 2860/**
1201bac8 2861 * Filter for selecting containers from objects
6fd6de0a 2862 */
1201bac8 2863static bool ContainerSelectionFilter(NetObj *object, void *userData)
4d0c32f3 2864{
c42b4551 2865 return (object->getObjectClass() == OBJECT_CONTAINER) && !object->isDeleted() && ((Container *)object)->isAutoBindEnabled();
1201bac8
VK
2866}
2867
2868/**
2869 * Update container membership
2870 */
2871void Node::updateContainerMembership()
2872{
2873 ObjectArray<NetObj> *containers = g_idxObjectById.getObjects(true, ContainerSelectionFilter);
2874 for(int i = 0; i < containers->size(); i++)
5039dede 2875 {
1201bac8
VK
2876 Container *pContainer = (Container *)containers->get(i);
2877 if (pContainer->isSuitableForNode(this))
27de5dab 2878 {
c42b4551 2879 if (!pContainer->isChild(m_id))
4d0c32f3 2880 {
1201bac8 2881 DbgPrintf(4, _T("Node::updateContainerMembership(): binding node %d \"%s\" to container %d \"%s\""),
c42b4551 2882 m_id, m_name, pContainer->getId(), pContainer->getName());
1201bac8
VK
2883 pContainer->AddChild(this);
2884 AddParent(pContainer);
c42b4551 2885 PostEvent(EVENT_CONTAINER_AUTOBIND, g_dwMgmtNode, "isis", m_id, m_name, pContainer->getId(), pContainer->getName());
c75e9ee4 2886 pContainer->calculateCompoundStatus();
4d0c32f3 2887 }
27de5dab
VK
2888 }
2889 else
2890 {
c42b4551 2891 if (pContainer->isAutoUnbindEnabled() && pContainer->isChild(m_id))
4d0c32f3 2892 {
1201bac8 2893 DbgPrintf(4, _T("Node::updateContainerMembership(): removing node %d \"%s\" from container %d \"%s\""),
c42b4551 2894 m_id, m_name, pContainer->getId(), pContainer->getName());
1201bac8
VK
2895 pContainer->DeleteChild(this);
2896 DeleteParent(pContainer);
c42b4551 2897 PostEvent(EVENT_CONTAINER_AUTOUNBIND, g_dwMgmtNode, "isis", m_id, m_name, pContainer->getId(), pContainer->getName());
c75e9ee4 2898 pContainer->calculateCompoundStatus();
4d0c32f3 2899 }
27de5dab 2900 }
1201bac8 2901 pContainer->decRefCount();
5039dede 2902 }
1201bac8 2903 delete containers;
5039dede
AK
2904}
2905
a3050773 2906/**
805171de
VK
2907 * Perform instance discovery poll on node
2908 */
2909void Node::instanceDiscoveryPoll(ClientSession *session, UINT32 requestId, int pollerId)
2910{
2911 if (m_dwDynamicFlags & NDF_DELETE_IN_PROGRESS)
2912 {
2913 if (requestId == 0)
2914 m_dwDynamicFlags &= ~NDF_QUEUED_FOR_INSTANCE_POLL;
2915 return;
2916 }
2917
2918 SetPollerInfo(pollerId, _T("wait for lock"));
2919 pollerLock();
2920 m_pollRequestor = session;
2921 sendPollerMsg(requestId, _T("Starting instance discovery poll for node %s\r\n"), m_name);
2922 DbgPrintf(4, _T("Starting instance discovery poll for node %s (ID: %d)"), m_name, m_id);
2923
2924 // Check if node is marked as unreachable
2925 if (!(m_dwDynamicFlags & NDF_UNREACHABLE))
2926 {
2927 SetPollerInfo(pollerId, _T("instance discovery"));
2928 doInstanceDiscovery(requestId);
2929
2930 // Execute hook script
2931 SetPollerInfo(pollerId, _T("hook"));
2932 executeHookScript(_T("InstancePoll"));
2933 }
2934 else
2935 {
2936 sendPollerMsg(requestId, POLLER_WARNING _T("Node is marked as unreachable, instance discovery poll aborted\r\n"));
2937 DbgPrintf(4, _T("Node is marked as unreachable, instance discovery poll aborted"));
2938 }
2939
2940 m_lastInstancePoll = time(NULL);
2941
2942 // Finish instance discovery poll
2943 SetPollerInfo(pollerId, _T("cleanup"));
2944 if (requestId == 0)
2945 m_dwDynamicFlags &= ~NDF_QUEUED_FOR_INSTANCE_POLL;
2946 pollerUnlock();
2947 DbgPrintf(4, _T("Finished instance discovery poll for node %s (ID: %d)"), m_name, m_id);
2948}
2949
2950/**
d51f2182
VK
2951 * Do instance discovery
2952 */
f7e3eb5a 2953void Node::doInstanceDiscovery(UINT32 requestId)
d51f2182 2954{
f7e3eb5a
VK
2955 sendPollerMsg(requestId, _T("Running DCI instance discovery\r\n"));
2956
d51f2182
VK
2957 // collect instance discovery DCIs
2958 ObjectArray<DCItem> rootItems;
b06436f4 2959 lockDciAccess(false);
d51f2182
VK
2960 for(int i = 0; i < m_dcObjects->size(); i++)
2961 {
2962 DCObject *object = m_dcObjects->get(i);
2963 if ((object->getType() == DCO_TYPE_ITEM) && (((DCItem *)object)->getInstanceDiscoveryMethod() != IDM_NONE))
2964 {
2965 object->setBusyFlag(TRUE);
2966 rootItems.add((DCItem *)object);
2967 }
2968 }
2969 unlockDciAccess();
2970
2971 // process instance discovery DCIs
2972 // it should be done that way to prevent DCI list lock for long time
2973 for(int i = 0; i < rootItems.size(); i++)
2974 {
2975 DCItem *dci = rootItems.get(i);
2976 DbgPrintf(5, _T("Node::doInstanceDiscovery(%s [%u]): Updating instances for instance discovery DCI %s [%d]"),
c42b4551 2977 m_name, m_id, dci->getName(), dci->getId());
f7e3eb5a 2978 sendPollerMsg(requestId, _T(" Updating instances for %s [%d]\r\n"), dci->getName(), dci->getId());
a6312bd6 2979 StringMap *instances = getInstanceList(dci);
d51f2182
VK
2980 if (instances != NULL)
2981 {
c42b4551 2982 DbgPrintf(5, _T("Node::doInstanceDiscovery(%s [%u]): read %d values"), m_name, m_id, instances->size());
27bbb906 2983 dci->filterInstanceList(instances);
f7e3eb5a 2984 updateInstances(dci, instances, requestId);
d51f2182
VK
2985 delete instances;
2986 }
2987 else
2988 {
2989 DbgPrintf(5, _T("Node::doInstanceDiscovery(%s [%u]): failed to get instance list for DCI %s [%d]"),
c42b4551 2990 m_name, m_id, dci->getName(), dci->getId());
f7e3eb5a 2991 sendPollerMsg(requestId, POLLER_ERROR _T(" Failed to get instance list\r\n"));
d51f2182
VK
2992 }
2993 dci->setBusyFlag(FALSE);
2994 }