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