implemented array initializers in NXSL
[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;
35f836fe
VK
1005 SendPollerMsg(dwRqId, _T("Starting status poll for node %s\r\n"), m_szName);
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
VK
1027 SetPollerInfo(nPoller, _T("check SNMP"));
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);
35f836fe 1036 SendPollerMsg(dwRqId, POLLER_INFO _T("Connectivity with SNMP agent restored\r\n"));
5039dede
AK
1037 }
1038 }
1039 else
1040 {
35f836fe 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;
35f836fe 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"));
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);
35f836fe 1081 SendPollerMsg(dwRqId, POLLER_INFO _T("Connectivity with NetXMS agent restored\r\n"));
5039dede 1082 }
5039dede
AK
1083 }
1084 else
1085 {
35f836fe
VK
1086 DbgPrintf(6, _T("StatusPoll(%s): agent unreachable, error=%d, socketError=%d"), m_szName, (int)error, (int)socketError);
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;
35f836fe 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)
1136 pPollerNode->IncRefCount();
1137 }
1138 else
1139 {
1140 pPollerNode->IncRefCount();
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 {
1149 m_pChildList[i]->IncRefCount();
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 }
1177 ppPollList[i]->DecRefCount();
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 }
35f836fe 1253 SendPollerMsg(dwRqId, POLLER_ERROR _T("Node is unreachable\r\n"));
15d6f8c9
VK
1254 }
1255 else
1256 {
35f836fe 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);
35f836fe 1267 SendPollerMsg(dwRqId, POLLER_INFO _T("Node recovered from unreachable state\r\n"));
15d6f8c9
VK
1268 goto restart_agent_check;
1269 }
1270 else
1271 {
35f836fe 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
AK
1299 if (pPollerNode != NULL)
1300 pPollerNode->DecRefCount();
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);
35f836fe
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.
1348 SendPollerMsg(dwRqId, _T("Checking network path...\r\n"));
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());
1372 SendPollerMsg(dwRqId, POLLER_WARNING _T(" Upstream node %s is down\r\n"), hop->object->Name());
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);
1403 if ((object != NULL) && (!object->IsChild(m_dwId)))
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
AK
1496 m_pPollRequestor = pSession;
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 {
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"));
5039dede 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();
1548 SendPollerMsg(dwRqId, POLLER_INFO _T(" CheckPoint SNMP agent on port 260 is active\r\n"));
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"));
df8a4ca2 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
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
AK
1576 {
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
AK
1580 {
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 {
1586 SendPollerMsg(dwRqId, POLLER_WARNING _T("Node name cannot be resolved\r\n"));
1587 }
1588 }
1589 else
1590 {
1591 if (g_dwFlags & AF_SYNC_NODE_NAMES_WITH_DNS)
1592 {
1593 SendPollerMsg(dwRqId, _T("Syncing node name with DNS\r\n"));
35f836fe 1594 SetPollerInfo(nPoller, _T("resolving name"));
024c3faf 1595 if (resolveName(TRUE))
5039dede
AK
1596 {
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 {
1603 SendPollerMsg(dwRqId, _T("Node name is OK\r\n"));
1604 }
1605 }
1606
6fd6de0a 1607 applyUserTemplates();
7c521895 1608 updateContainerMembership();
d51f2182 1609 doInstanceDiscovery();
4d0c32f3 1610
df8a4ca2 1611 SendPollerMsg(dwRqId, _T("Finished configuration poll for node %s\r\n"), m_szName);
76720a09 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
1659 SendPollerMsg(dwRqId, _T(" Checking NetXMS agent...\r\n"));
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);
1672 SendPollerMsg(dwRqId, POLLER_INFO _T(" Connectivity with NetXMS agent restored\r\n"));
1673 }
1674 else
1675 {
1676 SendPollerMsg(dwRqId, POLLER_INFO _T(" NetXMS native agent is active\r\n"));
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;
1688 SendPollerMsg(dwRqId, _T(" NetXMS agent version changed to %s\r\n"), m_szAgentVersion);
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;
1700 SendPollerMsg(dwRqId, _T(" Platform name changed to %s\r\n"), m_szPlatformName);
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;
1726 SendPollerMsg(dwRqId, _T(" System description changed to %s\r\n"), m_sysDescription);
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;
1741 UnlockData();
1742 }
1743 else
1744 {
1745 DbgPrintf(5, _T("ConfPoll(%s): AgentConnection::getSupportedParameters() failed: rcc=%d"), m_szName, rcc);
1746 }
1747
1748 checkAgentPolicyBinding(pAgentConn);
1749
1750 pAgentConn->disconnect();
1751 }
1752 else
1753 {
1754 DbgPrintf(5, _T("ConfPoll(%s): checking for NetXMS agent - failed to connect"), m_szName);
1755 }
1756 delete pAgentConn;
1757 DbgPrintf(5, _T("ConfPoll(%s): checking for NetXMS agent - finished"), m_szName);
1758 return hasChanges;
1759}
1760
1761/**
1762 * SNMP walker callback which just counts number of varbinds
1763 */
1764static DWORD CountingSnmpWalkerCallback(DWORD version, SNMP_Variable *var, SNMP_Transport *transport, void *arg)
1765{
1766 (*((int *)arg))++;
1767 return SNMP_ERR_SUCCESS;
1768}
1769
1770/**
1771 * Configuration poll: check for SNMP
1772 */
1773bool Node::confPollSnmp(DWORD dwRqId)
1774{
1775 if (((m_dwFlags & NF_IS_SNMP) && (m_dwDynamicFlags & NDF_SNMP_UNREACHABLE)) ||
1776 (m_dwIpAddr == 0) || (m_dwFlags & NF_DISABLE_SNMP))
1777 return false;
1778
1779 bool hasChanges = false;
1780
1781 SendPollerMsg(dwRqId, _T(" Checking SNMP...\r\n"));
1782 DbgPrintf(5, _T("ConfPoll(%s): calling SnmpCheckCommSettings()"), m_szName);
1783 SNMP_Transport *pTransport = createSnmpTransport();
1784 if (pTransport == NULL)
1785 {
1786 DbgPrintf(5, _T("ConfPoll(%s): unable to create SNMP transport"), m_szName);
1787 return false;
1788 }
1789
1790 SNMP_SecurityContext *newCtx = SnmpCheckCommSettings(pTransport, &m_snmpVersion, m_snmpSecurity);
1791 if (newCtx != NULL)
1792 {
1793 LockData();
1794 delete m_snmpSecurity;
1795 m_snmpSecurity = newCtx;
1796 m_dwFlags |= NF_IS_SNMP;
1797 if (m_dwDynamicFlags & NDF_SNMP_UNREACHABLE)
1798 {
1799 m_dwDynamicFlags &= ~NDF_SNMP_UNREACHABLE;
1800 PostEvent(EVENT_SNMP_OK, m_dwId, NULL);
1801 SendPollerMsg(dwRqId, POLLER_INFO _T(" Connectivity with SNMP agent restored\r\n"));
1802 }
1803 UnlockData();
1804 SendPollerMsg(dwRqId, _T(" SNMP agent is active (version %s)\r\n"),
1805 (m_snmpVersion == SNMP_VERSION_3) ? _T("3") : ((m_snmpVersion == SNMP_VERSION_2C) ? _T("2c") : _T("1")));
1806
1807 TCHAR szBuffer[4096];
1808 if (SnmpGet(m_snmpVersion, pTransport,
1809 _T(".1.3.6.1.2.1.1.2.0"), NULL, 0, szBuffer, 4096, SG_STRING_RESULT) == SNMP_ERR_SUCCESS)
1810 {
1811 LockData();
1812 if (_tcscmp(m_szObjectId, szBuffer))
1813 {
1814 nx_strncpy(m_szObjectId, szBuffer, MAX_OID_LEN * 4);
1815 hasChanges = true;
1816 }
1817 UnlockData();
1818 }
1819
1820 // Get system description
1821 if (SnmpGet(m_snmpVersion, pTransport,
1822 _T(".1.3.6.1.2.1.1.1.0"), NULL, 0, szBuffer, MAX_DB_STRING, SG_STRING_RESULT) == SNMP_ERR_SUCCESS)
1823 {
1824 TranslateStr(szBuffer, _T("\r\n"), _T(" "));
1825 TranslateStr(szBuffer, _T("\n"), _T(" "));
1826 TranslateStr(szBuffer, _T("\r"), _T(" "));
1827 LockData();
1828 if ((m_sysDescription == NULL) || _tcscmp(m_sysDescription, szBuffer))
1829 {
1830 safe_free(m_sysDescription);
1831 m_sysDescription = _tcsdup(szBuffer);
1832 hasChanges = true;
1833 SendPollerMsg(dwRqId, _T(" System description changed to %s\r\n"), m_sysDescription);
1834 }
1835 UnlockData();
1836 }
1837
1838 // Select device driver
1839 NetworkDeviceDriver *driver = FindDriverForNode(this, pTransport);
1840 DbgPrintf(5, _T("ConfPoll(%s): selected device driver %s"), m_szName, driver->getName());
1841 LockData();
1842 if (driver != m_driver)
1843 {
1844 m_driver = driver;
1845 SendPollerMsg(dwRqId, _T(" New network device driver selected: %s\r\n"), m_driver->getName());
1846 }
1847 UnlockData();
1848
1849 // Allow driver to gather additional info
ae32341d 1850 m_driver->analyzeDevice(pTransport, m_szObjectId, &m_customAttributes, &m_driverData);
76720a09
VK
1851
1852 // Get sysName
1853 if (SnmpGet(m_snmpVersion, pTransport,
1854 _T(".1.3.6.1.2.1.1.5.0"), NULL, 0, szBuffer, MAX_DB_STRING, SG_STRING_RESULT) == SNMP_ERR_SUCCESS)
1855 {
1856 LockData();
1857 if ((m_sysName == NULL) || _tcscmp(m_sysName, szBuffer))
1858 {
1859 safe_free(m_sysName);
1860 m_sysName = _tcsdup(szBuffer);
1861 hasChanges = true;
1862 SendPollerMsg(dwRqId, _T(" System name changed to %s\r\n"), m_sysName);
1863 }
1864 UnlockData();
1865 }
1866
1867 // Check IP forwarding
074498ac 1868 if (checkSNMPIntegerValue(pTransport, _T(".1.3.6.1.2.1.4.1.0"), 1))
76720a09
VK
1869 {
1870 LockData();
1871 m_dwFlags |= NF_IS_ROUTER;
1872 UnlockData();
1873 }
1874 else
1875 {
1876 LockData();
1877 m_dwFlags &= ~NF_IS_ROUTER;
1878 UnlockData();
1879 }
1880
1881 checkIfXTable(pTransport);
1882 checkBridgeMib(pTransport);
eec253a8 1883
76720a09
VK
1884 // Check for ENTITY-MIB support
1885 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)
1886 {
1887 LockData();
1888 m_dwFlags |= NF_HAS_ENTITY_MIB;
1889 UnlockData();
1890
1891 ComponentTree *components = BuildComponentTree(this, pTransport);
1892 LockData();
1893 if (m_components != NULL)
1894 m_components->decRefCount();
1895 m_components = components;
1896 UnlockData();
1897 }
1898 else
1899 {
1900 LockData();
1901 m_dwFlags &= ~NF_HAS_ENTITY_MIB;
1902 if (m_components != NULL)
1903 {
1904 m_components->decRefCount();
1905 m_components = NULL;
1906 }
1907 UnlockData();
1908 }
1909
1910 // Check for printer MIB support
1911 int count = 0;
1912 SnmpEnumerate(m_snmpVersion, pTransport, _T(".1.3.6.1.2.1.43.5.1.1.17"), CountingSnmpWalkerCallback, &count, FALSE);
1913 if (count > 0)
1914 {
1915 LockData();
1916 m_dwFlags |= NF_IS_PRINTER;
1917 UnlockData();
1918 }
1919 else
1920 {
1921 LockData();
1922 m_dwFlags &= ~NF_IS_PRINTER;
1923 UnlockData();
1924 }
1925
1926 // Check for CDP (Cisco Discovery Protocol) support
074498ac 1927 if (checkSNMPIntegerValue(pTransport, _T(".1.3.6.1.4.1.9.9.23.1.3.1.0"), 1))
76720a09
VK
1928 {
1929 LockData();
1930 m_dwFlags |= NF_IS_CDP;
1931 UnlockData();
1932 }
1933 else
1934 {
1935 LockData();
1936 m_dwFlags &= ~NF_IS_CDP;
1937 UnlockData();
1938 }
1939
1940 // Check for NDP (Nortel Discovery Protocol) support
074498ac 1941 if (checkSNMPIntegerValue(pTransport, _T(".1.3.6.1.4.1.45.1.6.13.1.2.0"), 1))
76720a09
VK
1942 {
1943 LockData();
1944 m_dwFlags |= NF_IS_NDP;
1945 UnlockData();
1946 }
1947 else
1948 {
1949 LockData();
1950 m_dwFlags &= ~NF_IS_NDP;
1951 UnlockData();
1952 }
1953
1954 // Check for LLDP (Link Layer Discovery Protocol) support
1955 if (SnmpGet(m_snmpVersion, pTransport, _T(".1.0.8802.1.1.2.1.3.2.0"), NULL, 0, szBuffer, 4096, 0) == SNMP_ERR_SUCCESS)
1956 {
1957 LockData();
1958 m_dwFlags |= NF_IS_LLDP;
1959 UnlockData();
1960
1961 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)
1962 {
1963 _tcscat(szBuffer, _T("@"));
1964 int len = (int)_tcslen(szBuffer);
1965 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)
1966 {
1967 LockData();
1968 if ((m_lldpNodeId == NULL) || _tcscmp(m_lldpNodeId, szBuffer))
1969 {
1970 safe_free(m_lldpNodeId);
1971 m_lldpNodeId = _tcsdup(szBuffer);
1972 hasChanges = true;
1973 SendPollerMsg(dwRqId, _T(" LLDP node ID changed to %s\r\n"), m_lldpNodeId);
1974 }
1975 UnlockData();
1976 }
1977 }
3a82d5ae
VK
1978
1979 ObjectArray<LLDP_LOCAL_PORT_INFO> *lldpPorts = GetLLDPLocalPortInfo(pTransport);
1980 LockData();
1981 delete m_lldpLocalPortInfo;
1982 m_lldpLocalPortInfo = lldpPorts;
1983 UnlockData();
76720a09
VK
1984 }
1985 else
1986 {
1987 LockData();
1988 m_dwFlags &= ~NF_IS_LLDP;
1989 UnlockData();
1990 }
1991
1992 // Check for 802.1x support
074498ac 1993 if (checkSNMPIntegerValue(pTransport, _T(".1.0.8802.1.1.1.1.1.1.0"), 1))
76720a09
VK
1994 {
1995 LockData();
1996 m_dwFlags |= NF_IS_8021X;
1997 UnlockData();
1998 }
1999 else
2000 {
2001 LockData();
2002 m_dwFlags &= ~NF_IS_8021X;
2003 UnlockData();
2004 }
2005
074498ac 2006 checkOSPFSupport(pTransport);
76720a09
VK
2007
2008 // Get VRRP information
2009 VrrpInfo *vrrpInfo = GetVRRPInfo(this);
2010 if (vrrpInfo != NULL)
2011 {
2012 LockData();
2013 m_dwFlags |= NF_IS_VRRP;
2014 delete m_vrrpInfo;
2015 m_vrrpInfo = vrrpInfo;
2016 UnlockData();
2017 }
2018 else
2019 {
2020 LockData();
2021 m_dwFlags &= ~NF_IS_VRRP;
2022 UnlockData();
2023 }
2024 }
2025 else
2026 {
2027 // Check for CheckPoint SNMP agent on port 161
2028 DbgPrintf(5, _T("ConfPoll(%s): checking for CheckPoint SNMP"), m_szName);
2029 TCHAR szBuffer[4096];
2030 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)
2031 {
2032 LockData();
2033 if (_tcscmp(m_szObjectId, _T(".1.3.6.1.4.1.2620.1.1")))
2034 {
2035 nx_strncpy(m_szObjectId, _T(".1.3.6.1.4.1.2620.1.1"), MAX_OID_LEN * 4);
2036 hasChanges = true;
2037 }
2038
2039 m_dwFlags |= NF_IS_SNMP | NF_IS_ROUTER;
2040 m_dwDynamicFlags &= ~NDF_SNMP_UNREACHABLE;
2041 UnlockData();
2042 SendPollerMsg(dwRqId, POLLER_INFO _T(" CheckPoint SNMP agent on port 161 is active\r\n"));
2043 }
2044 }
2045 delete pTransport;
2046 return hasChanges;
2047}
2048
2049/**
2050 * Configuration poll: check for BRIDGE MIB
2051 */
2052void Node::checkBridgeMib(SNMP_Transport *pTransport)
2053{
2054 TCHAR szBuffer[4096];
2055 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)
2056 {
2057 LockData();
2058 m_dwFlags |= NF_IS_BRIDGE;
2059 memcpy(m_baseBridgeAddress, szBuffer, 6);
2060 UnlockData();
2061
2062 // Check for Spanning Tree (IEEE 802.1d) MIB support
074498ac 2063 if (checkSNMPIntegerValue(pTransport, _T(".1.3.6.1.2.1.17.2.1.0"), 3))
76720a09
VK
2064 {
2065 LockData();
2066 m_dwFlags |= NF_IS_STP;
2067 UnlockData();
2068 }
2069 else
2070 {
2071 LockData();
2072 m_dwFlags &= ~NF_IS_STP;
2073 UnlockData();
2074 }
2075 }
2076 else
2077 {
2078 LockData();
2079 m_dwFlags &= ~(NF_IS_BRIDGE | NF_IS_STP);
2080 UnlockData();
2081 }
2082}
2083
2084/**
2085 * Configuration poll: check for ifXTable
2086 */
2087void Node::checkIfXTable(SNMP_Transport *pTransport)
2088{
2089 int count = 0;
2090 SnmpEnumerate(m_snmpVersion, pTransport, _T(".1.3.6.1.2.1.31.1.1.1.1"), CountingSnmpWalkerCallback, &count, FALSE);
2091 if (count > 0)
2092 {
2093 LockData();
2094 m_dwFlags |= NF_HAS_IFXTABLE;
2095 UnlockData();
2096 }
2097 else
2098 {
2099 LockData();
2100 m_dwFlags &= ~NF_HAS_IFXTABLE;
2101 UnlockData();
2102 }
2103}
2104
2105/**
2106 * Update interface configuration
2107 */
eec253a8
VK
2108BOOL Node::updateInterfaceConfiguration(DWORD dwRqId, DWORD dwNetMask)
2109{
98762401 2110 InterfaceList *pIfList;
eec253a8
VK
2111 Interface **ppDeleteList;
2112 int i, j, iDelCount;
2113 BOOL hasChanges = FALSE;
2114 Cluster *pCluster = getMyCluster();
2115
2116 SendPollerMsg(dwRqId, _T("Checking interface configuration...\r\n"));
2117 pIfList = getInterfaceList();
2118 if (pIfList != NULL)
2119 {
22d657d2 2120 DbgPrintf(6, _T("Node::updateInterfaceConfiguration(%s [%u]): got %d interfaces"), m_szName, m_dwId, pIfList->getSize());
eec253a8
VK
2121 // Remove cluster virtual interfaces from list
2122 if (pCluster != NULL)
2123 {
98762401 2124 for(i = 0; i < pIfList->getSize(); i++)
eec253a8 2125 {
98762401 2126 if (pCluster->isVirtualAddr(pIfList->get(i)->dwIpAddr))
eec253a8 2127 {
98762401 2128 pIfList->remove(i);
eec253a8
VK
2129 i--;
2130 }
2131 }
2132 }
2133
2134 // Find non-existing interfaces
2135 LockChildList(FALSE);
2136 ppDeleteList = (Interface **)malloc(sizeof(Interface *) * m_dwChildCount);
2137 for(i = 0, iDelCount = 0; i < (int)m_dwChildCount; i++)
2138 {
2139 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
2140 {
2141 Interface *pInterface = (Interface *)m_pChildList[i];
9214177b
VK
2142 if (!pInterface->isManuallyCreated())
2143 {
2144 for(j = 0; j < pIfList->getSize(); j++)
2145 {
2146 if ((pIfList->get(j)->dwIndex == pInterface->getIfIndex()) &&
2147 (pIfList->get(j)->dwIpAddr == pInterface->IpAddr()))
2148 break;
2149 }
eec253a8 2150
9214177b
VK
2151 if (j == pIfList->getSize())
2152 {
2153 // No such interface in current configuration, add it to delete list
2154 ppDeleteList[iDelCount++] = pInterface;
2155 }
2156 }
eec253a8
VK
2157 }
2158 }
2159 UnlockChildList();
2160
2161 // Delete non-existent interfaces
2162 if (iDelCount > 0)
2163 {
2164 for(j = 0; j < iDelCount; j++)
2165 {
2166 SendPollerMsg(dwRqId, POLLER_WARNING _T(" Interface \"%s\" is no longer exist\r\n"),
2167 ppDeleteList[j]->Name());
2168 PostEvent(EVENT_INTERFACE_DELETED, m_dwId, "dsaa", ppDeleteList[j]->getIfIndex(),
2169 ppDeleteList[j]->Name(), ppDeleteList[j]->IpAddr(), ppDeleteList[j]->getIpNetMask());
2170 deleteInterface(ppDeleteList[j]);
2171 }
2172 hasChanges = TRUE;
2173 }
2174 safe_free(ppDeleteList);
2175
2176 // Add new interfaces and check configuration of existing
98762401 2177 for(j = 0; j < pIfList->getSize(); j++)
eec253a8 2178 {
60557d06 2179 NX_INTERFACE_INFO *ifInfo = pIfList->get(j);
eec253a8
VK
2180 BOOL bNewInterface = TRUE;
2181
2182 LockChildList(FALSE);
2183 for(i = 0; i < (int)m_dwChildCount; i++)
2184 {
2185 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
2186 {
2187 Interface *pInterface = (Interface *)m_pChildList[i];
2188
98762401
VK
2189 if ((ifInfo->dwIndex == pInterface->getIfIndex()) &&
2190 (ifInfo->dwIpAddr == pInterface->IpAddr()))
eec253a8
VK
2191 {
2192 // Existing interface, check configuration
22d657d2 2193 if (memcmp(ifInfo->bMacAddr, "\x00\x00\x00\x00\x00\x00", MAC_ADDR_LENGTH) && memcmp(ifInfo->bMacAddr, pInterface->getMacAddr(), MAC_ADDR_LENGTH))
eec253a8
VK
2194 {
2195 TCHAR szOldMac[16], szNewMac[16];
2196
2197 BinToStr((BYTE *)pInterface->getMacAddr(), MAC_ADDR_LENGTH, szOldMac);
98762401 2198 BinToStr(ifInfo->bMacAddr, MAC_ADDR_LENGTH, szNewMac);
eec253a8
VK
2199 PostEvent(EVENT_MAC_ADDR_CHANGED, m_dwId, "idsss",
2200 pInterface->Id(), pInterface->getIfIndex(),
2201 pInterface->Name(), szOldMac, szNewMac);
98762401 2202 pInterface->setMacAddr(ifInfo->bMacAddr);
eec253a8 2203 }
98762401 2204 if (_tcscmp(ifInfo->szName, pInterface->Name()))
eec253a8 2205 {
98762401 2206 pInterface->setName(ifInfo->szName);
eec253a8 2207 }
478d4ff4
VK
2208 if (_tcscmp(ifInfo->szDescription, pInterface->getDescription()))
2209 {
2210 pInterface->setDescription(ifInfo->szDescription);
2211 }
98762401 2212 if (ifInfo->dwBridgePortNumber != pInterface->getBridgePortNumber())
eec253a8 2213 {
98762401 2214 pInterface->setBridgePortNumber(ifInfo->dwBridgePortNumber);
eec253a8 2215 }
98762401 2216 if (ifInfo->dwSlotNumber != pInterface->getSlotNumber())
76f9abfd 2217 {
98762401 2218 pInterface->setSlotNumber(ifInfo->dwSlotNumber);
76f9abfd 2219 }
98762401 2220 if (ifInfo->dwPortNumber != pInterface->getPortNumber())
76f9abfd 2221 {
98762401 2222 pInterface->setPortNumber(ifInfo->dwPortNumber);
76f9abfd 2223 }
4c16cdc7
VK
2224 if (ifInfo->isPhysicalPort != pInterface->isPhysicalPort())
2225 {
2226 pInterface->setPhysicalPortFlag(ifInfo->isPhysicalPort);
2227 }
98762401 2228 if ((ifInfo->dwIpNetMask != 0) && (ifInfo->dwIpNetMask != pInterface->getIpNetMask()))
dfb38baf 2229 {
98762401 2230 pInterface->setIpNetMask(ifInfo->dwIpNetMask);
dfb38baf 2231 }
eec253a8
VK
2232 bNewInterface = FALSE;
2233 break;
2234 }
2235 }
2236 }
2237 UnlockChildList();
2238
2239 if (bNewInterface)
2240 {
2241 // New interface
98762401
VK
2242 SendPollerMsg(dwRqId, POLLER_INFO _T(" Found new interface \"%s\"\r\n"), ifInfo->szName);
2243 createNewInterface(ifInfo->dwIpAddr,
2244 ifInfo->dwIpNetMask,
2245 ifInfo->szName,
478d4ff4 2246 ifInfo->szDescription,
98762401
VK
2247 ifInfo->dwIndex,
2248 ifInfo->dwType,
2249 ifInfo->bMacAddr,
2250 ifInfo->dwBridgePortNumber,
2251 ifInfo->dwSlotNumber,
4c16cdc7
VK
2252 ifInfo->dwPortNumber,
2253 ifInfo->isPhysicalPort);
eec253a8
VK
2254 hasChanges = TRUE;
2255 }
2256 }
2257
2258 // Check if address we are using to communicate with node
2259 // is configured on one of node's interfaces
98762401
VK
2260 for(i = 0; i < pIfList->getSize(); i++)
2261 if (pIfList->get(i)->dwIpAddr == m_dwIpAddr)
eec253a8
VK
2262 break;
2263
98762401 2264 if (i == (DWORD)pIfList->getSize())
eec253a8 2265 {
eec253a8 2266 // Node is behind NAT
563179bc 2267 if (!(m_dwFlags & NF_BEHIND_NAT))
eec253a8 2268 {
563179bc
VK
2269 m_dwFlags |= NF_BEHIND_NAT;
2270 hasChanges = TRUE;
2271 }
eec253a8
VK
2272 }
2273 else
2274 {
2275 // Check if NF_BEHIND_NAT flag set incorrectly
2276 if (m_dwFlags & NF_BEHIND_NAT)
2277 {
eec253a8
VK
2278 m_dwFlags &= ~NF_BEHIND_NAT;
2279 hasChanges = TRUE;
2280 }
2281 }
2282
024c3faf 2283 checkSubnetBinding(pIfList);
eec253a8 2284
98762401 2285 delete pIfList;
eec253a8
VK
2286 }
2287 else /* pIfList == NULL */
2288 {
2289 Interface *pInterface;
2290 DWORD dwCount;
2291
2292 SendPollerMsg(dwRqId, POLLER_ERROR _T("Unable to get interface list from node\r\n"));
22d657d2 2293 DbgPrintf(6, _T("Node::updateInterfaceConfiguration(%s [%u]): Unable to get interface list from node"), m_szName, m_dwId);
eec253a8
VK
2294
2295 // Delete all existing interfaces in case of forced capability recheck
2296 if (m_dwDynamicFlags & NDF_RECHECK_CAPABILITIES)
2297 {
2298 LockChildList(FALSE);
2299 ppDeleteList = (Interface **)malloc(sizeof(Interface *) * m_dwChildCount);
2300 for(i = 0, iDelCount = 0; i < (int)m_dwChildCount; i++)
2301 {
9214177b 2302 if ((m_pChildList[i]->Type() == OBJECT_INTERFACE) && !((Interface *)m_pChildList[i])->isManuallyCreated())
eec253a8
VK
2303 ppDeleteList[iDelCount++] = (Interface *)m_pChildList[i];
2304 }
2305 UnlockChildList();
2306 for(j = 0; j < iDelCount; j++)
2307 {
2308 SendPollerMsg(dwRqId, POLLER_WARNING _T(" Interface \"%s\" is no longer exist\r\n"),
2309 ppDeleteList[j]->Name());
2310 PostEvent(EVENT_INTERFACE_DELETED, m_dwId, "dsaa", ppDeleteList[j]->getIfIndex(),
2311 ppDeleteList[j]->Name(), ppDeleteList[j]->IpAddr(), ppDeleteList[j]->getIpNetMask());
2312 deleteInterface(ppDeleteList[j]);
2313 }
2314 safe_free(ppDeleteList);
2315 }
2316
2317 // Check if we have pseudo-interface object
baa5324c
AK
2318 BYTE macAddr[MAC_ADDR_LENGTH];
2319 BYTE *pMacAddr;
eec253a8
VK
2320 dwCount = getInterfaceCount(&pInterface);
2321 if (dwCount == 1)
2322 {
2323 if (pInterface->isFake())
2324 {
2325 // Check if primary IP is different from interface's IP
2326 if (pInterface->IpAddr() != m_dwIpAddr)
2327 {
2328 deleteInterface(pInterface);
2329 if (m_dwIpAddr != 0)
baa5324c
AK
2330 {
2331 memset(macAddr, 0, MAC_ADDR_LENGTH);
2332 Subnet *pSubnet = FindSubnetForNode(m_zoneId, m_dwIpAddr);
2333 if (pSubnet != NULL)
2334 pSubnet->findMacAddress(m_dwIpAddr, macAddr);
22d657d2 2335 pMacAddr = !memcmp(macAddr, "\x00\x00\x00\x00\x00\x00", MAC_ADDR_LENGTH) ? NULL : macAddr;
baa5324c
AK
2336 TCHAR szMac[20];
2337 MACToStr(macAddr, szMac);
dab4332d 2338 DbgPrintf(5, _T("Node::updateInterfaceConfiguration(%s [%u]): got MAC for unknown interface: %s"), m_szName, m_dwId, szMac);
baa5324c
AK
2339 createNewInterface(m_dwIpAddr, dwNetMask, NULL, NULL, 0, 0, pMacAddr);
2340 }
eec253a8 2341 }
22d657d2
VK
2342 else
2343 {
2344 // check MAC address
2345 memset(macAddr, 0, MAC_ADDR_LENGTH);
2346 Subnet *pSubnet = FindSubnetForNode(m_zoneId, m_dwIpAddr);
2347 if (pSubnet != NULL)
2348 pSubnet->findMacAddress(m_dwIpAddr, macAddr);
2349 if (memcmp(macAddr, "\x00\x00\x00\x00\x00\x00", MAC_ADDR_LENGTH) && memcmp(macAddr, pInterface->getMacAddr(), MAC_ADDR_LENGTH))
2350 {
2351 TCHAR szOldMac[16], szNewMac[16];
2352
2353 BinToStr((BYTE *)pInterface->getMacAddr(), MAC_ADDR_LENGTH, szOldMac);
2354 BinToStr(macAddr, MAC_ADDR_LENGTH, szNewMac);
dab4332d 2355 DbgPrintf(5, _T("Node::updateInterfaceConfiguration(%s [%u]): MAC change for unknown interface: %s to %s"),
22d657d2
VK
2356 m_szName, m_dwId, szOldMac, szNewMac);
2357 PostEvent(EVENT_MAC_ADDR_CHANGED, m_dwId, "idsss",
2358 pInterface->Id(), pInterface->getIfIndex(),
2359 pInterface->Name(), szOldMac, szNewMac);
2360 pInterface->setMacAddr(macAddr);
2361 }
2362 }
eec253a8
VK
2363 }
2364 }
2365 else if (dwCount == 0)
2366 {
2367 // No interfaces at all, create pseudo-interface
2368 if (m_dwIpAddr != 0)
baa5324c
AK
2369 {
2370 memset(macAddr, 0, MAC_ADDR_LENGTH);
2371 Subnet *pSubnet = FindSubnetForNode(m_zoneId, m_dwIpAddr);
2372 if (pSubnet != NULL)
2373 pSubnet->findMacAddress(m_dwIpAddr, macAddr);
22d657d2 2374 pMacAddr = !memcmp(macAddr, "\x00\x00\x00\x00\x00\x00", MAC_ADDR_LENGTH) ? NULL : macAddr;
baa5324c
AK
2375 TCHAR szMac[20];
2376 MACToStr(macAddr, szMac);
d51f2182 2377 DbgPrintf(5, _T("Node::updateInterfaceConfiguration(%s [%u]): got MAC for unknown interface: %s"), m_szName, m_dwId, szMac);
baa5324c
AK
2378 createNewInterface(m_dwIpAddr, dwNetMask, NULL, NULL, 0, 0, pMacAddr);
2379 }
eec253a8 2380 }
22d657d2 2381 DbgPrintf(6, _T("Node::updateInterfaceConfiguration(%s [%u]): pflist == NULL, dwCount = %ld"), m_szName, m_dwId, dwCount);
eec253a8
VK
2382 }
2383
2384 SendPollerMsg(dwRqId, _T("Interface configuration check finished\r\n"));
2385 return hasChanges;
2386}
2387
6fd6de0a 2388/**
7aad6641 2389 * Callback: apply template to nodes
6fd6de0a 2390 */
27de5dab 2391static void ApplyTemplate(NetObj *object, void *node)
4d0c32f3 2392{
6ff21d27 2393 if ((object->Type() == OBJECT_TEMPLATE) && !object->isDeleted())
4d0c32f3 2394 {
27de5dab
VK
2395 Template *pTemplate = (Template *)object;
2396 if (pTemplate->isApplicable((Node *)node))
2397 {
2398 if (!pTemplate->IsChild(((Node *)node)->Id()))
5039dede 2399 {
27de5dab
VK
2400 DbgPrintf(4, _T("Node::ApplyUserTemplates(): applying template %d \"%s\" to node %d \"%s\""),
2401 pTemplate->Id(), pTemplate->Name(), ((Node *)node)->Id(), ((Node *)node)->Name());
6fd6de0a 2402 pTemplate->applyToTarget((Node *)node);
9b64b406 2403 PostEvent(EVENT_TEMPLATE_AUTOAPPLY, g_dwMgmtNode, "isis", ((Node *)node)->Id(), ((Node *)node)->Name(), pTemplate->Id(), pTemplate->Name());
5039dede 2404 }
27de5dab
VK
2405 }
2406 else
2407 {
9b64b406 2408 if (pTemplate->isAutoRemoveEnabled() && pTemplate->IsChild(((Node *)node)->Id()))
5039dede 2409 {
27de5dab
VK
2410 DbgPrintf(4, _T("Node::ApplyUserTemplates(): removing template %d \"%s\" from node %d \"%s\""),
2411 pTemplate->Id(), pTemplate->Name(), ((Node *)node)->Id(), ((Node *)node)->Name());
2412 pTemplate->DeleteChild((Node *)node);
2413 ((Node *)node)->DeleteParent(pTemplate);
6fd6de0a 2414 pTemplate->queueRemoveFromTarget(((Node *)node)->Id(), TRUE);
9b64b406 2415 PostEvent(EVENT_TEMPLATE_AUTOREMOVE, g_dwMgmtNode, "isis", ((Node *)node)->Id(), ((Node *)node)->Name(), pTemplate->Id(), pTemplate->Name());
5039dede 2416 }
27de5dab 2417 }
5039dede 2418 }
27de5dab
VK
2419}
2420
7aad6641
VK
2421/**
2422 * Apply user templates
2423 */
6fd6de0a 2424void Node::applyUserTemplates()
27de5dab
VK
2425{
2426 g_idxObjectById.forEach(ApplyTemplate, this);
4d0c32f3 2427}
5039dede 2428
6fd6de0a 2429/**
7aad6641 2430 * Callback: update container membership
6fd6de0a 2431 */
27de5dab 2432static void UpdateContainerBinding(NetObj *object, void *node)
4d0c32f3 2433{
6ff21d27 2434 if ((object->Type() == OBJECT_CONTAINER) && !object->isDeleted())
5039dede 2435 {
27de5dab 2436 Container *pContainer = (Container *)object;
926e8ce7 2437 if (pContainer->isSuitableForNode((Node *)node))
27de5dab
VK
2438 {
2439 if (!pContainer->IsChild(((Node *)node)->Id()))
4d0c32f3 2440 {
27de5dab
VK
2441 DbgPrintf(4, _T("Node::UpdateContainerMembership(): binding node %d \"%s\" to container %d \"%s\""),
2442 ((Node *)node)->Id(), ((Node *)node)->Name(), pContainer->Id(), pContainer->Name());
2443 pContainer->AddChild((Node *)node);
2444 ((Node *)node)->AddParent(pContainer);
9b64b406 2445 PostEvent(EVENT_CONTAINER_AUTOBIND, g_dwMgmtNode, "isis", ((Node *)node)->Id(), ((Node *)node)->Name(), pContainer->Id(), pContainer->Name());
4d0c32f3 2446 }
27de5dab
VK
2447 }
2448 else
2449 {
9b64b406 2450 if (pContainer->isAutoUnbindEnabled() && pContainer->IsChild(((Node *)node)->Id()))
4d0c32f3 2451 {
27de5dab
VK
2452 DbgPrintf(4, _T("Node::UpdateContainerMembership(): removing node %d \"%s\" from container %d \"%s\""),
2453 ((Node *)node)->Id(), ((Node *)node)->Name(), pContainer->Id(), pContainer->Name());
2454 pContainer->DeleteChild((Node *)node);
2455 ((Node *)node)->DeleteParent(pContainer);
9b64b406 2456 PostEvent(EVENT_CONTAINER_AUTOUNBIND, g_dwMgmtNode, "isis", ((Node *)node)->Id(), ((Node *)node)->Name(), pContainer->Id(), pContainer->Name());
4d0c32f3 2457 }
27de5dab 2458 }
5039dede 2459 }
27de5dab
VK
2460}
2461
7aad6641
VK
2462/**
2463 * Update container membership
2464 */
27de5dab
VK
2465void Node::updateContainerMembership()
2466{
2467 g_idxObjectById.forEach(UpdateContainerBinding, this);
5039dede
AK
2468}
2469
a3050773 2470/**
d51f2182
VK
2471 * Do instance discovery
2472 */
2473void Node::doInstanceDiscovery()
2474{
2475 // collect instance discovery DCIs
2476 ObjectArray<DCItem> rootItems;
2477 lockDciAccess();
2478 for(int i = 0; i < m_dcObjects->size(); i++)
2479 {
2480 DCObject *object = m_dcObjects->get(i);
2481 if ((object->getType() == DCO_TYPE_ITEM) && (((DCItem *)object)->getInstanceDiscoveryMethod() != IDM_NONE))
2482 {
2483 object->setBusyFlag(TRUE);
2484 rootItems.add((DCItem *)object);
2485 }
2486 }
2487 unlockDciAccess();
2488
2489 // process instance discovery DCIs
2490 // it should be done that way to prevent DCI list lock for long time
2491 for(int i = 0; i < rootItems.size(); i++)
2492 {
2493 DCItem *dci = rootItems.get(i);
2494 DbgPrintf(5, _T("Node::doInstanceDiscovery(%s [%u]): Updating instances for instance discovery DCI %s [%d]"),
2495 m_szName, m_dwId, dci->getName(), dci->getId());
2496 StringList *instances = getInstanceList(dci);
2497 if (instances != NULL)
2498 {
2499 DbgPrintf(5, _T("Node::doInstanceDiscovery(%s [%u]): read %d values"), m_szName, m_dwId, instances->getSize());
27bbb906 2500 dci->filterInstanceList(instances);
d51f2182
VK
2501 updateInstances(dci, instances);
2502 delete instances;
2503 }
2504 else
2505 {
2506 DbgPrintf(5, _T("Node::doInstanceDiscovery(%s [%u]): failed to get instance list for DCI %s [%d]"),
2507 m_szName, m_dwId, dci->getName(), dci->getId());
2508 }
2509 dci->setBusyFlag(FALSE);
2510 }
2511}
2512
2513/**
2514 * Get instances for instance discovery DCI
2515 */
2516StringList *Node::getInstanceList(DCItem *dci)
2517{
2518 if (dci->getInstanceDiscoveryData() == NULL)
2519 return NULL;
2520
2521 StringList *instances;
2522 switch(dci->getInstanceDiscoveryMethod())
2523 {
2524 case IDM_AGENT_LIST:
2525 getListFromAgent(dci->getInstanceDiscoveryData(), &instances);
2526 break;
7aad6641
VK
2527 case IDM_SNMP_WALK_VALUES:
2528 getListFromSNMP(dci->getSnmpPort(), dci->getInstanceDiscoveryData(), &instances);
2529 break;
2530 case IDM_SNMP_WALK_OIDS:
2531 getOIDSuffixListFromSNMP(dci->getSnmpPort(), dci->getInstanceDiscoveryData(), &instances);
2532 break;
d51f2182
VK
2533 default:
2534 instances = NULL;
2535 break;
2536 }
2537 return instances;
2538}
2539
2540/**
2541 * Update instance DCIs created from instance discovery DCI
2542 */
2543void Node::updateInstances(DCItem *root, StringList *instances)
2544{
2545 lockDciAccess();
2546
2547 // Delete DCIs for missing instances and update existing
2548 ObjectArray<void> deleteList;
2549 for(int i = 0; i < m_dcObjects->size(); i++)
2550 {
2551 DCObject *object = m_dcObjects->get(i);
2552 if ((object->getType() != DCO_TYPE_ITEM) ||
2553 (object->getTemplateId() != m_dwId) ||
2554 (object->getTemplateItemId() != root->getId()))
2555 continue;
2556
2557 int j;
2558 for(j = 0; j < instances->getSize(); j++)
2559 if (!_tcscmp(((DCItem *)object)->getInstance(), instances->getValue(j)))
2560 break;
2561
2562 if (j < instances->getSize())
2563 {
2564 // found, remove value from instances
2565 DbgPrintf(5, _T("Node::updateInstances(%s [%u], %s [%u]): instance \"%s\" found"),
2566 m_szName, m_dwId, root->getName(), root->getId(), instances->getValue(j));
2567 instances->remove(j);
2568 }
2569 else
2570 {
2571 // not found, delete DCI
2572 DbgPrintf(5, _T("Node::updateInstances(%s [%u], %s [%u]): instance \"%s\" not found, instance DCI will be deleted"),
27bbb906 2573 m_szName, m_dwId, root->getName(), root->getId(), ((DCItem *)object)->getInstance());
d51f2182
VK
2574 deleteList.add(CAST_TO_POINTER(object->getId(), void *));
2575 }
2576 }
2577
2578 for(int i = 0; i < deleteList.size(); i++)
2579 deleteDCObject(CAST_FROM_POINTER(deleteList.get(i), DWORD), false);
2580
2581 // Create new instances
2582 for(int i = 0; i < instances->getSize(); i++)
2583 {
27bbb906
VK
2584 DbgPrintf(5, _T("Node::updateInstances(%s [%u], %s [%u]): creating new DCI for instance \"%s\""),
2585 m_szName, m_dwId, root->getName(), root->getId(), instances->getValue(i));
2586
d51f2182
VK
2587 DCItem *dci = new DCItem(root);
2588 dci->setTemplateId(m_dwId, root->getId());
2589 dci->setInstance(instances->getValue(i));
2590 dci->setInstanceDiscoveryMethod(IDM_NONE);
2591 dci->setInstanceDiscoveryData(NULL);
2592 dci->setInstanceFilter(NULL);
2593 dci->expandInstance();
2594 dci->changeBinding(CreateUniqueId(IDG_ITEM), this, FALSE);
2595 addDCObject(dci, true);
2596 }
2597
2598 unlockDciAccess();
2599}
2600
2601/**
a3050773
VK
2602 * Connect to native agent. Assumes that access to agent connection is already locked.
2603 */
a4569c4d 2604BOOL Node::connectToAgent(DWORD *error, DWORD *socketError)
5039dede
AK
2605{
2606 BOOL bRet;
2607
2608 // Create new agent connection object if needed
2609 if (m_pAgentConnection == NULL)
4b91844f 2610 {
d1c1c522 2611 m_pAgentConnection = new AgentConnectionEx(m_dwId, htonl(m_dwIpAddr), m_wAgentPort, m_wAuthMethod, m_szSharedSecret);
4b91844f
VK
2612 DbgPrintf(7, _T("Node::connectToAgent(%s [%d]): new agent connection created"), m_szName, m_dwId);
2613 }
2614 else
2615 {
2616 // Check if we already connected
2617 if (m_pAgentConnection->nop() == ERR_SUCCESS)
2618 {
2619 DbgPrintf(7, _T("Node::connectToAgent(%s [%d]): already connected"), m_szName, m_dwId);
2620 return TRUE;
2621 }
5039dede 2622
4b91844f
VK
2623 // Close current connection or clean up after broken connection
2624 m_pAgentConnection->disconnect();
2625 DbgPrintf(7, _T("Node::connectToAgent(%s [%d]): existing connection reset"), m_szName, m_dwId);
2626 }
7c521895 2627 m_pAgentConnection->setPort(m_wAgentPort);
35f836fe
VK
2628#ifdef UNICODE
2629 char mbSecret[MAX_SECRET_LENGTH];
2630 WideCharToMultiByte(CP_ACP, WC_DEFAULTCHAR | WC_COMPOSITECHECK, m_szSharedSecret, -1, mbSecret, MAX_SECRET_LENGTH, NULL, NULL);
2631 mbSecret[MAX_SECRET_LENGTH - 1] = 0;
2632 m_pAgentConnection->setAuthData(m_wAuthMethod, mbSecret);
2633#else
7c521895 2634 m_pAgentConnection->setAuthData(m_wAuthMethod, m_szSharedSecret);
35f836fe 2635#endif
7c521895 2636 setAgentProxy(m_pAgentConnection);
4b91844f 2637 DbgPrintf(7, _T("Node::connectToAgent(%s [%d]): calling connect on port %d"), m_szName, m_dwId, (int)m_wAgentPort);
a4569c4d 2638 bRet = m_pAgentConnection->connect(g_pServerKey, FALSE, error, socketError);
5039dede
AK
2639 if (bRet)
2640 {
45d84f8a
VK
2641 m_pAgentConnection->setCommandTimeout(g_dwAgentCommandTimeout);
2642 m_pAgentConnection->enableTraps();
5039dede
AK
2643 }
2644 return bRet;
2645}
2646
a3050773
VK
2647/**
2648 * Get DCI value via SNMP
2649 */
e320f8ce 2650DWORD Node::getItemFromSNMP(WORD port, const TCHAR *szParam, DWORD dwBufSize, TCHAR *szBuffer, int interpretRawValue)
5039dede
AK
2651{
2652 DWORD dwResult;
2653
2fd7144f 2654 if ((((m_dwDynamicFlags & NDF_SNMP_UNREACHABLE) || !(m_dwFlags & NF_IS_SNMP)) && (port == 0)) ||
6b2bb22c
VK
2655 (m_dwDynamicFlags & NDF_UNREACHABLE) ||
2656 (m_dwFlags & NF_DISABLE_SNMP))
5039dede
AK
2657 {
2658 dwResult = SNMP_ERR_COMM;
2659 }
2660 else
2661 {
2662 SNMP_Transport *pTransport;
2663
65e2005b 2664 pTransport = createSnmpTransport(port);
803d47be
VK
2665 if (pTransport != NULL)
2666 {
e320f8ce
VK
2667 if (interpretRawValue == SNMP_RAWTYPE_NONE)
2668 {
2669 dwResult = SnmpGet(m_snmpVersion, pTransport, szParam, NULL, 0, szBuffer, dwBufSize, SG_PSTRING_RESULT);
2670 }
2671 else
2672 {
2673 BYTE rawValue[1024];
2674 memset(rawValue, 0, 1024);
2675 dwResult = SnmpGet(m_snmpVersion, pTransport, szParam, NULL, 0, rawValue, 1024, SG_RAW_RESULT);
2676 if (dwResult == SNMP_ERR_SUCCESS)
2677 {
2678 switch(interpretRawValue)
2679 {
2680 case SNMP_RAWTYPE_INT32:
2681 _sntprintf(szBuffer, dwBufSize, _T("%d"), ntohl(*((LONG *)rawValue)));
2682 break;
2683 case SNMP_RAWTYPE_UINT32:
2684 _sntprintf(szBuffer, dwBufSize, _T("%u"), ntohl(*((DWORD *)rawValue)));
2685 break;
2686 case SNMP_RAWTYPE_INT64:
7aad6641 2687 _sntprintf(szBuffer, dwBufSize, INT64_FMT, (INT64)ntohq(*((INT64 *)rawValue)));
e320f8ce
VK
2688 break;
2689 case SNMP_RAWTYPE_UINT64:
2690 _sntprintf(szBuffer, dwBufSize, UINT64_FMT, ntohq(*((QWORD *)rawValue)));
2691 break;
2692 case SNMP_RAWTYPE_DOUBLE:
2693 _sntprintf(szBuffer, dwBufSize, _T("%f"), ntohd(*((double *)rawValue)));
2694 break;
2695 case SNMP_RAWTYPE_IP_ADDR:
2696 IpToStr(ntohl(*((DWORD *)rawValue)), szBuffer);
2697 break;
2698 case SNMP_RAWTYPE_MAC_ADDR:
2699 MACToStr(rawValue, szBuffer);
2700 break;
2701 default:
2702 szBuffer[0] = 0;
2703 break;
2704 }
2705 }
2706 }
803d47be
VK
2707 delete pTransport;
2708 }
2709 else
2710 {
2711 dwResult = SNMP_ERR_COMM;
2712 }
5039dede 2713 }
35f836fe 2714 DbgPrintf(7, _T("Node(%s)->GetItemFromSNMP(%s): dwResult=%d"), m_szName, szParam, dwResult);
5039dede
AK
2715 return (dwResult == SNMP_ERR_SUCCESS) ? DCE_SUCCESS :
2716 ((dwResult == SNMP_ERR_NO_OBJECT) ? DCE_NOT_SUPPORTED : DCE_COMM_ERROR);
2717}
2718
6fd6de0a 2719/**
7aad6641
VK
2720 * Callback for SnmpEnumerate in Node::getListFromSNMP
2721 */
2722static DWORD SNMPGetListCallback(DWORD snmpVersion, SNMP_Variable *varbind, SNMP_Transport *snmp, void *arg)
2723{
2724 bool convert = false;
2725 TCHAR buffer[256];
2726 ((StringList *)arg)->add(varbind->getValueAsPrintableString(buffer, 256, &convert));
2727 return SNMP_ERR_SUCCESS;
2728}
2729
2730/**
2731 * Get list of values from SNMP
2732 */
2733DWORD Node::getListFromSNMP(WORD port, const TCHAR *oid, StringList **list)
2734{
2735 *list = NULL;
2736 SNMP_Transport *snmp = createSnmpTransport(port);
2737 if (snmp == NULL)
2738 return DCE_COMM_ERROR;
2739
2740 *list = new StringList;
2741 DWORD rc = SnmpEnumerate(snmp->getSnmpVersion(), snmp, oid, SNMPGetListCallback, *list, FALSE);
2742 delete snmp;
2743 if (rc != SNMP_ERR_SUCCESS)
2744 {
2745 delete *list;
2746 *list = NULL;
2747 }
2748 return (rc == SNMP_ERR_SUCCESS) ? DCE_SUCCESS : ((rc == SNMP_ERR_NO_OBJECT) ? DCE_NOT_SUPPORTED : DCE_COMM_ERROR);
2749}
2750
2751/**
2752 * Information for SNMPOIDSuffixListCallback
2753 */
2754struct SNMPOIDSuffixListCallback_Data
2755{
2756 DWORD oidLen;
2757 StringList *values;
2758};
2759
2760/**
2761 * Callback for SnmpEnumerate in Node::getOIDSuffixListFromSNMP
2762 */
2763static DWORD SNMPOIDSuffixListCallback(DWORD snmpVersion, SNMP_Variable *varbind, SNMP_Transport *snmp, void *arg)
2764{
2765 SNMPOIDSuffixListCallback_Data *data = (SNMPOIDSuffixListCallback_Data *)arg;
2766 SNMP_ObjectId *oid = varbind->GetName();
2767 if (oid->getLength() <= data->oidLen)
2768 return SNMP_ERR_SUCCESS;
2769 TCHAR buffer[256];
2770 SNMPConvertOIDToText(oid->getLength() - data->oidLen, &(oid->getValue()[data->oidLen]), buffer, 256);
2771 data->values->add((buffer[0] == _T('.')) ? &buffer[1] : buffer);
2772 return SNMP_ERR_SUCCESS;
2773}
2774
2775/**
2776 * Get list of OID suffixes from SNMP
2777 */
2778DWORD Node::getOIDSuffixListFromSNMP(WORD port, const TCHAR *oid, StringList **list)
2779{
2780 *list = NULL;
2781 SNMP_Transport *snmp = createSnmpTransport(port);
2782 if (snmp == NULL)
2783 return DCE_COMM_ERROR;
2784
2785 SNMPOIDSuffixListCallback_Data data;
2786 DWORD oidBin[256];
2787 data.oidLen = SNMPParseOID(oid, oidBin, 256);
2788 if (data.oidLen == 0)
2789 {
2790 delete snmp;
2791 return DCE_NOT_SUPPORTED;
2792 }
2793
2794 data.values = new StringList;
2795 DWORD rc = SnmpEnumerate(snmp->getSnmpVersion(), snmp, oid, SNMPOIDSuffixListCallback, &data, FALSE);
2796 delete snmp;
2797 if (rc == SNMP_ERR_SUCCESS)
2798 {
2799 *list = data.values;
2800 }
2801 else
2802 {
2803 delete data.values;
2804 }
2805 return (rc == SNMP_ERR_SUCCESS) ? DCE_SUCCESS : ((rc == SNMP_ERR_NO_OBJECT) ? DCE_NOT_SUPPORTED : DCE_COMM_ERROR);
2806}
2807
2808/**
6fd6de0a
VK
2809 * Get item's value via SNMP from CheckPoint's agent
2810 */
2811DWORD Node::getItemFromCheckPointSNMP(const TCHAR *szParam, DWORD dwBufSize, TCHAR *szBuffer)
5039dede
AK
2812{
2813 DWORD dwResult;
2814
2815 if ((m_dwDynamicFlags & NDF_CPSNMP_UNREACHABLE) ||
2816 (m_dwDynamicFlags & NDF_UNREACHABLE))
2817 {
2818 dwResult = SNMP_ERR_COMM;
2819 }
2820 else
2821 {
2822 SNMP_Transport *pTransport;
2823
2824 pTransport = new SNMP_UDPTransport;
c4366266 2825 ((SNMP_UDPTransport *)pTransport)->createUDPTransport(NULL, htonl(m_dwIpAddr), CHECKPOINT_SNMP_PORT);
f42b8099 2826 dwResult = SnmpGet(SNMP_VERSION_1, pTransport, szParam, NULL, 0, szBuffer, dwBufSize, SG_STRING_RESULT);
5039dede
AK
2827 delete pTransport;
2828 }
35f836fe 2829 DbgPrintf(7, _T("Node(%s)->GetItemFromCheckPointSNMP(%s): dwResult=%d"), m_szName, szParam, dwResult);
5039dede
AK
2830 return (dwResult == SNMP_ERR_SUCCESS) ? DCE_SUCCESS :
2831 ((dwResult == SNMP_ERR_NO_OBJECT) ? DCE_NOT_SUPPORTED : DCE_COMM_ERROR);
2832}
2833
6fd6de0a
VK
2834/**
2835 * Get item's value via native agent
2836 */
2837DWORD Node::getItemFromAgent(const TCHAR *szParam, DWORD dwBufSize, TCHAR *szBuffer)
5039dede 2838{
912b994d 2839 DWORD dwError = ERR_NOT_CONNECTED, dwResult = DCE_COMM_ERROR;
5039dede
AK
2840 DWORD dwTries = 3;
2841
2842 if ((m_dwDynamicFlags & NDF_AGENT_UNREACHABLE) ||
2fd7144f
VK
2843 (m_dwDynamicFlags & NDF_UNREACHABLE) ||
2844 (m_dwFlags & NF_DISABLE_NXCP) ||
2845 !(m_dwFlags & NF_IS_NATIVE_AGENT))
5039dede
AK
2846 return DCE_COMM_ERROR;
2847
7c521895 2848 agentLock();
5039dede
AK
2849
2850 // Establish connection if needed
2851 if (m_pAgentConnection == NULL)
7c521895 2852 if (!connectToAgent())
5039dede
AK
2853 goto end_loop;
2854
2855 // Get parameter from agent
2856 while(dwTries-- > 0)
2857 {
4687826e 2858 dwError = m_pAgentConnection->getParameter(szParam, dwBufSize, szBuffer);
5039dede
AK
2859 switch(dwError)
2860 {
2861 case ERR_SUCCESS:
2862 dwResult = DCE_SUCCESS;
2863 goto end_loop;
2864 case ERR_UNKNOWN_PARAMETER:
2865 dwResult = DCE_NOT_SUPPORTED;
2866 goto end_loop;
2867 case ERR_NOT_CONNECTED:
2868 case ERR_CONNECTION_BROKEN:
7c521895 2869 if (!connectToAgent())
5039dede
AK
2870 goto end_loop;
2871 break;
2872 case ERR_REQUEST_TIMEOUT:
2873 // Reset connection to agent after timeout
2874 DbgPrintf(7, _T("Node(%s)->GetItemFromAgent(%s): timeout; resetting connection to agent..."), m_szName, szParam);
2875 delete_and_null(m_pAgentConnection);
7c521895 2876 if (!connectToAgent())
5039dede
AK
2877 goto end_loop;
2878 DbgPrintf(7, _T("Node(%s)->GetItemFromAgent(%s): connection to agent restored successfully"), m_szName, szParam);
2879 break;
2880 }
2881 }
2882
2883end_loop:
7c521895 2884 agentUnlock();
adac28be
VK
2885 DbgPrintf(7, _T("Node(%s)->GetItemFromAgent(%s): dwError=%d dwResult=%d"), m_szName, szParam, dwError, dwResult);
2886 return dwResult;
2887}
2888
d51f2182
VK
2889/**
2890 * Get table from agent
2891 */
adac28be
VK
2892DWORD Node::getTableFromAgent(const TCHAR *name, Table **table)
2893{
2894 DWORD dwError = ERR_NOT_CONNECTED, dwResult = DCE_COMM_ERROR;
2895 DWORD dwTries = 3;
2896
2897 *table = NULL;
2898
2899 if ((m_dwDynamicFlags & NDF_AGENT_UNREACHABLE) ||
2900 (m_dwDynamicFlags & NDF_UNREACHABLE) ||
2901 (m_dwFlags & NF_DISABLE_NXCP) ||
2902 !(m_dwFlags & NF_IS_NATIVE_AGENT))
2903 return DCE_COMM_ERROR;
2904
2905 agentLock();
2906
2907 // Establish connection if needed
2908 if (m_pAgentConnection == NULL)
2909 if (!connectToAgent())
2910 goto end_loop;
2911
2912 // Get parameter from agent
2913 while(dwTries-- > 0)
2914 {
2915 dwError = m_pAgentConnection->getTable(name, table);
2916 switch(dwError)
2917 {
2918 case ERR_SUCCESS:
2919 dwResult = DCE_SUCCESS;
2920 goto end_loop;
2921 case ERR_UNKNOWN_PARAMETER:
2922 dwResult = DCE_NOT_SUPPORTED;
2923 goto end_loop;
2924 case ERR_NOT_CONNECTED:
2925 case ERR_CONNECTION_BROKEN:
2926 if (!connectToAgent())
2927 goto end_loop;
2928 break;
2929 case ERR_REQUEST_TIMEOUT:
2930 // Reset connection to agent after timeout
2931 DbgPrintf(7, _T("Node(%s)->getTableFromAgent(%s): timeout; resetting connection to agent..."), m_szName, name);
2932 delete_and_null(m_pAgentConnection);
2933 if (!connectToAgent())
2934 goto end_loop;
2935 DbgPrintf(7, _T("Node(%s)->getTableFromAgent(%s): connection to agent restored successfully"), m_szName, name);
2936 break;
2937 }
2938 }
2939
2940end_loop:
2941 agentUnlock();
2942 DbgPrintf(7, _T("Node(%s)->getTableFromAgent(%s): dwError=%d dwResult=%d"), m_szName, name, dwError, dwResult);
5039dede
AK
2943 return dwResult;
2944}
2945
6fd6de0a 2946/**
d51f2182
VK
2947 * Get list from agent
2948 */
2949DWORD Node::getListFromAgent(const TCHAR *name, StringList **list)
2950{
2951 DWORD dwError = ERR_NOT_CONNECTED, dwResult = DCE_COMM_ERROR;
2952 DWORD i, dwTries = 3;
2953
2954 *list = NULL;
2955
2956 if ((m_dwDynamicFlags & NDF_AGENT_UNREACHABLE) ||
2957 (m_dwDynamicFlags & NDF_UNREACHABLE) ||
2958 (m_dwFlags & NF_DISABLE_NXCP) ||
2959 !(m_dwFlags & NF_IS_NATIVE_AGENT))
2960 return DCE_COMM_ERROR;
2961
2962 agentLock();
2963
2964 // Establish connection if needed
2965 if (m_pAgentConnection == NULL)
2966 if (!connectToAgent())
2967 goto end_loop;
2968
2969 // Get parameter from agent
2970 while(dwTries-- > 0)
2971 {
2972 dwError = m_pAgentConnection->getList(name);
2973 switch(dwError)
2974 {
2975 case ERR_SUCCESS:
2976 dwResult = DCE_SUCCESS;
2977 *list = new StringList;
2978 for(i = 0; i < m_pAgentConnection->getNumDataLines(); i++)
2979 (*list)->add(m_pAgentConnection->getDataLine(i));
2980 goto end_loop;
2981 case ERR_UNKNOWN_PARAMETER:
2982 dwResult = DCE_NOT_SUPPORTED;
2983 goto end_loop;
2984 case ERR_NOT_CONNECTED:
2985 case ERR_CONNECTION_BROKEN:
2986 if (!connectToAgent())
2987 goto end_loop;
2988 break;
2989 case ERR_REQUEST_TIMEOUT:
2990 // Reset connection to agent after timeout
2991 DbgPrintf(7, _T("Node(%s)->getListFromAgent(%s): timeout; resetting connection to agent..."), m_szName, name);
2992 delete_and_null(m_pAgentConnection);
2993 if (!connectToAgent())
2994 goto end_loop;
2995 DbgPrintf(7, _T("Node(%s)->getListFromAgent(%s): connection to agent restored successfully"), m_szName, name);
2996 break;
2997 }
2998 }
2999
3000end_loop:
3001 agentUnlock();
3002 DbgPrintf(7, _T("Node(%s)->getListFromAgent(%s): dwError=%d dwResult=%d"), m_szName, name, dwError, dwResult);
3003 return dwResult;
3004}
3005
3006/**
6fd6de0a
VK
3007 * Get value for server's internal parameter
3008 */
3009DWORD Node::getInternalItem(const TCHAR *param, DWORD bufSize, TCHAR *buffer)
5039dede 3010{
6fd6de0a 3011 DWORD rc = DataCollectionTarget::getInternalItem(param, bufSize, buffer);
90a4971c
VK
3012 if (rc == DCE_SUCCESS)
3013 return DCE_SUCCESS;
3014 rc = DCE_SUCCESS;
5039dede 3015
eb0c0312 3016 if (MatchString(_T("Net.IP.NextHop(*)"), param, FALSE))
20c842ee
VK
3017 {
3018 if ((m_dwFlags & NF_IS_NATIVE_AGENT) || (m_dwFlags & NF_IS_SNMP))
3019 {
3020 TCHAR arg[256] = _T("");
3021 DWORD destAddr, nextHop, ifIndex;
3022 BOOL isVpn;
3023
6fd6de0a 3024 AgentGetParameterArg(param, 1, arg, 256);
20c842ee
VK
3025 destAddr = ntohl(_t_inet_addr(arg));
3026 if ((destAddr > 0) && (destAddr < 0xE0000000))
3027 {
3028 if (getNextHop(m_dwIpAddr, destAddr, &nextHop, &ifIndex, &isVpn))
3029 {
6fd6de0a 3030 IpToStr(nextHop, buffer);
20c842ee
VK
3031 }
3032 else
3033 {
6fd6de0a 3034 _tcscpy(buffer, _T("UNREACHABLE"));
20c842ee
VK
3035 }
3036 }
3037 else
3038 {
6fd6de0a 3039 rc = DCE_NOT_SUPPORTED;
20c842ee
VK
3040 }
3041 }
3042 else
3043 {
6fd6de0a 3044 rc = DCE_NOT_SUPPORTED;
20c842ee
VK
3045 }
3046 }
6fd6de0a 3047 else if (!_tcsicmp(param, _T("AgentStatus")))
5039dede
AK
3048 {
3049 if (m_dwFlags & NF_IS_NATIVE_AGENT)
3050 {
6fd6de0a
VK
3051 buffer[0] = (m_dwDynamicFlags & NDF_AGENT_UNREACHABLE) ? _T('1') : _T('0');
3052 buffer[1] = 0;
5039dede
AK
3053 }
3054 else
3055 {
6fd6de0a 3056 rc = DCE_NOT_SUPPORTED;
5039dede
AK
3057 }
3058 }
3059 else if (m_dwFlags & NF_IS_LOCAL_MGMT)
3060 {
6fd6de0a 3061 if (!_tcsicmp(param, _T("Server.AverageDCPollerQueueSize")))
5039dede 3062 {
6fd6de0a 3063 _sntprintf(buffer, bufSize, _T("%f"), g_dAvgPollerQueueSize);
5039dede 3064 }
6fd6de0a 3065 else if (!_tcsicmp(param, _T("Server.AverageDBWriterQueueSize")))
5039dede 3066 {
6fd6de0a 3067 _sntprintf(buffer, bufSize, _T("%f"), g_dAvgDBAndIDataWriterQueueSize);
cf084617 3068 }
6fd6de0a 3069 else if (!_tcsicmp(param, _T("Server.AverageDBWriterQueueSize.Other")))
cf084617 3070 {
6fd6de0a 3071 _sntprintf(buffer, bufSize, _T("%f"), g_dAvgDBWriterQueueSize);
5039dede 3072 }
6fd6de0a 3073 else if (!_tcsicmp(param, _T("Server.AverageDBWriterQueueSize.IData")))
cf084617 3074 {
6fd6de0a 3075 _sntprintf(buffer, bufSize, _T("%f"), g_dAvgIDataWriterQueueSize);
cf084617 3076 }
6fd6de0a 3077 else if (!_tcsicmp(param, _T("Server.AverageStatusPollerQueueSize")))
5039dede 3078 {
6fd6de0a 3079 _sntprintf(buffer, bufSize, _T("%f"), g_dAvgStatusPollerQueueSize);
5039dede 3080 }
6fd6de0a 3081 else if (!_tcsicmp(param, _T("Server.AverageConfigurationPollerQueueSize")))
5039dede 3082 {
6fd6de0a 3083 _sntprintf(buffer, bufSize, _T("%f"), g_dAvgConfigPollerQueueSize);
5039dede 3084 }
6fd6de0a 3085 else if (!_tcsicmp(param, _T("Server.AverageDCIQueuingTime")))
5039dede 3086 {
6fd6de0a 3087 _sntprintf(buffer, bufSize, _T("%u"), g_dwAvgDCIQueuingTime);
5039dede 3088 }
6fd6de0a 3089 else if (!_tcsicmp(param, _T("Server.TotalEventsProcessed")))
5039dede 3090 {
6fd6de0a 3091 _sntprintf(buffer, bufSize, INT64_FMT, g_totalEventsProcessed);
5039dede
AK
3092 }
3093 else
3094 {
6fd6de0a 3095 rc = DCE_NOT_SUPPORTED;
5039dede
AK
3096 }
3097 }
3098 else
3099 {
6fd6de0a 3100 rc = DCE_NOT_SUPPORTED;
5039dede
AK
3101 }
3102
6fd6de0a 3103 return rc;
5039dede
AK
3104}
3105
6fd6de0a
VK
3106/**
3107 * Translate DCI error code into RCC
3108 */
adac28be
VK
3109static DWORD RCCFromDCIError(DWORD error)
3110{
3188558a 3111 switch(error)
adac28be
VK
3112 {
3113 case DCE_SUCCESS:
3114 return RCC_SUCCESS;
3115 case DCE_COMM_ERROR:
3116 return RCC_COMM_FAILURE;
3117 case DCE_NOT_SUPPORTED:
3118 return RCC_DCI_NOT_SUPPORTED;
3119 default:
3120 return RCC_SYSTEM_FAILURE;
3121 }
3122}
3123
6fd6de0a
VK
3124/**
3125 * Get item's value for client
3126 */
adac28be 3127DWORD Node::getItemForClient(int iOrigin, const TCHAR *pszParam, TCHAR *pszBuffer, DWORD dwBufSize)
5039dede
AK
3128{
3129 DWORD dwResult = 0, dwRetCode;
3130
3131 // Get data from node
3132 switch(iOrigin)
3133 {
3134 case DS_INTERNAL:
6fd6de0a 3135 dwRetCode = getInternalItem(pszParam, dwBufSize, pszBuffer);
5039dede
AK
3136 break;
3137 case DS_NATIVE_AGENT:
6fd6de0a 3138 dwRetCode = getItemFromAgent(pszParam, dwBufSize, pszBuffer);
5039dede
AK
3139 break;
3140 case DS_SNMP_AGENT:
e320f8ce 3141 dwRetCode = getItemFromSNMP(0, pszParam, dwBufSize, pszBuffer, SNMP_RAWTYPE_NONE);
5039dede
AK
3142 break;
3143 case DS_CHECKPOINT_AGENT:
6fd6de0a 3144 dwRetCode = getItemFromCheckPointSNMP(pszParam, dwBufSize, pszBuffer);
5039dede
AK
3145 break;
3146 default:
3147 dwResult = RCC_INVALID_ARGUMENT;
3148 break;
3149 }
3150
3151 // Translate return code to RCC
3152 if (dwResult != RCC_INVALID_ARGUMENT)
adac28be 3153 dwResult = RCCFromDCIError(dwRetCode);
5039dede
AK
3154
3155 return dwResult;
3156}
3157
d51f2182
VK
3158/**
3159 * Get table for client
3160 */
3188558a 3161DWORD Node::getTableForClient(const TCHAR *name, Table **table)
adac28be 3162{
3188558a 3163 DWORD dwRetCode = getTableFromAgent(name, table);
adac28be
VK
3164 return RCCFromDCIError(dwRetCode);
3165}
3166
6fd6de0a
VK
3167/**
3168 * Create CSCP message with object's data
3169 */
5039dede
AK
3170void Node::CreateMessage(CSCPMessage *pMsg)
3171{
6fd6de0a 3172 DataCollectionTarget::CreateMessage(pMsg);
5a7d6a10 3173 pMsg->SetVariable(VID_PRIMARY_NAME, m_primaryName);
5039dede 3174 pMsg->SetVariable(VID_FLAGS, m_dwFlags);
fa6173b9 3175 pMsg->SetVariable(VID_RUNTIME_FLAGS, m_dwDynamicFlags);
5039dede
AK
3176 pMsg->SetVariable(VID_AGENT_PORT, m_wAgentPort);
3177 pMsg->SetVariable(VID_AUTH_METHOD, m_wAuthMethod);
3178 pMsg->SetVariable(VID_SHARED_SECRET, m_szSharedSecret);
35f836fe
VK
3179 pMsg->SetVariableFromMBString(VID_SNMP_AUTH_OBJECT, m_snmpSecurity->getCommunity());
3180 pMsg->SetVariableFromMBString(VID_SNMP_AUTH_PASSWORD, m_snmpSecurity->getAuthPassword());
3181 pMsg->SetVariableFromMBString(VID_SNMP_PRIV_PASSWORD, m_snmpSecurity->getPrivPassword());
4b7d8903 3182 pMsg->SetVariable(VID_SNMP_USM_METHODS, (WORD)((WORD)m_snmpSecurity->getAuthMethod() | ((WORD)m_snmpSecurity->getPrivMethod() << 8)));
5039dede 3183 pMsg->SetVariable(VID_SNMP_OID, m_szObjectId);
65e2005b 3184 pMsg->SetVariable(VID_SNMP_PORT, m_wSNMPPort);
5d2c5741 3185 pMsg->SetVariable(VID_SNMP_VERSION, (WORD)m_snmpVersion);
5039dede
AK
3186 pMsg->SetVariable(VID_AGENT_VERSION, m_szAgentVersion);
3187 pMsg->SetVariable(VID_PLATFORM_NAME, m_szPlatformName);
3188 pMsg->SetVariable(VID_POLLER_NODE_ID, m_dwPollerNode);
52c31148 3189 pMsg->SetVariable(VID_ZONE_ID, m_zoneId);
e41fced3 3190 pMsg->SetVariable(VID_AGENT_PROXY, m_dwProxyNode);
5039dede
AK
3191 pMsg->SetVariable(VID_SNMP_PROXY, m_dwSNMPProxy);
3192 pMsg->SetVariable(VID_REQUIRED_POLLS, (WORD)m_iRequiredPollCount);
0ecc2200
VK
3193 pMsg->SetVariable(VID_SYS_NAME, CHECK_NULL_EX(m_sysName));
3194 pMsg->SetVariable(VID_SYS_DESCRIPTION, CHECK_NULL_EX(m_sysDescription));
3eab63f2 3195 pMsg->SetVariable(VID_BRIDGE_BASE_ADDRESS, m_baseBridgeAddress, 6);
0ecc2200
VK
3196 if (m_lldpNodeId != NULL)
3197 pMsg->SetVariable(VID_LLDP_NODE_ID, m_lldpNodeId);
5039dede 3198 pMsg->SetVariable(VID_USE_IFXTABLE, (WORD)m_nUseIfXTable);
0d75ea88
VK
3199 if (m_vrrpInfo != NULL)
3200 {
3201 pMsg->SetVariable(VID_VRRP_VERSION, (WORD)m_vrrpInfo->getVersion());
3202 pMsg->SetVariable(VID_VRRP_VR_COUNT, (WORD)m_vrrpInfo->getSize());
3203 }
9796ce45
VK
3204 if (m_driver != NULL)
3205 {
3206 pMsg->SetVariable(VID_DRIVER_NAME, m_driver->getName());
3207 pMsg->SetVariable(VID_DRIVER_VERSION, m_driver->getVersion());
3208 }
5039dede
AK
3209}
3210
a3050773
VK
3211/**
3212 * Modify object from NXCP message
3213 */
5039dede
AK
3214DWORD Node::ModifyFromMessage(CSCPMessage *pRequest, BOOL bAlreadyLocked)
3215{
3216 if (!bAlreadyLocked)
3217 LockData();
3218
3219 // Change primary IP address
3220 if (pRequest->IsVariableExist(VID_IP_ADDRESS))
3221 {
3222 DWORD i, dwIpAddr;
3223
3224 dwIpAddr = pRequest->GetVariableLong(VID_IP_ADDRESS);
3225
3226 // Check if received IP address is one of node's interface addresses
3227 LockChildList(FALSE);
3228 for(i = 0; i < m_dwChildCount; i++)
3229 if ((m_pChildList[i]->Type() == OBJECT_INTERFACE) &&
3230 (m_pChildList[i]->IpAddr() == dwIpAddr))
3231 break;
3232 UnlockChildList();
3233 if (i == m_dwChildCount)
3234 {
3235 UnlockData();
3236 return RCC_INVALID_IP_ADDR;
3237 }
3238
2467ed57 3239 setPrimaryIPAddress(dwIpAddr);
5a7d6a10
VK
3240
3241 // Update primary name if it is not set with the same message
3242 if (!pRequest->IsVariableExist(VID_PRIMARY_NAME))
3243 {
3244 IpToStr(m_dwIpAddr, m_primaryName);
3245 }
a3050773
VK
3246
3247 agentLock();
3248 delete_and_null(m_pAgentConnection);
3249 agentUnlock();
3250 }
5039dede 3251
fed33789 3252 // Change primary host name
5a7d6a10
VK
3253 if (pRequest->IsVariableExist(VID_PRIMARY_NAME))
3254 {
3255 pRequest->GetVariableStr(VID_PRIMARY_NAME, m_primaryName, MAX_DNS_NAME);
3256 m_dwDynamicFlags |= NDF_FORCE_CONFIGURATION_POLL | NDF_RECHECK_CAPABILITIES;
3257 }
3258
5039dede
AK
3259 // Poller node ID
3260 if (pRequest->IsVariableExist(VID_POLLER_NODE_ID))
3261 {
3262 DWORD dwNodeId;
3263 NetObj *pObject;
3264
3265 dwNodeId = pRequest->GetVariableLong(VID_POLLER_NODE_ID);
41f9a6dc
VK
3266 if (dwNodeId != 0)
3267 {
3268 pObject = FindObjectById(dwNodeId);
5039dede 3269
41f9a6dc
VK
3270 // Check if received id is a valid node id
3271 if (pObject == NULL)
3272 {
3273 UnlockData();
3274 return RCC_INVALID_OBJECT_ID;
3275 }
3276 if (pObject->Type() != OBJECT_NODE)
3277 {
3278 UnlockData();
3279 return RCC_INVALID_OBJECT_ID;
3280 }
3281 }
3282 m_dwPollerNode = dwNodeId;
5039dede
AK
3283 }
3284
3285 // Change listen port of native agent
3286 if (pRequest->IsVariableExist(VID_AGENT_PORT))
3287 m_wAgentPort = pRequest->GetVariableShort(VID_AGENT_PORT);
3288
3289 // Change authentication method of native agent
3290 if (pRequest->IsVariableExist(VID_AUTH_METHOD))
3291 m_wAuthMethod = pRequest->GetVariableShort(VID_AUTH_METHOD);
3292
3293 // Change shared secret of native agent
3294 if (pRequest->IsVariableExist(VID_SHARED_SECRET))
3295 pRequest->GetVariableStr(VID_SHARED_SECRET, m_szSharedSecret, MAX_SECRET_LENGTH);
3296
3297 // Change SNMP protocol version
3298 if (pRequest->IsVariableExist(VID_SNMP_VERSION))
14b98b3a 3299 {
5d2c5741 3300 m_snmpVersion = pRequest->GetVariableShort(VID_SNMP_VERSION);
14b98b3a
VK
3301 m_snmpSecurity->setSecurityModel((m_snmpVersion == SNMP_VERSION_3) ? SNMP_SECURITY_MODEL_USM : SNMP_SECURITY_MODEL_V2C);
3302 }
5039dede 3303
65e2005b
VK
3304 // Change SNMP port
3305 if (pRequest->IsVariableExist(VID_SNMP_PORT))
3306 m_wSNMPPort = pRequest->GetVariableShort(VID_SNMP_PORT);
3307
4b7d8903
VK
3308 // Change SNMP authentication data
3309 if (pRequest->IsVariableExist(VID_SNMP_AUTH_OBJECT))
5d2c5741 3310 {
35f836fe
VK
3311 char mbBuffer[256];
3312
3313 pRequest->GetVariableStrA(VID_SNMP_AUTH_OBJECT, mbBuffer, 256);
3314 m_snmpSecurity->setAuthName(mbBuffer);
4b7d8903 3315
35f836fe
VK
3316 pRequest->GetVariableStrA(VID_SNMP_AUTH_PASSWORD, mbBuffer, 256);
3317 m_snmpSecurity->setAuthPassword(mbBuffer);
4b7d8903 3318
35f836fe
VK
3319 pRequest->GetVariableStrA(VID_SNMP_PRIV_PASSWORD, mbBuffer, 256);
3320 m_snmpSecurity->setPrivPassword(mbBuffer);
4b7d8903
VK
3321
3322 WORD methods = pRequest->GetVariableShort(VID_SNMP_USM_METHODS);
3323 m_snmpSecurity->setAuthMethod((int)(methods & 0xFF));
3324 m_snmpSecurity->setPrivMethod((int)(methods >> 8));
5d2c5741 3325 }
5039dede
AK
3326
3327 // Change proxy node
e41fced3
VK
3328 if (pRequest->IsVariableExist(VID_AGENT_PROXY))
3329 m_dwProxyNode = pRequest->GetVariableLong(VID_AGENT_PROXY);
5039dede
AK
3330
3331 // Change SNMP proxy node
3332 if (pRequest->IsVariableExist(VID_SNMP_PROXY))
3333 m_dwSNMPProxy = pRequest->GetVariableLong(VID_SNMP_PROXY);
3334
3335 // Number of required polls
3336 if (pRequest->IsVariableExist(VID_REQUIRED_POLLS))
3337 m_iRequiredPollCount = (int)pRequest->GetVariableShort(VID_REQUIRED_POLLS);
3338
3339 // Enable/disable usage of ifXTable
3340 if (pRequest->IsVariableExist(VID_USE_IFXTABLE))
3341 m_nUseIfXTable = (BYTE)pRequest->GetVariableShort(VID_USE_IFXTABLE);
3342
3343 // Change flags
3344 if (pRequest->IsVariableExist(VID_FLAGS))
3345 {
3346 m_dwFlags &= NF_SYSTEM_FLAGS;
3347 m_dwFlags |= pRequest->GetVariableLong(VID_FLAGS) & NF_USER_FLAGS;
3348 }
3349
6fd6de0a 3350 return DataCollectionTarget::ModifyFromMessage(pRequest, TRUE);
5039dede
AK
3351}
3352
6fd6de0a
VK
3353/**
3354 * Wakeup node using magic packet
3355 */
7c521895 3356DWORD Node::wakeUp()
5039dede
AK
3357{
3358 DWORD i, dwResult = RCC_NO_WOL_INTERFACES;
3359
3360 LockChildList(FALSE);
3361
3362 for(i = 0; i < m_dwChildCount; i++)
3363 if ((m_pChildList[i]->Type() == OBJECT_INTERFACE) &&
3364 (m_pChildList[i]->Status() != STATUS_UNMANAGED) &&
3365 (m_pChildList[i]->IpAddr() != 0))
3366 {
7c521895 3367 dwResult = ((Interface *)m_pChildList[i])->wakeUp();
5039dede
AK
3368 break;
3369 }
3370
3371 UnlockChildList();
3372 return dwResult;
3373}
3374
6fd6de0a
VK
3375/**
3376 * Get status of interface with given index from SNMP agent
3377 */
1e6b68a6 3378void Node::getInterfaceStatusFromSNMP(SNMP_Transport *pTransport, DWORD dwIndex, int *adminState, int *operState)
5039dede 3379{
1e6b68a6 3380 SnmpGetInterfaceStatus(m_snmpVersion, pTransport, dwIndex, adminState, operState);
5039dede
AK
3381}
3382
6fd6de0a
VK
3383/**
3384 * Get status of interface with given index from native agent
3385 */
1e6b68a6 3386void Node::getInterfaceStatusFromAgent(DWORD dwIndex, int *adminState, int *operState)
5039dede 3387{
35f836fe 3388 TCHAR szParam[128], szBuffer[32];
5039dede
AK
3389
3390 // Get administrative status
35f836fe 3391 _sntprintf(szParam, 128, _T("Net.Interface.AdminStatus(%u)"), dwIndex);
6fd6de0a 3392 if (getItemFromAgent(szParam, 32, szBuffer) == DCE_SUCCESS)
5039dede 3393 {
1e6b68a6 3394 *adminState = _tcstol(szBuffer, NULL, 0);
5039dede 3395
1e6b68a6 3396 switch(*adminState)
5039dede 3397 {
1e6b68a6
VK
3398 case IF_ADMIN_STATE_TESTING:
3399 *operState = IF_OPER_STATE_UNKNOWN;
5039dede 3400 break;
1e6b68a6 3401 case IF_ADMIN_STATE_DOWN:
5039dede 3402 case 0: // Agents before 0.2.1 may return 0 instead of 2
1e6b68a6 3403 *operState = IF_OPER_STATE_DOWN;
5039dede 3404 break;
1e6b68a6 3405 case IF_ADMIN_STATE_UP: // Interface administratively up, check link state
35f836fe 3406 _sntprintf(szParam, 128, _T("Net.Interface.Link(%u)"), dwIndex);
6fd6de0a 3407 if (getItemFromAgent(szParam, 32, szBuffer) == DCE_SUCCESS)
5039dede 3408 {
1e6b68a6
VK
3409 DWORD dwLinkState = _tcstoul(szBuffer, NULL, 0);
3410 *operState = (dwLinkState == 0) ? IF_OPER_STATE_DOWN : IF_OPER_STATE_UP;
5039dede
AK