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