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