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