set SQLite page size on export to 64K to allow maximum possible export file size...
[public/netxms.git] / src / server / core / node.cpp
CommitLineData
5039dede
AK
1/*
2** NetXMS - Network Management System
33ae8896 3** Copyright (C) 2003-2012 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
25
26//
46117060
VK
27// Externals
28//
29
30extern Queue g_statusPollQueue;
31extern Queue g_configPollQueue;
32extern Queue g_topologyPollQueue;
33extern Queue g_routePollQueue;
34extern Queue g_discoveryPollQueue;
35
c50f77f7
VK
36/**
37 * Node class default constructor
38 */
6fd6de0a 39Node::Node() : DataCollectionTarget()
5039dede 40{
5ad2167d 41 m_primaryName[0] = 0;
a65c1819 42 m_iStatus = STATUS_UNKNOWN;
5039dede
AK
43 m_dwFlags = 0;
44 m_dwDynamicFlags = 0;
52c31148 45 m_zoneId = 0;
5039dede
AK
46 m_wAgentPort = AGENT_LISTEN_PORT;
47 m_wAuthMethod = AUTH_NONE;
48 m_szSharedSecret[0] = 0;
49 m_iStatusPollType = POLL_ICMP_PING;
5d2c5741 50 m_snmpVersion = SNMP_VERSION_1;
5039dede 51 m_wSNMPPort = SNMP_DEFAULT_PORT;
35f836fe
VK
52 char community[MAX_COMMUNITY_LENGTH];
53 ConfigReadStrA(_T("DefaultCommunityString"), community, MAX_COMMUNITY_LENGTH, "public");
5d2c5741 54 m_snmpSecurity = new SNMP_SecurityContext(community);
5039dede
AK
55 m_szObjectId[0] = 0;
56 m_tLastDiscoveryPoll = 0;
57 m_tLastStatusPoll = 0;
58 m_tLastConfigurationPoll = 0;
b6ead716 59 m_tLastTopologyPoll = 0;
5039dede 60 m_tLastRTUpdate = 0;
dd42ad0a 61 m_tDownSince = 0;
5039dede
AK
62 m_hPollerMutex = MutexCreate();
63 m_hAgentAccessMutex = MutexCreate();
64 m_mutexRTAccess = MutexCreate();
65 m_mutexTopoAccess = MutexCreate();
66 m_pAgentConnection = NULL;
fed33789 67 m_lastAgentTrapId = 0;
5039dede
AK
68 m_szAgentVersion[0] = 0;
69 m_szPlatformName[0] = 0;
0ecc2200
VK
70 m_sysDescription = NULL;
71 m_sysName = NULL;
72 m_lldpNodeId = NULL;
3a82d5ae 73 m_lldpLocalPortInfo = NULL;
cc8ce218
VK
74 m_paramList = NULL;
75 m_tableList = NULL;
5039dede
AK
76 m_dwPollerNode = 0;
77 m_dwProxyNode = 0;
78 m_dwSNMPProxy = 0;
79 memset(m_qwLastEvents, 0, sizeof(QWORD) * MAX_LAST_EVENTS);
80 m_pRoutingTable = NULL;
81 m_tFailTimeSNMP = 0;
82 m_tFailTimeAgent = 0;
040c45fa 83 m_linkLayerNeighbors = NULL;
0d75ea88 84 m_vrrpInfo = NULL;
5039dede 85 m_tLastTopologyPoll = 0;
040c45fa
VK
86 m_pTopology = NULL;
87 m_topologyRebuildTimestamp = 0;
5039dede
AK
88 m_iPendingStatus = -1;
89 m_iPollCount = 0;
90 m_iRequiredPollCount = 0; // Use system default
91 m_nUseIfXTable = IFXTABLE_DEFAULT; // Use system default
ab621f39 92 m_jobQueue = new ServerJobQueue();
f42b8099 93 m_fdb = NULL;
7f632dfe 94 m_vlans = NULL;
9796ce45 95 m_driver = NULL;
ae32341d 96 m_driverData = NULL;
50d0de67 97 m_components = NULL;
3eab63f2 98 memset(m_baseBridgeAddress, 0, MAC_ADDR_LENGTH);
5039dede
AK
99}
100
c50f77f7
VK
101/**
102 * Constructor for new node object
103 */
6fd6de0a 104Node::Node(DWORD dwAddr, DWORD dwFlags, DWORD dwProxyNode, DWORD dwSNMPProxy, DWORD dwZone) : DataCollectionTarget()
5039dede 105{
5ad2167d 106 IpToStr(dwAddr, m_primaryName);
a65c1819 107 m_iStatus = STATUS_UNKNOWN;
5039dede
AK
108 m_dwIpAddr = dwAddr;
109 m_dwFlags = dwFlags;
110 m_dwDynamicFlags = 0;
52c31148 111 m_zoneId = dwZone;
5039dede
AK
112 m_wAgentPort = AGENT_LISTEN_PORT;
113 m_wAuthMethod = AUTH_NONE;
114 m_szSharedSecret[0] = 0;
115 m_iStatusPollType = POLL_ICMP_PING;
5d2c5741 116 m_snmpVersion = SNMP_VERSION_1;
5039dede 117 m_wSNMPPort = SNMP_DEFAULT_PORT;
35f836fe
VK
118 char community[MAX_COMMUNITY_LENGTH];
119 ConfigReadStrA(_T("DefaultCommunityString"), community, MAX_COMMUNITY_LENGTH, "public");
5d2c5741 120 m_snmpSecurity = new SNMP_SecurityContext(community);
5039dede
AK
121 IpToStr(dwAddr, m_szName); // Make default name from IP address
122 m_szObjectId[0] = 0;
123 m_tLastDiscoveryPoll = 0;
124 m_tLastStatusPoll = 0;
125 m_tLastConfigurationPoll = 0;
b6ead716 126 m_tLastTopologyPoll = 0;
5039dede 127 m_tLastRTUpdate = 0;
dd42ad0a 128 m_tDownSince = 0;
5039dede
AK
129 m_hPollerMutex = MutexCreate();
130 m_hAgentAccessMutex = MutexCreate();
131 m_mutexRTAccess = MutexCreate();
132 m_mutexTopoAccess = MutexCreate();
133 m_pAgentConnection = NULL;
fed33789 134 m_lastAgentTrapId = 0;
5039dede
AK
135 m_szAgentVersion[0] = 0;
136 m_szPlatformName[0] = 0;
0ecc2200
VK
137 m_sysDescription = NULL;
138 m_sysName = NULL;
139 m_lldpNodeId = NULL;
3a82d5ae 140 m_lldpLocalPortInfo = NULL;
cc8ce218
VK
141 m_paramList = NULL;
142 m_tableList = NULL;
5039dede
AK
143 m_dwPollerNode = 0;
144 m_dwProxyNode = dwProxyNode;
145 m_dwSNMPProxy = dwSNMPProxy;
146 memset(m_qwLastEvents, 0, sizeof(QWORD) * MAX_LAST_EVENTS);
147 m_bIsHidden = TRUE;
148 m_pRoutingTable = NULL;
149 m_tFailTimeSNMP = 0;
150 m_tFailTimeAgent = 0;
040c45fa 151 m_linkLayerNeighbors = NULL;
0d75ea88 152 m_vrrpInfo = NULL;
5039dede 153 m_tLastTopologyPoll = 0;
040c45fa
VK
154 m_pTopology = NULL;
155 m_topologyRebuildTimestamp = 0;
5039dede
AK
156 m_iPendingStatus = -1;
157 m_iPollCount = 0;
158 m_iRequiredPollCount = 0; // Use system default
159 m_nUseIfXTable = IFXTABLE_DEFAULT; // Use system default
ab621f39 160 m_jobQueue = new ServerJobQueue();
f42b8099 161 m_fdb = NULL;
7f632dfe 162 m_vlans = NULL;
9796ce45 163 m_driver = NULL;
ae32341d 164 m_driverData = NULL;
50d0de67 165 m_components = NULL;
3eab63f2 166 memset(m_baseBridgeAddress, 0, MAC_ADDR_LENGTH);
5039dede
AK
167}
168
c50f77f7
VK
169/**
170 * Node destructor
171 */
5039dede
AK
172Node::~Node()
173{
ae32341d
VK
174 if ((m_driver != NULL) && (m_driverData != NULL))
175 m_driver->destroyDriverData(m_driverData);
5039dede
AK
176 MutexDestroy(m_hPollerMutex);
177 MutexDestroy(m_hAgentAccessMutex);
178 MutexDestroy(m_mutexRTAccess);
179 MutexDestroy(m_mutexTopoAccess);
180 delete m_pAgentConnection;
cc8ce218
VK
181 delete m_paramList;
182 delete m_tableList;
bb151059 183 safe_free(m_sysDescription);
5039dede 184 DestroyRoutingTable(m_pRoutingTable);
a9ad9f61
VK
185 if (m_linkLayerNeighbors != NULL)
186 m_linkLayerNeighbors->decRefCount();
0d75ea88 187 delete m_vrrpInfo;
5039dede 188 delete m_pTopology;
ab621f39 189 delete m_jobQueue;
5d2c5741 190 delete m_snmpSecurity;
f42b8099
VK
191 if (m_fdb != NULL)
192 m_fdb->decRefCount();
7f632dfe
VK
193 if (m_vlans != NULL)
194 m_vlans->decRefCount();
50d0de67 195 delete m_components;
3a82d5ae 196 delete m_lldpLocalPortInfo;
5039dede
AK
197}
198
c50f77f7
VK
199/**
200 * Create object from database data
201 */
5039dede
AK
202BOOL Node::CreateFromDB(DWORD dwId)
203{
5039dede
AK
204 int i, iNumRows;
205 DWORD dwSubnetId;
206 NetObj *pObject;
207 BOOL bResult = FALSE;
208
209 m_dwId = dwId;
210
89135050 211 if (!loadCommonProperties())
5039dede 212 {
35f836fe 213 DbgPrintf(2, _T("Cannot load common properties for node object %d"), dwId);
5039dede
AK
214 return FALSE;
215 }
216
4866d57b
VK
217 DB_STATEMENT hStmt = DBPrepare(g_hCoreDB,
218 _T("SELECT primary_name,primary_ip,node_flags,")
219 _T("snmp_version,auth_method,secret,")
220 _T("agent_port,status_poll_type,snmp_oid,agent_version,")
221 _T("platform_name,poller_node_id,zone_guid,")
222 _T("proxy_node,snmp_proxy,required_polls,uname,")
223 _T("use_ifxtable,snmp_port,community,usm_auth_password,")
224 _T("usm_priv_password,usm_methods,snmp_sys_name,bridge_base_addr,")
dd42ad0a 225 _T("runtime_flags,down_since FROM nodes WHERE id=?"));
4866d57b
VK
226 if (hStmt == NULL)
227 return FALSE;
228
229 DBBind(hStmt, 1, DB_SQLTYPE_INTEGER, dwId);
92c51b1d 230 DB_RESULT hResult = DBSelectPrepared(hStmt);
e2babedf 231 if (hResult == NULL)
b1789b80
VK
232 {
233 DBFreeStatement(hStmt);
5039dede 234 return FALSE; // Query failed
b1789b80 235 }
5039dede
AK
236
237 if (DBGetNumRows(hResult) == 0)
238 {
239 DBFreeResult(hResult);
b1789b80 240 DBFreeStatement(hStmt);
5d2c5741 241 DbgPrintf(2, _T("Missing record in \"nodes\" table for node object %d"), dwId);
5039dede
AK
242 return FALSE;
243 }
244
5ad2167d
VK
245 DBGetField(hResult, 0, 0, m_primaryName, MAX_DNS_NAME);
246 m_dwIpAddr = DBGetFieldIPAddr(hResult, 0, 1);
247 m_dwFlags = DBGetFieldULong(hResult, 0, 2);
248 m_snmpVersion = DBGetFieldLong(hResult, 0, 3);
249 m_wAuthMethod = (WORD)DBGetFieldLong(hResult, 0, 4);
250 DBGetField(hResult, 0, 5, m_szSharedSecret, MAX_SECRET_LENGTH);
251 m_wAgentPort = (WORD)DBGetFieldLong(hResult, 0, 6);
252 m_iStatusPollType = DBGetFieldLong(hResult, 0, 7);
253 DBGetField(hResult, 0, 8, m_szObjectId, MAX_OID_LEN * 4);
5d2c5741 254 DBGetField(hResult, 0, 9, m_szAgentVersion, MAX_AGENT_VERSION_LEN);
5d2c5741 255 DBGetField(hResult, 0, 10, m_szPlatformName, MAX_PLATFORM_NAME_LEN);
5d2c5741 256 m_dwPollerNode = DBGetFieldULong(hResult, 0, 11);
52c31148 257 m_zoneId = DBGetFieldULong(hResult, 0, 12);
5d2c5741
VK
258 m_dwProxyNode = DBGetFieldULong(hResult, 0, 13);
259 m_dwSNMPProxy = DBGetFieldULong(hResult, 0, 14);
260 m_iRequiredPollCount = DBGetFieldLong(hResult, 0, 15);
0ecc2200 261 m_sysDescription = DBGetField(hResult, 0, 16, NULL, 0);
5d2c5741 262 m_nUseIfXTable = (BYTE)DBGetFieldLong(hResult, 0, 17);
65e2005b 263 m_wSNMPPort = (WORD)DBGetFieldLong(hResult, 0, 18);
5039dede 264
5d2c5741 265 // SNMP authentication parameters
35f836fe
VK
266 char snmpAuthObject[256], snmpAuthPassword[256], snmpPrivPassword[256];
267 DBGetFieldA(hResult, 0, 19, snmpAuthObject, 256);
268 DBGetFieldA(hResult, 0, 20, snmpAuthPassword, 256);
269 DBGetFieldA(hResult, 0, 21, snmpPrivPassword, 256);
3408b115 270 int snmpMethods = DBGetFieldLong(hResult, 0, 22);
5d2c5741
VK
271 delete m_snmpSecurity;
272 m_snmpSecurity = new SNMP_SecurityContext(snmpAuthObject, snmpAuthPassword, snmpPrivPassword, snmpMethods & 0xFF, snmpMethods >> 8);
14b98b3a 273 m_snmpSecurity->setSecurityModel((m_snmpVersion == SNMP_VERSION_3) ? SNMP_SECURITY_MODEL_USM : SNMP_SECURITY_MODEL_V2C);
5d2c5741 274
88ac8e88 275 m_sysName = DBGetField(hResult, 0, 23, NULL, 0);
d74c80ea 276
3eab63f2
VK
277 TCHAR baseAddr[16];
278 TCHAR *value = DBGetField(hResult, 0, 24, baseAddr, 16);
279 if (value != NULL)
280 StrToBin(value, m_baseBridgeAddress, MAC_ADDR_LENGTH);
281
4866d57b 282 m_dwDynamicFlags = DBGetFieldULong(hResult, 0, 25);
b6ead716 283 m_dwDynamicFlags &= NDF_PERSISTENT; // Clear out all non-persistent runtime flags
4866d57b 284
dd42ad0a
AK
285 m_tDownSince = DBGetFieldLong(hResult, 0, 26);
286
5039dede 287 DBFreeResult(hResult);
b1789b80 288 DBFreeStatement(hStmt);
5039dede
AK
289
290 if (!m_bIsDeleted)
291 {
292 // Link node to subnets
92c51b1d
VK
293 hStmt = DBPrepare(g_hCoreDB, _T("SELECT subnet_id FROM nsmap WHERE node_id=?"));
294 if (hStmt == NULL)
295 return FALSE;
296
297 DBBind(hStmt, 1, DB_SQLTYPE_INTEGER, m_dwId);
298 hResult = DBSelectPrepared(hStmt);
5039dede 299 if (hResult == NULL)
92c51b1d
VK
300 {
301 DBFreeStatement(hStmt);
5039dede 302 return FALSE; // Query failed
92c51b1d 303 }
5039dede
AK
304
305 iNumRows = DBGetNumRows(hResult);
5039dede
AK
306 for(i = 0; i < iNumRows; i++)
307 {
308 dwSubnetId = DBGetFieldULong(hResult, i, 0);
309 pObject = FindObjectById(dwSubnetId);
310 if (pObject == NULL)
311 {
312 nxlog_write(MSG_INVALID_SUBNET_ID, EVENTLOG_ERROR_TYPE, "dd", dwId, dwSubnetId);
313 break;
314 }
315 else if (pObject->Type() != OBJECT_SUBNET)
316 {
317 nxlog_write(MSG_SUBNET_NOT_SUBNET, EVENTLOG_ERROR_TYPE, "dd", dwId, dwSubnetId);
318 break;
319 }
320 else
321 {
322 pObject->AddChild(this);
323 AddParent(pObject);
5039dede
AK
324 }
325 }
326
327 DBFreeResult(hResult);
92c51b1d
VK
328 DBFreeStatement(hStmt);
329
330 loadItemsFromDB();
89135050 331 loadACLFromDB();
5039dede
AK
332
333 // Walk through all items in the node and load appropriate thresholds
a6c1f07f 334 bResult = TRUE;
16d6f798
VK
335 for(i = 0; i < m_dcObjects->size(); i++)
336 if (!m_dcObjects->get(i)->loadThresholdsFromDB())
5039dede 337 {
5d2c5741 338 DbgPrintf(3, _T("Cannot load thresholds for DCI %d of node %d (%s)"),
16d6f798 339 m_dcObjects->get(i)->getId(), dwId, m_szName);
5039dede
AK
340 bResult = FALSE;
341 }
342 }
343 else
344 {
345 bResult = TRUE;
346 }
347
348 return bResult;
349}
350
c50f77f7
VK
351/**
352 * Save object to database
353 */
5039dede
AK
354BOOL Node::SaveToDB(DB_HANDLE hdb)
355{
3eab63f2 356 TCHAR szQuery[4096], szIpAddr[16], baseAddress[16];
5039dede
AK
357 BOOL bNewObject = TRUE;
358 BOOL bResult;
359
360 // Lock object's access
361 LockData();
362
89135050 363 saveCommonProperties(hdb);
5039dede 364
5039dede 365 // Form and execute INSERT or UPDATE query
4b7d8903 366 int snmpMethods = m_snmpSecurity->getAuthMethod() | (m_snmpSecurity->getPrivMethod() << 8);
ba89fed2
VK
367 if (IsDatabaseRecordExist(hdb, _T("nodes"), _T("id"), m_dwId))
368 {
369 _sntprintf(szQuery, 4096,
370 _T("UPDATE nodes SET primary_ip='%s',primary_name=%s,snmp_port=%d,")
371 _T("node_flags=%d,snmp_version=%d,community=%s,")
372 _T("status_poll_type=%d,agent_port=%d,auth_method=%d,secret=%s,")
373 _T("snmp_oid=%s,uname=%s,agent_version=%s,platform_name=%s,poller_node_id=%d,")
374 _T("zone_guid=%d,proxy_node=%d,snmp_proxy=%d,")
375 _T("required_polls=%d,use_ifxtable=%d,usm_auth_password=%s,")
376 _T("usm_priv_password=%s,usm_methods=%d,snmp_sys_name=%s,bridge_base_addr='%s',")
dd42ad0a 377 _T("runtime_flags=%d,down_since=%d WHERE id=%d"),
ba89fed2
VK
378 IpToStr(m_dwIpAddr, szIpAddr), (const TCHAR *)DBPrepareString(hdb, m_primaryName), m_wSNMPPort,
379 m_dwFlags, m_snmpVersion, (const TCHAR *)DBPrepareStringA(hdb, m_snmpSecurity->getCommunity()),
380 m_iStatusPollType, m_wAgentPort, m_wAuthMethod,
381 (const TCHAR *)DBPrepareString(hdb, m_szSharedSecret),
382 (const TCHAR *)DBPrepareString(hdb, m_szObjectId),
383 (const TCHAR *)DBPrepareString(hdb, m_sysDescription),
384 (const TCHAR *)DBPrepareString(hdb, m_szAgentVersion),
385 (const TCHAR *)DBPrepareString(hdb, m_szPlatformName), m_dwPollerNode, m_zoneId,
386 m_dwProxyNode, m_dwSNMPProxy, m_iRequiredPollCount,
387 m_nUseIfXTable, (const TCHAR *)DBPrepareStringA(hdb, m_snmpSecurity->getAuthPassword()),
388 (const TCHAR *)DBPrepareStringA(hdb, m_snmpSecurity->getPrivPassword()), snmpMethods,
389 (const TCHAR *)DBPrepareString(hdb, m_sysName),
dd42ad0a
AK
390 BinToStr(m_baseBridgeAddress, MAC_ADDR_LENGTH, baseAddress), (int)m_dwDynamicFlags,
391 (int)m_tDownSince, (int)m_dwId);
ba89fed2
VK
392 }
393 else
65e2005b
VK
394 {
395 _sntprintf(szQuery, 4096,
5ad2167d 396 _T("INSERT INTO nodes (id,primary_ip,primary_name,snmp_port,")
65e2005b
VK
397 _T("node_flags,snmp_version,community,status_poll_type,")
398 _T("agent_port,auth_method,secret,snmp_oid,proxy_node,")
5ad2167d 399 _T("agent_version,platform_name,uname,")
65e2005b 400 _T("poller_node_id,zone_guid,snmp_proxy,required_polls,")
3eab63f2 401 _T("use_ifxtable,usm_auth_password,usm_priv_password,usm_methods,")
dd42ad0a
AK
402 _T("snmp_sys_name,bridge_base_addr,runtime_flags,down_since) VALUES ")
403 _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 404 (int)m_dwId, IpToStr(m_dwIpAddr, szIpAddr), (const TCHAR *)DBPrepareString(hdb, m_primaryName),
5ad2167d
VK
405 (int)m_wSNMPPort, m_dwFlags, m_snmpVersion, (
406 const TCHAR *)DBPrepareStringA(hdb, m_snmpSecurity->getCommunity()),
65e2005b
VK
407 m_iStatusPollType, (int)m_wAgentPort, m_wAuthMethod,
408 (const TCHAR *)DBPrepareString(hdb, m_szSharedSecret),
409 (const TCHAR *)DBPrepareString(hdb, m_szObjectId),
5ad2167d 410 m_dwProxyNode, (const TCHAR *)DBPrepareString(hdb, m_szAgentVersion),
65e2005b 411 (const TCHAR *)DBPrepareString(hdb, m_szPlatformName),
0ecc2200 412 (const TCHAR *)DBPrepareString(hdb, m_sysDescription),
52c31148 413 m_dwPollerNode, m_zoneId, m_dwSNMPProxy, m_iRequiredPollCount, m_nUseIfXTable,
35f836fe 414 (const TCHAR *)DBPrepareStringA(hdb, m_snmpSecurity->getAuthPassword()),
d74c80ea 415 (const TCHAR *)DBPrepareStringA(hdb, m_snmpSecurity->getPrivPassword()), snmpMethods,
3eab63f2 416 (const TCHAR *)DBPrepareString(hdb, m_sysName),
dd42ad0a
AK
417 BinToStr(m_baseBridgeAddress, MAC_ADDR_LENGTH, baseAddress), (int)m_dwDynamicFlags,
418 (int)m_tDownSince);
65e2005b 419 }
5039dede 420 bResult = DBQuery(hdb, szQuery);
5039dede 421
7c521895 422 // Save access list
89135050 423 saveACLToDB(hdb);
7c521895
VK
424
425 UnlockData();
426
5039dede
AK
427 // Save data collection items
428 if (bResult)
429 {
7c521895 430 lockDciAccess();
16d6f798
VK
431 for(int i = 0; i < m_dcObjects->size(); i++)
432 m_dcObjects->get(i)->saveToDB(hdb);
7c521895 433 unlockDciAccess();
5039dede
AK
434 }
435
7c521895
VK
436 // Clear modifications flag
437 LockData();
5039dede 438 m_bIsModified = FALSE;
7c521895 439 UnlockData();
5039dede 440
7c521895 441 return bResult;
5039dede
AK
442}
443
171c2fd6
VK
444/**
445 * Delete object from database
446 */
65e2005b 447BOOL Node::DeleteFromDB()
5039dede 448{
35f836fe 449 TCHAR szQuery[256];
5039dede
AK
450 BOOL bSuccess;
451
6fd6de0a 452 bSuccess = DataCollectionTarget::DeleteFromDB();
5039dede
AK
453 if (bSuccess)
454 {
d6124fa0 455 _sntprintf(szQuery, 256, _T("DELETE FROM nodes WHERE id=%d"), (int)m_dwId);
5039dede 456 QueueSQLRequest(szQuery);
d6124fa0 457 _sntprintf(szQuery, 256, _T("DELETE FROM nsmap WHERE node_id=%d"), (int)m_dwId);
5039dede 458 QueueSQLRequest(szQuery);
5039dede
AK
459 }
460 return bSuccess;
461}
462
80e0db05
VK
463/**
464 * Get ARP cache from node
465 */
7c521895 466ARP_CACHE *Node::getArpCache()
5039dede
AK
467{
468 ARP_CACHE *pArpCache = NULL;
469
470 if (m_dwFlags & NF_IS_LOCAL_MGMT)
471 {
472 pArpCache = GetLocalArpCache();
473 }
474 else if (m_dwFlags & NF_IS_NATIVE_AGENT)
475 {
7c521895
VK
476 agentLock();
477 if (connectToAgent())
4687826e 478 pArpCache = m_pAgentConnection->getArpCache();
7c521895 479 agentUnlock();
5039dede
AK
480 }
481 else if (m_dwFlags & NF_IS_SNMP)
482 {
483 SNMP_Transport *pTransport;
484
cd9f247e 485 pTransport = createSnmpTransport();
803d47be
VK
486 if (pTransport != NULL)
487 {
488 pArpCache = SnmpGetArpCache(m_snmpVersion, pTransport);
489 delete pTransport;
490 }
5039dede
AK
491 }
492
493 return pArpCache;
494}
495
80e0db05
VK
496/**
497 * Get list of interfaces from node
498 */
98762401 499InterfaceList *Node::getInterfaceList()
5039dede 500{
98762401 501 InterfaceList *pIfList = NULL;
5039dede 502
b741f151 503 if ((m_dwFlags & NF_IS_NATIVE_AGENT) && (!(m_dwFlags & NF_DISABLE_NXCP)))
5039dede 504 {
7c521895
VK
505 agentLock();
506 if (connectToAgent())
5039dede 507 {
4687826e 508 pIfList = m_pAgentConnection->getInterfaceList();
5039dede 509 }
7c521895 510 agentUnlock();
5039dede 511 }
b741f151
VK
512 if ((pIfList == NULL) && (m_dwFlags & NF_IS_LOCAL_MGMT))
513 {
514 pIfList = GetLocalInterfaceList();
515 }
5039dede 516 if ((pIfList == NULL) && (m_dwFlags & NF_IS_SNMP) &&
9796ce45 517 (!(m_dwFlags & NF_DISABLE_SNMP)) && (m_driver != NULL))
5039dede
AK
518 {
519 SNMP_Transport *pTransport;
9796ce45 520 bool useIfXTable;
5039dede 521
cd9f247e 522 pTransport = createSnmpTransport();
803d47be 523 if (pTransport != NULL)
5039dede 524 {
803d47be
VK
525 if (m_nUseIfXTable == IFXTABLE_DEFAULT)
526 {
9796ce45 527 useIfXTable = (ConfigReadInt(_T("UseIfXTable"), 1) != 0) ? true : false;
803d47be
VK
528 }
529 else
530 {
9796ce45 531 useIfXTable = (m_nUseIfXTable == IFXTABLE_ENABLED) ? true : false;
803d47be 532 }
9796ce45
VK
533
534 int useAliases = ConfigReadInt(_T("UseInterfaceAliases"), 0);
ae32341d 535 pIfList = m_driver->getInterfaces(pTransport, &m_customAttributes, m_driverData, useAliases, useIfXTable);
eec253a8 536
8792fe27 537 if ((pIfList != NULL) && (m_dwFlags & NF_IS_BRIDGE))
eec253a8
VK
538 {
539 BridgeMapPorts(m_snmpVersion, pTransport, pIfList);
540 }
803d47be 541 delete pTransport;
5039dede 542 }
5039dede
AK
543 }
544
545 if (pIfList != NULL)
0d75ea88 546 {
024c3faf 547 checkInterfaceNames(pIfList);
0d75ea88
VK
548 addVrrpInterfaces(pIfList);
549 }
5039dede
AK
550
551 return pIfList;
552}
553
6fd6de0a
VK
554/**
555 * Add VRRP interfaces to interface list
556 */
98762401 557void Node::addVrrpInterfaces(InterfaceList *ifList)
0d75ea88
VK
558{
559 int i, j, k;
560 TCHAR buffer[32];
561
562 LockData();
563 if (m_vrrpInfo != NULL)
564 {
565 DbgPrintf(6, _T("Node::addVrrpInterfaces(node=%s [%d]): m_vrrpInfo->getSize()=%d"), m_szName, (int)m_dwId, m_vrrpInfo->getSize());
566
567 for(i = 0; i < m_vrrpInfo->getSize(); i++)
568 {
569 VrrpRouter *router = m_vrrpInfo->getRouter(i);
570 DbgPrintf(6, _T("Node::addVrrpInterfaces(node=%s [%d]): vrouter %d state=%d"), m_szName, (int)m_dwId, i, router->getState());
571 if (router->getState() != VRRP_STATE_MASTER)
572 continue; // Do not add interfaces if router is not in master state
573
574 // Get netmask for this VR
575 DWORD netmask = 0;
98762401
VK
576 for(j = 0; j < ifList->getSize(); j++)
577 if (ifList->get(j)->dwIndex == router->getIfIndex())
0d75ea88 578 {
98762401 579 netmask = ifList->get(j)->dwIpNetMask;
0d75ea88
VK
580 break;
581 }
582
583 // Walk through all VR virtual IPs
584 for(j = 0; j < router->getVipCount(); j++)
585 {
586 DWORD vip = router->getVip(j);
587 DbgPrintf(6, _T("Node::addVrrpInterfaces(node=%s [%d]): checking VIP %s@%d"), m_szName, (int)m_dwId, IpToStr(vip, buffer), i);
588 if (vip != 0)
589 {
98762401
VK
590 for(k = 0; k < ifList->getSize(); k++)
591 if (ifList->get(k)->dwIpAddr == vip)
0d75ea88 592 break;
98762401 593 if (k == ifList->getSize())
0d75ea88 594 {
60557d06
VK
595 NX_INTERFACE_INFO iface;
596 memset(&iface, 0, sizeof(NX_INTERFACE_INFO));
98762401
VK
597 _sntprintf(iface.szName, MAX_DB_STRING, _T("vrrp.%u.%u.%d"), router->getId(), router->getIfIndex(), j);
598 memcpy(iface.bMacAddr, router->getVirtualMacAddr(), MAC_ADDR_LENGTH);
599 iface.dwIpAddr = vip;
600 iface.dwIpNetMask = netmask;
601 ifList->add(&iface);
602 DbgPrintf(6, _T("Node::addVrrpInterfaces(node=%s [%d]): added interface %s"), m_szName, (int)m_dwId, iface.szName);
0d75ea88
VK
603 }
604 }
605 }
606 }
607 }
608 UnlockData();
609}
610
7946bd7c
VK
611/**
612 * Find interface by index and node IP
613 * Returns pointer to interface object or NULL if appropriate interface couldn't be found
614 */
58b3e451 615Interface *Node::findInterface(DWORD dwIndex, DWORD dwHostAddr)
5039dede
AK
616{
617 DWORD i;
618 Interface *pInterface;
619
620 LockChildList(FALSE);
621 for(i = 0; i < m_dwChildCount; i++)
622 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
623 {
624 pInterface = (Interface *)m_pChildList[i];
eec253a8 625 if (pInterface->getIfIndex() == dwIndex)
5039dede 626 {
eec253a8
VK
627 if (((pInterface->IpAddr() & pInterface->getIpNetMask()) ==
628 (dwHostAddr & pInterface->getIpNetMask())) ||
5039dede
AK
629 (dwHostAddr == INADDR_ANY))
630 {
631 UnlockChildList();
632 return pInterface;
633 }
634 }
635 }
636 UnlockChildList();
637 return NULL;
638}
639
7946bd7c
VK
640/**
641 * Find interface by name or description
642 * Returns pointer to interface object or NULL if appropriate interface couldn't be found
643 */
630e15d6
VK
644Interface *Node::findInterface(const TCHAR *name)
645{
646 DWORD i;
647 Interface *pInterface;
648
649 LockChildList(FALSE);
650 for(i = 0; i < m_dwChildCount; i++)
651 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
652 {
653 pInterface = (Interface *)m_pChildList[i];
478d4ff4 654 if (!_tcsicmp(pInterface->Name(), name) || !_tcsicmp(pInterface->getDescription(), name))
630e15d6
VK
655 {
656 UnlockChildList();
657 return pInterface;
658 }
659 }
660 UnlockChildList();
661 return NULL;
662}
663
7946bd7c
VK
664/**
665 * Find interface by slot/port pair
666 * Returns pointer to interface object or NULL if appropriate interface couldn't be found
667 */
3f8c54e7
VK
668Interface *Node::findInterfaceBySlotAndPort(DWORD slot, DWORD port)
669{
670 DWORD i;
671 Interface *pInterface;
672
3f8c54e7
VK
673 LockChildList(FALSE);
674 for(i = 0; i < m_dwChildCount; i++)
675 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
676 {
677 pInterface = (Interface *)m_pChildList[i];
4c16cdc7 678 if (pInterface->isPhysicalPort() && (pInterface->getSlotNumber() == slot) && (pInterface->getPortNumber() == port))
3f8c54e7
VK
679 {
680 UnlockChildList();
681 return pInterface;
682 }
683 }
684 UnlockChildList();
685 return NULL;
686}
687
7946bd7c
VK
688/**
689 * Find interface by MAC address
690 * Returns pointer to interface object or NULL if appropriate interface couldn't be found
691 */
eec253a8
VK
692Interface *Node::findInterfaceByMAC(const BYTE *macAddr)
693{
694 DWORD i;
695 Interface *pInterface;
696
697 LockChildList(FALSE);
698 for(i = 0; i < m_dwChildCount; i++)
699 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
700 {
701 pInterface = (Interface *)m_pChildList[i];
702 if (!memcmp(pInterface->getMacAddr(), macAddr, MAC_ADDR_LENGTH))
703 {
704 UnlockChildList();
705 return pInterface;
706 }
707 }
708 UnlockChildList();
709 return NULL;
710}
711
7946bd7c
VK
712/**
713 * Find interface by IP address
714 * Returns pointer to interface object or NULL if appropriate interface couldn't be found
715 */
eec253a8
VK
716Interface *Node::findInterfaceByIP(DWORD ipAddr)
717{
718 DWORD i;
719 Interface *pInterface;
720
721 if (ipAddr == 0)
722 return NULL;
723
724 LockChildList(FALSE);
725 for(i = 0; i < m_dwChildCount; i++)
726 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
727 {
728 pInterface = (Interface *)m_pChildList[i];
729 if (pInterface->IpAddr() == ipAddr)
730 {
731 UnlockChildList();
732 return pInterface;
733 }
734 }
735 UnlockChildList();
736 return NULL;
737}
738
7946bd7c
VK
739/**
740 * Find interface by bridge port number
741 */
eec253a8
VK
742Interface *Node::findBridgePort(DWORD bridgePortNumber)
743{
744 DWORD i;
745 Interface *pInterface;
746
747 LockChildList(FALSE);
748 for(i = 0; i < m_dwChildCount; i++)
749 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
750 {
751 pInterface = (Interface *)m_pChildList[i];
752 if (pInterface->getBridgePortNumber() == bridgePortNumber)
753 {
754 UnlockChildList();
755 return pInterface;
756 }
757 }
758 UnlockChildList();
759 return NULL;
760}
761
7946bd7c
VK
762/**
763 * Find connection point for node
764 */
46117060 765Interface *Node::findConnectionPoint(DWORD *localIfId, BYTE *localMacAddr, bool *exactMatch)
f42b8099
VK
766{
767 Interface *cp = NULL;
768 LockChildList(FALSE);
769 for(DWORD i = 0; i < m_dwChildCount; i++)
770 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
771 {
772 Interface *iface = (Interface *)m_pChildList[i];
46117060 773 cp = FindInterfaceConnectionPoint(iface->getMacAddr(), exactMatch);
f42b8099 774 if (cp != NULL)
06a93345
VK
775 {
776 *localIfId = iface->Id();
eec253a8 777 memcpy(localMacAddr, iface->getMacAddr(), MAC_ADDR_LENGTH);
f42b8099 778 break;
06a93345 779 }
f42b8099
VK
780 }
781 UnlockChildList();
782 return cp;
783}
784
a3050773
VK
785/**
786 * Check if given IP address is one of node's interfaces
787 */
58b3e451 788BOOL Node::isMyIP(DWORD dwIpAddr)
5039dede
AK
789{
790 DWORD i;
791
792 LockChildList(FALSE);
793 for(i = 0; i < m_dwChildCount; i++)
794 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
795 {
796 if (((Interface *)m_pChildList[i])->IpAddr() == dwIpAddr)
797 {
798 UnlockChildList();
799 return TRUE;
800 }
801 }
802 UnlockChildList();
803 return FALSE;
804}
805
a3050773
VK
806/**
807 * Create new interface
808 */
9214177b
VK
809Interface *Node::createNewInterface(DWORD dwIpAddr, DWORD dwNetMask, const TCHAR *name, const TCHAR *descr,
810 DWORD dwIndex, DWORD dwType, BYTE *pbMacAddr, DWORD bridgePort,
811 DWORD slot, DWORD port, bool physPort, bool manuallyCreated)
5039dede
AK
812{
813 Interface *pInterface;
814 Subnet *pSubnet = NULL;
815 Cluster *pCluster;
eec253a8 816 bool bAddToSubnet, bSyntheticMask = false;
5039dede 817
f7694811
VK
818 DbgPrintf(5, _T("Node::createNewInterface(%08X, %08X, %s, %d, %d, bp=%d, slot=%d, port=%d) called for node %s [%d]"),
819 dwIpAddr, dwNetMask, CHECK_NULL(name), dwIndex, dwType, bridgePort, slot, port, m_szName, m_dwId);
5039dede
AK
820
821 // Find subnet to place interface object to
314c4f38 822 if ((dwIpAddr != 0) && (dwType != IFTYPE_SOFTWARE_LOOPBACK) && ((dwIpAddr & 0xFF000000) != 0x7F000000))
5039dede 823 {
7c521895 824 pCluster = getMyCluster();
58b3e451 825 bAddToSubnet = (pCluster != NULL) ? !pCluster->isSyncAddr(dwIpAddr) : TRUE;
f7694811 826 DbgPrintf(5, _T("Node::createNewInterface: node=%s [%d] cluster=%s [%d] add=%d"),
5039dede
AK
827 m_szName, m_dwId, (pCluster != NULL) ? pCluster->Name() : _T("(null)"),
828 (pCluster != NULL) ? pCluster->Id() : 0, bAddToSubnet);
829 if (bAddToSubnet)
830 {
89135050 831 pSubnet = FindSubnetForNode(m_zoneId, dwIpAddr);
5039dede
AK
832 if (pSubnet == NULL)
833 {
834 // Check if netmask is 0 (detect), and if yes, create
835 // new subnet with class mask
836 if (dwNetMask == 0)
837 {
838 bSyntheticMask = TRUE;
839 if (dwIpAddr < 0xE0000000)
840 {
841 dwNetMask = 0xFFFFFF00; // Class A, B or C
842 }
843 else
844 {
845 TCHAR szBuffer[16];
846
847 // Multicast address??
848 DbgPrintf(2, _T("Attempt to create interface object with multicast address %s"),
849 IpToStr(dwIpAddr, szBuffer));
850 }
851 }
852
853 // Create new subnet object
6449475d 854 // Ignore mask 255.255.255.255 - some point-to-point interfaces can have such mask
674164eb
VK
855 // Ignore mask 255.255.255.254 - it's invalid
856 if ((dwIpAddr < 0xE0000000) && (dwNetMask != 0xFFFFFFFF) && (dwNetMask != 0xFFFFFFFE))
5039dede 857 {
52c31148 858 pSubnet = new Subnet(dwIpAddr & dwNetMask, dwNetMask, m_zoneId, bSyntheticMask);
5039dede 859 NetObjInsert(pSubnet, TRUE);
89135050
VK
860 if (IsZoningEnabled())
861 {
024c3faf 862 Zone *zone = FindZoneByGUID(m_zoneId);
89135050
VK
863 if (zone != NULL)
864 {
865 zone->addSubnet(pSubnet);
866 }
867 }
868 else
869 {
870 g_pEntireNet->AddSubnet(pSubnet);
871 }
5039dede
AK
872 }
873 }
874 else
875 {
876 // Set correct netmask if we was asked for it
877 if (dwNetMask == 0)
878 {
eec253a8
VK
879 dwNetMask = pSubnet->getIpNetMask();
880 bSyntheticMask = pSubnet->isSyntheticMask();
5039dede
AK
881 }
882 }
883 }
884 }
885
886 // Create interface object
35f836fe 887 if (name != NULL)
4ecd55b3 888 pInterface = new Interface(name, (descr != NULL) ? descr : name, dwIndex, dwIpAddr, dwNetMask, dwType, m_zoneId);
5039dede 889 else
4ecd55b3 890 pInterface = new Interface(dwIpAddr, dwNetMask, m_zoneId, bSyntheticMask);
5039dede 891 if (pbMacAddr != NULL)
eec253a8
VK
892 pInterface->setMacAddr(pbMacAddr);
893 pInterface->setBridgePortNumber(bridgePort);
76f9abfd
VK
894 pInterface->setSlotNumber(slot);
895 pInterface->setPortNumber(port);
4c16cdc7 896 pInterface->setPhysicalPortFlag(physPort);
9214177b 897 pInterface->setManualCreationFlag(manuallyCreated);
5039dede
AK
898
899 // Insert to objects' list and generate event
900 NetObjInsert(pInterface, TRUE);
eec253a8 901 addInterface(pInterface);
5039dede 902 if (!m_bIsHidden)
478d4ff4 903 pInterface->unhide();
5039dede
AK
904 PostEvent(EVENT_INTERFACE_ADDED, m_dwId, "dsaad", pInterface->Id(),
905 pInterface->Name(), pInterface->IpAddr(),
eec253a8 906 pInterface->getIpNetMask(), pInterface->getIfIndex());
5039dede
AK
907
908 // Bind node to appropriate subnet
909 if (pSubnet != NULL)
910 {
911 pSubnet->AddNode(this);
912
913 // Check if subnet mask is correct on interface
eec253a8 914 if ((pSubnet->getIpNetMask() != pInterface->getIpNetMask()) && !pSubnet->isSyntheticMask())
5039dede
AK
915 {
916 PostEvent(EVENT_INCORRECT_NETMASK, m_dwId, "idsaa", pInterface->Id(),
eec253a8
VK
917 pInterface->getIfIndex(), pInterface->Name(),
918 pInterface->getIpNetMask(), pSubnet->getIpNetMask());
5039dede
AK
919 }
920 }
9214177b
VK
921
922 return pInterface;
5039dede
AK
923}
924
a3050773
VK
925/**
926 * Delete interface from node
927 */
eec253a8 928void Node::deleteInterface(Interface *pInterface)
5039dede
AK
929{
930 DWORD i;
931
dfb38baf
VK
932 DbgPrintf(5, _T("Node::deleteInterface(node=%s [%d], interface=%s [%d])"), m_szName, m_dwId, pInterface->Name(), pInterface->Id());
933
5039dede 934 // Check if we should unlink node from interface's subnet
314c4f38 935 if ((pInterface->IpAddr() != 0) && !pInterface->isExcludedFromTopology())
5039dede
AK
936 {
937 BOOL bUnlink = TRUE;
938
939 LockChildList(FALSE);
940 for(i = 0; i < m_dwChildCount; i++)
941 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
942 if (m_pChildList[i] != pInterface)
eec253a8
VK
943 if ((((Interface *)m_pChildList[i])->IpAddr() & ((Interface *)m_pChildList[i])->getIpNetMask()) ==
944 (pInterface->IpAddr() & pInterface->getIpNetMask()))
5039dede
AK
945 {
946 bUnlink = FALSE;
947 break;
948 }
949 UnlockChildList();
950
951 if (bUnlink)
952 {
953 // Last interface in subnet, should unlink node
89135050 954 Subnet *pSubnet = FindSubnetByIP(m_zoneId, pInterface->IpAddr() & pInterface->getIpNetMask());
5039dede
AK
955 if (pSubnet != NULL)
956 {
957 DeleteParent(pSubnet);
958 pSubnet->DeleteChild(this);
959 }
dfb38baf 960 DbgPrintf(5, _T("Node::deleteInterface(node=%s [%d], interface=%s [%d]): unlinked from subnet %s [%d]"),
2a1431bc
VK
961 m_szName, m_dwId, pInterface->Name(), pInterface->Id(),
962 (pSubnet != NULL) ? pSubnet->Name() : _T("(null)"),
963 (pSubnet != NULL) ? pSubnet->Id() : 0);
5039dede
AK
964 }
965 }
27de5dab 966 pInterface->deleteObject();
5039dede
AK
967}
968
7946bd7c
VK
969/**
970 * Calculate node status based on child objects status
971 */
27f9598d 972void Node::calculateCompoundStatus(BOOL bForcedRecalc)
5039dede
AK
973{
974 int iOldStatus = m_iStatus;
975 static DWORD dwEventCodes[] = { EVENT_NODE_NORMAL, EVENT_NODE_MINOR,
976 EVENT_NODE_WARNING, EVENT_NODE_MAJOR, EVENT_NODE_CRITICAL,
977 EVENT_NODE_UNKNOWN, EVENT_NODE_UNMANAGED };
978
27f9598d 979 NetObj::calculateCompoundStatus(bForcedRecalc);
5039dede
AK
980 if (m_iStatus != iOldStatus)
981 PostEvent(dwEventCodes[m_iStatus], m_dwId, "d", iOldStatus);
982}
983
a5ee7b3b
VK
984/**
985 * Perform status poll on node
986 */
58b3e451 987void Node::statusPoll(ClientSession *pSession, DWORD dwRqId, int nPoller)
5039dede 988{
46117060
VK
989 if (m_dwDynamicFlags & NDF_DELETE_IN_PROGRESS)
990 {
991 if (dwRqId == 0)
992 m_dwDynamicFlags &= ~NDF_QUEUED_FOR_STATUS_POLL;
993 return;
994 }
995
5039dede
AK
996 DWORD i, dwPollListSize, dwOldFlags = m_dwFlags;
997 NetObj *pPollerNode = NULL, **ppPollList;
998 BOOL bAllDown;
5039dede
AK
999 SNMP_Transport *pTransport;
1000 Cluster *pCluster;
1001 time_t tNow, tExpire;
1002
c1482463 1003 Queue *pQueue = new Queue; // Delayed event queue
35f836fe 1004 SetPollerInfo(nPoller, _T("wait for lock"));
7c521895 1005 pollerLock();
5039dede 1006 m_pPollRequestor = pSession;
35f836fe
VK
1007 SendPollerMsg(dwRqId, _T("Starting status poll for node %s\r\n"), m_szName);
1008 DbgPrintf(5, _T("Starting status poll for node %s (ID: %d)"), m_szName, m_dwId);
5039dede
AK
1009
1010 // Read capability expiration time and current time
1011 tExpire = (time_t)ConfigReadULong(_T("CapabilityExpirationTime"), 604800);
1012 tNow = time(NULL);
1013
1014 // Check SNMP agent connectivity
1015restart_agent_check:
15d6f8c9 1016 if ((m_dwFlags & NF_IS_SNMP) && (!(m_dwFlags & NF_DISABLE_SNMP)) && (m_dwIpAddr != 0))
5039dede
AK
1017 {
1018 TCHAR szBuffer[256];
1019 DWORD dwResult;
1020
35f836fe 1021 DbgPrintf(6, _T("StatusPoll(%s): check SNMP"), m_szName);
cd9f247e 1022 pTransport = createSnmpTransport();
803d47be
VK
1023 if (pTransport == NULL)
1024 {
35f836fe 1025 DbgPrintf(6, _T("StatusPoll(%s): cannot create SNMP transport"), m_szName);
803d47be
VK
1026 goto skip_snmp_check;
1027 }
1028
35f836fe
VK
1029 SetPollerInfo(nPoller, _T("check SNMP"));
1030 SendPollerMsg(dwRqId, _T("Checking SNMP agent connectivity\r\n"));
f42b8099 1031 dwResult = SnmpGet(m_snmpVersion, pTransport, _T(".1.3.6.1.2.1.1.2.0"), NULL, 0, szBuffer, 256, 0);
5039dede
AK
1032 if ((dwResult == SNMP_ERR_SUCCESS) || (dwResult == SNMP_ERR_NO_OBJECT))
1033 {
1034 if (m_dwDynamicFlags & NDF_SNMP_UNREACHABLE)
1035 {
1036 m_dwDynamicFlags &= ~NDF_SNMP_UNREACHABLE;
1037 PostEventEx(pQueue, EVENT_SNMP_OK, m_dwId, NULL);
35f836fe 1038 SendPollerMsg(dwRqId, POLLER_INFO _T("Connectivity with SNMP agent restored\r\n"));
5039dede
AK
1039 }
1040 }
1041 else
1042 {
35f836fe 1043 SendPollerMsg(dwRqId, POLLER_ERROR _T("SNMP agent unreachable\r\n"));
5039dede
AK
1044 if (m_dwDynamicFlags & NDF_SNMP_UNREACHABLE)
1045 {
1046 if ((tNow > m_tFailTimeSNMP + tExpire) &&
1047 (!(m_dwDynamicFlags & NDF_UNREACHABLE)))
1048 {
1049 m_dwFlags &= ~NF_IS_SNMP;
1050 m_dwDynamicFlags &= ~NDF_SNMP_UNREACHABLE;
1051 m_szObjectId[0] = 0;
35f836fe 1052 SendPollerMsg(dwRqId, POLLER_WARNING _T("Attribute isSNMP set to FALSE\r\n"));
5039dede
AK
1053 }
1054 }
1055 else
1056 {
1057 m_dwDynamicFlags |= NDF_SNMP_UNREACHABLE;
1058 PostEventEx(pQueue, EVENT_SNMP_FAIL, m_dwId, NULL);
1059 m_tFailTimeSNMP = tNow;
1060 }
1061 }
1062 delete pTransport;
35f836fe 1063 DbgPrintf(6, _T("StatusPoll(%s): SNMP check finished"), m_szName);
5039dede
AK
1064 }
1065
803d47be 1066skip_snmp_check:
5039dede 1067 // Check native agent connectivity
15d6f8c9 1068 if ((m_dwFlags & NF_IS_NATIVE_AGENT) && (!(m_dwFlags & NF_DISABLE_NXCP)) && (m_dwIpAddr != 0))
5039dede 1069 {
35f836fe
VK
1070 DbgPrintf(6, _T("StatusPoll(%s): checking agent"), m_szName);
1071 SetPollerInfo(nPoller, _T("check agent"));
1072 SendPollerMsg(dwRqId, _T("Checking NetXMS agent connectivity\r\n"));
c3acd0f6
VK
1073
1074 DWORD error, socketError;
a4569c4d
VK
1075 agentLock();
1076 if (connectToAgent(&error, &socketError))
5039dede 1077 {
35f836fe 1078 DbgPrintf(7, _T("StatusPoll(%s): connected to agent"), m_szName);
5039dede
AK
1079 if (m_dwDynamicFlags & NDF_AGENT_UNREACHABLE)
1080 {
1081 m_dwDynamicFlags &= ~NDF_AGENT_UNREACHABLE;
1082 PostEventEx(pQueue, EVENT_AGENT_OK, m_dwId, NULL);
35f836fe 1083 SendPollerMsg(dwRqId, POLLER_INFO _T("Connectivity with NetXMS agent restored\r\n"));
5039dede 1084 }
5039dede
AK
1085 }
1086 else
1087 {
35f836fe
VK
1088 DbgPrintf(6, _T("StatusPoll(%s): agent unreachable, error=%d, socketError=%d"), m_szName, (int)error, (int)socketError);
1089 SendPollerMsg(dwRqId, POLLER_ERROR _T("NetXMS agent unreachable\r\n"));
5039dede
AK
1090 if (m_dwDynamicFlags & NDF_AGENT_UNREACHABLE)
1091 {
1092 if ((tNow > m_tFailTimeAgent + tExpire) &&
1093 (!(m_dwDynamicFlags & NDF_UNREACHABLE)))
1094 {
1095 m_dwFlags &= ~NF_IS_NATIVE_AGENT;
1096 m_dwDynamicFlags &= ~NDF_AGENT_UNREACHABLE;
1097 m_szPlatformName[0] = 0;
1098 m_szAgentVersion[0] = 0;
35f836fe 1099 SendPollerMsg(dwRqId, POLLER_WARNING _T("Attribute isNetXMSAgent set to FALSE\r\n"));
5039dede
AK
1100 }
1101 }
1102 else
1103 {
1104 m_dwDynamicFlags |= NDF_AGENT_UNREACHABLE;
1105 PostEventEx(pQueue, EVENT_AGENT_FAIL, m_dwId, NULL);
1106 m_tFailTimeAgent = tNow;
1107 }
1108 }
a4569c4d 1109 agentUnlock();
35f836fe 1110 DbgPrintf(7, _T("StatusPoll(%s): agent check finished"), m_szName);
5039dede
AK
1111 }
1112
35f836fe 1113 SetPollerInfo(nPoller, _T("prepare polling list"));
5039dede
AK
1114
1115 // Find service poller node object
1116 LockData();
1117 if (m_dwPollerNode != 0)
1118 {
f3218755
VK
1119 DWORD id = m_dwPollerNode;
1120 UnlockData();
1121 pPollerNode = FindObjectById(id);
5039dede
AK
1122 if (pPollerNode != NULL)
1123 {
1124 if (pPollerNode->Type() != OBJECT_NODE)
1125 pPollerNode = NULL;
1126 }
1127 }
f3218755
VK
1128 else
1129 {
1130 UnlockData();
1131 }
5039dede
AK
1132
1133 // If nothing found, use management server
1134 if (pPollerNode == NULL)
1135 {
1136 pPollerNode = FindObjectById(g_dwMgmtNode);
1137 if (pPollerNode != NULL)
1138 pPollerNode->IncRefCount();
1139 }
1140 else
1141 {
1142 pPollerNode->IncRefCount();
1143 }
1144
1145 // Create polling list
1146 ppPollList = (NetObj **)malloc(sizeof(NetObj *) * m_dwChildCount);
1147 LockChildList(FALSE);
1148 for(i = 0, dwPollListSize = 0; i < m_dwChildCount; i++)
1149 if (m_pChildList[i]->Status() != STATUS_UNMANAGED)
1150 {
1151 m_pChildList[i]->IncRefCount();
1152 ppPollList[dwPollListSize++] = m_pChildList[i];
1153 }
1154 UnlockChildList();
1155
1156 // Poll interfaces and services
35f836fe
VK
1157 SetPollerInfo(nPoller, _T("child poll"));
1158 DbgPrintf(7, _T("StatusPoll(%s): starting child object poll"), m_szName);
7c521895 1159 pCluster = getMyCluster();
cd9f247e 1160 pTransport = createSnmpTransport();
5039dede
AK
1161 for(i = 0; i < dwPollListSize; i++)
1162 {
1163 switch(ppPollList[i]->Type())
1164 {
1165 case OBJECT_INTERFACE:
35f836fe 1166 DbgPrintf(7, _T("StatusPoll(%s): polling interface %d [%s]"), m_szName, ppPollList[i]->Id(), ppPollList[i]->Name());
5039dede 1167 ((Interface *)ppPollList[i])->StatusPoll(pSession, dwRqId, pQueue,
58b3e451 1168 (pCluster != NULL) ? pCluster->isSyncAddr(((Interface *)ppPollList[i])->IpAddr()) : FALSE,
5039dede
AK
1169 pTransport);
1170 break;
1171 case OBJECT_NETWORKSERVICE:
35f836fe 1172 DbgPrintf(7, _T("StatusPoll(%s): polling network service %d [%s]"), m_szName, ppPollList[i]->Id(), ppPollList[i]->Name());
5039dede
AK
1173 ((NetworkService *)ppPollList[i])->StatusPoll(pSession, dwRqId,
1174 (Node *)pPollerNode, pQueue);
1175 break;
1176 default:
1177 break;
1178 }
1179 ppPollList[i]->DecRefCount();
1180 }
1181 delete pTransport;
1182 safe_free(ppPollList);
35f836fe 1183 DbgPrintf(7, _T("StatusPoll(%s): finished child object poll"), m_szName);
5039dede
AK
1184
1185 // Check if entire node is down
15d6f8c9
VK
1186 // This check is disabled for nodes without IP address
1187 if (m_dwIpAddr != 0)
5039dede 1188 {
15d6f8c9
VK
1189 LockChildList(FALSE);
1190 if (m_dwChildCount > 0)
1191 {
1192 for(i = 0, bAllDown = TRUE; i < m_dwChildCount; i++)
1193 if ((m_pChildList[i]->Type() == OBJECT_INTERFACE) &&
1194 (m_pChildList[i]->Status() != STATUS_CRITICAL) &&
1195 (m_pChildList[i]->Status() != STATUS_UNKNOWN) &&
1196 (m_pChildList[i]->Status() != STATUS_UNMANAGED) &&
1197 (m_pChildList[i]->Status() != STATUS_DISABLED))
1198 {
1199 bAllDown = FALSE;
1200 break;
1201 }
1202 }
1203 else
1204 {
1205 bAllDown = FALSE;
1206 }
1207 UnlockChildList();
1208 if (bAllDown && (m_dwFlags & NF_IS_NATIVE_AGENT) &&
1209 (!(m_dwFlags & NF_DISABLE_NXCP)))
1210 if (!(m_dwDynamicFlags & NDF_AGENT_UNREACHABLE))
1211 bAllDown = FALSE;
1212 if (bAllDown && (m_dwFlags & NF_IS_SNMP) &&
1213 (!(m_dwFlags & NF_DISABLE_SNMP)))
1214 if (!(m_dwDynamicFlags & NDF_SNMP_UNREACHABLE))
1215 bAllDown = FALSE;
c1482463
VK
1216
1217 DbgPrintf(6, _T("StatusPoll(%s): bAllDown=%s, dynFlags=0x%08X"), m_szName, bAllDown ? _T("true") : _T("false"), m_dwDynamicFlags);
15d6f8c9
VK
1218 if (bAllDown)
1219 {
1220 if (!(m_dwDynamicFlags & NDF_UNREACHABLE))
1221 {
1222 m_dwDynamicFlags |= NDF_UNREACHABLE;
dd42ad0a 1223 m_tDownSince = time(NULL);
7946bd7c 1224 SetPollerInfo(nPoller, _T("check network path"));
c1482463
VK
1225 if (checkNetworkPath(dwRqId))
1226 {
1227 m_dwDynamicFlags |= NDF_NETWORK_PATH_PROBLEM;
1228
1229 // Set interfaces and network services to UNKNOWN state
1230 LockChildList(FALSE);
1231 for(i = 0, bAllDown = TRUE; i < m_dwChildCount; i++)
1232 if (((m_pChildList[i]->Type() == OBJECT_INTERFACE) || (m_pChildList[i]->Type() == OBJECT_NETWORKSERVICE)) &&
1233 (m_pChildList[i]->Status() == STATUS_CRITICAL))
1234 {
1235 m_pChildList[i]->resetStatus();
1236 }
1237 UnlockChildList();
1238
1239 // Clear delayed event queue
1240 while(1)
1241 {
1242 Event *pEvent = (Event *)pQueue->Get();
1243 if (pEvent == NULL)
1244 break;
1245 delete pEvent;
1246 }
1247 delete_and_null(pQueue);
1248
1249 PostEvent(EVENT_NODE_UNREACHABLE, m_dwId, NULL);
1250 }
1251 else
1252 {
1253 PostEvent(EVENT_NODE_DOWN, m_dwId, NULL);
1254 }
35f836fe 1255 SendPollerMsg(dwRqId, POLLER_ERROR _T("Node is unreachable\r\n"));
15d6f8c9
VK
1256 }
1257 else
1258 {
35f836fe 1259 SendPollerMsg(dwRqId, POLLER_WARNING _T("Node is still unreachable\r\n"));
15d6f8c9
VK
1260 }
1261 }
1262 else
1263 {
dd42ad0a 1264 m_tDownSince = 0;
15d6f8c9
VK
1265 if (m_dwDynamicFlags & NDF_UNREACHABLE)
1266 {
c1482463 1267 m_dwDynamicFlags &= ~(NDF_UNREACHABLE | NDF_SNMP_UNREACHABLE | NDF_AGENT_UNREACHABLE | NDF_NETWORK_PATH_PROBLEM);
15d6f8c9 1268 PostEvent(EVENT_NODE_UP, m_dwId, NULL);
35f836fe 1269 SendPollerMsg(dwRqId, POLLER_INFO _T("Node recovered from unreachable state\r\n"));
15d6f8c9
VK
1270 goto restart_agent_check;
1271 }
1272 else
1273 {
35f836fe 1274 SendPollerMsg(dwRqId, POLLER_INFO _T("Node is connected\r\n"));
15d6f8c9
VK
1275 }
1276 }
5039dede 1277 }
5039dede
AK
1278
1279 // Send delayed events and destroy delayed event queue
c1482463
VK
1280 if (pQueue != NULL)
1281 {
1282 ResendEvents(pQueue);
1283 delete pQueue;
1284 }
a5ee7b3b
VK
1285
1286 // Call hooks in loaded modules
1287 for(DWORD i = 0; i < g_dwNumModules; i++)
1288 {
1289 if (g_pModuleList[i].pfStatusPollHook != NULL)
1290 {
1291 DbgPrintf(5, _T("StatusPoll(%s [%d]): calling hook in module %s"), m_szName, m_dwId, g_pModuleList[i].szName);
1292 g_pModuleList[i].pfStatusPollHook(this, pSession, dwRqId, nPoller);
1293 }
1294 }
5039dede 1295
a5ee7b3b
VK
1296 // Execute hook script
1297 SetPollerInfo(nPoller, _T("hook"));
1298 executeHookScript(_T("StatusPoll"));
1299
35f836fe 1300 SetPollerInfo(nPoller, _T("cleanup"));
5039dede
AK
1301 if (pPollerNode != NULL)
1302 pPollerNode->DecRefCount();
1303
1304 if (dwOldFlags != m_dwFlags)
1305 {
1306 PostEvent(EVENT_NODE_FLAGS_CHANGED, m_dwId, "xx", dwOldFlags, m_dwFlags);
1307 LockData();
1308 Modify();
1309 UnlockData();
1310 }
1311
27f9598d 1312 calculateCompoundStatus();
5039dede 1313 m_tLastStatusPoll = time(NULL);
35f836fe
VK
1314 SendPollerMsg(dwRqId, _T("Finished status poll for node %s\r\n"), m_szName);
1315 SendPollerMsg(dwRqId, _T("Node status after poll is %s\r\n"), g_szStatusText[m_iStatus]);
5039dede
AK
1316 m_pPollRequestor = NULL;
1317 if (dwRqId == 0)
1318 m_dwDynamicFlags &= ~NDF_QUEUED_FOR_STATUS_POLL;
7c521895 1319 pollerUnlock();
35f836fe 1320 DbgPrintf(5, _T("Finished status poll for node %s (ID: %d)"), m_szName, m_dwId);
5039dede
AK
1321}
1322
7946bd7c
VK
1323/**
1324 * Check network path between node and management server to detect possible intermediate node failure
c1482463
VK
1325 *
1326 * @return true if network path problems found
7946bd7c 1327 */
c1482463 1328bool Node::checkNetworkPath(DWORD dwRqId)
7946bd7c
VK
1329{
1330 Node *mgmtNode = (Node *)FindObjectById(g_dwMgmtNode);
1331 if (mgmtNode == NULL)
1332 {
1333 DbgPrintf(5, _T("Node::checkNetworkPath(%s [%d]): cannot find management node"), m_szName, m_dwId);
c1482463 1334 return false;
7946bd7c 1335 }
5039dede 1336
7946bd7c
VK
1337 NetworkPath *trace = TraceRoute(mgmtNode, this);
1338 if (trace == NULL)
1339 {
1340 DbgPrintf(5, _T("Node::checkNetworkPath(%s [%d]): trace not available"), m_szName, m_dwId);
c1482463 1341 return false;
7946bd7c
VK
1342 }
1343 DbgPrintf(5, _T("Node::checkNetworkPath(%s [%d]): trace available, %d hops, %s"),
1344 m_szName, m_dwId, trace->getHopCount(), trace->isComplete() ? _T("complete") : _T("incomplete"));
1345
1346 // We will do path check in two passes
1347 // If unreachable intermediate node will be found on first pass,
1348 // then method will just return true. Otherwise, we will do
1349 // second pass, this time forcing status poll on each node in the path.
1350 SendPollerMsg(dwRqId, _T("Checking network path...\r\n"));
1351 time_t now = time(NULL);
1352 bool secondPass = false;
1353 bool pathProblemFound = false;
1354restart:
1355 for(int i = 0; i < trace->getHopCount(); i++)
1356 {
1357 HOP_INFO *hop = trace->getHopInfo(i);
1358 if ((hop->object == NULL) || (hop->object == this) || (hop->object->Type() != OBJECT_NODE))
1359 continue;
1360
1361 DbgPrintf(6, _T("Node::checkNetworkPath(%s [%d]): checking upstream node %s [%d]"),
1362 m_szName, m_dwId, hop->object->Name(), hop->object->Id());
1363 if (secondPass && !((Node *)hop->object)->isDown() && (((Node *)hop->object)->m_tLastStatusPoll < now - 1))
1364 {
1365 DbgPrintf(6, _T("Node::checkNetworkPath(%s [%d]): forced status poll on node %s [%d]"),
1366 m_szName, m_dwId, hop->object->Name(), hop->object->Id());
1367 ((Node *)hop->object)->statusPoll(NULL, 0, 0);
1368 }
1f385e47 1369
7946bd7c
VK
1370 if (((Node *)hop->object)->isDown())
1371 {
1372 DbgPrintf(5, _T("Node::checkNetworkPath(%s [%d]): upstream node %s [%d] is down"),
1373 m_szName, m_dwId, hop->object->Name(), hop->object->Id());
1374 SendPollerMsg(dwRqId, POLLER_WARNING _T(" Upstream node %s is down\r\n"), hop->object->Name());
1375 pathProblemFound = true;
1376 break;
1377 }
1378 }
1379 if (!secondPass && !pathProblemFound)
1380 {
1381 DbgPrintf(5, _T("Node::checkNetworkPath(%s [%d]): will do second pass"), m_szName, m_dwId);
1382 secondPass = true;
1383 goto restart;
1384 }
1385 delete trace;
c1482463 1386 return pathProblemFound;
7946bd7c
VK
1387}
1388
1389/**
1390 * Check agent policy binding
1391 * Intended to be called only from configuration poller
1392 */
1f385e47
VK
1393void Node::checkAgentPolicyBinding(AgentConnection *conn)
1394{
1395 AgentPolicyInfo *ap;
1396 DWORD rcc = conn->getPolicyInventory(&ap);
1397 if (rcc == ERR_SUCCESS)
1398 {
1399 // Check for unbound but installed policies
1400 for(int i = 0; i < ap->getSize(); i++)
1401 {
1402 uuid_t guid;
1403 ap->getGuid(i, guid);
1404 NetObj *object = FindObjectByGUID(guid, -1);
1405 if ((object != NULL) && (!object->IsChild(m_dwId)))
1406 {
1407 object->AddChild(this);
1408 AddParent(object);
1409 DbgPrintf(5, _T("ConfPoll(%s): bound to policy object %s [%d]"), m_szName, object->Name(), object->Id());
1410 }
1411 }
1412
1413 // Check for bound but not installed policies
1414 LockParentList(FALSE);
1415 NetObj **unbindList = (NetObj **)malloc(sizeof(NetObj *) * m_dwParentCount);
1416 int unbindListSize = 0;
1417 for(DWORD i = 0; i < m_dwParentCount; i++)
1418 {
1419 if (IsAgentPolicyObject(m_pParentList[i]))
1420 {
1421 uuid_t guid1, guid2;
1422 int j;
1423
1424 m_pParentList[i]->getGuid(guid1);
1425 for(j = 0; j < ap->getSize(); j++)
1426 {
1427 ap->getGuid(j, guid2);
1428 if (!uuid_compare(guid1, guid2))
1429 break;
1430 }
1431 if (j == ap->getSize())
1432 unbindList[unbindListSize++] = m_pParentList[i];
1433 }
1434 }
1435 UnlockParentList();
1436
1437 for(int i = 0; i < unbindListSize; i++)
1438 {
1439 unbindList[i]->DeleteChild(this);
1440 DeleteParent(unbindList[i]);
1441 DbgPrintf(5, _T("ConfPoll(%s): unbound from policy object %s [%d]"), m_szName, unbindList[i]->Name(), unbindList[i]->Id());
1442 }
1443 safe_free(unbindList);
1444
1445 delete ap;
1446 }
1447 else
1448 {
1449 DbgPrintf(5, _T("ConfPoll(%s): AgentConnection::getPolicyInventory() failed: rcc=%d"), m_szName, rcc);
1450 }
1451}
1452
a3050773
VK
1453/**
1454 * Update primary IP address from primary name
1455 */
5a7d6a10
VK
1456void Node::updatePrimaryIpAddr()
1457{
1458 if (m_primaryName[0] == 0)
1459 return;
1460
1461 DWORD ipAddr = ntohl(ResolveHostName(m_primaryName));
1462 if ((ipAddr != m_dwIpAddr) && (ipAddr != INADDR_ANY) && (ipAddr != INADDR_NONE))
1463 {
1464 TCHAR buffer1[32], buffer2[32];
1465
1466 DbgPrintf(4, _T("IP address for node %s [%d] changed from %s to %s"),
1467 m_szName, (int)m_dwId, IpToStr(m_dwIpAddr, buffer1), IpToStr(ipAddr, buffer2));
1468 PostEvent(EVENT_IP_ADDRESS_CHANGED, m_dwId, "aa", ipAddr, m_dwIpAddr);
1469 m_dwIpAddr = 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
VK
2388/**
2389 * Apply user templates
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
6fd6de0a 2421void Node::applyUserTemplates()
27de5dab
VK
2422{
2423 g_idxObjectById.forEach(ApplyTemplate, this);
4d0c32f3 2424}
5039dede 2425
6fd6de0a
VK
2426/**
2427 * Update container membership
2428 */
27de5dab 2429static void UpdateContainerBinding(NetObj *object, void *node)
4d0c32f3 2430{
6ff21d27 2431 if ((object->Type() == OBJECT_CONTAINER) && !object->isDeleted())
5039dede 2432 {
27de5dab 2433 Container *pContainer = (Container *)object;
926e8ce7 2434 if (pContainer->isSuitableForNode((Node *)node))
27de5dab
VK
2435 {
2436 if (!pContainer->IsChild(((Node *)node)->Id()))
4d0c32f3 2437 {
27de5dab
VK
2438 DbgPrintf(4, _T("Node::UpdateContainerMembership(): binding node %d \"%s\" to container %d \"%s\""),
2439 ((Node *)node)->Id(), ((Node *)node)->Name(), pContainer->Id(), pContainer->Name());
2440 pContainer->AddChild((Node *)node);
2441 ((Node *)node)->AddParent(pContainer);
9b64b406 2442 PostEvent(EVENT_CONTAINER_AUTOBIND, g_dwMgmtNode, "isis", ((Node *)node)->Id(), ((Node *)node)->Name(), pContainer->Id(), pContainer->Name());
4d0c32f3 2443 }
27de5dab
VK
2444 }
2445 else
2446 {
9b64b406 2447 if (pContainer->isAutoUnbindEnabled() && pContainer->IsChild(((Node *)node)->Id()))
4d0c32f3 2448 {
27de5dab
VK
2449 DbgPrintf(4, _T("Node::UpdateContainerMembership(): removing node %d \"%s\" from container %d \"%s\""),
2450 ((Node *)node)->Id(), ((Node *)node)->Name(), pContainer->Id(), pContainer->Name());
2451 pContainer->DeleteChild((Node *)node);
2452 ((Node *)node)->DeleteParent(pContainer);
9b64b406 2453 PostEvent(EVENT_CONTAINER_AUTOUNBIND, g_dwMgmtNode, "isis", ((Node *)node)->Id(), ((Node *)node)->Name(), pContainer->Id(), pContainer->Name());
4d0c32f3 2454 }
27de5dab 2455 }
5039dede 2456 }
27de5dab
VK
2457}
2458
2459void Node::updateContainerMembership()
2460{
2461 g_idxObjectById.forEach(UpdateContainerBinding, this);
5039dede
AK
2462}
2463
a3050773 2464/**
d51f2182
VK
2465 * Do instance discovery
2466 */
2467void Node::doInstanceDiscovery()
2468{
2469 // collect instance discovery DCIs
2470 ObjectArray<DCItem> rootItems;
2471 lockDciAccess();
2472 for(int i = 0; i < m_dcObjects->size(); i++)
2473 {
2474 DCObject *object = m_dcObjects->get(i);
2475 if ((object->getType() == DCO_TYPE_ITEM) && (((DCItem *)object)->getInstanceDiscoveryMethod() != IDM_NONE))
2476 {
2477 object->setBusyFlag(TRUE);
2478 rootItems.add((DCItem *)object);
2479 }
2480 }
2481 unlockDciAccess();
2482
2483 // process instance discovery DCIs
2484 // it should be done that way to prevent DCI list lock for long time
2485 for(int i = 0; i < rootItems.size(); i++)
2486 {
2487 DCItem *dci = rootItems.get(i);
2488 DbgPrintf(5, _T("Node::doInstanceDiscovery(%s [%u]): Updating instances for instance discovery DCI %s [%d]"),
2489 m_szName, m_dwId, dci->getName(), dci->getId());
2490 StringList *instances = getInstanceList(dci);
2491 if (instances != NULL)
2492 {
2493 DbgPrintf(5, _T("Node::doInstanceDiscovery(%s [%u]): read %d values"), m_szName, m_dwId, instances->getSize());
27bbb906 2494 dci->filterInstanceList(instances);
d51f2182
VK
2495 updateInstances(dci, instances);
2496 delete instances;
2497 }
2498 else
2499 {
2500 DbgPrintf(5, _T("Node::doInstanceDiscovery(%s [%u]): failed to get instance list for DCI %s [%d]"),
2501 m_szName, m_dwId, dci->getName(), dci->getId());
2502 }
2503 dci->setBusyFlag(FALSE);
2504 }
2505}
2506
2507/**
2508 * Get instances for instance discovery DCI
2509 */
2510StringList *Node::getInstanceList(DCItem *dci)
2511{
2512 if (dci->getInstanceDiscoveryData() == NULL)
2513 return NULL;
2514
2515 StringList *instances;
2516 switch(dci->getInstanceDiscoveryMethod())
2517 {
2518 case IDM_AGENT_LIST:
2519 getListFromAgent(dci->getInstanceDiscoveryData(), &instances);
2520 break;
2521 default:
2522 instances = NULL;
2523 break;
2524 }
2525 return instances;
2526}
2527
2528/**
2529 * Update instance DCIs created from instance discovery DCI
2530 */
2531void Node::updateInstances(DCItem *root, StringList *instances)
2532{
2533 lockDciAccess();
2534
2535 // Delete DCIs for missing instances and update existing
2536 ObjectArray<void> deleteList;
2537 for(int i = 0; i < m_dcObjects->size(); i++)
2538 {
2539 DCObject *object = m_dcObjects->get(i);
2540 if ((object->getType() != DCO_TYPE_ITEM) ||
2541 (object->getTemplateId() != m_dwId) ||
2542 (object->getTemplateItemId() != root->getId()))
2543 continue;
2544
2545 int j;
2546 for(j = 0; j < instances->getSize(); j++)
2547 if (!_tcscmp(((DCItem *)object)->getInstance(), instances->getValue(j)))
2548 break;
2549
2550 if (j < instances->getSize())
2551 {
2552 // found, remove value from instances
2553 DbgPrintf(5, _T("Node::updateInstances(%s [%u], %s [%u]): instance \"%s\" found"),
2554 m_szName, m_dwId, root->getName(), root->getId(), instances->getValue(j));
2555 instances->remove(j);
2556 }
2557 else
2558 {
2559 // not found, delete DCI
2560 DbgPrintf(5, _T("Node::updateInstances(%s [%u], %s [%u]): instance \"%s\" not found, instance DCI will be deleted"),
27bbb906 2561 m_szName, m_dwId, root->getName(), root->getId(), ((DCItem *)object)->getInstance());
d51f2182
VK
2562 deleteList.add(CAST_TO_POINTER(object->getId(), void *));
2563 }
2564 }
2565
2566 for(int i = 0; i < deleteList.size(); i++)
2567 deleteDCObject(CAST_FROM_POINTER(deleteList.get(i), DWORD), false);
2568
2569 // Create new instances
2570 for(int i = 0; i < instances->getSize(); i++)
2571 {
27bbb906
VK
2572 DbgPrintf(5, _T("Node::updateInstances(%s [%u], %s [%u]): creating new DCI for instance \"%s\""),
2573 m_szName, m_dwId, root->getName(), root->getId(), instances->getValue(i));
2574
d51f2182
VK
2575 DCItem *dci = new DCItem(root);
2576 dci->setTemplateId(m_dwId, root->getId());
2577 dci->setInstance(instances->getValue(i));
2578 dci->setInstanceDiscoveryMethod(IDM_NONE);
2579 dci->setInstanceDiscoveryData(NULL);
2580 dci->setInstanceFilter(NULL);
2581 dci->expandInstance();
2582 dci->changeBinding(CreateUniqueId(IDG_ITEM), this, FALSE);
2583 addDCObject(dci, true);
2584 }
2585
2586 unlockDciAccess();
2587}
2588
2589/**
a3050773
VK
2590 * Connect to native agent. Assumes that access to agent connection is already locked.
2591 */
a4569c4d 2592BOOL Node::connectToAgent(DWORD *error, DWORD *socketError)
5039dede
AK
2593{
2594 BOOL bRet;
2595
2596 // Create new agent connection object if needed
2597 if (m_pAgentConnection == NULL)
4b91844f 2598 {
d1c1c522 2599 m_pAgentConnection = new AgentConnectionEx(m_dwId, htonl(m_dwIpAddr), m_wAgentPort, m_wAuthMethod, m_szSharedSecret);
4b91844f
VK
2600 DbgPrintf(7, _T("Node::connectToAgent(%s [%d]): new agent connection created"), m_szName, m_dwId);
2601 }
2602 else
2603 {
2604 // Check if we already connected
2605 if (m_pAgentConnection->nop() == ERR_SUCCESS)
2606 {
2607 DbgPrintf(7, _T("Node::connectToAgent(%s [%d]): already connected"), m_szName, m_dwId);
2608 return TRUE;
2609 }
5039dede 2610
4b91844f
VK
2611 // Close current connection or clean up after broken connection
2612 m_pAgentConnection->disconnect();
2613 DbgPrintf(7, _T("Node::connectToAgent(%s [%d]): existing connection reset"), m_szName, m_dwId);
2614 }
7c521895 2615 m_pAgentConnection->setPort(m_wAgentPort);
35f836fe
VK
2616#ifdef UNICODE
2617 char mbSecret[MAX_SECRET_LENGTH];
2618 WideCharToMultiByte(CP_ACP, WC_DEFAULTCHAR | WC_COMPOSITECHECK, m_szSharedSecret, -1, mbSecret, MAX_SECRET_LENGTH, NULL, NULL);
2619 mbSecret[MAX_SECRET_LENGTH - 1] = 0;
2620 m_pAgentConnection->setAuthData(m_wAuthMethod, mbSecret);
2621#else
7c521895 2622 m_pAgentConnection->setAuthData(m_wAuthMethod, m_szSharedSecret);
35f836fe 2623#endif
7c521895 2624 setAgentProxy(m_pAgentConnection);
4b91844f 2625 DbgPrintf(7, _T("Node::connectToAgent(%s [%d]): calling connect on port %d"), m_szName, m_dwId, (int)m_wAgentPort);
a4569c4d 2626 bRet = m_pAgentConnection->connect(g_pServerKey, FALSE, error, socketError);
5039dede
AK
2627 if (bRet)
2628 {
45d84f8a
VK
2629 m_pAgentConnection->setCommandTimeout(g_dwAgentCommandTimeout);
2630 m_pAgentConnection->enableTraps();
5039dede
AK
2631 }
2632 return bRet;
2633}
2634
a3050773
VK
2635/**
2636 * Get DCI value via SNMP
2637 */
e320f8ce 2638DWORD Node::getItemFromSNMP(WORD port, const TCHAR *szParam, DWORD dwBufSize, TCHAR *szBuffer, int interpretRawValue)
5039dede
AK
2639{
2640 DWORD dwResult;
2641
2fd7144f 2642 if ((((m_dwDynamicFlags & NDF_SNMP_UNREACHABLE) || !(m_dwFlags & NF_IS_SNMP)) && (port == 0)) ||
6b2bb22c
VK
2643 (m_dwDynamicFlags & NDF_UNREACHABLE) ||
2644 (m_dwFlags & NF_DISABLE_SNMP))
5039dede
AK
2645 {
2646 dwResult = SNMP_ERR_COMM;
2647 }
2648 else
2649 {
2650 SNMP_Transport *pTransport;
2651
65e2005b 2652 pTransport = createSnmpTransport(port);
803d47be
VK
2653 if (pTransport != NULL)
2654 {
e320f8ce
VK
2655 if (interpretRawValue == SNMP_RAWTYPE_NONE)
2656 {
2657 dwResult = SnmpGet(m_snmpVersion, pTransport, szParam, NULL, 0, szBuffer, dwBufSize, SG_PSTRING_RESULT);
2658 }
2659 else
2660 {
2661 BYTE rawValue[1024];
2662 memset(rawValue, 0, 1024);
2663 dwResult = SnmpGet(m_snmpVersion, pTransport, szParam, NULL, 0, rawValue, 1024, SG_RAW_RESULT);
2664 if (dwResult == SNMP_ERR_SUCCESS)
2665 {
2666 switch(interpretRawValue)
2667 {
2668 case SNMP_RAWTYPE_INT32:
2669 _sntprintf(szBuffer, dwBufSize, _T("%d"), ntohl(*((LONG *)rawValue)));
2670 break;
2671 case SNMP_RAWTYPE_UINT32:
2672 _sntprintf(szBuffer, dwBufSize, _T("%u"), ntohl(*((DWORD *)rawValue)));
2673 break;
2674 case SNMP_RAWTYPE_INT64:
2675 _sntprintf(szBuffer, dwBufSize, INT64_FMT, ntohq(*((INT64 *)rawValue)));
2676 break;
2677 case SNMP_RAWTYPE_UINT64:
2678 _sntprintf(szBuffer, dwBufSize, UINT64_FMT, ntohq(*((QWORD *)rawValue)));
2679 break;
2680 case SNMP_RAWTYPE_DOUBLE:
2681 _sntprintf(szBuffer, dwBufSize, _T("%f"), ntohd(*((double *)rawValue)));
2682 break;
2683 case SNMP_RAWTYPE_IP_ADDR:
2684 IpToStr(ntohl(*((DWORD *)rawValue)), szBuffer);
2685 break;
2686 case SNMP_RAWTYPE_MAC_ADDR:
2687 MACToStr(rawValue, szBuffer);
2688 break;
2689 default:
2690 szBuffer[0] = 0;
2691 break;
2692 }
2693 }
2694 }
803d47be
VK
2695 delete pTransport;
2696 }
2697 else
2698 {
2699 dwResult = SNMP_ERR_COMM;
2700 }
5039dede 2701 }
35f836fe 2702 DbgPrintf(7, _T("Node(%s)->GetItemFromSNMP(%s): dwResult=%d"), m_szName, szParam, dwResult);
5039dede
AK
2703 return (dwResult == SNMP_ERR_SUCCESS) ? DCE_SUCCESS :
2704 ((dwResult == SNMP_ERR_NO_OBJECT) ? DCE_NOT_SUPPORTED : DCE_COMM_ERROR);
2705}
2706
6fd6de0a
VK
2707/**
2708 * Get item's value via SNMP from CheckPoint's agent
2709 */
2710DWORD Node::getItemFromCheckPointSNMP(const TCHAR *szParam, DWORD dwBufSize, TCHAR *szBuffer)
5039dede
AK
2711{
2712 DWORD dwResult;
2713
2714 if ((m_dwDynamicFlags & NDF_CPSNMP_UNREACHABLE) ||
2715 (m_dwDynamicFlags & NDF_UNREACHABLE))
2716 {
2717 dwResult = SNMP_ERR_COMM;
2718 }
2719 else
2720 {
2721 SNMP_Transport *pTransport;
2722
2723 pTransport = new SNMP_UDPTransport;
c4366266 2724 ((SNMP_UDPTransport *)pTransport)->createUDPTransport(NULL, htonl(m_dwIpAddr), CHECKPOINT_SNMP_PORT);
f42b8099 2725 dwResult = SnmpGet(SNMP_VERSION_1, pTransport, szParam, NULL, 0, szBuffer, dwBufSize, SG_STRING_RESULT);
5039dede
AK
2726 delete pTransport;
2727 }
35f836fe 2728 DbgPrintf(7, _T("Node(%s)->GetItemFromCheckPointSNMP(%s): dwResult=%d"), m_szName, szParam, dwResult);
5039dede
AK
2729 return (dwResult == SNMP_ERR_SUCCESS) ? DCE_SUCCESS :
2730 ((dwResult == SNMP_ERR_NO_OBJECT) ? DCE_NOT_SUPPORTED : DCE_COMM_ERROR);
2731}
2732
6fd6de0a
VK
2733/**
2734 * Get item's value via native agent
2735 */
2736DWORD Node::getItemFromAgent(const TCHAR *szParam, DWORD dwBufSize, TCHAR *szBuffer)
5039dede 2737{
912b994d 2738 DWORD dwError = ERR_NOT_CONNECTED, dwResult = DCE_COMM_ERROR;
5039dede
AK
2739 DWORD dwTries = 3;
2740
2741 if ((m_dwDynamicFlags & NDF_AGENT_UNREACHABLE) ||
2fd7144f
VK
2742 (m_dwDynamicFlags & NDF_UNREACHABLE) ||
2743 (m_dwFlags & NF_DISABLE_NXCP) ||
2744 !(m_dwFlags & NF_IS_NATIVE_AGENT))
5039dede
AK
2745 return DCE_COMM_ERROR;
2746
7c521895 2747 agentLock();
5039dede
AK
2748
2749 // Establish connection if needed
2750 if (m_pAgentConnection == NULL)
7c521895 2751 if (!connectToAgent())
5039dede
AK
2752 goto end_loop;
2753
2754 // Get parameter from agent
2755 while(dwTries-- > 0)
2756 {
4687826e 2757 dwError = m_pAgentConnection->getParameter(szParam, dwBufSize, szBuffer);
5039dede
AK
2758 switch(dwError)
2759 {
2760 case ERR_SUCCESS:
2761 dwResult = DCE_SUCCESS;
2762 goto end_loop;
2763 case ERR_UNKNOWN_PARAMETER:
2764 dwResult = DCE_NOT_SUPPORTED;
2765 goto end_loop;
2766 case ERR_NOT_CONNECTED:
2767 case ERR_CONNECTION_BROKEN:
7c521895 2768 if (!connectToAgent())
5039dede
AK
2769 goto end_loop;
2770 break;
2771 case ERR_REQUEST_TIMEOUT:
2772 // Reset connection to agent after timeout
2773 DbgPrintf(7, _T("Node(%s)->GetItemFromAgent(%s): timeout; resetting connection to agent..."), m_szName, szParam);
2774 delete_and_null(m_pAgentConnection);
7c521895 2775 if (!connectToAgent())
5039dede
AK
2776 goto end_loop;
2777 DbgPrintf(7, _T("Node(%s)->GetItemFromAgent(%s): connection to agent restored successfully"), m_szName, szParam);
2778 break;
2779 }
2780 }
2781
2782end_loop:
7c521895 2783 agentUnlock();
adac28be
VK
2784 DbgPrintf(7, _T("Node(%s)->GetItemFromAgent(%s): dwError=%d dwResult=%d"), m_szName, szParam, dwError, dwResult);
2785 return dwResult;
2786}
2787
d51f2182
VK
2788/**
2789 * Get table from agent
2790 */
adac28be
VK
2791DWORD Node::getTableFromAgent(const TCHAR *name, Table **table)
2792{
2793 DWORD dwError = ERR_NOT_CONNECTED, dwResult = DCE_COMM_ERROR;
2794 DWORD dwTries = 3;
2795
2796 *table = NULL;
2797
2798 if ((m_dwDynamicFlags & NDF_AGENT_UNREACHABLE) ||
2799 (m_dwDynamicFlags & NDF_UNREACHABLE) ||
2800 (m_dwFlags & NF_DISABLE_NXCP) ||
2801 !(m_dwFlags & NF_IS_NATIVE_AGENT))
2802 return DCE_COMM_ERROR;
2803
2804 agentLock();
2805
2806 // Establish connection if needed
2807 if (m_pAgentConnection == NULL)
2808 if (!connectToAgent())
2809 goto end_loop;
2810
2811 // Get parameter from agent
2812 while(dwTries-- > 0)
2813 {
2814 dwError = m_pAgentConnection->getTable(name, table);
2815 switch(dwError)
2816 {
2817 case ERR_SUCCESS:
2818 dwResult = DCE_SUCCESS;
2819 goto end_loop;
2820 case ERR_UNKNOWN_PARAMETER:
2821 dwResult = DCE_NOT_SUPPORTED;
2822 goto end_loop;
2823 case ERR_NOT_CONNECTED:
2824 case ERR_CONNECTION_BROKEN:
2825 if (!connectToAgent())
2826 goto end_loop;
2827 break;
2828 case ERR_REQUEST_TIMEOUT:
2829 // Reset connection to agent after timeout
2830 DbgPrintf(7, _T("Node(%s)->getTableFromAgent(%s): timeout; resetting connection to agent..."), m_szName, name);
2831 delete_and_null(m_pAgentConnection);
2832 if (!connectToAgent())
2833 goto end_loop;
2834 DbgPrintf(7, _T("Node(%s)->getTableFromAgent(%s): connection to agent restored successfully"), m_szName, name);
2835 break;
2836 }
2837 }
2838
2839end_loop:
2840 agentUnlock();
2841 DbgPrintf(7, _T("Node(%s)->getTableFromAgent(%s): dwError=%d dwResult=%d"), m_szName, name, dwError, dwResult);
5039dede
AK
2842 return dwResult;
2843}
2844
6fd6de0a 2845/**
d51f2182
VK
2846 * Get list from agent
2847 */
2848DWORD Node::getListFromAgent(const TCHAR *name, StringList **list)
2849{
2850 DWORD dwError = ERR_NOT_CONNECTED, dwResult = DCE_COMM_ERROR;
2851 DWORD i, dwTries = 3;
2852
2853 *list = NULL;
2854
2855 if ((m_dwDynamicFlags & NDF_AGENT_UNREACHABLE) ||
2856 (m_dwDynamicFlags & NDF_UNREACHABLE) ||
2857 (m_dwFlags & NF_DISABLE_NXCP) ||
2858 !(m_dwFlags & NF_IS_NATIVE_AGENT))
2859 return DCE_COMM_ERROR;
2860
2861 agentLock();
2862
2863 // Establish connection if needed
2864 if (m_pAgentConnection == NULL)
2865 if (!connectToAgent())
2866 goto end_loop;
2867
2868 // Get parameter from agent
2869 while(dwTries-- > 0)
2870 {
2871 dwError = m_pAgentConnection->getList(name);
2872 switch(dwError)
2873 {
2874 case ERR_SUCCESS:
2875 dwResult = DCE_SUCCESS;
2876 *list = new StringList;
2877 for(i = 0; i < m_pAgentConnection->getNumDataLines(); i++)
2878 (*list)->add(m_pAgentConnection->getDataLine(i));
2879 goto end_loop;
2880 case ERR_UNKNOWN_PARAMETER:
2881 dwResult = DCE_NOT_SUPPORTED;
2882 goto end_loop;
2883 case ERR_NOT_CONNECTED:
2884 case ERR_CONNECTION_BROKEN:
2885 if (!connectToAgent())
2886 goto end_loop;
2887 break;
2888 case ERR_REQUEST_TIMEOUT:
2889 // Reset connection to agent after timeout
2890 DbgPrintf(7, _T("Node(%s)->getListFromAgent(%s): timeout; resetting connection to agent..."), m_szName, name);
2891 delete_and_null(m_pAgentConnection);
2892 if (!connectToAgent())
2893 goto end_loop;
2894 DbgPrintf(7, _T("Node(%s)->getListFromAgent(%s): connection to agent restored successfully"), m_szName, name);
2895 break;
2896 }
2897 }
2898
2899end_loop:
2900 agentUnlock();
2901 DbgPrintf(7, _T("Node(%s)->getListFromAgent(%s): dwError=%d dwResult=%d"), m_szName, name, dwError, dwResult);
2902 return dwResult;
2903}
2904
2905/**
6fd6de0a
VK
2906 * Get value for server's internal parameter
2907 */
2908DWORD Node::getInternalItem(const TCHAR *param, DWORD bufSize, TCHAR *buffer)
5039dede 2909{
6fd6de0a 2910 DWORD rc = DataCollectionTarget::getInternalItem(param, bufSize, buffer);
90a4971c
VK
2911 if (rc == DCE_SUCCESS)
2912 return DCE_SUCCESS;
2913 rc = DCE_SUCCESS;
5039dede 2914
eb0c0312 2915 if (MatchString(_T("Net.IP.NextHop(*)"), param, FALSE))
20c842ee
VK
2916 {
2917 if ((m_dwFlags & NF_IS_NATIVE_AGENT) || (m_dwFlags & NF_IS_SNMP))
2918 {
2919 TCHAR arg[256] = _T("");
2920 DWORD destAddr, nextHop, ifIndex;
2921 BOOL isVpn;
2922
6fd6de0a 2923 AgentGetParameterArg(param, 1, arg, 256);
20c842ee
VK
2924 destAddr = ntohl(_t_inet_addr(arg));
2925 if ((destAddr > 0) && (destAddr < 0xE0000000))
2926 {
2927 if (getNextHop(m_dwIpAddr, destAddr, &nextHop, &ifIndex, &isVpn))
2928 {
6fd6de0a 2929 IpToStr(nextHop, buffer);
20c842ee
VK
2930 }
2931 else
2932 {
6fd6de0a 2933 _tcscpy(buffer, _T("UNREACHABLE"));
20c842ee
VK
2934 }
2935 }
2936 else
2937 {
6fd6de0a 2938 rc = DCE_NOT_SUPPORTED;
20c842ee
VK
2939 }
2940 }
2941 else
2942 {
6fd6de0a 2943 rc = DCE_NOT_SUPPORTED;
20c842ee
VK
2944 }
2945 }
6fd6de0a 2946 else if (!_tcsicmp(param, _T("AgentStatus")))
5039dede
AK
2947 {
2948 if (m_dwFlags & NF_IS_NATIVE_AGENT)
2949 {
6fd6de0a
VK
2950 buffer[0] = (m_dwDynamicFlags & NDF_AGENT_UNREACHABLE) ? _T('1') : _T('0');
2951 buffer[1] = 0;
5039dede
AK
2952 }
2953 else
2954 {
6fd6de0a 2955 rc = DCE_NOT_SUPPORTED;
5039dede
AK
2956 }
2957 }
2958 else if (m_dwFlags & NF_IS_LOCAL_MGMT)
2959 {
6fd6de0a 2960 if (!_tcsicmp(param, _T("Server.AverageDCPollerQueueSize")))
5039dede 2961 {
6fd6de0a 2962 _sntprintf(buffer, bufSize, _T("%f"), g_dAvgPollerQueueSize);
5039dede 2963 }
6fd6de0a 2964 else if (!_tcsicmp(param, _T("Server.AverageDBWriterQueueSize")))
5039dede 2965 {
6fd6de0a 2966 _sntprintf(buffer, bufSize, _T("%f"), g_dAvgDBAndIDataWriterQueueSize);
cf084617 2967 }
6fd6de0a 2968 else if (!_tcsicmp(param, _T("Server.AverageDBWriterQueueSize.Other")))
cf084617 2969 {
6fd6de0a 2970 _sntprintf(buffer, bufSize, _T("%f"), g_dAvgDBWriterQueueSize);
5039dede 2971 }
6fd6de0a 2972 else if (!_tcsicmp(param, _T("Server.AverageDBWriterQueueSize.IData")))
cf084617 2973 {
6fd6de0a 2974 _sntprintf(buffer, bufSize, _T("%f"), g_dAvgIDataWriterQueueSize);
cf084617 2975 }
6fd6de0a 2976 else if (!_tcsicmp(param, _T("Server.AverageStatusPollerQueueSize")))
5039dede 2977 {
6fd6de0a 2978 _sntprintf(buffer, bufSize, _T("%f"), g_dAvgStatusPollerQueueSize);
5039dede 2979 }
6fd6de0a 2980 else if (!_tcsicmp(param, _T("Server.AverageConfigurationPollerQueueSize")))
5039dede 2981 {
6fd6de0a 2982 _sntprintf(buffer, bufSize, _T("%f"), g_dAvgConfigPollerQueueSize);
5039dede 2983 }
6fd6de0a 2984 else if (!_tcsicmp(param, _T("Server.AverageDCIQueuingTime")))
5039dede 2985 {
6fd6de0a 2986 _sntprintf(buffer, bufSize, _T("%u"), g_dwAvgDCIQueuingTime);
5039dede 2987 }
6fd6de0a 2988 else if (!_tcsicmp(param, _T("Server.TotalEventsProcessed")))
5039dede 2989 {
6fd6de0a 2990 _sntprintf(buffer, bufSize, INT64_FMT, g_totalEventsProcessed);
5039dede
AK
2991 }
2992 else
2993 {
6fd6de0a 2994 rc = DCE_NOT_SUPPORTED;
5039dede
AK
2995 }
2996 }
2997 else
2998 {
6fd6de0a 2999 rc = DCE_NOT_SUPPORTED;
5039dede
AK
3000 }
3001
6fd6de0a 3002 return rc;
5039dede
AK
3003}
3004
6fd6de0a
VK
3005/**
3006 * Translate DCI error code into RCC
3007 */
adac28be
VK
3008static DWORD RCCFromDCIError(DWORD error)
3009{
3188558a 3010 switch(error)
adac28be
VK
3011 {
3012 case DCE_SUCCESS:
3013 return RCC_SUCCESS;
3014 case DCE_COMM_ERROR:
3015 return RCC_COMM_FAILURE;
3016 case DCE_NOT_SUPPORTED:
3017 return RCC_DCI_NOT_SUPPORTED;
3018 default:
3019 return RCC_SYSTEM_FAILURE;
3020 }
3021}
3022
6fd6de0a
VK
3023/**
3024 * Get item's value for client
3025 */
adac28be 3026DWORD Node::getItemForClient(int iOrigin, const TCHAR *pszParam, TCHAR *pszBuffer, DWORD dwBufSize)
5039dede
AK
3027{
3028 DWORD dwResult = 0, dwRetCode;
3029
3030 // Get data from node
3031 switch(iOrigin)
3032 {
3033 case DS_INTERNAL:
6fd6de0a 3034 dwRetCode = getInternalItem(pszParam, dwBufSize, pszBuffer);
5039dede
AK
3035 break;
3036 case DS_NATIVE_AGENT:
6fd6de0a 3037 dwRetCode = getItemFromAgent(pszParam, dwBufSize, pszBuffer);
5039dede
AK
3038 break;
3039 case DS_SNMP_AGENT:
e320f8ce 3040 dwRetCode = getItemFromSNMP(0, pszParam, dwBufSize, pszBuffer, SNMP_RAWTYPE_NONE);
5039dede
AK
3041 break;
3042 case DS_CHECKPOINT_AGENT:
6fd6de0a 3043 dwRetCode = getItemFromCheckPointSNMP(pszParam, dwBufSize, pszBuffer);
5039dede
AK
3044 break;
3045 default:
3046 dwResult = RCC_INVALID_ARGUMENT;
3047 break;
3048 }
3049
3050 // Translate return code to RCC
3051 if (dwResult != RCC_INVALID_ARGUMENT)
adac28be 3052 dwResult = RCCFromDCIError(dwRetCode);
5039dede
AK
3053
3054 return dwResult;
3055}
3056
d51f2182
VK
3057/**
3058 * Get table for client
3059 */
3188558a 3060DWORD Node::getTableForClient(const TCHAR *name, Table **table)
adac28be 3061{
3188558a 3062 DWORD dwRetCode = getTableFromAgent(name, table);
adac28be
VK
3063 return RCCFromDCIError(dwRetCode);
3064}
3065
6fd6de0a
VK
3066/**
3067 * Create CSCP message with object's data
3068 */
5039dede
AK
3069void Node::CreateMessage(CSCPMessage *pMsg)
3070{
6fd6de0a 3071 DataCollectionTarget::CreateMessage(pMsg);
5a7d6a10 3072 pMsg->SetVariable(VID_PRIMARY_NAME, m_primaryName);
5039dede 3073 pMsg->SetVariable(VID_FLAGS, m_dwFlags);
fa6173b9 3074 pMsg->SetVariable(VID_RUNTIME_FLAGS, m_dwDynamicFlags);
5039dede
AK
3075 pMsg->SetVariable(VID_AGENT_PORT, m_wAgentPort);
3076 pMsg->SetVariable(VID_AUTH_METHOD, m_wAuthMethod);
3077 pMsg->SetVariable(VID_SHARED_SECRET, m_szSharedSecret);
35f836fe
VK
3078 pMsg->SetVariableFromMBString(VID_SNMP_AUTH_OBJECT, m_snmpSecurity->getCommunity());
3079 pMsg->SetVariableFromMBString(VID_SNMP_AUTH_PASSWORD, m_snmpSecurity->getAuthPassword());
3080 pMsg->SetVariableFromMBString(VID_SNMP_PRIV_PASSWORD, m_snmpSecurity->getPrivPassword());
4b7d8903 3081 pMsg->SetVariable(VID_SNMP_USM_METHODS, (WORD)((WORD)m_snmpSecurity->getAuthMethod() | ((WORD)m_snmpSecurity->getPrivMethod() << 8)));
5039dede 3082 pMsg->SetVariable(VID_SNMP_OID, m_szObjectId);
65e2005b 3083 pMsg->SetVariable(VID_SNMP_PORT, m_wSNMPPort);
5d2c5741 3084 pMsg->SetVariable(VID_SNMP_VERSION, (WORD)m_snmpVersion);
5039dede
AK
3085 pMsg->SetVariable(VID_AGENT_VERSION, m_szAgentVersion);
3086 pMsg->SetVariable(VID_PLATFORM_NAME, m_szPlatformName);
3087 pMsg->SetVariable(VID_POLLER_NODE_ID, m_dwPollerNode);
52c31148 3088 pMsg->SetVariable(VID_ZONE_ID, m_zoneId);
e41fced3 3089 pMsg->SetVariable(VID_AGENT_PROXY, m_dwProxyNode);
5039dede
AK
3090 pMsg->SetVariable(VID_SNMP_PROXY, m_dwSNMPProxy);
3091 pMsg->SetVariable(VID_REQUIRED_POLLS, (WORD)m_iRequiredPollCount);
0ecc2200
VK
3092 pMsg->SetVariable(VID_SYS_NAME, CHECK_NULL_EX(m_sysName));
3093 pMsg->SetVariable(VID_SYS_DESCRIPTION, CHECK_NULL_EX(m_sysDescription));
3eab63f2 3094 pMsg->SetVariable(VID_BRIDGE_BASE_ADDRESS, m_baseBridgeAddress, 6);
0ecc2200
VK
3095 if (m_lldpNodeId != NULL)
3096 pMsg->SetVariable(VID_LLDP_NODE_ID, m_lldpNodeId);
5039dede 3097 pMsg->SetVariable(VID_USE_IFXTABLE, (WORD)m_nUseIfXTable);
0d75ea88
VK
3098 if (m_vrrpInfo != NULL)
3099 {
3100 pMsg->SetVariable(VID_VRRP_VERSION, (WORD)m_vrrpInfo->getVersion());
3101 pMsg->SetVariable(VID_VRRP_VR_COUNT, (WORD)m_vrrpInfo->getSize());
3102 }
9796ce45
VK
3103 if (m_driver != NULL)
3104 {
3105 pMsg->SetVariable(VID_DRIVER_NAME, m_driver->getName());
3106 pMsg->SetVariable(VID_DRIVER_VERSION, m_driver->getVersion());
3107 }
5039dede
AK
3108}
3109
a3050773
VK
3110/**
3111 * Modify object from NXCP message
3112 */
5039dede
AK
3113DWORD Node::ModifyFromMessage(CSCPMessage *pRequest, BOOL bAlreadyLocked)
3114{
3115 if (!bAlreadyLocked)
3116 LockData();
3117
3118 // Change primary IP address
3119 if (pRequest->IsVariableExist(VID_IP_ADDRESS))
3120 {
3121 DWORD i, dwIpAddr;
3122
3123 dwIpAddr = pRequest->GetVariableLong(VID_IP_ADDRESS);
3124
3125 // Check if received IP address is one of node's interface addresses
3126 LockChildList(FALSE);
3127 for(i = 0; i < m_dwChildCount; i++)
3128 if ((m_pChildList[i]->Type() == OBJECT_INTERFACE) &&
3129 (m_pChildList[i]->IpAddr() == dwIpAddr))
3130 break;
3131 UnlockChildList();
3132 if (i == m_dwChildCount)
3133 {
3134 UnlockData();
3135 return RCC_INVALID_IP_ADDR;
3136 }
3137
5039dede 3138 m_dwIpAddr = dwIpAddr;
5a7d6a10
VK
3139
3140 // Update primary name if it is not set with the same message
3141 if (!pRequest->IsVariableExist(VID_PRIMARY_NAME))
3142 {
3143 IpToStr(m_dwIpAddr, m_primaryName);
3144 }
a3050773
VK
3145
3146 agentLock();
3147 delete_and_null(m_pAgentConnection);
3148 agentUnlock();
3149 }
5039dede 3150
fed33789 3151 // Change primary host name
5a7d6a10
VK
3152 if (pRequest->IsVariableExist(VID_PRIMARY_NAME))
3153 {
3154 pRequest->GetVariableStr(VID_PRIMARY_NAME, m_primaryName, MAX_DNS_NAME);
3155 m_dwDynamicFlags |= NDF_FORCE_CONFIGURATION_POLL | NDF_RECHECK_CAPABILITIES;
3156 }
3157
5039dede
AK
3158 // Poller node ID
3159 if (pRequest->IsVariableExist(VID_POLLER_NODE_ID))
3160 {
3161 DWORD dwNodeId;
3162 NetObj *pObject;
3163
3164 dwNodeId = pRequest->GetVariableLong(VID_POLLER_NODE_ID);
41f9a6dc
VK
3165 if (dwNodeId != 0)
3166 {
3167 pObject = FindObjectById(dwNodeId);
5039dede 3168
41f9a6dc
VK
3169 // Check if received id is a valid node id
3170 if (pObject == NULL)
3171 {
3172 UnlockData();
3173 return RCC_INVALID_OBJECT_ID;
3174 }
3175 if (pObject->Type() != OBJECT_NODE)
3176 {
3177 UnlockData();
3178 return RCC_INVALID_OBJECT_ID;
3179 }
3180 }
3181 m_dwPollerNode = dwNodeId;
5039dede
AK
3182 }
3183
3184 // Change listen port of native agent
3185 if (pRequest->IsVariableExist(VID_AGENT_PORT))
3186 m_wAgentPort = pRequest->GetVariableShort(VID_AGENT_PORT);
3187
3188 // Change authentication method of native agent
3189 if (pRequest->IsVariableExist(VID_AUTH_METHOD))
3190 m_wAuthMethod = pRequest->GetVariableShort(VID_AUTH_METHOD);
3191
3192 // Change shared secret of native agent
3193 if (pRequest->IsVariableExist(VID_SHARED_SECRET))
3194 pRequest->GetVariableStr(VID_SHARED_SECRET, m_szSharedSecret, MAX_SECRET_LENGTH);
3195
3196 // Change SNMP protocol version
3197 if (pRequest->IsVariableExist(VID_SNMP_VERSION))
14b98b3a 3198 {
5d2c5741 3199 m_snmpVersion = pRequest->GetVariableShort(VID_SNMP_VERSION);
14b98b3a
VK
3200 m_snmpSecurity->setSecurityModel((m_snmpVersion == SNMP_VERSION_3) ? SNMP_SECURITY_MODEL_USM : SNMP_SECURITY_MODEL_V2C);
3201 }
5039dede 3202
65e2005b
VK
3203 // Change SNMP port
3204 if (pRequest->IsVariableExist(VID_SNMP_PORT))
3205 m_wSNMPPort = pRequest->GetVariableShort(VID_SNMP_PORT);
3206
4b7d8903
VK
3207 // Change SNMP authentication data
3208 if (pRequest->IsVariableExist(VID_SNMP_AUTH_OBJECT))
5d2c5741 3209 {
35f836fe
VK
3210 char mbBuffer[256];
3211
3212 pRequest->GetVariableStrA(VID_SNMP_AUTH_OBJECT, mbBuffer, 256);
3213 m_snmpSecurity->setAuthName(mbBuffer);
4b7d8903 3214
35f836fe
VK
3215 pRequest->GetVariableStrA(VID_SNMP_AUTH_PASSWORD, mbBuffer, 256);
3216 m_snmpSecurity->setAuthPassword(mbBuffer);
4b7d8903 3217
35f836fe
VK
3218 pRequest->GetVariableStrA(VID_SNMP_PRIV_PASSWORD, mbBuffer, 256);
3219 m_snmpSecurity->setPrivPassword(mbBuffer);
4b7d8903
VK
3220
3221 WORD methods = pRequest->GetVariableShort(VID_SNMP_USM_METHODS);
3222 m_snmpSecurity->setAuthMethod((int)(methods & 0xFF));
3223 m_snmpSecurity->setPrivMethod((int)(methods >> 8));
5d2c5741 3224 }
5039dede
AK
3225
3226 // Change proxy node
e41fced3
VK
3227 if (pRequest->IsVariableExist(VID_AGENT_PROXY))
3228 m_dwProxyNode = pRequest->GetVariableLong(VID_AGENT_PROXY);
5039dede
AK
3229
3230 // Change SNMP proxy node
3231 if (pRequest->IsVariableExist(VID_SNMP_PROXY))
3232 m_dwSNMPProxy = pRequest->GetVariableLong(VID_SNMP_PROXY);
3233
3234 // Number of required polls
3235 if (pRequest->IsVariableExist(VID_REQUIRED_POLLS))
3236 m_iRequiredPollCount = (int)pRequest->GetVariableShort(VID_REQUIRED_POLLS);
3237
3238 // Enable/disable usage of ifXTable
3239 if (pRequest->IsVariableExist(VID_USE_IFXTABLE))
3240 m_nUseIfXTable = (BYTE)pRequest->GetVariableShort(VID_USE_IFXTABLE);
3241
3242 // Change flags
3243 if (pRequest->IsVariableExist(VID_FLAGS))
3244 {
3245 m_dwFlags &= NF_SYSTEM_FLAGS;
3246 m_dwFlags |= pRequest->GetVariableLong(VID_FLAGS) & NF_USER_FLAGS;
3247 }
3248
6fd6de0a 3249 return DataCollectionTarget::ModifyFromMessage(pRequest, TRUE);
5039dede
AK
3250}
3251
6fd6de0a
VK
3252/**
3253 * Wakeup node using magic packet
3254 */
7c521895 3255DWORD Node::wakeUp()
5039dede
AK
3256{
3257 DWORD i, dwResult = RCC_NO_WOL_INTERFACES;
3258
3259 LockChildList(FALSE);
3260
3261 for(i = 0; i < m_dwChildCount; i++)
3262 if ((m_pChildList[i]->Type() == OBJECT_INTERFACE) &&
3263 (m_pChildList[i]->Status() != STATUS_UNMANAGED) &&
3264 (m_pChildList[i]->IpAddr() != 0))
3265 {
7c521895 3266 dwResult = ((Interface *)m_pChildList[i])->wakeUp();
5039dede
AK
3267 break;
3268 }
3269
3270 UnlockChildList();
3271 return dwResult;
3272}
3273
6fd6de0a
VK
3274/**
3275 * Get status of interface with given index from SNMP agent
3276 */
1e6b68a6 3277void Node::getInterfaceStatusFromSNMP(SNMP_Transport *pTransport, DWORD dwIndex, int *adminState, int *operState)
5039dede 3278{
1e6b68a6 3279 SnmpGetInterfaceStatus(m_snmpVersion, pTransport, dwIndex, adminState, operState);
5039dede
AK
3280}
3281
6fd6de0a
VK
3282/**
3283 * Get status of interface with given index from native agent
3284 */
1e6b68a6 3285void Node::getInterfaceStatusFromAgent(DWORD dwIndex, int *adminState, int *operState)
5039dede 3286{
35f836fe 3287 TCHAR szParam[128], szBuffer[32];
5039dede
AK
3288
3289 // Get administrative status
35f836fe 3290 _sntprintf(szParam, 128, _T("Net.Interface.AdminStatus(%u)"), dwIndex);
6fd6de0a 3291 if (getItemFromAgent(szParam, 32, szBuffer) == DCE_SUCCESS)
5039dede 3292 {
1e6b68a6 3293 *adminState = _tcstol(szBuffer, NULL, 0);
5039dede 3294
1e6b68a6 3295 switch(*adminState)
5039dede 3296 {
1e6b68a6
VK
3297 case IF_ADMIN_STATE_TESTING:
3298 *operState = IF_OPER_STATE_UNKNOWN;
5039dede 3299 break;
1e6b68a6 3300 case IF_ADMIN_STATE_DOWN:
5039dede 3301 case 0: // Agents before 0.2.1 may return 0 instead of 2
1e6b68a6 3302 *operState = IF_OPER_STATE_DOWN;
5039dede 3303 break;
1e6b68a6 3304 case IF_ADMIN_STATE_UP: // Interface administratively up, check link state
35f836fe 3305 _sntprintf(szParam, 128, _T("Net.Interface.Link(%u)"), dwIndex);
6fd6de0a 3306 if (getItemFromAgent(szParam, 32, szBuffer) == DCE_SUCCESS)
5039dede 3307 {
1e6b68a6
VK
3308 DWORD dwLinkState = _tcstoul(szBuffer, NULL, 0);
3309 *operState = (dwLinkState == 0) ? IF_OPER_STATE_DOWN : IF_OPER_STATE_UP;
5039dede
AK
3310 }
3311 else
3312 {
1e6b68a6 3313 *operState = IF_OPER_STATE_UNKNOWN;
5039dede
AK
3314 }
3315 break;
3316 default:
1e6b68a6
VK
3317 *adminState = IF_ADMIN_STATE_UNKNOWN;
3318 *operState = IF_OPER_STATE_UNKNOWN;
5039dede
AK
3319 break;
3320 }
3321 }
3322 else
3323 {
1e6b68a6
VK
3324 *adminState = IF_ADMIN_STATE_UNKNOWN;
3325 *operState = IF_OPER_STATE_UNKNOWN;
5039dede 3326 }
5039dede
AK
3327}
3328
074498ac
VK
3329/**
3330 * Put list of supported parameters into NXCP message
3331 */
3332void Node::writeParamListToMessage(CSCPMessage *pMsg, WORD flags)
5039dede 3333{
5039dede 3334 LockData();
cc8ce218 3335
074498ac 3336 if ((flags & 0x01) && (m_paramList != NULL))
5039dede 3337 {
cc8ce218
VK
3338 pMsg->SetVariable(VID_NUM_PARAMETERS, (DWORD)m_paramList->size());
3339
3340 int i;
3341 DWORD dwId;
3342 for(i = 0, dwId = VID_PARAM_LIST_BASE; i < m_paramList->size(); i++)
5039dede 3343 {
cc8ce218
VK
3344 NXC_AGENT_PARAM *p = m_paramList->get(i);
3345 pMsg->SetVariable(dwId++, p->szName);
3346 pMsg->SetVariable(dwId++, p->szDescription);
3347 pMsg->SetVariable(dwId++, (WORD)p->iDataType);
5039dede 3348 }
cc8ce218 3349 DbgPrintf(6, _T("Node[%s]::writeParamListToMessage(): sending %d parameters"), m_szName, m_paramList->size());
5039dede
AK
3350 }
3351 else
3352 {
cc8ce218 3353 DbgPrintf(6, _T("Node[%s]::writeParamListToMessage(): m_paramList == NULL"), m_szName);
5039dede
AK
3354 pMsg->SetVariable(VID_NUM_PARAMETERS, (DWORD)0);
3355 }
cc8ce218 3356
074498ac 3357 if ((flags & 0x02) && (m_tableList != NULL))
cc8ce218
VK
3358 {
3359 pMsg->SetVariable(VID_NUM_TABLES, (DWORD)m_tableList->size());
3360
3361 int i;
3362 DWORD dwId;
3363 for(i = 0, dwId = VID_TABLE_LIST_BASE; i < m_tableList->size(); i++)
3364 {
3365 NXC_AGENT_TABLE *t = m_tableList->get(i);
3366 pMsg->SetVariable(dwId++, t->name);
3367 pMsg->SetVariable(dwId++, t->instanceColumn);
3368 pMsg->SetVariable(dwId++, t->description);
3369 }
3370 DbgPrintf(6, _T("Node[%s]::writeParamListToMessage(): sending %d tables"), m_szName, m_tableList->size());
3371 }
3372 else
3373 {
3374 DbgPrintf(6, _T("Node[%s]::writeParamListToMessage(): m_tableList == NULL"), m_szName);
3375 pMsg->SetVariable(VID_NUM_TABLES, (DWORD)0);
3376 }
3377
3378 UnlockData();
5039dede
AK
3379}
3380
074498ac
VK
3381/**
3382 * Open list of supported parameters for reading
3383 */
cc8ce218 3384void Node::openParamList(StructArray<NXC_AGENT_PARAM> **paramList)
5039dede
AK
3385{
3386 LockData();
cc8ce218 3387 *paramList = m_paramList;
5039dede
AK
3388}
3389
074498ac
VK
3390/**
3391 * Open list of supported tables for reading
3392 */
3393void Node::openTableList(StructArray<NXC_AGENT_TABLE> **tableList)
3394{
3395 LockData();