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