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