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