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