fixed bug in OpenSSL detection
[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
7aad6641 25/**
c50f77f7
VK
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 628 {
9c5ebc32 629 BridgeMapPorts(pTransport, pIfList);
eec253a8 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
a3050773 883/**
8f26db67
VK
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
923/**
386f88e3
VK
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
944/**
a3050773
VK
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
a3050773 965/**
e55ff6f9
VK
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
1017/**
e95680e5
VK
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
1029/**
a3050773
VK
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
a5ee7b3b 1207/**
208d7427
VK
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
1225/**
a5ee7b3b
VK
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 1305 }
23c1cb5d
VK
1306 else if ((dwResult == SNMP_ERR_ENGINE_ID) && (m_snmpVersion == SNMP_VERSION_3))
1307 {
1308 // Reset authoritative engine data
1309 lockProperties();
1310 m_snmpSecurity->setAuthoritativeEngine(SNMP_Engine());
1311 unlockProperties();
1312 goto restart_agent_check;
1313 }
5039dede
AK
1314 else
1315 {
61b359bc
VK
1316 sendPollerMsg(dwRqId, POLLER_ERROR _T("SNMP agent unreachable\r\n"));
1317 if (m_dwDynamicFlags & NDF_SNMP_UNREACHABLE)
1318 {
1319 if ((tNow > m_failTimeSNMP + tExpire) &&
1320 (!(m_dwDynamicFlags & NDF_UNREACHABLE)))
1321 {
1322 m_dwFlags &= ~NF_IS_SNMP;
1323 m_dwDynamicFlags &= ~NDF_SNMP_UNREACHABLE;
1324 m_szObjectId[0] = 0;
1325 sendPollerMsg(dwRqId, POLLER_WARNING _T("Attribute isSNMP set to FALSE\r\n"));
1326 }
1327 }
1328 else
1329 {
1330 m_dwDynamicFlags |= NDF_SNMP_UNREACHABLE;
c42b4551 1331 PostEventEx(pQueue, EVENT_SNMP_FAIL, m_id, NULL);
61b359bc
VK
1332 m_failTimeSNMP = tNow;
1333 }
5039dede 1334 }
61b359bc
VK
1335 delete pTransport;
1336 }
1337 else
1338 {
c42b4551 1339 DbgPrintf(6, _T("StatusPoll(%s): cannot create SNMP transport"), m_name);
5039dede 1340 }
c42b4551 1341 DbgPrintf(6, _T("StatusPoll(%s): SNMP check finished"), m_name);
5039dede
AK
1342 }
1343
1344 // Check native agent connectivity
c75e9ee4 1345 if ((m_dwFlags & NF_IS_NATIVE_AGENT) && (!(m_dwFlags & NF_DISABLE_NXCP)) && m_ipAddress.isValidUnicast())
5039dede 1346 {
c42b4551 1347 DbgPrintf(6, _T("StatusPoll(%s): checking agent"), m_name);
208d7427 1348 poller->setStatus(_T("check agent"));
21c9acce 1349 sendPollerMsg(dwRqId, _T("Checking NetXMS agent connectivity\r\n"));
c3acd0f6 1350
967893bb 1351 UINT32 error, socketError;
a01c2a20 1352 bool newConnection;
a4569c4d 1353 agentLock();
a01c2a20 1354 if (connectToAgent(&error, &socketError, &newConnection, true))
5039dede 1355 {
c42b4551 1356 DbgPrintf(7, _T("StatusPoll(%s): connected to agent"), m_name);
5039dede
AK
1357 if (m_dwDynamicFlags & NDF_AGENT_UNREACHABLE)
1358 {
1359 m_dwDynamicFlags &= ~NDF_AGENT_UNREACHABLE;
c42b4551 1360 PostEventEx(pQueue, EVENT_AGENT_OK, m_id, NULL);
21c9acce 1361 sendPollerMsg(dwRqId, POLLER_INFO _T("Connectivity with NetXMS agent restored\r\n"));
5039dede 1362 }
5039dede
AK
1363 }
1364 else
1365 {
c42b4551 1366 DbgPrintf(6, _T("StatusPoll(%s): agent unreachable, error=%d, socketError=%d"), m_name, (int)error, (int)socketError);
21c9acce 1367 sendPollerMsg(dwRqId, POLLER_ERROR _T("NetXMS agent unreachable\r\n"));
5039dede
AK
1368 if (m_dwDynamicFlags & NDF_AGENT_UNREACHABLE)
1369 {
71e4ed3a 1370 if ((tNow > m_failTimeAgent + tExpire) && !(m_dwDynamicFlags & NDF_UNREACHABLE))
5039dede
AK
1371 {
1372 m_dwFlags &= ~NF_IS_NATIVE_AGENT;
1373 m_dwDynamicFlags &= ~NDF_AGENT_UNREACHABLE;
1374 m_szPlatformName[0] = 0;
1375 m_szAgentVersion[0] = 0;
21c9acce 1376 sendPollerMsg(dwRqId, POLLER_WARNING _T("Attribute isNetXMSAgent set to FALSE\r\n"));
5039dede
AK
1377 }
1378 }
1379 else
1380 {
1381 m_dwDynamicFlags |= NDF_AGENT_UNREACHABLE;
c42b4551 1382 PostEventEx(pQueue, EVENT_AGENT_FAIL, m_id, NULL);
8573e935 1383 m_failTimeAgent = tNow;
0ab347c0 1384 //cancel file monitoring locally(on agent it is canceled if agent have fallen)
f9f79a93 1385 g_monitoringList.removeDisconnectedNode(m_id);
5039dede
AK
1386 }
1387 }
a4569c4d 1388 agentUnlock();
c42b4551 1389 DbgPrintf(7, _T("StatusPoll(%s): agent check finished"), m_name);
f9f79a93
VK
1390
1391 // If file update connection is active, send NOP command to prevent disconnection by idle timeout
1392 AgentConnection *fileUpdateConnection;
1393 lockProperties();
1394 fileUpdateConnection = m_fileUpdateConn;
1395 if (fileUpdateConnection != NULL)
1396 fileUpdateConnection->incRefCount();
1397 unlockProperties();
1398 if (fileUpdateConnection != NULL)
1399 {
1400 nxlog_debug(6, _T("StatusPoll(%s): sending keepalive command on file monitoring connection"), m_name);
1401 fileUpdateConnection->nop();
1402 fileUpdateConnection->decRefCount();
1403 }
5039dede
AK
1404 }
1405
208d7427 1406 poller->setStatus(_T("prepare polling list"));
5039dede
AK
1407
1408 // Find service poller node object
c42b4551 1409 lockProperties();
9208c84b 1410 if (m_pollerNode != 0)
5039dede 1411 {
9208c84b 1412 UINT32 id = m_pollerNode;
c42b4551 1413 unlockProperties();
f3218755 1414 pPollerNode = FindObjectById(id);
5039dede
AK
1415 if (pPollerNode != NULL)
1416 {
c42b4551 1417 if (pPollerNode->getObjectClass() != OBJECT_NODE)
5039dede
AK
1418 pPollerNode = NULL;
1419 }
1420 }
f3218755
VK
1421 else
1422 {
c42b4551 1423 unlockProperties();
f3218755 1424 }
5039dede
AK
1425
1426 // If nothing found, use management server
1427 if (pPollerNode == NULL)
1428 {
1429 pPollerNode = FindObjectById(g_dwMgmtNode);
1430 if (pPollerNode != NULL)
21c9acce 1431 pPollerNode->incRefCount();
5039dede
AK
1432 }
1433 else
1434 {
21c9acce 1435 pPollerNode->incRefCount();
5039dede
AK
1436 }
1437
1438 // Create polling list
1439 ppPollList = (NetObj **)malloc(sizeof(NetObj *) * m_dwChildCount);
1440 LockChildList(FALSE);
1441 for(i = 0, dwPollListSize = 0; i < m_dwChildCount; i++)
1442 if (m_pChildList[i]->Status() != STATUS_UNMANAGED)
1443 {
21c9acce 1444 m_pChildList[i]->incRefCount();
5039dede
AK
1445 ppPollList[dwPollListSize++] = m_pChildList[i];
1446 }
1447 UnlockChildList();
1448
1449 // Poll interfaces and services
208d7427 1450 poller->setStatus(_T("child poll"));
c42b4551 1451 DbgPrintf(7, _T("StatusPoll(%s): starting child object poll"), m_name);
7c521895 1452 pCluster = getMyCluster();
cd9f247e 1453 pTransport = createSnmpTransport();
5039dede
AK
1454 for(i = 0; i < dwPollListSize; i++)
1455 {
c42b4551 1456 switch(ppPollList[i]->getObjectClass())
5039dede
AK
1457 {
1458 case OBJECT_INTERFACE:
c42b4551 1459 DbgPrintf(7, _T("StatusPoll(%s): polling interface %d [%s]"), m_name, ppPollList[i]->getId(), ppPollList[i]->getName());
c30c0c0f 1460 ((Interface *)ppPollList[i])->statusPoll(pSession, dwRqId, pQueue, pCluster, pTransport, m_icmpProxy);
5039dede
AK
1461 break;
1462 case OBJECT_NETWORKSERVICE:
c42b4551 1463 DbgPrintf(7, _T("StatusPoll(%s): polling network service %d [%s]"), m_name, ppPollList[i]->getId(), ppPollList[i]->getName());
0910fe05 1464 ((NetworkService *)ppPollList[i])->statusPoll(pSession, dwRqId, (Node *)pPollerNode, pQueue);
5039dede 1465 break;
56d5289b 1466 case OBJECT_ACCESSPOINT:
c42b4551 1467 DbgPrintf(7, _T("StatusPoll(%s): polling access point %d [%s]"), m_name, ppPollList[i]->getId(), ppPollList[i]->getName());
ac58ffe9 1468 ((AccessPoint *)ppPollList[i])->statusPoll(pSession, dwRqId, pQueue, this, pTransport);
56d5289b 1469 break;
5039dede 1470 default:
c42b4551 1471 DbgPrintf(7, _T("StatusPoll(%s): skipping object %d [%s] class %d"), m_name, ppPollList[i]->getId(), ppPollList[i]->getName(), ppPollList[i]->getObjectClass());
5039dede
AK
1472 break;
1473 }
21c9acce 1474 ppPollList[i]->decRefCount();
5039dede
AK
1475 }
1476 delete pTransport;
1477 safe_free(ppPollList);
c42b4551 1478 DbgPrintf(7, _T("StatusPoll(%s): finished child object poll"), m_name);
5039dede
AK
1479
1480 // Check if entire node is down
15d6f8c9 1481 // This check is disabled for nodes without IP address
c75e9ee4 1482 if (m_ipAddress.isValidUnicast())
5039dede 1483 {
59176978 1484 bool allDown = true;
15d6f8c9
VK
1485 LockChildList(FALSE);
1486 if (m_dwChildCount > 0)
1487 {
59176978 1488 for(i = 0; i < m_dwChildCount; i++)
c42b4551 1489 if ((m_pChildList[i]->getObjectClass() == OBJECT_INTERFACE) &&
59176978 1490 (((Interface *)m_pChildList[i])->getAdminState() != IF_ADMIN_STATE_DOWN) &&
93be072b 1491 (((Interface *)m_pChildList[i])->getConfirmedOperState() == IF_OPER_STATE_UP) &&
59176978 1492 (m_pChildList[i]->Status() != STATUS_UNMANAGED))
15d6f8c9 1493 {
59176978 1494 allDown = false;
15d6f8c9
VK
1495 break;
1496 }
1497 }
15d6f8c9 1498 UnlockChildList();
59176978 1499 if (allDown && (m_dwFlags & NF_IS_NATIVE_AGENT) &&
15d6f8c9
VK
1500 (!(m_dwFlags & NF_DISABLE_NXCP)))
1501 if (!(m_dwDynamicFlags & NDF_AGENT_UNREACHABLE))
59176978
VK
1502 allDown = false;
1503 if (allDown && (m_dwFlags & NF_IS_SNMP) &&
15d6f8c9
VK
1504 (!(m_dwFlags & NF_DISABLE_SNMP)))
1505 if (!(m_dwDynamicFlags & NDF_SNMP_UNREACHABLE))
59176978 1506 allDown = false;
c1482463 1507
59176978
VK
1508 DbgPrintf(6, _T("StatusPoll(%s): allDown=%s, dynFlags=0x%08X"), m_name, allDown ? _T("true") : _T("false"), m_dwDynamicFlags);
1509 if (allDown)
15d6f8c9
VK
1510 {
1511 if (!(m_dwDynamicFlags & NDF_UNREACHABLE))
1512 {
1513 m_dwDynamicFlags |= NDF_UNREACHABLE;
8573e935 1514 m_downSince = time(NULL);
208d7427 1515 poller->setStatus(_T("check network path"));
c1482463
VK
1516 if (checkNetworkPath(dwRqId))
1517 {
1518 m_dwDynamicFlags |= NDF_NETWORK_PATH_PROBLEM;
1519
1520 // Set interfaces and network services to UNKNOWN state
1521 LockChildList(FALSE);
59176978 1522 for(i = 0; i < m_dwChildCount; i++)
c42b4551 1523 if (((m_pChildList[i]->getObjectClass() == OBJECT_INTERFACE) || (m_pChildList[i]->getObjectClass() == OBJECT_NETWORKSERVICE)) &&
c1482463
VK
1524 (m_pChildList[i]->Status() == STATUS_CRITICAL))
1525 {
1526 m_pChildList[i]->resetStatus();
1527 }
1528 UnlockChildList();
1529
1530 // Clear delayed event queue
1531 while(1)
1532 {
19dbc8ef 1533 Event *pEvent = (Event *)pQueue->get();
c1482463
VK
1534 if (pEvent == NULL)
1535 break;
1536 delete pEvent;
1537 }
1538 delete_and_null(pQueue);
1539
c42b4551 1540 PostEvent(EVENT_NODE_UNREACHABLE, m_id, NULL);
c1482463
VK
1541 }
1542 else
1543 {
c42b4551 1544 PostEvent(EVENT_NODE_DOWN, m_id, NULL);
c1482463 1545 }
21c9acce 1546 sendPollerMsg(dwRqId, POLLER_ERROR _T("Node is unreachable\r\n"));
15d6f8c9
VK
1547 }
1548 else
1549 {
21c9acce 1550 sendPollerMsg(dwRqId, POLLER_WARNING _T("Node is still unreachable\r\n"));
15d6f8c9
VK
1551 }
1552 }
1553 else
1554 {
8573e935 1555 m_downSince = 0;
15d6f8c9
VK
1556 if (m_dwDynamicFlags & NDF_UNREACHABLE)
1557 {
a26861ad 1558 int reason = (m_dwDynamicFlags & NDF_NETWORK_PATH_PROBLEM) ? 1 : 0;
c1482463 1559 m_dwDynamicFlags &= ~(NDF_UNREACHABLE | NDF_SNMP_UNREACHABLE | NDF_AGENT_UNREACHABLE | NDF_NETWORK_PATH_PROBLEM);
c42b4551 1560 PostEvent(EVENT_NODE_UP, m_id, "d", reason);
21c9acce 1561 sendPollerMsg(dwRqId, POLLER_INFO _T("Node recovered from unreachable state\r\n"));
15d6f8c9
VK
1562 goto restart_agent_check;
1563 }
1564 else
1565 {
21c9acce 1566 sendPollerMsg(dwRqId, POLLER_INFO _T("Node is connected\r\n"));
15d6f8c9
VK
1567 }
1568 }
5039dede 1569 }
5039dede 1570
71e4ed3a
VK
1571 // Get uptime and update boot time
1572 if (!(m_dwDynamicFlags & NDF_UNREACHABLE))
1573 {
1574 TCHAR buffer[MAX_RESULT_LENGTH];
1575 if (getItemFromAgent(_T("System.Uptime"), MAX_RESULT_LENGTH, buffer) == DCE_SUCCESS)
1576 {
1577 m_bootTime = time(NULL) - _tcstol(buffer, NULL, 0);
c42b4551 1578 DbgPrintf(5, _T("StatusPoll(%s [%d]): boot time set to %u from agent"), m_name, m_id, (UINT32)m_bootTime);
71e4ed3a 1579 }
296ae03d 1580 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
1581 {
1582 m_bootTime = time(NULL) - _tcstol(buffer, NULL, 0) / 100; // sysUpTime is in hundredths of a second
c42b4551 1583 DbgPrintf(5, _T("StatusPoll(%s [%d]): boot time set to %u from SNMP"), m_name, m_id, (UINT32)m_bootTime);
71e4ed3a
VK
1584 }
1585 else
1586 {
c42b4551 1587 DbgPrintf(5, _T("StatusPoll(%s [%d]): unable to get system uptime"), m_name, m_id);
71e4ed3a
VK
1588 }
1589 }
1590 else
1591 {
1592 m_bootTime = 0;
1593 }
1594
0ab347c0 1595 // Get agent uptime to check if it was restared
1596 if (!(m_dwDynamicFlags & NDF_UNREACHABLE))
1597 {
1598 TCHAR buffer[MAX_RESULT_LENGTH];
1599 if (getItemFromAgent(_T("Agent.Uptime"), MAX_RESULT_LENGTH, buffer) == DCE_SUCCESS)
1600 {
1601 time_t oldAgentuptime = m_agentUpTime;
1602 m_agentUpTime = _tcstol(buffer, NULL, 0);
2e22746d 1603 if ((UINT32)oldAgentuptime > (UINT32)m_agentUpTime)
0ab347c0 1604 {
1605 //cancel file monitoring locally(on agent it is canceled if agent have fallen)
f9f79a93 1606 g_monitoringList.removeDisconnectedNode(m_id);
0ab347c0 1607 }
1608 }
1609 else
1610 {
c42b4551 1611 DbgPrintf(5, _T("StatusPoll(%s [%d]): unable to get agent uptime"), m_name, m_id);
f9f79a93 1612 g_monitoringList.removeDisconnectedNode(m_id);
0ab347c0 1613 m_agentUpTime = 0;
1614 }
1615 }
1616 else
1617 {
f9f79a93 1618 g_monitoringList.removeDisconnectedNode(m_id);
0ab347c0 1619 m_agentUpTime = 0;
1620 }
1621
2e22746d
VK
1622 // Get geolocation
1623 if (!(m_dwDynamicFlags & NDF_UNREACHABLE))
1624 {
1625 TCHAR buffer[MAX_RESULT_LENGTH];
1626 if (getItemFromAgent(_T("GPS.LocationData"), MAX_RESULT_LENGTH, buffer) == DCE_SUCCESS)
1627 {
1628 GeoLocation loc = GeoLocation::parseAgentData(buffer);
1629 if (loc.getType() != GL_UNSET)
1630 {
1631 DbgPrintf(5, _T("StatusPoll(%s [%d]): location set to %s, %s from agent"), m_name, m_id, loc.getLatitudeAsString(), loc.getLongitudeAsString());
1632 lockProperties();
1633 m_geoLocation = loc;
1634 setModified();
1635 unlockProperties();
1636 }
1637 }
1638 else
1639 {
1640 DbgPrintf(5, _T("StatusPoll(%s [%d]): unable to get system location"), m_name, m_id);
1641 }
1642 }
1643
849e464d 1644 // Get agent log and agent local database status
b123ecb3 1645 if (!(m_dwDynamicFlags & NDF_UNREACHABLE))
1646 {
1647 TCHAR buffer[MAX_RESULT_LENGTH];
7b30e5ae 1648 if (getItemFromAgent(_T("Agent.LogFile.Status"), MAX_RESULT_LENGTH, buffer) == DCE_SUCCESS)
b123ecb3 1649 {
1650 UINT32 status = _tcstol(buffer, NULL, 0);
7b30e5ae
VK
1651 if (status != 0)
1652 PostEvent(EVENT_AGENT_LOG_PROBLEM, m_id, "ds", status, _T("could not open"));
b123ecb3 1653 }
1654 else
1655 {
7b30e5ae 1656 nxlog_debug(5, _T("StatusPoll(%s [%d]): unable to get agent log status"), m_name, m_id);
b123ecb3 1657 }
1658
7b30e5ae 1659 if (getItemFromAgent(_T("Agent.LocalDatabase.Status"), MAX_RESULT_LENGTH, buffer) == DCE_SUCCESS)
b123ecb3 1660 {
1661 UINT32 status = _tcstol(buffer, NULL, 0);
1662 const TCHAR *statusDescription[3]= {
1663 _T("normal"),
1664 _T("could not open database"),
1665 _T("could not update database"),
1666 };
7b30e5ae
VK
1667 if (status != 0)
1668 PostEvent(EVENT_AGENT_LOCAL_DATABASE_PROBLEM, m_id, "ds", status, statusDescription[status]);
b123ecb3 1669 }
1670 else
1671 {
7b30e5ae 1672 nxlog_debug(5, _T("StatusPoll(%s [%d]): unable to get agent local database status"), m_name, m_id);
b123ecb3 1673 }
1674 }
1675
5039dede 1676 // Send delayed events and destroy delayed event queue
c1482463
VK
1677 if (pQueue != NULL)
1678 {
1679 ResendEvents(pQueue);
1680 delete pQueue;
1681 }
a5ee7b3b
VK
1682
1683 // Call hooks in loaded modules
967893bb 1684 for(UINT32 i = 0; i < g_dwNumModules; i++)
a5ee7b3b
VK
1685 {
1686 if (g_pModuleList[i].pfStatusPollHook != NULL)
1687 {
c42b4551 1688 DbgPrintf(5, _T("StatusPoll(%s [%d]): calling hook in module %s"), m_name, m_id, g_pModuleList[i].szName);
208d7427 1689 g_pModuleList[i].pfStatusPollHook(this, pSession, dwRqId, poller);
a5ee7b3b
VK
1690 }
1691 }
4d2c3a54 1692
a5ee7b3b 1693 // Execute hook script
208d7427 1694 poller->setStatus(_T("hook"));
a5ee7b3b
VK
1695 executeHookScript(_T("StatusPoll"));
1696
208d7427 1697 poller->setStatus(_T("cleanup"));
5039dede 1698 if (pPollerNode != NULL)
21c9acce 1699 pPollerNode->decRefCount();
5039dede
AK
1700
1701 if (dwOldFlags != m_dwFlags)
1702 {
c42b4551
VK
1703 PostEvent(EVENT_NODE_FLAGS_CHANGED, m_id, "xx", dwOldFlags, m_dwFlags);
1704 lockProperties();
1705 setModified();
1706 unlockProperties();
5039dede
AK
1707 }
1708
27f9598d 1709 calculateCompoundStatus();
8573e935 1710 m_lastStatusPoll = time(NULL);
c42b4551 1711 sendPollerMsg(dwRqId, _T("Finished status poll for node %s\r\n"), m_name);
6fec127d 1712 sendPollerMsg(dwRqId, _T("Node status after poll is %s\r\n"), GetStatusAsText(m_iStatus, true));
c59466d2 1713 m_pollRequestor = NULL;
5039dede
AK
1714 if (dwRqId == 0)
1715 m_dwDynamicFlags &= ~NDF_QUEUED_FOR_STATUS_POLL;
7c521895 1716 pollerUnlock();
c42b4551 1717 DbgPrintf(5, _T("Finished status poll for node %s (ID: %d)"), m_name, m_id);
5039dede
AK
1718}
1719
7946bd7c 1720/**
1babf64d 1721 * Check single element of network path
beae365a 1722 */
967893bb 1723bool Node::checkNetworkPathElement(UINT32 nodeId, const TCHAR *nodeType, bool isProxy, UINT32 dwRqId)
beae365a
VK
1724{
1725 Node *node = (Node *)FindObjectById(nodeId, OBJECT_NODE);
1726 if (node == NULL)
1727 return false;
1728
c42b4551 1729 DbgPrintf(6, _T("Node::checkNetworkPathElement(%s [%d]): found %s: %s [%d]"), m_name, m_id, nodeType, node->getName(), node->getId());
beae365a
VK
1730 if (node->isDown())
1731 {
1732 DbgPrintf(5, _T("Node::checkNetworkPathElement(%s [%d]): %s %s [%d] is down"),
c42b4551
VK
1733 m_name, m_id, nodeType, node->getName(), node->getId());
1734 sendPollerMsg(dwRqId, POLLER_WARNING _T(" %s %s is down\r\n"), nodeType, node->getName());
beae365a
VK
1735 return true;
1736 }
66d15dff
VK
1737 if (isProxy && node->isNativeAgent() && (node->getRuntimeFlags() & NDF_AGENT_UNREACHABLE))
1738 {
1739 DbgPrintf(5, _T("Node::checkNetworkPathElement(%s [%d]): agent on %s %s [%d] is down"),
c42b4551
VK
1740 m_name, m_id, nodeType, node->getName(), node->getId());
1741 sendPollerMsg(dwRqId, POLLER_WARNING _T(" Agent on %s %s is down\r\n"), nodeType, node->getName());
66d15dff
VK
1742 return true;
1743 }
8573e935 1744 if (node->m_lastStatusPoll < time(NULL) - 1)
beae365a
VK
1745 {
1746 DbgPrintf(6, _T("Node::checkNetworkPathElement(%s [%d]): forced status poll on node %s [%d]"),
c42b4551 1747 m_name, m_id, node->getName(), node->getId());
208d7427
VK
1748 PollerInfo *poller = RegisterPoller(POLLER_TYPE_STATUS, node);
1749 poller->startExecution();
1750 node->statusPoll(NULL, 0, poller);
1751 delete poller;
beae365a
VK
1752 if (node->isDown())
1753 {
1754 DbgPrintf(5, _T("Node::checkNetworkPathElement(%s [%d]): %s %s [%d] is down"),
c42b4551
VK
1755 m_name, m_id, nodeType, node->getName(), node->getId());
1756 sendPollerMsg(dwRqId, POLLER_WARNING _T(" %s %s is down\r\n"), nodeType, node->getName());
beae365a
VK
1757 return true;
1758 }
66d15dff
VK
1759 if (isProxy && node->isNativeAgent() && (node->getRuntimeFlags() & NDF_AGENT_UNREACHABLE))
1760 {
1761 DbgPrintf(5, _T("Node::checkNetworkPathElement(%s [%d]): agent on %s %s [%d] is down"),
c42b4551
VK
1762 m_name, m_id, nodeType, node->getName(), node->getId());
1763 sendPollerMsg(dwRqId, POLLER_WARNING _T(" Agent on %s %s is down\r\n"), nodeType, node->getName());
66d15dff
VK
1764 return true;
1765 }
beae365a
VK
1766 }
1767 return false;
1768}
1769
1770/**
7946bd7c 1771 * Check network path between node and management server to detect possible intermediate node failure
c1482463
VK
1772 *
1773 * @return true if network path problems found
7946bd7c 1774 */
967893bb 1775bool Node::checkNetworkPath(UINT32 dwRqId)
7946bd7c 1776{
67c45b4d
VK
1777 time_t now = time(NULL);
1778
beae365a
VK
1779 // Check proxy node(s)
1780 if (IsZoningEnabled() && (m_zoneId != 0))
1781 {
1782 Zone *zone = (Zone *)g_idxZoneByGUID.get(m_zoneId);
1783 if ((zone != NULL) && ((zone->getAgentProxy() != 0) || (zone->getSnmpProxy() != 0) || (zone->getIcmpProxy() != 0)))
1784 {
1785 bool allProxyDown = true;
1786 if (zone->getIcmpProxy() != 0)
66d15dff 1787 allProxyDown = checkNetworkPathElement(zone->getIcmpProxy(), _T("ICMP proxy"), true, dwRqId);
beae365a 1788 if (allProxyDown && (zone->getSnmpProxy() != 0) && (zone->getSnmpProxy() != zone->getIcmpProxy()))
66d15dff 1789 allProxyDown = checkNetworkPathElement(zone->getSnmpProxy(), _T("SNMP proxy"), true, dwRqId);
beae365a 1790 if (allProxyDown && (zone->getAgentProxy() != 0) && (zone->getAgentProxy() != zone->getIcmpProxy()) && (zone->getAgentProxy() != zone->getSnmpProxy()))
66d15dff 1791 allProxyDown = checkNetworkPathElement(zone->getAgentProxy(), _T("agent proxy"), true, dwRqId);
beae365a
VK
1792 if (allProxyDown)
1793 return true;
1794 }
1795 }
1796
67c45b4d
VK
1797 // Check directly connected switch
1798 sendPollerMsg(dwRqId, _T("Checking ethernet connectivity...\r\n"));
c75e9ee4 1799 Interface *iface = findInterfaceByIP(m_ipAddress);
664b42a4 1800 if ((iface != NULL) && (iface->getPeerNodeId() != 0))
67c45b4d 1801 {
c42b4551 1802 DbgPrintf(6, _T("Node::checkNetworkPath(%s [%d]): found interface object for primary IP: %s [%d]"), m_name, m_id, iface->getName(), iface->getId());
66d15dff 1803 if (checkNetworkPathElement(iface->getPeerNodeId(), _T("upstream switch"), false, dwRqId))
beae365a 1804 return true;
67c45b4d 1805 }
664b42a4
VK
1806 else
1807 {
c42b4551 1808 DbgPrintf(5, _T("Node::checkNetworkPath(%s [%d]): cannot find interface object for primary IP"), m_name, m_id);
664b42a4 1809 }
67c45b4d 1810
7946bd7c
VK
1811 Node *mgmtNode = (Node *)FindObjectById(g_dwMgmtNode);
1812 if (mgmtNode == NULL)
1813 {
c42b4551 1814 DbgPrintf(5, _T("Node::checkNetworkPath(%s [%d]): cannot find management node"), m_name, m_id);
c1482463 1815 return false;
7946bd7c 1816 }
5039dede 1817
7946bd7c
VK
1818 NetworkPath *trace = TraceRoute(mgmtNode, this);
1819 if (trace == NULL)
1820 {
c42b4551 1821 DbgPrintf(5, _T("Node::checkNetworkPath(%s [%d]): trace not available"), m_name, m_id);
c1482463 1822 return false;
7946bd7c
VK
1823 }
1824 DbgPrintf(5, _T("Node::checkNetworkPath(%s [%d]): trace available, %d hops, %s"),
c42b4551 1825 m_name, m_id, trace->getHopCount(), trace->isComplete() ? _T("complete") : _T("incomplete"));
7946bd7c
VK
1826
1827 // We will do path check in two passes
1828 // If unreachable intermediate node will be found on first pass,
1829 // then method will just return true. Otherwise, we will do
1830 // second pass, this time forcing status poll on each node in the path.
21c9acce 1831 sendPollerMsg(dwRqId, _T("Checking network path...\r\n"));
7946bd7c
VK
1832 bool secondPass = false;
1833 bool pathProblemFound = false;
1834restart:
1835 for(int i = 0; i < trace->getHopCount(); i++)
1836 {
1837 HOP_INFO *hop = trace->getHopInfo(i);
c42b4551 1838 if ((hop->object == NULL) || (hop->object == this) || (hop->object->getObjectClass() != OBJECT_NODE))
7946bd7c
VK
1839 continue;
1840
1841 DbgPrintf(6, _T("Node::checkNetworkPath(%s [%d]): checking upstream node %s [%d]"),
c42b4551 1842 m_name, m_id, hop->object->getName(), hop->object->getId());
8573e935 1843 if (secondPass && !((Node *)hop->object)->isDown() && (((Node *)hop->object)->m_lastStatusPoll < now - 1))
7946bd7c
VK
1844 {
1845 DbgPrintf(6, _T("Node::checkNetworkPath(%s [%d]): forced status poll on node %s [%d]"),
c42b4551 1846 m_name, m_id, hop->object->getName(), hop->object->getId());
208d7427
VK
1847 PollerInfo *poller = RegisterPoller(POLLER_TYPE_STATUS, (Node *)hop->object);
1848 poller->startExecution();
1849 ((Node *)hop->object)->statusPoll(NULL, 0, poller);
1850 delete poller;
7946bd7c 1851 }
1f385e47 1852
7946bd7c
VK
1853 if (((Node *)hop->object)->isDown())
1854 {
1855 DbgPrintf(5, _T("Node::checkNetworkPath(%s [%d]): upstream node %s [%d] is down"),
c42b4551
VK
1856 m_name, m_id, hop->object->getName(), hop->object->getId());
1857 sendPollerMsg(dwRqId, POLLER_WARNING _T(" Upstream node %s is down\r\n"), hop->object->getName());
7946bd7c
VK
1858 pathProblemFound = true;
1859 break;
1860 }
1861 }
1862 if (!secondPass && !pathProblemFound)
1863 {
c42b4551 1864 DbgPrintf(5, _T("Node::checkNetworkPath(%s [%d]): will do second pass"), m_name, m_id);
7946bd7c
VK
1865 secondPass = true;
1866 goto restart;
1867 }
1868 delete trace;
c1482463 1869 return pathProblemFound;
7946bd7c
VK
1870}
1871
1872/**
1873 * Check agent policy binding
1874 * Intended to be called only from configuration poller
1875 */
1f385e47
VK
1876void Node::checkAgentPolicyBinding(AgentConnection *conn)
1877{
1878 AgentPolicyInfo *ap;
967893bb 1879 UINT32 rcc = conn->getPolicyInventory(&ap);
1f385e47
VK
1880 if (rcc == ERR_SUCCESS)
1881 {
1882 // Check for unbound but installed policies
a6312bd6 1883 for(int i = 0; i < ap->size(); i++)
1f385e47 1884 {
de4af576 1885 uuid guid = ap->getGuid(i);
1f385e47 1886 NetObj *object = FindObjectByGUID(guid, -1);
c42b4551 1887 if ((object != NULL) && (!object->isChild(m_id)))
1f385e47 1888 {
1f8be1f4
VK
1889 object->addChild(this);
1890 addParent(object);
c42b4551 1891 DbgPrintf(5, _T("ConfPoll(%s): bound to policy object %s [%d]"), m_name, object->getName(), object->getId());
1f385e47
VK
1892 }
1893 }
1894
28e59088
TD
1895 // Check for bound but not installed policies and schedule it's installation again
1896 //Job will be unbound if it was not possible to add job
1f385e47
VK
1897 LockParentList(FALSE);
1898 NetObj **unbindList = (NetObj **)malloc(sizeof(NetObj *) * m_dwParentCount);
28e59088 1899 int unbindListSize = 0;
967893bb 1900 for(UINT32 i = 0; i < m_dwParentCount; i++)
1f385e47
VK
1901 {
1902 if (IsAgentPolicyObject(m_pParentList[i]))
1903 {
de4af576
VK
1904 const uuid& guid = m_pParentList[i]->getGuid();
1905 int j;
a6312bd6 1906 for(j = 0; j < ap->size(); j++)
1f385e47 1907 {
4e5e861c 1908 if (ap->getGuid(j).equals(guid))
1f385e47
VK
1909 break;
1910 }
a6312bd6 1911 if (j == ap->size())
28e59088
TD
1912 {
1913 ServerJob *job = new PolicyDeploymentJob(this, (AgentPolicy *)m_pParentList[i], 0); //TODO: change to system user
1914 if (AddJob(job))
1915 {
b123ecb3 1916 DbgPrintf(5, _T("ConfPoll(%s): \"%s\" policy deploy scheduled for \"%s\" node"), m_name, m_pParentList[i]->getName(), m_name );
28e59088
TD
1917 }
1918 else
1919 {
b123ecb3 1920 DbgPrintf(5, _T("ConfPoll(%s): \"%s\" policy deploy is not possible to scheduled for \"%s\" node"), m_name, m_pParentList[i]->getName(), m_name);
28e59088
TD
1921 delete job;
1922 unbindList[unbindListSize++] = m_pParentList[i];
1923 }
1924 }
1f385e47
VK
1925 }
1926 }
1927 UnlockParentList();
1928
1929 for(int i = 0; i < unbindListSize; i++)
1930 {
1f8be1f4
VK
1931 unbindList[i]->deleteChild(this);
1932 deleteParent(unbindList[i]);
c42b4551 1933 DbgPrintf(5, _T("ConfPoll(%s): unbound from policy object %s [%d]"), m_name, unbindList[i]->getName(), unbindList[i]->getId());
1f385e47
VK
1934 }
1935 safe_free(unbindList);
1936
1937 delete ap;
1938 }
1939 else
1940 {
c42b4551 1941 DbgPrintf(5, _T("ConfPoll(%s): AgentConnection::getPolicyInventory() failed: rcc=%d"), m_name, rcc);
1f385e47
VK
1942 }
1943}
1944
a3050773
VK
1945/**
1946 * Update primary IP address from primary name
1947 */
5a7d6a10
VK
1948void Node::updatePrimaryIpAddr()
1949{
1950 if (m_primaryName[0] == 0)
1951 return;
1952
177ead0c
VK
1953 InetAddress ipAddr = InetAddress::parse(m_primaryName);
1954 if (!ipAddr.isValid() && (m_zoneId != 0))
1955 {
1956 // resolve address through proxy agent
1957 Zone *zone = FindZoneByGUID(m_zoneId);
1958 if (zone != NULL)
1959 {
1960 Node *proxy = (Node *)FindObjectById(zone->getAgentProxy(), OBJECT_NODE);
1961 if (proxy != NULL)
1962 {
1963 TCHAR query[256], buffer[128];
1964 _sntprintf(query, 256, _T("Net.Resolver.AddressByName(%s)"), m_primaryName);
1965 if (proxy->getItemFromAgent(query, 128, buffer) == ERR_SUCCESS)
1966 {
1967 ipAddr = InetAddress::parse(buffer);
1968 }
1969 }
1970 }
1971 }
1972
1973 // Resolve address through local resolver
1974 if (!ipAddr.isValid())
1975 {
1976 ipAddr = InetAddress::resolveHostName(m_primaryName);
1977 }
1978
c75e9ee4 1979 if (!ipAddr.equals(m_ipAddress) && (ipAddr.isValidUnicast() || !_tcscmp(m_primaryName, _T("0.0.0.0"))))
5a7d6a10 1980 {
c75e9ee4 1981 TCHAR buffer1[64], buffer2[64];
5a7d6a10 1982
4d2c3a54 1983 DbgPrintf(4, _T("IP address for node %s [%d] changed from %s to %s"),
c75e9ee4
VK
1984 m_name, (int)m_id, m_ipAddress.toString(buffer1), ipAddr.toString(buffer2));
1985 PostEvent(EVENT_IP_ADDRESS_CHANGED, m_id, "AA", &ipAddr, &m_ipAddress);
2467ed57 1986
dbe28185
VK
1987 if (m_dwFlags & NF_REMOTE_AGENT)
1988 {
c42b4551 1989 lockProperties();
c75e9ee4 1990 m_ipAddress = ipAddr;
c42b4551
VK
1991 setModified();
1992 unlockProperties();
dbe28185
VK
1993 }
1994 else
1995 {
1996 setPrimaryIPAddress(ipAddr);
1997 }
a3050773
VK
1998
1999 agentLock();
a416f8f6 2000 deleteAgentConnection();
a3050773 2001 agentUnlock();
5a7d6a10
VK
2002 }
2003}
2004
76720a09 2005/**
208d7427
VK
2006 * Entry point for configuration poller
2007 */
2008void Node::configurationPoll(PollerInfo *poller)
2009{
2010 poller->startExecution();
2011 ObjectTransactionStart();
2012 configurationPoll(NULL, 0, poller, 0);
2013 ObjectTransactionEnd();
2014 delete poller;
2015}
2016
2017/**
76720a09
VK
2018 * Perform configuration poll on node
2019 */
208d7427 2020void Node::configurationPoll(ClientSession *pSession, UINT32 dwRqId, PollerInfo *poller, int maskBits)
5039dede 2021{
46117060
VK
2022 if (m_dwDynamicFlags & NDF_DELETE_IN_PROGRESS)
2023 {
2024 if (dwRqId == 0)
2025 m_dwDynamicFlags &= ~NDF_QUEUED_FOR_CONFIG_POLL;
2026 return;
2027 }
2028
88dc9091 2029 if (IsShutdownInProgress())
9fc9ec2c
VK
2030 return;
2031
967893bb 2032 UINT32 dwOldFlags = m_dwFlags;
35f836fe 2033 TCHAR szBuffer[4096];
76720a09 2034 bool hasChanges = false;
5039dede 2035
208d7427 2036 poller->setStatus(_T("wait for lock"));
7c521895 2037 pollerLock();
88dc9091
VK
2038
2039 if (IsShutdownInProgress())
2040 {
2041 pollerUnlock();
2042 return;
2043 }
2044
c59466d2 2045 m_pollRequestor = pSession;
c42b4551
VK
2046 sendPollerMsg(dwRqId, _T("Starting configuration poll for node %s\r\n"), m_name);
2047 DbgPrintf(4, _T("Starting configuration poll for node %s (ID: %d)"), m_name, m_id);
5039dede
AK
2048
2049 // Check for forced capabilities recheck
2050 if (m_dwDynamicFlags & NDF_RECHECK_CAPABILITIES)
2051 {
f7e3eb5a 2052 sendPollerMsg(dwRqId, POLLER_WARNING _T("Capability reset\r\n"));
5039dede 2053 m_dwFlags &= ~(NF_IS_NATIVE_AGENT | NF_IS_SNMP | NF_IS_CPSNMP |
0ecc2200 2054 NF_IS_BRIDGE | NF_IS_ROUTER | NF_IS_OSPF | NF_IS_PRINTER |
76720a09 2055 NF_IS_CDP | NF_IS_LLDP | NF_IS_SONMP | NF_IS_VRRP | NF_HAS_VLANS |
46ee6286
VK
2056 NF_IS_8021X | NF_IS_STP | NF_HAS_ENTITY_MIB | NF_HAS_IFXTABLE |
2057 NF_HAS_WINPDH);
e4a64da2 2058 m_dwDynamicFlags &= ~NDF_CONFIGURATION_POLL_PASSED;
5039dede
AK
2059 m_szObjectId[0] = 0;
2060 m_szPlatformName[0] = 0;
2061 m_szAgentVersion[0] = 0;
0ecc2200
VK
2062 safe_free_and_null(m_sysDescription);
2063 safe_free_and_null(m_sysName);
cf38357f
VK
2064 safe_free_and_null(m_sysContact);
2065 safe_free_and_null(m_sysLocation);
0ecc2200 2066 safe_free_and_null(m_lldpNodeId);
5039dede
AK
2067 }
2068
2069 // Check if node is marked as unreachable
2070 if ((m_dwDynamicFlags & NDF_UNREACHABLE) && !(m_dwDynamicFlags & NDF_RECHECK_CAPABILITIES))
2071 {
21c9acce 2072 sendPollerMsg(dwRqId, POLLER_WARNING _T("Node is marked as unreachable, configuration poll aborted\r\n"));
35f836fe 2073 DbgPrintf(4, _T("Node is marked as unreachable, configuration poll aborted"));
8573e935 2074 m_lastConfigurationPoll = time(NULL);
5039dede
AK
2075 }
2076 else
2077 {
5a7d6a10
VK
2078 updatePrimaryIpAddr();
2079
208d7427 2080 poller->setStatus(_T("capability check"));
21c9acce 2081 sendPollerMsg(dwRqId, _T("Checking node's capabilities...\r\n"));
5039dede 2082
99058170
VK
2083 if (confPollAgent(dwRqId))
2084 hasChanges = true;
2085 if (confPollSnmp(dwRqId))
2086 hasChanges = true;
5039dede
AK
2087
2088 // Check for CheckPoint SNMP agent on port 260
9a1f7c45 2089 if (ConfigReadInt(_T("EnableCheckPointSNMP"), 0))
5039dede 2090 {
c42b4551 2091 DbgPrintf(5, _T("ConfPoll(%s): checking for CheckPoint SNMP on port 260"), m_name);
c75e9ee4 2092 if (!((m_dwFlags & NF_IS_CPSNMP) && (m_dwDynamicFlags & NDF_CPSNMP_UNREACHABLE)) && m_ipAddress.isValidUnicast())
5039dede 2093 {
2fbf7e1c 2094 SNMP_Transport *pTransport = new SNMP_UDPTransport;
c75e9ee4 2095 ((SNMP_UDPTransport *)pTransport)->createUDPTransport(m_ipAddress, CHECKPOINT_SNMP_PORT);
9a1f7c45
VK
2096 if (SnmpGet(SNMP_VERSION_1, pTransport,
2097 _T(".1.3.6.1.4.1.2620.1.1.10.0"), NULL, 0, szBuffer, sizeof(szBuffer), 0) == SNMP_ERR_SUCCESS)
2098 {
c42b4551 2099 lockProperties();
9a1f7c45
VK
2100 m_dwFlags |= NF_IS_CPSNMP | NF_IS_ROUTER;
2101 m_dwDynamicFlags &= ~NDF_CPSNMP_UNREACHABLE;
c42b4551 2102 unlockProperties();
9a1f7c45
VK
2103 sendPollerMsg(dwRqId, POLLER_INFO _T(" CheckPoint SNMP agent on port 260 is active\r\n"));
2104 }
2105 delete pTransport;
5039dede 2106 }
5039dede
AK
2107 }
2108
2109 // Generate event if node flags has been changed
2110 if (dwOldFlags != m_dwFlags)
2111 {
c42b4551 2112 PostEvent(EVENT_NODE_FLAGS_CHANGED, m_id, "xx", dwOldFlags, m_dwFlags);
76720a09 2113 hasChanges = true;
5039dede
AK
2114 }
2115
5039dede 2116 // Retrieve interface list
208d7427 2117 poller->setStatus(_T("interface check"));
21c9acce 2118 sendPollerMsg(dwRqId, _T("Capability check finished\r\n"));
5039dede 2119
bf2ad753 2120 if (updateInterfaceConfiguration(dwRqId, maskBits))
76720a09 2121 hasChanges = true;
5039dede 2122
8573e935 2123 m_lastConfigurationPoll = time(NULL);
5039dede
AK
2124
2125 // Check node name
21c9acce 2126 sendPollerMsg(dwRqId, _T("Checking node name\r\n"));
c42b4551 2127 UINT32 dwAddr = ntohl(_t_inet_addr(m_name));
c8076b19 2128 if ((g_flags & AF_RESOLVE_NODE_NAMES) &&
4d2c3a54 2129 (dwAddr != INADDR_NONE) &&
5039dede 2130 (dwAddr != INADDR_ANY) &&
58b3e451 2131 isMyIP(dwAddr))
5039dede 2132 {
21c9acce 2133 sendPollerMsg(dwRqId, _T("Node name is an IP address and need to be resolved\r\n"));
208d7427 2134 poller->setStatus(_T("resolving name"));
024c3faf 2135 if (resolveName(FALSE))
5039dede 2136 {
c42b4551 2137 sendPollerMsg(dwRqId, POLLER_INFO _T("Node name resolved to %s\r\n"), m_name);
76720a09 2138 hasChanges = true;
5039dede
AK
2139 }
2140 else
2141 {
21c9acce 2142 sendPollerMsg(dwRqId, POLLER_WARNING _T("Node name cannot be resolved\r\n"));
5039dede
AK
2143 }
2144 }
2145 else
2146 {
c8076b19 2147 if (g_flags & AF_SYNC_NODE_NAMES_WITH_DNS)
5039dede 2148 {
21c9acce 2149 sendPollerMsg(dwRqId, _T("Syncing node name with DNS\r\n"));
208d7427 2150 poller->setStatus(_T("resolving name"));
024c3faf 2151 if (resolveName(TRUE))
5039dede 2152 {
c42b4551 2153 sendPollerMsg(dwRqId, POLLER_INFO _T("Node name resolved to %s\r\n"), m_name);
76720a09 2154 hasChanges = true;
5039dede
AK
2155 }
2156 }
2157 else
2158 {
21c9acce 2159 sendPollerMsg(dwRqId, _T("Node name is OK\r\n"));
5039dede
AK
2160 }
2161 }
2162
6fd6de0a 2163 applyUserTemplates();
7c521895 2164 updateContainerMembership();
4d0c32f3 2165
caa04e26
VK
2166 // Get list of installed products
2167 if (m_dwFlags & NF_IS_NATIVE_AGENT)
2168 {
208d7427 2169 poller->setStatus(_T("software check"));
caa04e26
VK
2170 sendPollerMsg(dwRqId, _T("Reading list of installed software packages\r\n"));
2171
2172 Table *table;
2173 if (getTableFromAgent(_T("System.InstalledProducts"), &table) == DCE_SUCCESS)
2174 {
c42b4551 2175 lockProperties();
caa04e26
VK
2176 delete m_softwarePackages;
2177 m_softwarePackages = new ObjectArray<SoftwarePackage>(table->getNumRows(), 16, true);
2178 for(int i = 0; i < table->getNumRows(); i++)
2179 m_softwarePackages->add(new SoftwarePackage(table, i));
c42b4551 2180 unlockProperties();
caa04e26
VK
2181 delete table;
2182 sendPollerMsg(dwRqId, POLLER_INFO _T("Got information about %d installed software packages\r\n"), m_softwarePackages->size());
2183 }
2184 else
2185 {
2186 delete_and_null(m_softwarePackages);
2187 sendPollerMsg(dwRqId, POLLER_WARNING _T("Unable to get information about installed software packages\r\n"));
2188 }
2189 }
2190
a3050773 2191 // Call hooks in loaded modules
967893bb 2192 for(UINT32 i = 0; i < g_dwNumModules; i++)
a5ee7b3b 2193 {
a3050773
VK
2194 if (g_pModuleList[i].pfConfPollHook != NULL)
2195 {
c42b4551 2196 DbgPrintf(5, _T("ConfigurationPoll(%s [%d]): calling hook in module %s"), m_name, m_id, g_pModuleList[i].szName);
208d7427 2197 if (g_pModuleList[i].pfConfPollHook(this, pSession, dwRqId, poller))
3368b8b7 2198 hasChanges = true;
a3050773 2199 }
a5ee7b3b 2200 }
a5ee7b3b 2201
270c946c
VK
2202 // Setup permanent connection to agent if not present (needed for proper configuration re-sync)
2203 if (m_dwFlags & NF_IS_NATIVE_AGENT)
2204 {
2205 agentLock();
2206 connectToAgent();
2207 agentUnlock();
2208 }
2209
a3050773 2210 // Execute hook script
208d7427 2211 poller->setStatus(_T("hook"));
a3050773
VK
2212 executeHookScript(_T("ConfigurationPoll"));
2213
270c946c
VK
2214 sendPollerMsg(dwRqId, _T("Finished configuration poll for node %s\r\n"), m_name);
2215 sendPollerMsg(dwRqId, _T("Node configuration was%schanged after poll\r\n"), hasChanges ? _T(" ") : _T(" not "));
2216
a3050773
VK
2217 m_dwDynamicFlags |= NDF_CONFIGURATION_POLL_PASSED;
2218 }
1824629a 2219
4d0c32f3 2220 // Finish configuration poll
208d7427 2221 poller->setStatus(_T("cleanup"));
4d0c32f3
VK
2222 if (dwRqId == 0)
2223 m_dwDynamicFlags &= ~NDF_QUEUED_FOR_CONFIG_POLL;
2224 m_dwDynamicFlags &= ~NDF_RECHECK_CAPABILITIES;
7c521895 2225 pollerUnlock();
c42b4551 2226 DbgPrintf(4, _T("Finished configuration poll for node %s (ID: %d)"), m_name, m_id);
4d0c32f3 2227
76720a09 2228 if (hasChanges)
4d0c32f3 2229 {
c42b4551
VK
2230 lockProperties();
2231 setModified();
2232 unlockProperties();
4d0c32f3
VK
2233 }
2234}
2235
76720a09
VK
2236/**
2237 * Configuration poll: check for NetXMS agent
2238 */
967893bb 2239bool Node::confPollAgent(UINT32 dwRqId)
76720a09 2240{
c42b4551 2241 DbgPrintf(5, _T("ConfPoll(%s): checking for NetXMS agent Flags={%08X} DynamicFlags={%08X}"), m_name, m_dwFlags, m_dwDynamicFlags);
76720a09 2242 if (((m_dwFlags & NF_IS_NATIVE_AGENT) && (m_dwDynamicFlags & NDF_AGENT_UNREACHABLE)) ||
c75e9ee4 2243 !m_ipAddress.isValidUnicast() || (m_dwFlags & NF_DISABLE_NXCP))
76720a09
VK
2244 return false;
2245
2246 bool hasChanges = false;
2247
21c9acce 2248 sendPollerMsg(dwRqId, _T(" Checking NetXMS agent...\r\n"));
c75e9ee4 2249 AgentConnection *pAgentConn = new AgentConnectionEx(m_id, m_ipAddress, m_agentPort, m_agentAuthMethod, m_szSharedSecret);
76720a09 2250 setAgentProxy(pAgentConn);
c42b4551 2251 DbgPrintf(5, _T("ConfPoll(%s): checking for NetXMS agent - connecting"), m_name);
af21affe
VK
2252
2253 // Try to connect to agent
2254 UINT32 rcc;
2255 if (!pAgentConn->connect(g_pServerKey, FALSE, &rcc))
2256 {
2257 // If there are authentication problem, try default shared secret
2258 if ((rcc == ERR_AUTH_REQUIRED) || (rcc == ERR_AUTH_FAILED))
2259 {
2260 TCHAR secret[MAX_SECRET_LENGTH];
2261 ConfigReadStr(_T("AgentDefaultSharedSecret"), secret, MAX_SECRET_LENGTH, _T("netxms"));
ce9783d2 2262 DecryptPassword(_T("netxms"), secret, secret, MAX_SECRET_LENGTH);
af21affe
VK
2263 pAgentConn->setAuthData(AUTH_SHA1_HASH, secret);
2264 if (pAgentConn->connect(g_pServerKey, FALSE, &rcc))
2265 {
c42b4551 2266 m_agentAuthMethod = AUTH_SHA1_HASH;
af21affe 2267 nx_strncpy(m_szSharedSecret, secret, MAX_SECRET_LENGTH);
c42b4551 2268 DbgPrintf(5, _T("ConfPoll(%s): checking for NetXMS agent - shared secret changed to system default"), m_name);
af21affe
VK
2269 }
2270 }
2271 }
2272
2273 if (rcc == ERR_SUCCESS)
76720a09 2274 {
c42b4551
VK
2275 DbgPrintf(5, _T("ConfPoll(%s): checking for NetXMS agent - connected"), m_name);
2276 lockProperties();
76720a09
VK
2277 m_dwFlags |= NF_IS_NATIVE_AGENT;
2278 if (m_dwDynamicFlags & NDF_AGENT_UNREACHABLE)
2279 {
2280 m_dwDynamicFlags &= ~NDF_AGENT_UNREACHABLE;
c42b4551 2281 PostEvent(EVENT_AGENT_OK, m_id, NULL);
21c9acce 2282 sendPollerMsg(dwRqId, POLLER_INFO _T(" Connectivity with NetXMS agent restored\r\n"));
76720a09
VK
2283 }
2284 else
2285 {
21c9acce 2286 sendPollerMsg(dwRqId, POLLER_INFO _T(" NetXMS native agent is active\r\n"));
76720a09 2287 }
c42b4551 2288 unlockProperties();
4d0c32f3 2289
76720a09
VK
2290 TCHAR buffer[MAX_RESULT_LENGTH];
2291 if (pAgentConn->getParameter(_T("Agent.Version"), MAX_AGENT_VERSION_LEN, buffer) == ERR_SUCCESS)
2292 {
c42b4551 2293 lockProperties();
76720a09
VK
2294 if (_tcscmp(m_szAgentVersion, buffer))
2295 {
2296 _tcscpy(m_szAgentVersion, buffer);
2297 hasChanges = true;
21c9acce 2298 sendPollerMsg(dwRqId, _T(" NetXMS agent version changed to %s\r\n"), m_szAgentVersion);
76720a09 2299 }
c42b4551 2300 unlockProperties();
76720a09
VK
2301 }
2302
2303 if (pAgentConn->getParameter(_T("System.PlatformName"), MAX_PLATFORM_NAME_LEN, buffer) == ERR_SUCCESS)
2304 {
c42b4551 2305 lockProperties();
76720a09
VK
2306 if (_tcscmp(m_szPlatformName, buffer))
2307 {
2308 _tcscpy(m_szPlatformName, buffer);
2309 hasChanges = true;
21c9acce 2310 sendPollerMsg(dwRqId, _T(" Platform name changed to %s\r\n"), m_szPlatformName);
76720a09 2311 }
c42b4551 2312 unlockProperties();
76720a09
VK
2313 }
2314
2315 // Check IP forwarding status
2316 if (pAgentConn->getParameter(_T("Net.IP.Forwarding"), 16, buffer) == ERR_SUCCESS)
2317 {
2318 if (_tcstoul(buffer, NULL, 10) != 0)
2319 m_dwFlags |= NF_IS_ROUTER;
2320 else
2321 m_dwFlags &= ~NF_IS_ROUTER;
2322 }
2323
2324 // Get uname
2325 if (pAgentConn->getParameter(_T("System.Uname"), MAX_DB_STRING, buffer) == ERR_SUCCESS)
2326 {
2327 TranslateStr(buffer, _T("\r\n"), _T(" "));
2328 TranslateStr(buffer, _T("\n"), _T(" "));
2329 TranslateStr(buffer, _T("\r"), _T(" "));
c42b4551 2330 lockProperties();
76720a09
VK
2331 if ((m_sysDescription == NULL) || _tcscmp(m_sysDescription, buffer))
2332 {
2333 safe_free(m_sysDescription);
2334 m_sysDescription = _tcsdup(buffer);
2335 hasChanges = true;
21c9acce 2336 sendPollerMsg(dwRqId, _T(" System description changed to %s\r\n"), m_sysDescription);
76720a09 2337 }
c42b4551 2338 unlockProperties();
76720a09
VK
2339 }
2340
11b75b2e 2341 // Check for 64 bit counter support.
acf01215 2342 // if Net.Interface.64BitCounters not supported by agent then use
11b75b2e
VK
2343 // only presence of 64 bit parameters as indicator
2344 bool netIf64bitCounters = true;
2345 if (pAgentConn->getParameter(_T("Net.Interface.64BitCounters"), MAX_DB_STRING, buffer) == ERR_SUCCESS)
2346 {
2347 netIf64bitCounters = _tcstol(buffer, NULL, 10) ? true : false;
2348 }
2349
86c126f5
VK
2350 ObjectArray<AgentParameterDefinition> *plist;
2351 ObjectArray<AgentTableDefinition> *tlist;
967893bb 2352 UINT32 rcc = pAgentConn->getSupportedParameters(&plist, &tlist);
76720a09
VK
2353 if (rcc == ERR_SUCCESS)
2354 {
c42b4551 2355 lockProperties();
76720a09
VK
2356 delete m_paramList;
2357 delete m_tableList;
2358 m_paramList = plist;
2359 m_tableList = tlist;
084fb4c1
VK
2360
2361 // Check for 64-bit interface counters
2362 m_dwFlags &= ~NF_HAS_AGENT_IFXCOUNTERS;
11b75b2e
VK
2363 if (netIf64bitCounters)
2364 {
2365 for(int i = 0; i < plist->size(); i++)
2366 {
2367 if (!_tcsicmp(plist->get(i)->getName(), _T("Net.Interface.BytesIn64(*)")))
2368 {
2369 m_dwFlags |= NF_HAS_AGENT_IFXCOUNTERS;
2370 break;
2371 }
2372 }
2373 }
084fb4c1 2374
c42b4551 2375 unlockProperties();
76720a09
VK
2376 }
2377 else
2378 {
c42b4551 2379 DbgPrintf(5, _T("ConfPoll(%s): AgentConnection::getSupportedParameters() failed: rcc=%d"), m_name, rcc);
76720a09
VK
2380 }
2381
46ee6286
VK
2382 // Get supported Windows Performance Counters
2383 if (!_tcsncmp(m_szPlatformName, _T("windows-"), 8))
2384 {
2385 sendPollerMsg(dwRqId, _T(" Reading list of available Windows Performance Counters...\r\n"));
2386 ObjectArray<WinPerfObject> *perfObjects = WinPerfObject::getWinPerfObjectsFromNode(this, pAgentConn);
c42b4551 2387 lockProperties();
46ee6286
VK
2388 delete m_winPerfObjects;
2389 m_winPerfObjects = perfObjects;
2390 if (m_winPerfObjects != NULL)
2391 {
2392 sendPollerMsg(dwRqId, POLLER_INFO _T(" %d counters read\r\n"), m_winPerfObjects->size());
2393 if (!(m_dwFlags & NF_HAS_WINPDH))
2394 {
2395 m_dwFlags |= NF_HAS_WINPDH;
2396 hasChanges = true;
2397 }
2398 }
2399 else
2400 {
2401 sendPollerMsg(dwRqId, POLLER_ERROR _T(" unable to get Windows Performance Counters list\r\n"));
2402 if (m_dwFlags & NF_HAS_WINPDH)
2403 {
2404 m_dwFlags &= ~NF_HAS_WINPDH;
2405 hasChanges = true;
2406 }
2407 }
c42b4551 2408 unlockProperties();
46ee6286
VK
2409 }
2410
76720a09
VK
2411 checkAgentPolicyBinding(pAgentConn);
2412
2413 pAgentConn->disconnect();
2414 }
2415 else
2416 {
c42b4551 2417 DbgPrintf(5, _T("ConfPoll(%s): checking for NetXMS agent - failed to connect (error %d)"), m_name, rcc);
76720a09 2418 }
da4a8dfe 2419 pAgentConn->decRefCount();
c42b4551 2420 DbgPrintf(5, _T("ConfPoll(%s): checking for NetXMS agent - finished"), m_name);
76720a09
VK
2421 return hasChanges;
2422}
2423
2424/**
a93aa634 2425 * SNMP walker callback which sets indicator to true after first varbind and aborts walk
76720a09 2426 */
9c5ebc32 2427static UINT32 IndicatorSnmpWalkerCallback(SNMP_Variable *var, SNMP_Transport *transport, void *arg)
76720a09 2428{
a93aa634
VK
2429 (*((bool *)arg)) = true;
2430 return SNMP_ERR_COMM;
76720a09
VK
2431}
2432
2433/**
2434 * Configuration poll: check for SNMP
2435 */
967893bb 2436bool Node::confPollSnmp(UINT32 dwRqId)
76720a09
VK
2437{
2438 if (((m_dwFlags & NF_IS_SNMP) && (m_dwDynamicFlags & NDF_SNMP_UNREACHABLE)) ||
c75e9ee4 2439 !m_ipAddress.isValidUnicast() || (m_dwFlags & NF_DISABLE_SNMP))
76720a09
VK
2440 return false;
2441
2442 bool hasChanges = false;
2443
21c9acce 2444 sendPollerMsg(dwRqId, _T(" Checking SNMP...\r\n"));
c42b4551 2445 DbgPrintf(5, _T("ConfPoll(%s): calling SnmpCheckCommSettings()"), m_name);
1f4c37ee
VK
2446 StringList oids;
2447 const TCHAR *customOid = m_customAttributes.get(_T("snmp.testoid"));
2448 if (customOid != NULL)
2449 oids.add(customOid);
2450 oids.add(_T(".1.3.6.1.2.1.1.2.0"));
2451 oids.add(_T(".1.3.6.1.2.1.1.1.0"));
2452 AddDriverSpecificOids(&oids);
d0a2ada6 2453 SNMP_Transport *pTransport = SnmpCheckCommSettings(getEffectiveSnmpProxy(), (getEffectiveSnmpProxy() == m_id) ? InetAddress::LOOPBACK : m_ipAddress, &m_snmpVersion, m_snmpPort, m_snmpSecurity, &oids);
af7ca382 2454 if (pTransport == NULL)
76720a09 2455 {
d0a2ada6 2456 DbgPrintf(5, _T("ConfPoll(%s): unable to create SNMP transport"), m_name);
2457 return false;
2458 }
76720a09 2459
d0a2ada6 2460 lockProperties();
2461 m_snmpPort = pTransport->getPort();
2462 delete m_snmpSecurity;
2463 m_snmpSecurity = new SNMP_SecurityContext(pTransport->getSecurityContext());
2464 m_dwFlags |= NF_IS_SNMP;
2465 if (m_dwDynamicFlags & NDF_SNMP_UNREACHABLE)
2466 {
2467 m_dwDynamicFlags &= ~NDF_SNMP_UNREACHABLE;
2468 PostEvent(EVENT_SNMP_OK, m_id, NULL);
2469 sendPollerMsg(dwRqId, POLLER_INFO _T(" Connectivity with SNMP agent restored\r\n"));
2470 }
2471 unlockProperties();
2472 sendPollerMsg(dwRqId, _T(" SNMP agent is active (version %s)\r\n"),
2473 (m_snmpVersion == SNMP_VERSION_3) ? _T("3") : ((m_snmpVersion == SNMP_VERSION_2C) ? _T("2c") : _T("1")));
76720a09 2474
d0a2ada6 2475 TCHAR szBuffer[4096];
2476 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)
2477 {
2478 // Set snmp object ID to .0.0 if it cannot be read
2479 _tcscpy(szBuffer, _T(".0.0"));
2480 }
2481 lockProperties();
2482 if (_tcscmp(m_szObjectId, szBuffer))
2483 {
2484 nx_strncpy(m_szObjectId, szBuffer, MAX_OID_LEN * 4);
2485 hasChanges = true;
2486 }
2487 unlockProperties();
76720a09 2488
d0a2ada6 2489 // Get system description
2490 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)
2491 {
2492 TranslateStr(szBuffer, _T("\r\n"), _T(" "));
2493 TranslateStr(szBuffer, _T("\n"), _T(" "));
2494 TranslateStr(szBuffer, _T("\r"), _T(" "));
2495 lockProperties();
2496 if ((m_sysDescription == NULL) || _tcscmp(m_sysDescription, szBuffer))
76720a09 2497 {
d0a2ada6 2498 safe_free(m_sysDescription);
2499 m_sysDescription = _tcsdup(szBuffer);
2500 hasChanges = true;
2501 sendPollerMsg(dwRqId, _T(" System description changed to %s\r\n"), m_sysDescription);
76720a09 2502 }
d0a2ada6 2503 unlockProperties();
2504 }
76720a09 2505
d0a2ada6 2506 // Select device driver
2507 NetworkDeviceDriver *driver = FindDriverForNode(this, pTransport);
2508 DbgPrintf(5, _T("ConfPoll(%s): selected device driver %s"), m_name, driver->getName());
2509 lockProperties();
2510 if (driver != m_driver)
2511 {
2512 m_driver = driver;
2513 sendPollerMsg(dwRqId, _T(" New network device driver selected: %s\r\n"), m_driver->getName());
2514 }
2515 unlockProperties();
eec253a8 2516
d0a2ada6 2517 // Allow driver to gather additional info
2518 m_driver->analyzeDevice(pTransport, m_szObjectId, &m_customAttributes, &m_driverData);
76720a09 2519
cf38357f
VK
2520 // Get sysName, sysContact, sysLocation
2521 if (querySnmpSysProperty(pTransport, _T(".1.3.6.1.2.1.1.5.0"), _T("name"), dwRqId, &m_sysName))
2522 hasChanges = true;
2523 if (querySnmpSysProperty(pTransport, _T(".1.3.6.1.2.1.1.4.0"), _T("contact"), dwRqId, &m_sysContact))
2524 hasChanges = true;
2525 if (querySnmpSysProperty(pTransport, _T(".1.3.6.1.2.1.1.6.0"), _T("location"), dwRqId, &m_sysLocation))
2526 hasChanges = true;
76720a09 2527
d0a2ada6 2528 // Check IP forwarding
2529 if (checkSNMPIntegerValue(pTransport, _T(".1.3.6.1.2.1.4.1.0"), 1))
2530 {
2531 lockProperties();
2532 m_dwFlags |= NF_IS_ROUTER;
2533 unlockProperties();
2534 }
2535 else
2536 {
2537 lockProperties();
2538 m_dwFlags &= ~NF_IS_ROUTER;
2539 unlockProperties();
2540 }
76720a09 2541
d0a2ada6 2542 checkIfXTable(pTransport);
2543 checkBridgeMib(pTransport);
76720a09 2544
d0a2ada6 2545 // Check for ENTITY-MIB support
2546 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)
2547 {
2548 lockProperties();
2549 m_dwFlags |= NF_HAS_ENTITY_MIB;
2550 unlockProperties();
2551
2552 ComponentTree *components = BuildComponentTree(this, pTransport);
2553 lockProperties();
2554 if (m_components != NULL)
2555 m_components->decRefCount();
2556 m_components = components;
2557 unlockProperties();
2558 }
2559 else
2560 {
2561 lockProperties();
2562 m_dwFlags &= ~NF_HAS_ENTITY_MIB;
2563 if (m_components != NULL)
76720a09 2564 {
d0a2ada6 2565 m_components->decRefCount();
2566 m_components = NULL;
76720a09 2567 }
d0a2ada6 2568 unlockProperties();
2569 }
76720a09 2570
d0a2ada6 2571 // Check for printer MIB support
a93aa634 2572 bool present = false;
9c5ebc32 2573 SnmpWalk(pTransport, _T(".1.3.6.1.2.1.43"), IndicatorSnmpWalkerCallback, &present);
a93aa634 2574 if (present)
d0a2ada6 2575 {
2576 lockProperties();
2577 m_dwFlags |= NF_IS_PRINTER;
2578 unlockProperties();
2579 }
2580 else
2581 {
2582 lockProperties();
2583 m_dwFlags &= ~NF_IS_PRINTER;
2584 unlockProperties();
2585 }
76720a09 2586
d0a2ada6 2587 // Check for CDP (Cisco Discovery Protocol) support
2588 if (checkSNMPIntegerValue(pTransport, _T(".1.3.6.1.4.1.9.9.23.1.3.1.0"), 1))
2589 {
2590 lockProperties();
2591 m_dwFlags |= NF_IS_CDP;
2592 unlockProperties();
2593 }
2594 else
2595 {
2596 lockProperties();
2597 m_dwFlags &= ~NF_IS_CDP;
2598 unlockProperties();
2599 }
3a82d5ae 2600
d0a2ada6 2601 // Check for NDP (Nortel Discovery Protocol) support
2602 if (checkSNMPIntegerValue(pTransport, _T(".1.3.6.1.4.1.45.1.6.13.1.2.0"), 1))
2603 {
2604 lockProperties();
2605 m_dwFlags |= NF_IS_NDP;
2606 unlockProperties();
2607 }
2608 else
2609 {
2610 lockProperties();
2611 m_dwFlags &= ~NF_IS_NDP;
2612 unlockProperties();
2613 }
76720a09 2614
d0a2ada6 2615 // Check for LLDP (Link Layer Discovery Protocol) support
2616 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)
2617 {
2618 lockProperties();
2619 m_dwFlags |= NF_IS_LLDP;
2620 unlockProperties();
2621
2622 INT32 type;
2623 BYTE data[256];
2624 UINT32 dataLen;
2625 if ((SnmpGetEx(pTransport, _T(".1.0.8802.1.1.2.1.3.1.0"), NULL, 0, &type, sizeof(INT32), 0, NULL) == SNMP_ERR_SUCCESS) &&
2626 (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 2627 {
d0a2ada6 2628 BuildLldpId(type, data, dataLen, szBuffer, 1024);
2629 lockProperties();
2630 if ((m_lldpNodeId == NULL) || _tcscmp(m_lldpNodeId, szBuffer))
2631 {
2632 safe_free(m_lldpNodeId);
2633 m_lldpNodeId = _tcsdup(szBuffer);
2634 hasChanges = true;
2635 sendPollerMsg(dwRqId, _T(" LLDP node ID changed to %s\r\n"), m_lldpNodeId);
2636 }
2637 unlockProperties();
76720a09
VK
2638 }
2639
d0a2ada6 2640 ObjectArray<LLDP_LOCAL_PORT_INFO> *lldpPorts = GetLLDPLocalPortInfo(pTransport);
2641 lockProperties();
2642 delete m_lldpLocalPortInfo;
2643 m_lldpLocalPortInfo = lldpPorts;
2644 unlockProperties();
2645 }
2646 else
2647 {
2648 lockProperties();
2649 m_dwFlags &= ~NF_IS_LLDP;
2650 unlockProperties();
2651 }
76720a09 2652
d0a2ada6 2653 // Check for 802.1x support
2654 if (checkSNMPIntegerValue(pTransport, _T(".1.0.8802.1.1.1.1.1.1.0"), 1))
2655 {
2656 lockProperties();
2657 m_dwFlags |= NF_IS_8021X;
2658 unlockProperties();
2659 }
2660 else
2661 {
2662 lockProperties();
2663 m_dwFlags &= ~NF_IS_8021X;
2664 unlockProperties();
2665 }
c6afd26a 2666
d0a2ada6 2667 checkOSPFSupport(pTransport);
c6afd26a 2668
d0a2ada6 2669 // Get VRRP information
2670 VrrpInfo *vrrpInfo = GetVRRPInfo(this);
2671 if (vrrpInfo != NULL)
2672 {
2673 lockProperties();
2674 m_dwFlags |= NF_IS_VRRP;
2675 delete m_vrrpInfo;
2676 m_vrrpInfo = vrrpInfo;
2677 unlockProperties();
2678 }
2679 else
2680 {
2681 lockProperties();
2682 m_dwFlags &= ~NF_IS_VRRP;
2683 unlockProperties();
2684 }
8f26db67 2685
d0a2ada6 2686 // Get wireless controller data
2687 if ((m_driver != NULL) && m_driver->isWirelessController(pTransport, &m_customAttributes, m_driverData))
2688 {
2689 DbgPrintf(5, _T("ConfPoll(%s): node is wireless controller, reading access point information"), m_name);
2690 sendPollerMsg(dwRqId, _T(" Reading wireless access point information\r\n"));
2691 lockProperties();
2692 m_dwFlags |= NF_IS_WIFI_CONTROLLER;
2693 unlockProperties();
f1989a3a 2694
d0a2ada6 2695 int clusterMode = m_driver->getClusterMode(pTransport, &m_customAttributes, m_driverData);
4d2c3a54 2696
d0a2ada6 2697 ObjectArray<AccessPointInfo> *aps = m_driver->getAccessPoints(pTransport, &m_customAttributes, m_driverData);
2698 if (aps != NULL)
2699 {
2700 sendPollerMsg(dwRqId, POLLER_INFO _T(" %d wireless access points found\r\n"), aps->size());
2701 DbgPrintf(5, _T("ConfPoll(%s): got information about %d access points"), m_name, aps->size());
2702 int adopted = 0;
2703 for(int i = 0; i < aps->size(); i++)
2704 {
2705 AccessPointInfo *info = aps->get(i);
2706 if (info->getState() == AP_ADOPTED)
2707 adopted++;
2708
2709 bool newAp = false;
2710 AccessPoint *ap = (clusterMode == CLUSTER_MODE_STANDALONE) ? findAccessPointByMAC(info->getMacAddr()) : FindAccessPointByMAC(info->getMacAddr());
2711 if (ap == NULL)
2712 {
2713 String name;
2714
2715 if (info->getName() != NULL)
2716 {
2717 name = info->getName();
2718 }
2719 else
2720 {
2721 for(int j = 0; j < info->getRadioInterfaces()->size(); j++)
ffb44442 2722 {
d0a2ada6 2723 if (j > 0)
2724 name += _T("/");
2725 name += info->getRadioInterfaces()->get(j)->name;
ffb44442 2726 }
23ed00c4 2727 }
d0a2ada6 2728 ap = new AccessPoint((const TCHAR *)name, info->getIndex(), info->getMacAddr());
e7450f3b 2729 NetObjInsert(ap, true, false);
d0a2ada6 2730 DbgPrintf(5, _T("ConfPoll(%s): created new access point object %s [%d]"), m_name, ap->getName(), ap->getId());
2731 newAp = true;
2732 }
2733 ap->attachToNode(m_id);
2734 ap->setIpAddress(info->getIpAddr());
2735 if ((info->getState() == AP_ADOPTED) || newAp)
2736 {
2737 ap->updateRadioInterfaces(info->getRadioInterfaces());
2738 ap->updateInfo(info->getVendor(), info->getModel(), info->getSerial());
2739 }
2740 ap->unhide();
2741 ap->updateState(info->getState());
2742 }
f1989a3a 2743
d0a2ada6 2744 lockProperties();
2745 m_adoptedApCount = adopted;
2746 m_totalApCount = aps->size();
2747 unlockProperties();
f1989a3a 2748
d0a2ada6 2749 delete aps;
2750 }
2751 else
2752 {
2753 DbgPrintf(5, _T("ConfPoll(%s): failed to read access point information"), m_name);
2754 sendPollerMsg(dwRqId, POLLER_ERROR _T(" Failed to read access point information\r\n"));
2755 }
2756 }
2757 else
2758 {
2759 lockProperties();
2760 m_dwFlags &= ~NF_IS_WIFI_CONTROLLER;
2761 unlockProperties();
76720a09 2762 }
d0a2ada6 2763
2764 if (ConfigReadInt(_T("EnableCheckPointSNMP"), 0))
76720a09
VK
2765 {
2766 // Check for CheckPoint SNMP agent on port 161
c42b4551 2767 DbgPrintf(5, _T("ConfPoll(%s): checking for CheckPoint SNMP"), m_name);
76720a09 2768 TCHAR szBuffer[4096];
d525c9ed 2769 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 2770 {
c42b4551 2771 lockProperties();
76720a09
VK
2772 if (_tcscmp(m_szObjectId, _T(".1.3.6.1.4.1.2620.1.1")))
2773 {
2774 nx_strncpy(m_szObjectId, _T(".1.3.6.1.4.1.2620.1.1"), MAX_OID_LEN * 4);
2775 hasChanges = true;
2776 }
2777
2778 m_dwFlags |= NF_IS_SNMP | NF_IS_ROUTER;
2779 m_dwDynamicFlags &= ~NDF_SNMP_UNREACHABLE;
c42b4551 2780 unlockProperties();
21c9acce 2781 sendPollerMsg(dwRqId, POLLER_INFO _T(" CheckPoint SNMP agent on port 161 is active\r\n"));
76720a09
VK
2782 }
2783 }
2784 delete pTransport;
2785 return hasChanges;
2786}
2787
2788/**
cf38357f
VK
2789 * Query SNMP sys property (sysName, sysLocation, etc.)
2790 */
2791bool Node::querySnmpSysProperty(SNMP_Transport *snmp, const TCHAR *oid, const TCHAR *propName, UINT32 pollRqId, TCHAR **value)
2792{
2793 TCHAR buffer[256];
2794 bool hasChanges = false;
2795
2796 if (SnmpGet(m_snmpVersion, snmp, oid, NULL, 0, buffer, sizeof(buffer), SG_STRING_RESULT) == SNMP_ERR_SUCCESS)
2797 {
2798 lockProperties();
2799 if ((*value == NULL) || _tcscmp(*value, buffer))
2800 {
2801 safe_free(*value);
2802 *value = _tcsdup(buffer);
2803 hasChanges = true;
2804 sendPollerMsg(pollRqId, _T(" System %s changed to %s\r\n"), propName, *value);
2805 }
2806 unlockProperties();
2807 }
2808 return hasChanges;
2809}
2810
2811/**
76720a09
VK
2812 * Configuration poll: check for BRIDGE MIB
2813 */
2814void Node::checkBridgeMib(SNMP_Transport *pTransport)
2815{
2816 TCHAR szBuffer[4096];
d525c9ed 2817 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 2818 {
c42b4551 2819 lockProperties();
76720a09
VK
2820 m_dwFlags |= NF_IS_BRIDGE;
2821 memcpy(m_baseBridgeAddress, szBuffer, 6);
c42b4551 2822 unlockProperties();
76720a09
VK
2823
2824 // Check for Spanning Tree (IEEE 802.1d) MIB support
074498ac 2825 if (checkSNMPIntegerValue(pTransport, _T(".1.3.6.1.2.1.17.2.1.0"), 3))
76720a09 2826 {
c42b4551 2827 lockProperties();
76720a09 2828 m_dwFlags |= NF_IS_STP;
c42b4551 2829 unlockProperties();
76720a09
VK
2830 }
2831 else
2832 {
c42b4551 2833 lockProperties();
76720a09 2834 m_dwFlags &= ~NF_IS_STP;
c42b4551 2835 unlockProperties();
76720a09
VK
2836 }
2837 }
2838 else
2839 {
c42b4551 2840 lockProperties();
76720a09 2841 m_dwFlags &= ~(NF_IS_BRIDGE | NF_IS_STP);
c42b4551 2842 unlockProperties();
76720a09
VK
2843 }
2844}
2845
2846/**
2847 * Configuration poll: check for ifXTable
2848 */
2849void Node::checkIfXTable(SNMP_Transport *pTransport)
2850{
a93aa634 2851 bool present = false;
9c5ebc32 2852 SnmpWalk(pTransport, _T(".1.3.6.1.2.1.31.1.1.1.1"), IndicatorSnmpWalkerCallback, &present);
a93aa634 2853 if (present)
76720a09 2854 {
c42b4551 2855 lockProperties();
76720a09 2856 m_dwFlags |= NF_HAS_IFXTABLE;
c42b4551 2857 unlockProperties();
76720a09
VK
2858 }
2859 else
2860 {
c42b4551 2861 lockProperties();
76720a09 2862 m_dwFlags &= ~NF_HAS_IFXTABLE;
c42b4551 2863 unlockProperties();
76720a09
VK
2864 }
2865}
2866
2867/**
dfed892b
VK
2868 * Delete duplicate interfaces
2869 * (find and delete multiple interfaces with same ifIndex value created by version prior to 2.0-M3)
2870 */
2871bool Node::deleteDuplicateInterfaces(UINT32 rqid)
2872{
2873 ObjectArray<Interface> deleteList(16, 16, false);
2874
2875 LockChildList(FALSE);
2876 for(UINT32 i = 0; i < m_dwChildCount; i++)
2877 {
2878 if ((m_pChildList[i]->getObjectClass() != OBJECT_INTERFACE) ||
2879 ((Interface *)m_pChildList[i])->isManuallyCreated())
2880 continue;
2881 Interface *iface = (Interface *)m_pChildList[i];
2882 for(UINT32 j = i + 1; j < m_dwChildCount; j++)
2883 {
2884 if ((m_pChildList[j]->getObjectClass() != OBJECT_INTERFACE) ||
2885 ((Interface *)m_pChildList[j])->isManuallyCreated() ||
2886 (deleteList.contains((Interface *)m_pChildList[j])))
2887 continue;
2888 if (iface->getIfIndex() == ((Interface *)m_pChildList[j])->getIfIndex())
2889 {
2890 deleteList.add((Interface *)m_pChildList[j]);
2891 DbgPrintf(6, _T("Node::deleteDuplicateInterfaces(%s [%d]): found duplicate interface %s [%d], original %s [%d], ifIndex=%d"),
2892 m_name, m_id, m_pChildList[j]->getName(), m_pChildList[j]->getId(), iface->getName(), iface->getId(), iface->getIfIndex());
2893 }
2894 }
2895 }
2896 UnlockChildList();
2897
2898 for(int i = 0; i < deleteList.size(); i++)
2899 {
2900 Interface *iface = deleteList.get(i);
2901 sendPollerMsg(rqid, POLLER_WARNING _T(" Duplicate interface \"%s\" deleted\r\n"), iface->getName());
2902 deleteInterface(iface);
2903 }
2904
2905 return deleteList.size() > 0;
2906}
2907
2908/**
76720a09
VK
2909 * Update interface configuration
2910 */
dfed892b 2911bool Node::updateInterfaceConfiguration(UINT32 rqid, int maskBits)
eec253a8 2912{
dfed892b
VK
2913 sendPollerMsg(rqid, _T("Checking interface configuration...\r\n"));
2914
2915 bool hasChanges = deleteDuplicateInterfaces(rqid);
2916
2917 InterfaceList *pIfList = getInterfaceList();
eec253a8
VK
2918 if (pIfList != NULL)
2919 {
c42b4551 2920 DbgPrintf(6, _T("Node::updateInterfaceConfiguration(%s [%u]): got %d interfaces"), m_name, m_id, pIfList->size());
eec253a8
VK
2921
2922 // Find non-existing interfaces
2923 LockChildList(FALSE);
5f6e8d09 2924 ObjectArray<Interface> deleteList(m_dwChildCount, 8, false);
dfed892b 2925 for(UINT32 i = 0; i < m_dwChildCount; i++)
eec253a8 2926 {
c42b4551 2927 if (m_pChildList[i]->getObjectClass() == OBJECT_INTERFACE)
eec253a8
VK
2928 {
2929 Interface *pInterface = (Interface *)m_pChildList[i];
9214177b
VK
2930 if (!pInterface->isManuallyCreated())
2931 {
dfed892b 2932 int j;
a6312bd6 2933 for(j = 0; j < pIfList->size(); j++)
9214177b 2934 {
c30c0c0f 2935 if (pIfList->get(j)->index == pInterface->getIfIndex())
9214177b
VK
2936 break;
2937 }
eec253a8 2938
a6312bd6 2939 if (j == pIfList->size())
9214177b
VK
2940 {
2941 // No such interface in current configuration, add it to delete list
5f6e8d09 2942 deleteList.add(pInterface);
9214177b
VK
2943 }
2944 }
eec253a8
VK
2945 }
2946 }
2947 UnlockChildList();
2948
2949 // Delete non-existent interfaces
5f6e8d09 2950 if (deleteList.size() > 0)
eec253a8 2951 {
5f6e8d09 2952 for(int j = 0; j < deleteList.size(); j++)
eec253a8 2953 {
5f6e8d09
VK
2954 Interface *iface = deleteList.get(j);
2955 sendPollerMsg(rqid, POLLER_WARNING _T(" Interface \"%s\" is no longer exist\r\n"), iface->getName());
2956 const InetAddress& addr = iface->getFirstIpAddress();
2957 PostEvent(EVENT_INTERFACE_DELETED, m_id, "dsAd", iface->getIfIndex(), iface->getName(), &addr, addr.getMaskBits());
2958 deleteInterface(iface);
eec253a8 2959 }
dfed892b 2960 hasChanges = true;
eec253a8 2961 }
eec253a8
VK
2962
2963 // Add new interfaces and check configuration of existing
dfed892b 2964 for(int j = 0; j < pIfList->size(); j++)
eec253a8 2965 {
c30c0c0f 2966 InterfaceInfo *ifInfo = pIfList->get(j);
eec253a8
VK
2967 BOOL bNewInterface = TRUE;
2968
2969 LockChildList(FALSE);
dfed892b 2970 for(UINT32 i = 0; i < m_dwChildCount; i++)
eec253a8 2971 {
c42b4551 2972 if (m_pChildList[i]->getObjectClass() == OBJECT_INTERFACE)
eec253a8
VK
2973 {
2974 Interface *pInterface = (Interface *)m_pChildList[i];
2975
c30c0c0f 2976 if (ifInfo->index == pInterface->getIfIndex())
eec253a8
VK
2977 {
2978 // Existing interface, check configuration
e83d726c 2979 if (memcmp(ifInfo->macAddr, "\x00\x00\x00\x00\x00\x00", MAC_ADDR_LENGTH) &&
c30c0c0f 2980 memcmp(ifInfo->macAddr, pInterface->getMacAddr(), MAC_ADDR_LENGTH))
eec253a8
VK
2981 {
2982 TCHAR szOldMac[16], szNewMac[16];
2983
2984 BinToStr((BYTE *)pInterface->getMacAddr(), MAC_ADDR_LENGTH, szOldMac);
e95680e5 2985 BinToStr(ifInfo->macAddr, MAC_ADDR_LENGTH, szNewMac);
c42b4551
VK
2986 PostEvent(EVENT_MAC_ADDR_CHANGED, m_id, "idsss",
2987 pInterface->getId(), pInterface->getIfIndex(),
2988 pInterface->getName(), szOldMac, szNewMac);
e55ff6f9 2989 pInterface->setMacAddr(ifInfo->macAddr, true);
e95680e5
VK
2990 }
2991 if (_tcscmp(ifInfo->name, pInterface->getName()))
2992 {
2993 pInterface->setName(ifInfo->name);
eec253a8 2994 }
e95680e5 2995 if (_tcscmp(ifInfo->description, pInterface->getDescription()))
eec253a8 2996 {
e95680e5 2997 pInterface->setDescription(ifInfo->description);
eec253a8 2998 }
e95680e5 2999 if (_tcscmp(ifInfo->alias, pInterface->getAlias()))
478d4ff4 3000 {
e95680e5 3001 pInterface->setAlias(ifInfo->alias);
478d4ff4 3002 }
e95680e5 3003 if (ifInfo->bridgePort != pInterface->getBridgePortNumber())
eec253a8 3004 {
e95680e5 3005 pInterface->setBridgePortNumber(ifInfo->bridgePort);
eec253a8 3006 }
e95680e5 3007 if (ifInfo->slot != pInterface->getSlotNumber())
76f9abfd 3008 {
e95680e5 3009 pInterface->setSlotNumber(ifInfo->slot);
76f9abfd 3010 }
e95680e5 3011 if (ifInfo->port != pInterface->getPortNumber())
76f9abfd 3012 {
e95680e5 3013 pInterface->setPortNumber(ifInfo->port);
76f9abfd 3014 }
4c16cdc7
VK
3015 if (ifInfo->isPhysicalPort != pInterface->isPhysicalPort())
3016 {
3017 pInterface->setPhysicalPortFlag(ifInfo->isPhysicalPort);
3018 }
33560996
VK
3019 if (ifInfo->mtu != pInterface->getMTU())
3020 {
3021 pInterface->setMTU(ifInfo->mtu);
3022 }
3d37f7bf
VK
3023 if (ifInfo->speed != pInterface->getSpeed())
3024 {
3025 pInterface->setSpeed(ifInfo->speed);
3026 }
3027 if ((ifInfo->ifTableSuffixLength != pInterface->getIfTableSuffixLen()) ||
d0a2ada6 3028 memcmp(ifInfo->ifTableSuffix, pInterface->getIfTableSuffix(),
3d37f7bf
VK
3029 min(ifInfo->ifTableSuffixLength, pInterface->getIfTableSuffixLen())))
3030 {
3031 pInterface->setIfTableSuffix(ifInfo->ifTableSuffixLength, ifInfo->ifTableSuffix);
3032 }
c30c0c0f
VK
3033
3034 // Check for deleted IPs and changed masks
3035 const InetAddressList *ifList = pInterface->getIpAddressList();
3036 for(int n = 0; n < ifList->size(); n++)
3037 {
3038 const InetAddress& ifAddr = ifList->get(n);
3039 const InetAddress& addr = ifInfo->ipAddrList.findAddress(ifAddr);
3040 if (addr.isValid())
3041 {
3042 if (addr.getMaskBits() != ifAddr.getMaskBits())
3043 {
3044 PostEvent(EVENT_IF_MASK_CHANGED, m_id, "dsAddd", pInterface->getId(), pInterface->getName(),
3045 &addr, addr.getMaskBits(), pInterface->getIfIndex(), ifAddr.getMaskBits());
3046 pInterface->setNetMask(addr);
2400f29f 3047 sendPollerMsg(rqid, POLLER_INFO _T(" IP network mask changed to /%d on interface \"%s\" address %s\r\n"),
5f6e8d09 3048 addr.getMaskBits(), pInterface->getName(), (const TCHAR *)ifAddr.toString());
c30c0c0f
VK
3049 }
3050 }
3051 else
3052 {
2400f29f 3053 sendPollerMsg(rqid, POLLER_WARNING _T(" IP address %s removed from interface \"%s\"\r\n"),
5f6e8d09 3054 (const TCHAR *)ifAddr.toString(), pInterface->getName());
c30c0c0f
VK
3055 PostEvent(EVENT_IF_IPADDR_DELETED, m_id, "dsAdd", pInterface->getId(), pInterface->getName(),
3056 &ifAddr, ifAddr.getMaskBits(), pInterface->getIfIndex());
3057 pInterface->deleteIpAddress(ifAddr);
3058 }
3059 }
3060
3061 // Check for added IPs
3062 for(int m = 0; m < ifInfo->ipAddrList.size(); m++)
3063 {
3064 const InetAddress& addr = ifInfo->ipAddrList.get(m);
3065 if (!ifList->hasAddress(addr))
3066 {
3067 pInterface->addIpAddress(addr);
3068 PostEvent(EVENT_IF_IPADDR_ADDED, m_id, "dsAdd", pInterface->getId(), pInterface->getName(),
3069 &addr, addr.getMaskBits(), pInterface->getIfIndex());
2400f29f 3070 sendPollerMsg(rqid, POLLER_INFO _T(" IP address %s added to interface \"%s\"\r\n"),
5f6e8d09 3071 (const TCHAR *)addr.toString(), pInterface->getName());
c30c0c0f
VK
3072 }
3073 }
3074
eec253a8
VK
3075 bNewInterface = FALSE;
3076 break;
3077 }
3078 }
3079 }
3080 UnlockChildList();
3081
3082 if (bNewInterface)
3083 {