fixed visualisation problems in agent config editor
[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
76720a09
VK
1386/**
1387 * Perform configuration poll on node
1388 */
58b3e451 1389void Node::configurationPoll(ClientSession *pSession, DWORD dwRqId,
5039dede
AK
1390 int nPoller, DWORD dwNetMask)
1391{
46117060
VK
1392 if (m_dwDynamicFlags & NDF_DELETE_IN_PROGRESS)
1393 {
1394 if (dwRqId == 0)
1395 m_dwDynamicFlags &= ~NDF_QUEUED_FOR_CONFIG_POLL;
1396 return;
1397 }
1398
76720a09 1399 DWORD dwOldFlags = m_dwFlags;
35f836fe 1400 TCHAR szBuffer[4096];
5039dede 1401 SNMP_Transport *pTransport;
76720a09 1402 bool hasChanges = false;
5039dede 1403
35f836fe 1404 SetPollerInfo(nPoller, _T("wait for lock"));
7c521895 1405 pollerLock();
5039dede
AK
1406 m_pPollRequestor = pSession;
1407 SendPollerMsg(dwRqId, _T("Starting configuration poll for node %s\r\n"), m_szName);
35f836fe 1408 DbgPrintf(4, _T("Starting configuration poll for node %s (ID: %d)"), m_szName, m_dwId);
5039dede
AK
1409
1410 // Check for forced capabilities recheck
1411 if (m_dwDynamicFlags & NDF_RECHECK_CAPABILITIES)
1412 {
1413 m_dwFlags &= ~(NF_IS_NATIVE_AGENT | NF_IS_SNMP | NF_IS_CPSNMP |
0ecc2200 1414 NF_IS_BRIDGE | NF_IS_ROUTER | NF_IS_OSPF | NF_IS_PRINTER |
76720a09
VK
1415 NF_IS_CDP | NF_IS_LLDP | NF_IS_SONMP | NF_IS_VRRP | NF_HAS_VLANS |
1416 NF_IS_8021X | NF_IS_STP | NF_HAS_ENTITY_MIB | NF_HAS_IFXTABLE);
e4a64da2 1417 m_dwDynamicFlags &= ~NDF_CONFIGURATION_POLL_PASSED;
5039dede
AK
1418 m_szObjectId[0] = 0;
1419 m_szPlatformName[0] = 0;
1420 m_szAgentVersion[0] = 0;
0ecc2200
VK
1421 safe_free_and_null(m_sysDescription);
1422 safe_free_and_null(m_sysName);
1423 safe_free_and_null(m_lldpNodeId);
5039dede
AK
1424 }
1425
1426 // Check if node is marked as unreachable
1427 if ((m_dwDynamicFlags & NDF_UNREACHABLE) && !(m_dwDynamicFlags & NDF_RECHECK_CAPABILITIES))
1428 {
1429 SendPollerMsg(dwRqId, POLLER_WARNING _T("Node is marked as unreachable, configuration poll aborted\r\n"));
35f836fe 1430 DbgPrintf(4, _T("Node is marked as unreachable, configuration poll aborted"));
5039dede
AK
1431 m_tLastConfigurationPoll = time(NULL);
1432 }
1433 else
1434 {
5a7d6a10
VK
1435 updatePrimaryIpAddr();
1436
35f836fe 1437 SetPollerInfo(nPoller, _T("capability check"));
5039dede 1438 SendPollerMsg(dwRqId, _T("Checking node's capabilities...\r\n"));
5039dede 1439
76720a09
VK
1440 hasChanges = hasChanges || confPollAgent(dwRqId);
1441 hasChanges = hasChanges || confPollSnmp(dwRqId);
5039dede
AK
1442
1443 // Check for CheckPoint SNMP agent on port 260
35f836fe 1444 DbgPrintf(5, _T("ConfPoll(%s): checking for CheckPoint SNMP on port 260"), m_szName);
15d6f8c9 1445 if (!((m_dwFlags & NF_IS_CPSNMP) && (m_dwDynamicFlags & NDF_CPSNMP_UNREACHABLE)) && (m_dwIpAddr != 0))
5039dede
AK
1446 {
1447 pTransport = new SNMP_UDPTransport;
c4366266 1448 ((SNMP_UDPTransport *)pTransport)->createUDPTransport(NULL, htonl(m_dwIpAddr), CHECKPOINT_SNMP_PORT);
5d2c5741 1449 if (SnmpGet(SNMP_VERSION_1, pTransport,
f42b8099 1450 _T(".1.3.6.1.4.1.2620.1.1.10.0"), NULL, 0, szBuffer, 4096, 0) == SNMP_ERR_SUCCESS)
5039dede
AK
1451 {
1452 LockData();
1453 m_dwFlags |= NF_IS_CPSNMP | NF_IS_ROUTER;
1454 m_dwDynamicFlags &= ~NDF_CPSNMP_UNREACHABLE;
1455 UnlockData();
1456 SendPollerMsg(dwRqId, POLLER_INFO _T(" CheckPoint SNMP agent on port 260 is active\r\n"));
1457 }
1458 delete pTransport;
1459 }
1460
1461 // Generate event if node flags has been changed
1462 if (dwOldFlags != m_dwFlags)
1463 {
1464 PostEvent(EVENT_NODE_FLAGS_CHANGED, m_dwId, "xx", dwOldFlags, m_dwFlags);
76720a09 1465 hasChanges = true;
5039dede
AK
1466 }
1467
5039dede 1468 // Retrieve interface list
35f836fe 1469 SetPollerInfo(nPoller, _T("interface check"));
df8a4ca2 1470 SendPollerMsg(dwRqId, _T("Capability check finished\r\n"));
5039dede 1471
eec253a8 1472 if (updateInterfaceConfiguration(dwRqId, dwNetMask))
76720a09 1473 hasChanges = true;
5039dede
AK
1474
1475 m_tLastConfigurationPoll = time(NULL);
5039dede
AK
1476
1477 // Check node name
1478 SendPollerMsg(dwRqId, _T("Checking node name\r\n"));
76720a09 1479 DWORD dwAddr = ntohl(_t_inet_addr(m_szName));
5039dede
AK
1480 if ((g_dwFlags & AF_RESOLVE_NODE_NAMES) &&
1481 (dwAddr != INADDR_NONE) &&
1482 (dwAddr != INADDR_ANY) &&
58b3e451 1483 isMyIP(dwAddr))
5039dede
AK
1484 {
1485 SendPollerMsg(dwRqId, _T("Node name is an IP address and need to be resolved\r\n"));
35f836fe 1486 SetPollerInfo(nPoller, _T("resolving name"));
024c3faf 1487 if (resolveName(FALSE))
5039dede
AK
1488 {
1489 SendPollerMsg(dwRqId, POLLER_INFO _T("Node name resolved to %s\r\n"), m_szName);
76720a09 1490 hasChanges = true;
5039dede
AK
1491 }
1492 else
1493 {
1494 SendPollerMsg(dwRqId, POLLER_WARNING _T("Node name cannot be resolved\r\n"));
1495 }
1496 }
1497 else
1498 {
1499 if (g_dwFlags & AF_SYNC_NODE_NAMES_WITH_DNS)
1500 {
1501 SendPollerMsg(dwRqId, _T("Syncing node name with DNS\r\n"));
35f836fe 1502 SetPollerInfo(nPoller, _T("resolving name"));
024c3faf 1503 if (resolveName(TRUE))
5039dede
AK
1504 {
1505 SendPollerMsg(dwRqId, POLLER_INFO _T("Node name resolved to %s\r\n"), m_szName);
76720a09 1506 hasChanges = true;
5039dede
AK
1507 }
1508 }
1509 else
1510 {
1511 SendPollerMsg(dwRqId, _T("Node name is OK\r\n"));
1512 }
1513 }
1514
4d0c32f3
VK
1515 // Apply templates
1516 ApplySystemTemplates();
1517 ApplyUserTemplates();
1518
7c521895 1519 updateContainerMembership();
4d0c32f3 1520
df8a4ca2 1521 SendPollerMsg(dwRqId, _T("Finished configuration poll for node %s\r\n"), m_szName);
76720a09 1522 SendPollerMsg(dwRqId, _T("Node configuration was%schanged after poll\r\n"), hasChanges ? _T(" ") : _T(" not "));
e4a64da2
VK
1523
1524 m_dwDynamicFlags |= NDF_CONFIGURATION_POLL_PASSED;
4d0c32f3
VK
1525 }
1526
1824629a
VK
1527 // Execute hook script
1528 SetPollerInfo(nPoller, _T("hook"));
1529 executeHookScript(_T("ConfigurationPoll"));
1530
4d0c32f3 1531 // Finish configuration poll
35f836fe 1532 SetPollerInfo(nPoller, _T("cleanup"));
4d0c32f3
VK
1533 if (dwRqId == 0)
1534 m_dwDynamicFlags &= ~NDF_QUEUED_FOR_CONFIG_POLL;
1535 m_dwDynamicFlags &= ~NDF_RECHECK_CAPABILITIES;
7c521895 1536 pollerUnlock();
35f836fe 1537 DbgPrintf(4, _T("Finished configuration poll for node %s (ID: %d)"), m_szName, m_dwId);
4d0c32f3 1538
76720a09 1539 if (hasChanges)
4d0c32f3
VK
1540 {
1541 LockData();
1542 Modify();
1543 UnlockData();
1544 }
1545}
1546
76720a09
VK
1547/**
1548 * Configuration poll: check for NetXMS agent
1549 */
1550bool Node::confPollAgent(DWORD dwRqId)
1551{
1552 DbgPrintf(5, _T("ConfPoll(%s): checking for NetXMS agent Flags={%08X} DynamicFlags={%08X}"), m_szName, m_dwFlags, m_dwDynamicFlags);
1553 if (((m_dwFlags & NF_IS_NATIVE_AGENT) && (m_dwDynamicFlags & NDF_AGENT_UNREACHABLE)) ||
1554 (m_dwIpAddr == 0) || (m_dwFlags & NF_DISABLE_NXCP))
1555 return false;
1556
1557 bool hasChanges = false;
1558
1559 SendPollerMsg(dwRqId, _T(" Checking NetXMS agent...\r\n"));
1560 AgentConnection *pAgentConn = new AgentConnection(htonl(m_dwIpAddr), m_wAgentPort, m_wAuthMethod, m_szSharedSecret);
1561 setAgentProxy(pAgentConn);
1562 DbgPrintf(5, _T("ConfPoll(%s): checking for NetXMS agent - connecting"), m_szName);
1563 if (pAgentConn->connect(g_pServerKey))
1564 {
1565 DbgPrintf(5, _T("ConfPoll(%s): checking for NetXMS agent - connected"), m_szName);
1566 LockData();
1567 m_dwFlags |= NF_IS_NATIVE_AGENT;
1568 if (m_dwDynamicFlags & NDF_AGENT_UNREACHABLE)
1569 {
1570 m_dwDynamicFlags &= ~NDF_AGENT_UNREACHABLE;
1571 PostEvent(EVENT_AGENT_OK, m_dwId, NULL);
1572 SendPollerMsg(dwRqId, POLLER_INFO _T(" Connectivity with NetXMS agent restored\r\n"));
1573 }
1574 else
1575 {
1576 SendPollerMsg(dwRqId, POLLER_INFO _T(" NetXMS native agent is active\r\n"));
1577 }
1578 UnlockData();
4d0c32f3 1579
76720a09
VK
1580 TCHAR buffer[MAX_RESULT_LENGTH];
1581 if (pAgentConn->getParameter(_T("Agent.Version"), MAX_AGENT_VERSION_LEN, buffer) == ERR_SUCCESS)
1582 {
1583 LockData();
1584 if (_tcscmp(m_szAgentVersion, buffer))
1585 {
1586 _tcscpy(m_szAgentVersion, buffer);
1587 hasChanges = true;
1588 SendPollerMsg(dwRqId, _T(" NetXMS agent version changed to %s\r\n"), m_szAgentVersion);
1589 }
1590 UnlockData();
1591 }
1592
1593 if (pAgentConn->getParameter(_T("System.PlatformName"), MAX_PLATFORM_NAME_LEN, buffer) == ERR_SUCCESS)
1594 {
1595 LockData();
1596 if (_tcscmp(m_szPlatformName, buffer))
1597 {
1598 _tcscpy(m_szPlatformName, buffer);
1599 hasChanges = true;
1600 SendPollerMsg(dwRqId, _T(" Platform name changed to %s\r\n"), m_szPlatformName);
1601 }
1602 UnlockData();
1603 }
1604
1605 // Check IP forwarding status
1606 if (pAgentConn->getParameter(_T("Net.IP.Forwarding"), 16, buffer) == ERR_SUCCESS)
1607 {
1608 if (_tcstoul(buffer, NULL, 10) != 0)
1609 m_dwFlags |= NF_IS_ROUTER;
1610 else
1611 m_dwFlags &= ~NF_IS_ROUTER;
1612 }
1613
1614 // Get uname
1615 if (pAgentConn->getParameter(_T("System.Uname"), MAX_DB_STRING, buffer) == ERR_SUCCESS)
1616 {
1617 TranslateStr(buffer, _T("\r\n"), _T(" "));
1618 TranslateStr(buffer, _T("\n"), _T(" "));
1619 TranslateStr(buffer, _T("\r"), _T(" "));
1620 LockData();
1621 if ((m_sysDescription == NULL) || _tcscmp(m_sysDescription, buffer))
1622 {
1623 safe_free(m_sysDescription);
1624 m_sysDescription = _tcsdup(buffer);
1625 hasChanges = true;
1626 SendPollerMsg(dwRqId, _T(" System description changed to %s\r\n"), m_sysDescription);
1627 }
1628 UnlockData();
1629 }
1630
1631 StructArray<NXC_AGENT_PARAM> *plist;
1632 StructArray<NXC_AGENT_TABLE> *tlist;
1633 DWORD rcc = pAgentConn->getSupportedParameters(&plist, &tlist);
1634 if (rcc == ERR_SUCCESS)
1635 {
1636 LockData();
1637 delete m_paramList;
1638 delete m_tableList;
1639 m_paramList = plist;
1640 m_tableList = tlist;
1641 UnlockData();
1642 }
1643 else
1644 {
1645 DbgPrintf(5, _T("ConfPoll(%s): AgentConnection::getSupportedParameters() failed: rcc=%d"), m_szName, rcc);
1646 }
1647
1648 checkAgentPolicyBinding(pAgentConn);
1649
1650 pAgentConn->disconnect();
1651 }
1652 else
1653 {
1654 DbgPrintf(5, _T("ConfPoll(%s): checking for NetXMS agent - failed to connect"), m_szName);
1655 }
1656 delete pAgentConn;
1657 DbgPrintf(5, _T("ConfPoll(%s): checking for NetXMS agent - finished"), m_szName);
1658 return hasChanges;
1659}
1660
1661/**
1662 * SNMP walker callback which just counts number of varbinds
1663 */
1664static DWORD CountingSnmpWalkerCallback(DWORD version, SNMP_Variable *var, SNMP_Transport *transport, void *arg)
1665{
1666 (*((int *)arg))++;
1667 return SNMP_ERR_SUCCESS;
1668}
1669
1670/**
1671 * Configuration poll: check for SNMP
1672 */
1673bool Node::confPollSnmp(DWORD dwRqId)
1674{
1675 if (((m_dwFlags & NF_IS_SNMP) && (m_dwDynamicFlags & NDF_SNMP_UNREACHABLE)) ||
1676 (m_dwIpAddr == 0) || (m_dwFlags & NF_DISABLE_SNMP))
1677 return false;
1678
1679 bool hasChanges = false;
1680
1681 SendPollerMsg(dwRqId, _T(" Checking SNMP...\r\n"));
1682 DbgPrintf(5, _T("ConfPoll(%s): calling SnmpCheckCommSettings()"), m_szName);
1683 SNMP_Transport *pTransport = createSnmpTransport();
1684 if (pTransport == NULL)
1685 {
1686 DbgPrintf(5, _T("ConfPoll(%s): unable to create SNMP transport"), m_szName);
1687 return false;
1688 }
1689
1690 SNMP_SecurityContext *newCtx = SnmpCheckCommSettings(pTransport, &m_snmpVersion, m_snmpSecurity);
1691 if (newCtx != NULL)
1692 {
1693 LockData();
1694 delete m_snmpSecurity;
1695 m_snmpSecurity = newCtx;
1696 m_dwFlags |= NF_IS_SNMP;
1697 if (m_dwDynamicFlags & NDF_SNMP_UNREACHABLE)
1698 {
1699 m_dwDynamicFlags &= ~NDF_SNMP_UNREACHABLE;
1700 PostEvent(EVENT_SNMP_OK, m_dwId, NULL);
1701 SendPollerMsg(dwRqId, POLLER_INFO _T(" Connectivity with SNMP agent restored\r\n"));
1702 }
1703 UnlockData();
1704 SendPollerMsg(dwRqId, _T(" SNMP agent is active (version %s)\r\n"),
1705 (m_snmpVersion == SNMP_VERSION_3) ? _T("3") : ((m_snmpVersion == SNMP_VERSION_2C) ? _T("2c") : _T("1")));
1706
1707 TCHAR szBuffer[4096];
1708 if (SnmpGet(m_snmpVersion, pTransport,
1709 _T(".1.3.6.1.2.1.1.2.0"), NULL, 0, szBuffer, 4096, SG_STRING_RESULT) == SNMP_ERR_SUCCESS)
1710 {
1711 LockData();
1712 if (_tcscmp(m_szObjectId, szBuffer))
1713 {
1714 nx_strncpy(m_szObjectId, szBuffer, MAX_OID_LEN * 4);
1715 hasChanges = true;
1716 }
1717 UnlockData();
1718 }
1719
1720 // Get system description
1721 if (SnmpGet(m_snmpVersion, pTransport,
1722 _T(".1.3.6.1.2.1.1.1.0"), NULL, 0, szBuffer, MAX_DB_STRING, SG_STRING_RESULT) == SNMP_ERR_SUCCESS)
1723 {
1724 TranslateStr(szBuffer, _T("\r\n"), _T(" "));
1725 TranslateStr(szBuffer, _T("\n"), _T(" "));
1726 TranslateStr(szBuffer, _T("\r"), _T(" "));
1727 LockData();
1728 if ((m_sysDescription == NULL) || _tcscmp(m_sysDescription, szBuffer))
1729 {
1730 safe_free(m_sysDescription);
1731 m_sysDescription = _tcsdup(szBuffer);
1732 hasChanges = true;
1733 SendPollerMsg(dwRqId, _T(" System description changed to %s\r\n"), m_sysDescription);
1734 }
1735 UnlockData();
1736 }
1737
1738 // Select device driver
1739 NetworkDeviceDriver *driver = FindDriverForNode(this, pTransport);
1740 DbgPrintf(5, _T("ConfPoll(%s): selected device driver %s"), m_szName, driver->getName());
1741 LockData();
1742 if (driver != m_driver)
1743 {
1744 m_driver = driver;
1745 SendPollerMsg(dwRqId, _T(" New network device driver selected: %s\r\n"), m_driver->getName());
1746 }
1747 UnlockData();
1748
1749 // Allow driver to gather additional info
1750 m_driver->analyzeDevice(pTransport, m_szObjectId, &m_customAttributes);
1751
1752 // Get sysName
1753 if (SnmpGet(m_snmpVersion, pTransport,
1754 _T(".1.3.6.1.2.1.1.5.0"), NULL, 0, szBuffer, MAX_DB_STRING, SG_STRING_RESULT) == SNMP_ERR_SUCCESS)
1755 {
1756 LockData();
1757 if ((m_sysName == NULL) || _tcscmp(m_sysName, szBuffer))
1758 {
1759 safe_free(m_sysName);
1760 m_sysName = _tcsdup(szBuffer);
1761 hasChanges = true;
1762 SendPollerMsg(dwRqId, _T(" System name changed to %s\r\n"), m_sysName);
1763 }
1764 UnlockData();
1765 }
1766
1767 // Check IP forwarding
1768 if (CheckSNMPIntegerValue(pTransport, _T(".1.3.6.1.2.1.4.1.0"), 1))
1769 {
1770 LockData();
1771 m_dwFlags |= NF_IS_ROUTER;
1772 UnlockData();
1773 }
1774 else
1775 {
1776 LockData();
1777 m_dwFlags &= ~NF_IS_ROUTER;
1778 UnlockData();
1779 }
1780
1781 checkIfXTable(pTransport);
1782 checkBridgeMib(pTransport);
eec253a8 1783
76720a09
VK
1784 // Check for ENTITY-MIB support
1785 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)
1786 {
1787 LockData();
1788 m_dwFlags |= NF_HAS_ENTITY_MIB;
1789 UnlockData();
1790
1791 ComponentTree *components = BuildComponentTree(this, pTransport);
1792 LockData();
1793 if (m_components != NULL)
1794 m_components->decRefCount();
1795 m_components = components;
1796 UnlockData();
1797 }
1798 else
1799 {
1800 LockData();
1801 m_dwFlags &= ~NF_HAS_ENTITY_MIB;
1802 if (m_components != NULL)
1803 {
1804 m_components->decRefCount();
1805 m_components = NULL;
1806 }
1807 UnlockData();
1808 }
1809
1810 // Check for printer MIB support
1811 int count = 0;
1812 SnmpEnumerate(m_snmpVersion, pTransport, _T(".1.3.6.1.2.1.43.5.1.1.17"), CountingSnmpWalkerCallback, &count, FALSE);
1813 if (count > 0)
1814 {
1815 LockData();
1816 m_dwFlags |= NF_IS_PRINTER;
1817 UnlockData();
1818 }
1819 else
1820 {
1821 LockData();
1822 m_dwFlags &= ~NF_IS_PRINTER;
1823 UnlockData();
1824 }
1825
1826 // Check for CDP (Cisco Discovery Protocol) support
1827 if (CheckSNMPIntegerValue(pTransport, _T(".1.3.6.1.4.1.9.9.23.1.3.1.0"), 1))
1828 {
1829 LockData();
1830 m_dwFlags |= NF_IS_CDP;
1831 UnlockData();
1832 }
1833 else
1834 {
1835 LockData();
1836 m_dwFlags &= ~NF_IS_CDP;
1837 UnlockData();
1838 }
1839
1840 // Check for NDP (Nortel Discovery Protocol) support
1841 if (CheckSNMPIntegerValue(pTransport, _T(".1.3.6.1.4.1.45.1.6.13.1.2.0"), 1))
1842 {
1843 LockData();
1844 m_dwFlags |= NF_IS_NDP;
1845 UnlockData();
1846 }
1847 else
1848 {
1849 LockData();
1850 m_dwFlags &= ~NF_IS_NDP;
1851 UnlockData();
1852 }
1853
1854 // Check for LLDP (Link Layer Discovery Protocol) support
1855 if (SnmpGet(m_snmpVersion, pTransport, _T(".1.0.8802.1.1.2.1.3.2.0"), NULL, 0, szBuffer, 4096, 0) == SNMP_ERR_SUCCESS)
1856 {
1857 LockData();
1858 m_dwFlags |= NF_IS_LLDP;
1859 UnlockData();
1860
1861 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)
1862 {
1863 _tcscat(szBuffer, _T("@"));
1864 int len = (int)_tcslen(szBuffer);
1865 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)
1866 {
1867 LockData();
1868 if ((m_lldpNodeId == NULL) || _tcscmp(m_lldpNodeId, szBuffer))
1869 {
1870 safe_free(m_lldpNodeId);
1871 m_lldpNodeId = _tcsdup(szBuffer);
1872 hasChanges = true;
1873 SendPollerMsg(dwRqId, _T(" LLDP node ID changed to %s\r\n"), m_lldpNodeId);
1874 }
1875 UnlockData();
1876 }
1877 }
1878 }
1879 else
1880 {
1881 LockData();
1882 m_dwFlags &= ~NF_IS_LLDP;
1883 UnlockData();
1884 }
1885
1886 // Check for 802.1x support
1887 if (CheckSNMPIntegerValue(pTransport, _T(".1.0.8802.1.1.1.1.1.1.0"), 1))
1888 {
1889 LockData();
1890 m_dwFlags |= NF_IS_8021X;
1891 UnlockData();
1892 }
1893 else
1894 {
1895 LockData();
1896 m_dwFlags &= ~NF_IS_8021X;
1897 UnlockData();
1898 }
1899
1900 CheckOSPFSupport(pTransport);
1901
1902 // Get VRRP information
1903 VrrpInfo *vrrpInfo = GetVRRPInfo(this);
1904 if (vrrpInfo != NULL)
1905 {
1906 LockData();
1907 m_dwFlags |= NF_IS_VRRP;
1908 delete m_vrrpInfo;
1909 m_vrrpInfo = vrrpInfo;
1910 UnlockData();
1911 }
1912 else
1913 {
1914 LockData();
1915 m_dwFlags &= ~NF_IS_VRRP;
1916 UnlockData();
1917 }
1918 }
1919 else
1920 {
1921 // Check for CheckPoint SNMP agent on port 161
1922 DbgPrintf(5, _T("ConfPoll(%s): checking for CheckPoint SNMP"), m_szName);
1923 TCHAR szBuffer[4096];
1924 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)
1925 {
1926 LockData();
1927 if (_tcscmp(m_szObjectId, _T(".1.3.6.1.4.1.2620.1.1")))
1928 {
1929 nx_strncpy(m_szObjectId, _T(".1.3.6.1.4.1.2620.1.1"), MAX_OID_LEN * 4);
1930 hasChanges = true;
1931 }
1932
1933 m_dwFlags |= NF_IS_SNMP | NF_IS_ROUTER;
1934 m_dwDynamicFlags &= ~NDF_SNMP_UNREACHABLE;
1935 UnlockData();
1936 SendPollerMsg(dwRqId, POLLER_INFO _T(" CheckPoint SNMP agent on port 161 is active\r\n"));
1937 }
1938 }
1939 delete pTransport;
1940 return hasChanges;
1941}
1942
1943/**
1944 * Configuration poll: check for BRIDGE MIB
1945 */
1946void Node::checkBridgeMib(SNMP_Transport *pTransport)
1947{
1948 TCHAR szBuffer[4096];
1949 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)
1950 {
1951 LockData();
1952 m_dwFlags |= NF_IS_BRIDGE;
1953 memcpy(m_baseBridgeAddress, szBuffer, 6);
1954 UnlockData();
1955
1956 // Check for Spanning Tree (IEEE 802.1d) MIB support
1957 if (CheckSNMPIntegerValue(pTransport, _T(".1.3.6.1.2.1.17.2.1.0"), 3))
1958 {
1959 LockData();
1960 m_dwFlags |= NF_IS_STP;
1961 UnlockData();
1962 }
1963 else
1964 {
1965 LockData();
1966 m_dwFlags &= ~NF_IS_STP;
1967 UnlockData();
1968 }
1969 }
1970 else
1971 {
1972 LockData();
1973 m_dwFlags &= ~(NF_IS_BRIDGE | NF_IS_STP);
1974 UnlockData();
1975 }
1976}
1977
1978/**
1979 * Configuration poll: check for ifXTable
1980 */
1981void Node::checkIfXTable(SNMP_Transport *pTransport)
1982{
1983 int count = 0;
1984 SnmpEnumerate(m_snmpVersion, pTransport, _T(".1.3.6.1.2.1.31.1.1.1.1"), CountingSnmpWalkerCallback, &count, FALSE);
1985 if (count > 0)
1986 {
1987 LockData();
1988 m_dwFlags |= NF_HAS_IFXTABLE;
1989 UnlockData();
1990 }
1991 else
1992 {
1993 LockData();
1994 m_dwFlags &= ~NF_HAS_IFXTABLE;
1995 UnlockData();
1996 }
1997}
1998
1999/**
2000 * Update interface configuration
2001 */
eec253a8
VK
2002BOOL Node::updateInterfaceConfiguration(DWORD dwRqId, DWORD dwNetMask)
2003{
98762401 2004 InterfaceList *pIfList;
eec253a8
VK
2005 Interface **ppDeleteList;
2006 int i, j, iDelCount;
2007 BOOL hasChanges = FALSE;
2008 Cluster *pCluster = getMyCluster();
2009
2010 SendPollerMsg(dwRqId, _T("Checking interface configuration...\r\n"));
2011 pIfList = getInterfaceList();
2012 if (pIfList != NULL)
2013 {
22d657d2 2014 DbgPrintf(6, _T("Node::updateInterfaceConfiguration(%s [%u]): got %d interfaces"), m_szName, m_dwId, pIfList->getSize());
eec253a8
VK
2015 // Remove cluster virtual interfaces from list
2016 if (pCluster != NULL)
2017 {
98762401 2018 for(i = 0; i < pIfList->getSize(); i++)
eec253a8 2019 {
98762401 2020 if (pCluster->isVirtualAddr(pIfList->get(i)->dwIpAddr))
eec253a8 2021 {
98762401 2022 pIfList->remove(i);
eec253a8
VK
2023 i--;
2024 }
2025 }
2026 }
2027
2028 // Find non-existing interfaces
2029 LockChildList(FALSE);
2030 ppDeleteList = (Interface **)malloc(sizeof(Interface *) * m_dwChildCount);
2031 for(i = 0, iDelCount = 0; i < (int)m_dwChildCount; i++)
2032 {
2033 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
2034 {
2035 Interface *pInterface = (Interface *)m_pChildList[i];
9214177b
VK
2036 if (!pInterface->isManuallyCreated())
2037 {
2038 for(j = 0; j < pIfList->getSize(); j++)
2039 {
2040 if ((pIfList->get(j)->dwIndex == pInterface->getIfIndex()) &&
2041 (pIfList->get(j)->dwIpAddr == pInterface->IpAddr()))
2042 break;
2043 }
eec253a8 2044
9214177b
VK
2045 if (j == pIfList->getSize())
2046 {
2047 // No such interface in current configuration, add it to delete list
2048 ppDeleteList[iDelCount++] = pInterface;
2049 }
2050 }
eec253a8
VK
2051 }
2052 }
2053 UnlockChildList();
2054
2055 // Delete non-existent interfaces
2056 if (iDelCount > 0)
2057 {
2058 for(j = 0; j < iDelCount; j++)
2059 {
2060 SendPollerMsg(dwRqId, POLLER_WARNING _T(" Interface \"%s\" is no longer exist\r\n"),
2061 ppDeleteList[j]->Name());
2062 PostEvent(EVENT_INTERFACE_DELETED, m_dwId, "dsaa", ppDeleteList[j]->getIfIndex(),
2063 ppDeleteList[j]->Name(), ppDeleteList[j]->IpAddr(), ppDeleteList[j]->getIpNetMask());
2064 deleteInterface(ppDeleteList[j]);
2065 }
2066 hasChanges = TRUE;
2067 }
2068 safe_free(ppDeleteList);
2069
2070 // Add new interfaces and check configuration of existing
98762401 2071 for(j = 0; j < pIfList->getSize(); j++)
eec253a8 2072 {
60557d06 2073 NX_INTERFACE_INFO *ifInfo = pIfList->get(j);
eec253a8
VK
2074 BOOL bNewInterface = TRUE;
2075
2076 LockChildList(FALSE);
2077 for(i = 0; i < (int)m_dwChildCount; i++)
2078 {
2079 if (m_pChildList[i]->Type() == OBJECT_INTERFACE)
2080 {
2081 Interface *pInterface = (Interface *)m_pChildList[i];
2082
98762401
VK
2083 if ((ifInfo->dwIndex == pInterface->getIfIndex()) &&
2084 (ifInfo->dwIpAddr == pInterface->IpAddr()))
eec253a8
VK
2085 {
2086 // Existing interface, check configuration
22d657d2 2087 if (memcmp(ifInfo->bMacAddr, "\x00\x00\x00\x00\x00\x00", MAC_ADDR_LENGTH) && memcmp(ifInfo->bMacAddr, pInterface->getMacAddr(), MAC_ADDR_LENGTH))
eec253a8
VK
2088 {
2089 TCHAR szOldMac[16], szNewMac[16];
2090
2091 BinToStr((BYTE *)pInterface->getMacAddr(), MAC_ADDR_LENGTH, szOldMac);
98762401 2092 BinToStr(ifInfo->bMacAddr, MAC_ADDR_LENGTH, szNewMac);
eec253a8
VK
2093 PostEvent(EVENT_MAC_ADDR_CHANGED, m_dwId, "idsss",
2094 pInterface->Id(), pInterface->getIfIndex(),
2095 pInterface->Name(), szOldMac, szNewMac);
98762401 2096 pInterface->setMacAddr(ifInfo->bMacAddr);
eec253a8 2097 }
98762401 2098 if (_tcscmp(ifInfo->szName, pInterface->Name()))
eec253a8 2099 {
98762401 2100 pInterface->setName(ifInfo->szName);
eec253a8 2101 }
478d4ff4
VK
2102 if (_tcscmp(ifInfo->szDescription, pInterface->getDescription()))
2103 {
2104 pInterface->setDescription(ifInfo->szDescription);
2105 }
98762401 2106 if (ifInfo->dwBridgePortNumber != pInterface->getBridgePortNumber())
eec253a8 2107 {
98762401 2108 pInterface->setBridgePortNumber(ifInfo->dwBridgePortNumber);
eec253a8 2109 }
98762401 2110 if (ifInfo->dwSlotNumber != pInterface->getSlotNumber())
76f9abfd 2111 {
98762401 2112 pInterface->setSlotNumber(ifInfo->dwSlotNumber);
76f9abfd 2113 }
98762401 2114 if (ifInfo->dwPortNumber != pInterface->getPortNumber())
76f9abfd 2115 {
98762401 2116 pInterface->setPortNumber(ifInfo->dwPortNumber);
76f9abfd 2117 }
4c16cdc7
VK
2118 if (ifInfo->isPhysicalPort != pInterface->isPhysicalPort())
2119 {
2120 pInterface->setPhysicalPortFlag(ifInfo->isPhysicalPort);
2121 }
98762401 2122 if ((ifInfo->dwIpNetMask != 0) && (ifInfo->dwIpNetMask != pInterface->getIpNetMask()))
dfb38baf 2123 {
98762401 2124 pInterface->setIpNetMask(ifInfo->dwIpNetMask);
dfb38baf 2125 }
eec253a8
VK
2126 bNewInterface = FALSE;
2127 break;
2128 }
2129 }
2130 }
2131 UnlockChildList();
2132
2133 if (bNewInterface)
2134 {
2135 // New interface
98762401
VK
2136 SendPollerMsg(dwRqId, POLLER_INFO _T(" Found new interface \"%s\"\r\n"), ifInfo->szName);
2137 createNewInterface(ifInfo->dwIpAddr,
2138 ifInfo->dwIpNetMask,
2139 ifInfo->szName,
478d4ff4 2140 ifInfo->szDescription,
98762401
VK
2141 ifInfo->dwIndex,
2142 ifInfo->dwType,
2143 ifInfo->bMacAddr,
2144 ifInfo->dwBridgePortNumber,
2145 ifInfo->dwSlotNumber,
4c16cdc7
VK
2146 ifInfo->dwPortNumber,
2147 ifInfo->isPhysicalPort);
eec253a8
VK
2148 hasChanges = TRUE;
2149 }
2150 }
2151
2152 // Check if address we are using to communicate with node
2153 // is configured on one of node's interfaces
98762401
VK
2154 for(i = 0; i < pIfList->getSize(); i++)
2155 if (pIfList->get(i)->dwIpAddr == m_dwIpAddr)
eec253a8
VK
2156 break;
2157
98762401 2158 if (i == (DWORD)pIfList->getSize())
eec253a8 2159 {
eec253a8 2160 // Node is behind NAT
563179bc 2161 if (!(m_dwFlags & NF_BEHIND_NAT))
eec253a8 2162 {
563179bc
VK
2163 m_dwFlags |= NF_BEHIND_NAT;
2164 hasChanges = TRUE;
2165 }
eec253a8
VK
2166 }
2167 else
2168 {
2169 // Check if NF_BEHIND_NAT flag set incorrectly
2170 if (m_dwFlags & NF_BEHIND_NAT)
2171 {
eec253a8
VK
2172 m_dwFlags &= ~NF_BEHIND_NAT;
2173 hasChanges = TRUE;
2174 }
2175 }
2176
024c3faf 2177 checkSubnetBinding(pIfList);
eec253a8 2178
98762401 2179 delete pIfList;
eec253a8
VK
2180 }
2181 else /* pIfList == NULL */
2182 {
2183 Interface *pInterface;
2184 DWORD dwCount;
2185
2186 SendPollerMsg(dwRqId, POLLER_ERROR _T("Unable to get interface list from node\r\n"));
22d657d2 2187 DbgPrintf(6, _T("Node::updateInterfaceConfiguration(%s [%u]): Unable to get interface list from node"), m_szName, m_dwId);
eec253a8
VK
2188
2189 // Delete all existing interfaces in case of forced capability recheck
2190 if (m_dwDynamicFlags & NDF_RECHECK_CAPABILITIES)
2191 {
2192 LockChildList(FALSE);
2193 ppDeleteList = (Interface **)malloc(sizeof(Interface *) * m_dwChildCount);
2194 for(i = 0, iDelCount = 0; i < (int)m_dwChildCount; i++)
2195 {
9214177b 2196 if ((m_pChildList[i]->Type() == OBJECT_INTERFACE) && !((Interface *)m_pChildList[i])->isManuallyCreated())
eec253a8
VK
2197 ppDeleteList[iDelCount++] = (Interface *)m_pChildList[i];
2198 }
2199 UnlockChildList();
2200 for(j = 0; j < iDelCount; j++)
2201 {
2202 SendPollerMsg(dwRqId, POLLER_WARNING _T(" Interface \"%s\" is no longer exist\r\n"),
2203 ppDeleteList[j]->Name());
2204 PostEvent(EVENT_INTERFACE_DELETED, m_dwId, "dsaa", ppDeleteList[j]->getIfIndex(),
2205 ppDeleteList[j]->Name(), ppDeleteList[j]->IpAddr(), ppDeleteList[j]->getIpNetMask());
2206 deleteInterface(ppDeleteList[j]);
2207 }
2208 safe_free(ppDeleteList);
2209 }
2210
2211 // Check if we have pseudo-interface object
baa5324c
AK
2212 BYTE macAddr[MAC_ADDR_LENGTH];
2213 BYTE *pMacAddr;
eec253a8
VK
2214 dwCount = getInterfaceCount(&pInterface);
2215 if (dwCount == 1)
2216 {
2217 if (pInterface->isFake())
2218 {
2219 // Check if primary IP is different from interface's IP
2220 if (pInterface->IpAddr() != m_dwIpAddr)
2221 {
2222 deleteInterface(pInterface);
2223 if (m_dwIpAddr != 0)
baa5324c
AK
2224 {
2225 memset(macAddr, 0, MAC_ADDR_LENGTH);
2226 Subnet *pSubnet = FindSubnetForNode(m_zoneId, m_dwIpAddr);
2227 if (pSubnet != NULL)
2228 pSubnet->findMacAddress(m_dwIpAddr, macAddr);
22d657d2 2229 pMacAddr = !memcmp(macAddr, "\x00\x00\x00\x00\x00\x00", MAC_ADDR_LENGTH) ? NULL : macAddr;
baa5324c
AK
2230 TCHAR szMac[20];
2231 MACToStr(macAddr, szMac);
dab4332d 2232 DbgPrintf(5, _T("Node::updateInterfaceConfiguration(%s [%u]): got MAC for unknown interface: %s"), m_szName, m_dwId, szMac);
baa5324c
AK
2233 createNewInterface(m_dwIpAddr, dwNetMask, NULL, NULL, 0, 0, pMacAddr);
2234 }
eec253a8 2235 }
22d657d2
VK
2236 else
2237 {
2238 // check MAC address
2239 memset(macAddr, 0, MAC_ADDR_LENGTH);
2240 Subnet *pSubnet = FindSubnetForNode(m_zoneId, m_dwIpAddr);
2241 if (pSubnet != NULL)
2242 pSubnet->findMacAddress(m_dwIpAddr, macAddr);
2243 if (memcmp(macAddr, "\x00\x00\x00\x00\x00\x00", MAC_ADDR_LENGTH) && memcmp(macAddr, pInterface->getMacAddr(), MAC_ADDR_LENGTH))
2244 {
2245 TCHAR szOldMac[16], szNewMac[16];
2246
2247 BinToStr((BYTE *)pInterface->getMacAddr(), MAC_ADDR_LENGTH, szOldMac);
2248 BinToStr(macAddr, MAC_ADDR_LENGTH, szNewMac);
dab4332d 2249 DbgPrintf(5, _T("Node::updateInterfaceConfiguration(%s [%u]): MAC change for unknown interface: %s to %s"),
22d657d2
VK
2250 m_szName, m_dwId, szOldMac, szNewMac);
2251 PostEvent(EVENT_MAC_ADDR_CHANGED, m_dwId, "idsss",
2252 pInterface->Id(), pInterface->getIfIndex(),
2253 pInterface->Name(), szOldMac, szNewMac);
2254 pInterface->setMacAddr(macAddr);
2255 }
2256 }
eec253a8
VK
2257 }
2258 }
2259 else if (dwCount == 0)
2260 {
2261 // No interfaces at all, create pseudo-interface
2262 if (m_dwIpAddr != 0)
baa5324c
AK
2263 {
2264 memset(macAddr, 0, MAC_ADDR_LENGTH);
2265 Subnet *pSubnet = FindSubnetForNode(m_zoneId, m_dwIpAddr);
2266 if (pSubnet != NULL)
2267 pSubnet->findMacAddress(m_dwIpAddr, macAddr);
22d657d2 2268 pMacAddr = !memcmp(macAddr, "\x00\x00\x00\x00\x00\x00", MAC_ADDR_LENGTH) ? NULL : macAddr;
baa5324c
AK
2269 TCHAR szMac[20];
2270 MACToStr(macAddr, szMac);
dab4332d 2271 DbgPrintf(5, _T("Node::updateInterfaceConfiguration: got MAC for unknown interface: %s"), szMac);
baa5324c
AK
2272 createNewInterface(m_dwIpAddr, dwNetMask, NULL, NULL, 0, 0, pMacAddr);
2273 }
eec253a8 2274 }
22d657d2 2275 DbgPrintf(6, _T("Node::updateInterfaceConfiguration(%s [%u]): pflist == NULL, dwCount = %ld"), m_szName, m_dwId, dwCount);
eec253a8
VK
2276 }
2277
2278 SendPollerMsg(dwRqId, _T("Interface configuration check finished\r\n"));
2279 return hasChanges;
2280}
2281
2282
2283//
4d0c32f3
VK
2284// Apply system templates
2285//
2286
2287void Node::ApplySystemTemplates()
2288{
2289 Template *pTemplate;
2290
2291 pTemplate = FindTemplateByName(_T("@System.Agent"));
2292 if (pTemplate != NULL)
2293 {
7c521895 2294 if (isNativeAgent())
5039dede 2295 {
4d0c32f3 2296 if (!pTemplate->IsChild(m_dwId))
5039dede 2297 {
4d0c32f3
VK
2298 DbgPrintf(4, _T("Node::ApplySystemTemplates(): applying template %d \"%s\" to node %d \"%s\""),
2299 pTemplate->Id(), pTemplate->Name(), m_dwId, m_szName);
2300 pTemplate->ApplyToNode(this);
5039dede 2301 }
4d0c32f3
VK
2302 }
2303 else
2304 {
2305 if (pTemplate->IsChild(m_dwId))
5039dede 2306 {
4d0c32f3
VK
2307 DbgPrintf(4, _T("Node::ApplySystemTemplates(): removing template %d \"%s\" from node %d \"%s\""),
2308 pTemplate->Id(), pTemplate->Name(), m_dwId, m_szName);
2309 pTemplate->DeleteChild(this);
2310 DeleteParent(pTemplate);
fb05c05b 2311 pTemplate->queueRemoveFromNode(m_dwId, TRUE);
5039dede
AK
2312 }
2313 }
4d0c32f3 2314 }
4d0c32f3
VK
2315}
2316
2317
2318//
2319// Apply user templates
2320//
2321
27de5dab 2322static void ApplyTemplate(NetObj *object, void *node)
4d0c32f3 2323{
6ff21d27 2324 if ((object->Type() == OBJECT_TEMPLATE) && !object->isDeleted())
4d0c32f3 2325 {
27de5dab
VK
2326 Template *pTemplate = (Template *)object;
2327 if (pTemplate->isApplicable((Node *)node))
2328 {
2329 if (!pTemplate->IsChild(((Node *)node)->Id()))
5039dede 2330 {
27de5dab
VK
2331 DbgPrintf(4, _T("Node::ApplyUserTemplates(): applying template %d \"%s\" to node %d \"%s\""),
2332 pTemplate->Id(), pTemplate->Name(), ((Node *)node)->Id(), ((Node *)node)->Name());
2333 pTemplate->ApplyToNode((Node *)node);
9b64b406 2334 PostEvent(EVENT_TEMPLATE_AUTOAPPLY, g_dwMgmtNode, "isis", ((Node *)node)->Id(), ((Node *)node)->Name(), pTemplate->Id(), pTemplate->Name());
5039dede 2335 }
27de5dab
VK
2336 }
2337 else
2338 {
9b64b406 2339 if (pTemplate->isAutoRemoveEnabled() && pTemplate->IsChild(((Node *)node)->Id()))
5039dede 2340 {
27de5dab
VK
2341 DbgPrintf(4, _T("Node::ApplyUserTemplates(): removing template %d \"%s\" from node %d \"%s\""),
2342 pTemplate->Id(), pTemplate->Name(), ((Node *)node)->Id(), ((Node *)node)->Name());
2343 pTemplate->DeleteChild((Node *)node);
2344 ((Node *)node)->DeleteParent(pTemplate);
2345 pTemplate->queueRemoveFromNode(((Node *)node)->Id(), TRUE);
9b64b406 2346 PostEvent(EVENT_TEMPLATE_AUTOREMOVE, g_dwMgmtNode, "isis", ((Node *)node)->Id(), ((Node *)node)->Name(), pTemplate->Id(), pTemplate->Name());
5039dede 2347 }
27de5dab 2348 }
5039dede 2349 }
27de5dab
VK
2350}
2351
2352void Node::ApplyUserTemplates()
2353{
2354 g_idxObjectById.forEach(ApplyTemplate, this);
4d0c32f3 2355}
5039dede 2356
5039dede 2357
4d0c32f3
VK
2358//
2359// Update container membership
2360//
2361
27de5dab 2362static void UpdateContainerBinding(NetObj *object, void *node)
4d0c32f3 2363{
6ff21d27 2364 if ((object->Type() == OBJECT_CONTAINER) && !object->isDeleted())
5039dede 2365 {
27de5dab 2366 Container *pContainer = (Container *)object;
926e8ce7 2367 if (pContainer->isSuitableForNode((Node *)node))
27de5dab
VK
2368 {
2369 if (!pContainer->IsChild(((Node *)node)->Id()))
4d0c32f3 2370 {
27de5dab
VK
2371 DbgPrintf(4, _T("Node::UpdateContainerMembership(): binding node %d \"%s\" to container %d \"%s\""),
2372 ((Node *)node)->Id(), ((Node *)node)->Name(), pContainer->Id(), pContainer->Name());
2373 pContainer->AddChild((Node *)node);
2374 ((Node *)node)->AddParent(pContainer);
9b64b406 2375 PostEvent(EVENT_CONTAINER_AUTOBIND, g_dwMgmtNode, "isis", ((Node *)node)->Id(), ((Node *)node)->Name(), pContainer->Id(), pContainer->Name());
4d0c32f3 2376 }
27de5dab
VK
2377 }
2378 else
2379 {
9b64b406 2380 if (pContainer->isAutoUnbindEnabled() && pContainer->IsChild(((Node *)node)->Id()))
4d0c32f3 2381 {
27de5dab
VK
2382 DbgPrintf(4, _T("Node::UpdateContainerMembership(): removing node %d \"%s\" from container %d \"%s\""),
2383 ((Node *)node)->Id(), ((Node *)node)->Name(), pContainer->Id(), pContainer->Name());
2384 pContainer->DeleteChild((Node *)node);
2385 ((Node *)node)->DeleteParent(pContainer);
9b64b406 2386 PostEvent(EVENT_CONTAINER_AUTOUNBIND, g_dwMgmtNode, "isis", ((Node *)node)->Id(), ((Node *)node)->Name(), pContainer->Id(), pContainer->Name());
4d0c32f3 2387 }
27de5dab 2388 }
5039dede 2389 }
27de5dab
VK
2390}
2391
2392void Node::updateContainerMembership()
2393{
2394 g_idxObjectById.forEach(UpdateContainerBinding, this);
5039dede
AK
2395}
2396
2397
2398//
2399// Connect to native agent
2400//
2401
a4569c4d 2402BOOL Node::connectToAgent(DWORD *error, DWORD *socketError)
5039dede
AK
2403{
2404 BOOL bRet;
2405
2406 // Create new agent connection object if needed
2407 if (m_pAgentConnection == NULL)
4b91844f 2408 {
5039dede 2409 m_pAgentConnection = new AgentConnectionEx(htonl(m_dwIpAddr), m_wAgentPort, m_wAuthMethod, m_szSharedSecret);
4b91844f
VK
2410 DbgPrintf(7, _T("Node::connectToAgent(%s [%d]): new agent connection created"), m_szName, m_dwId);
2411 }
2412 else
2413 {
2414 // Check if we already connected
2415 if (m_pAgentConnection->nop() == ERR_SUCCESS)
2416 {
2417 DbgPrintf(7, _T("Node::connectToAgent(%s [%d]): already connected"), m_szName, m_dwId);
2418 return TRUE;
2419 }
5039dede 2420
4b91844f
VK
2421 // Close current connection or clean up after broken connection
2422 m_pAgentConnection->disconnect();
2423 DbgPrintf(7, _T("Node::connectToAgent(%s [%d]): existing connection reset"), m_szName, m_dwId);
2424 }
7c521895 2425 m_pAgentConnection->setPort(m_wAgentPort);
35f836fe
VK
2426#ifdef UNICODE
2427 char mbSecret[MAX_SECRET_LENGTH];
2428 WideCharToMultiByte(CP_ACP, WC_DEFAULTCHAR | WC_COMPOSITECHECK, m_szSharedSecret, -1, mbSecret, MAX_SECRET_LENGTH, NULL, NULL);
2429 mbSecret[MAX_SECRET_LENGTH - 1] = 0;
2430 m_pAgentConnection->setAuthData(m_wAuthMethod, mbSecret);
2431#else
7c521895 2432 m_pAgentConnection->setAuthData(m_wAuthMethod, m_szSharedSecret);
35f836fe 2433#endif
7c521895 2434 setAgentProxy(m_pAgentConnection);
4b91844f 2435 DbgPrintf(7, _T("Node::connectToAgent(%s [%d]): calling connect on port %d"), m_szName, m_dwId, (int)m_wAgentPort);
a4569c4d 2436 bRet = m_pAgentConnection->connect(g_pServerKey, FALSE, error, socketError);
5039dede
AK
2437 if (bRet)
2438 {
45d84f8a
VK
2439 m_pAgentConnection->setCommandTimeout(g_dwAgentCommandTimeout);
2440 m_pAgentConnection->enableTraps();
5039dede
AK
2441 }
2442 return bRet;
2443}
2444
2445
2446//
2447// Get item's value via SNMP
2448//
2449
e320f8ce 2450DWORD Node::getItemFromSNMP(WORD port, const TCHAR *szParam, DWORD dwBufSize, TCHAR *szBuffer, int interpretRawValue)
5039dede
AK
2451{
2452 DWORD dwResult;
2453
2fd7144f 2454 if ((((m_dwDynamicFlags & NDF_SNMP_UNREACHABLE) || !(m_dwFlags & NF_IS_SNMP)) && (port == 0)) ||
6b2bb22c
VK
2455 (m_dwDynamicFlags & NDF_UNREACHABLE) ||
2456 (m_dwFlags & NF_DISABLE_SNMP))
5039dede
AK
2457 {
2458 dwResult = SNMP_ERR_COMM;
2459 }
2460 else
2461 {
2462 SNMP_Transport *pTransport;
2463
65e2005b 2464 pTransport = createSnmpTransport(port);
803d47be
VK
2465 if (pTransport != NULL)
2466 {
e320f8ce
VK
2467 if (interpretRawValue == SNMP_RAWTYPE_NONE)
2468 {
2469 dwResult = SnmpGet(m_snmpVersion, pTransport, szParam, NULL, 0, szBuffer, dwBufSize, SG_PSTRING_RESULT);
2470 }
2471 else
2472 {
2473 BYTE rawValue[1024];
2474 memset(rawValue, 0, 1024);
2475 dwResult = SnmpGet(m_snmpVersion, pTransport, szParam, NULL, 0, rawValue, 1024, SG_RAW_RESULT);
2476 if (dwResult == SNMP_ERR_SUCCESS)
2477 {
2478 switch(interpretRawValue)
2479 {
2480 case SNMP_RAWTYPE_INT32:
2481 _sntprintf(szBuffer, dwBufSize, _T("%d"), ntohl(*((LONG *)rawValue)));
2482 break;
2483 case SNMP_RAWTYPE_UINT32:
2484 _sntprintf(szBuffer, dwBufSize, _T("%u"), ntohl(*((DWORD *)rawValue)));
2485 break;
2486 case SNMP_RAWTYPE_INT64:
2487 _sntprintf(szBuffer, dwBufSize, INT64_FMT, ntohq(*((INT64 *)rawValue)));
2488 break;
2489 case SNMP_RAWTYPE_UINT64:
2490 _sntprintf(szBuffer, dwBufSize, UINT64_FMT, ntohq(*((QWORD *)rawValue)));
2491 break;
2492 case SNMP_RAWTYPE_DOUBLE:
2493 _sntprintf(szBuffer, dwBufSize, _T("%f"), ntohd(*((double *)rawValue)));
2494 break;
2495 case SNMP_RAWTYPE_IP_ADDR:
2496 IpToStr(ntohl(*((DWORD *)rawValue)), szBuffer);
2497 break;
2498 case SNMP_RAWTYPE_MAC_ADDR:
2499 MACToStr(rawValue, szBuffer);
2500 break;
2501 default:
2502 szBuffer[0] = 0;
2503 break;
2504 }
2505 }
2506 }
803d47be
VK
2507 delete pTransport;
2508 }
2509 else
2510 {
2511 dwResult = SNMP_ERR_COMM;
2512 }
5039dede 2513 }
35f836fe 2514 DbgPrintf(7, _T("Node(%s)->GetItemFromSNMP(%s): dwResult=%d"), m_szName, szParam, dwResult);
5039dede
AK
2515 return (dwResult == SNMP_ERR_SUCCESS) ? DCE_SUCCESS :
2516 ((dwResult == SNMP_ERR_NO_OBJECT) ? DCE_NOT_SUPPORTED : DCE_COMM_ERROR);
2517}
2518
2519
2520//
2521// Get item's value via SNMP from CheckPoint's agent
2522//
2523
35f836fe 2524DWORD Node::GetItemFromCheckPointSNMP(const TCHAR *szParam, DWORD dwBufSize, TCHAR *szBuffer)
5039dede
AK
2525{
2526 DWORD dwResult;
2527
2528 if ((m_dwDynamicFlags & NDF_CPSNMP_UNREACHABLE) ||
2529 (m_dwDynamicFlags & NDF_UNREACHABLE))
2530 {
2531 dwResult = SNMP_ERR_COMM;
2532 }
2533 else
2534 {
2535 SNMP_Transport *pTransport;
2536
2537 pTransport = new SNMP_UDPTransport;
c4366266 2538 ((SNMP_UDPTransport *)pTransport)->createUDPTransport(NULL, htonl(m_dwIpAddr), CHECKPOINT_SNMP_PORT);
f42b8099 2539 dwResult = SnmpGet(SNMP_VERSION_1, pTransport, szParam, NULL, 0, szBuffer, dwBufSize, SG_STRING_RESULT);
5039dede
AK
2540 delete pTransport;
2541 }
35f836fe 2542 DbgPrintf(7, _T("Node(%s)->GetItemFromCheckPointSNMP(%s): dwResult=%d"), m_szName, szParam, dwResult);
5039dede
AK
2543 return (dwResult == SNMP_ERR_SUCCESS) ? DCE_SUCCESS :
2544 ((dwResult == SNMP_ERR_NO_OBJECT) ? DCE_NOT_SUPPORTED : DCE_COMM_ERROR);
2545}
2546
2547
2548//
2549// Get item's value via native agent
2550//
2551
35f836fe 2552DWORD Node::GetItemFromAgent(const TCHAR *szParam, DWORD dwBufSize, TCHAR *szBuffer)
5039dede 2553{
912b994d 2554 DWORD dwError = ERR_NOT_CONNECTED, dwResult = DCE_COMM_ERROR;
5039dede
AK
2555 DWORD dwTries = 3;
2556
2557 if ((m_dwDynamicFlags & NDF_AGENT_UNREACHABLE) ||
2fd7144f
VK
2558 (m_dwDynamicFlags & NDF_UNREACHABLE) ||
2559 (m_dwFlags & NF_DISABLE_NXCP) ||
2560 !(m_dwFlags & NF_IS_NATIVE_AGENT))
5039dede
AK
2561 return DCE_COMM_ERROR;
2562
7c521895 2563 agentLock();
5039dede
AK
2564
2565 // Establish connection if needed
2566 if (m_pAgentConnection == NULL)
7c521895 2567 if (!connectToAgent())
5039dede
AK
2568 goto end_loop;
2569
2570 // Get parameter from agent
2571 while(dwTries-- > 0)
2572 {
4687826e 2573 dwError = m_pAgentConnection->getParameter(szParam, dwBufSize, szBuffer);
5039dede
AK
2574 switch(dwError)
2575 {
2576 case ERR_SUCCESS:
2577 dwResult = DCE_SUCCESS;
2578 goto end_loop;
2579 case ERR_UNKNOWN_PARAMETER:
2580 dwResult = DCE_NOT_SUPPORTED;
2581 goto end_loop;
2582 case ERR_NOT_CONNECTED:
2583 case ERR_CONNECTION_BROKEN:
7c521895 2584 if (!connectToAgent())
5039dede
AK
2585 goto end_loop;
2586 break;
2587 case ERR_REQUEST_TIMEOUT:
2588 // Reset connection to agent after timeout
2589 DbgPrintf(7, _T("Node(%s)->GetItemFromAgent(%s): timeout; resetting connection to agent..."), m_szName, szParam);
2590 delete_and_null(m_pAgentConnection);
7c521895 2591 if (!connectToAgent())
5039dede
AK
2592 goto end_loop;
2593 DbgPrintf(7, _T("Node(%s)->GetItemFromAgent(%s): connection to agent restored successfully"), m_szName, szParam);
2594 break;
2595 }
2596 }
2597
2598end_loop:
7c521895 2599 agentUnlock();
adac28be
VK
2600 DbgPrintf(7, _T("Node(%s)->GetItemFromAgent(%s): dwError=%d dwResult=%d"), m_szName, szParam, dwError, dwResult);
2601 return dwResult;
2602}
2603
2604
2605//
2606// Get table from agent
2607//
2608
2609DWORD Node::getTableFromAgent(const TCHAR *name, Table **table)
2610{
2611 DWORD dwError = ERR_NOT_CONNECTED, dwResult = DCE_COMM_ERROR;
2612 DWORD dwTries = 3;
2613
2614 *table = NULL;
2615
2616 if ((m_dwDynamicFlags & NDF_AGENT_UNREACHABLE) ||
2617 (m_dwDynamicFlags & NDF_UNREACHABLE) ||
2618 (m_dwFlags & NF_DISABLE_NXCP) ||
2619 !(m_dwFlags & NF_IS_NATIVE_AGENT))
2620 return DCE_COMM_ERROR;
2621
2622 agentLock();
2623
2624 // Establish connection if needed
2625 if (m_pAgentConnection == NULL)
2626 if (!connectToAgent())
2627 goto end_loop;
2628
2629 // Get parameter from agent
2630 while(dwTries-- > 0)
2631 {
2632 dwError = m_pAgentConnection->getTable(name, table);
2633 switch(dwError)
2634 {
2635 case ERR_SUCCESS:
2636 dwResult = DCE_SUCCESS;
2637 goto end_loop;
2638 case ERR_UNKNOWN_PARAMETER:
2639 dwResult = DCE_NOT_SUPPORTED;
2640 goto end_loop;
2641 case ERR_NOT_CONNECTED:
2642 case ERR_CONNECTION_BROKEN:
2643 if (!connectToAgent())
2644 goto end_loop;
2645 break;
2646 case ERR_REQUEST_TIMEOUT:
2647 // Reset connection to agent after timeout
2648 DbgPrintf(7, _T("Node(%s)->getTableFromAgent(%s): timeout; resetting connection to agent..."), m_szName, name);
2649 delete_and_null(m_pAgentConnection);
2650 if (!connectToAgent())
2651 goto end_loop;
2652 DbgPrintf(7, _T("Node(%s)->getTableFromAgent(%s): connection to agent restored successfully"), m_szName, name);
2653 break;
2654 }
2655 }
2656
2657end_loop:
2658 agentUnlock();
2659 DbgPrintf(7, _T("Node(%s)->getTableFromAgent(%s): dwError=%d dwResult=%d"), m_szName, name, dwError, dwResult);
5039dede
AK
2660 return dwResult;
2661}
2662
2663
2664//
2665// Get value for server's internal parameter
2666//
2667
35f836fe 2668DWORD Node::GetInternalItem(const TCHAR *szParam, DWORD dwBufSize, TCHAR *szBuffer)
5039dede
AK
2669{
2670 DWORD dwError = DCE_SUCCESS;
2671
35f836fe 2672 if (!_tcsicmp(szParam, _T("Status")))
5039dede 2673 {
35f836fe 2674 _sntprintf(szBuffer, dwBufSize, _T("%d"), m_iStatus);
5039dede 2675 }
35f836fe 2676 else if (!_tcsicmp(szParam, _T("Dummy")))
5039dede
AK
2677 {
2678 _tcscpy(szBuffer, _T("0"));
2679 }
20c842ee
VK
2680 else if (MatchString(_T("Net.IP.NextHop(*)"), szParam, FALSE))
2681 {
2682 if ((m_dwFlags & NF_IS_NATIVE_AGENT) || (m_dwFlags & NF_IS_SNMP))
2683 {
2684 TCHAR arg[256] = _T("");
2685 DWORD destAddr, nextHop, ifIndex;
2686 BOOL isVpn;
2687
2688 AgentGetParameterArg(szParam, 1, arg, 256);
2689 destAddr = ntohl(_t_inet_addr(arg));
2690 if ((destAddr > 0) && (destAddr < 0xE0000000))
2691 {
2692 if (getNextHop(m_dwIpAddr, destAddr, &nextHop, &ifIndex, &isVpn))
2693 {
2694 IpToStr(nextHop, szBuffer);
2695 }
2696 else
2697 {
2698 _tcscpy(szBuffer, _T("UNREACHABLE"));
2699 }
2700 }
2701 else
2702 {
2703 dwError = DCE_NOT_SUPPORTED;
2704 }
2705 }
2706 else
2707 {
2708 dwError = DCE_NOT_SUPPORTED;
2709 }
2710 }
35f836fe 2711 else if (!_tcsicmp(szParam, _T("AgentStatus")))
5039dede
AK
2712 {
2713 if (m_dwFlags & NF_IS_NATIVE_AGENT)
2714 {
35f836fe 2715 szBuffer[0] = (m_dwDynamicFlags & NDF_AGENT_UNREACHABLE) ? _T('1') : _T('0');
5039dede
AK
2716 szBuffer[1] = 0;
2717 }
2718 else
2719 {
2720 dwError = DCE_NOT_SUPPORTED;
2721 }
2722 }
35f836fe 2723 else if (MatchString(_T("ChildStatus(*)"), szParam, FALSE))
5039dede 2724 {
35f836fe 2725 TCHAR *pEnd, szArg[256];
5039dede
AK
2726 DWORD i, dwId;
2727 NetObj *pObject = NULL;
2728
35f836fe
VK
2729 AgentGetParameterArg(szParam, 1, szArg, 256);
2730 dwId = _tcstoul(szArg, &pEnd, 0);
5039dede
AK
2731 if (*pEnd != 0)
2732 {
2733 // Argument is object's name
2734 dwId = 0;
2735 }
2736
2737 // Find child object with requested ID or name
2738 LockChildList(FALSE);
2739 for(i = 0; i < m_dwChildCount; i++)
2740 {
35f836fe 2741 if (((dwId == 0) && (!_tcsicmp(m_pChildList[i]->Name(), szArg))) ||
5039dede
AK
2742 (dwId == m_pChildList[i]->Id()))
2743 {
2744 pObject = m_pChildList[i];
2745 break;
2746 }
2747 }
2748 UnlockChildList();
2749
2750 if (pObject != NULL)
2751 {
35f836fe 2752 _sntprintf(szBuffer, dwBufSize, _T("%d"), pObject->Status());
5039dede
AK
2753 }
2754 else
2755 {
2756 dwError = DCE_NOT_SUPPORTED;
2757 }
2758 }
35f836fe 2759 else if (MatchString(_T("ConditionStatus(*)"), szParam, FALSE))
5039dede 2760 {
35f836fe 2761 TCHAR *pEnd, szArg[256];
5039dede
AK
2762 DWORD dwId;
2763 NetObj *pObject = NULL;
2764
35f836fe
VK
2765 AgentGetParameterArg(szParam, 1, szArg, 256);
2766 dwId = _tcstoul(szArg, &pEnd, 0);
5039dede
AK
2767 if (*pEnd == 0)
2768 {
2769 pObject = FindObjectById(dwId);
2770 if (pObject != NULL)
2771 if (pObject->Type() != OBJECT_CONDITION)
2772 pObject = NULL;
2773 }
2774 else
2775 {
2776 // Argument is object's name
2777 pObject = FindObjectByName(szArg, OBJECT_CONDITION);
2778 }
2779
2780 if (pObject != NULL)
2781 {
2782 if (pObject->IsTrustedNode(m_dwId))
2783 {
35f836fe 2784 _sntprintf(szBuffer, dwBufSize, _T("%d"), pObject->Status());
5039dede
AK
2785 }
2786 else
2787 {
2788 dwError = DCE_NOT_SUPPORTED;
2789 }
2790 }
2791 else
2792 {
2793 dwError = DCE_NOT_SUPPORTED;
2794 }
2795 }
2796 else if (m_dwFlags & NF_IS_LOCAL_MGMT)
2797 {
35f836fe 2798 if (!_tcsicmp(szParam, _T("Server.AverageDCPollerQueueSize")))
5039dede 2799 {
35f836fe 2800 _sntprintf(szBuffer, dwBufSize, _T("%f"), g_dAvgPollerQueueSize);
5039dede 2801 }
35f836fe 2802 else if (!_tcsicmp(szParam, _T("Server.AverageDBWriterQueueSize")))
5039dede 2803 {
cf084617
VK
2804 _sntprintf(szBuffer, dwBufSize, _T("%f"), g_dAvgDBAndIDataWriterQueueSize);
2805 }
2806 else if (!_tcsicmp(szParam, _T("Server.AverageDBWriterQueueSize.Other")))
2807 {
35f836fe 2808 _sntprintf(szBuffer, dwBufSize, _T("%f"), g_dAvgDBWriterQueueSize);
5039dede 2809 }
cf084617
VK
2810 else if (!_tcsicmp(szParam, _T("Server.AverageDBWriterQueueSize.IData")))
2811 {
2812 _sntprintf(szBuffer, dwBufSize, _T("%f"), g_dAvgIDataWriterQueueSize);
2813 }
35f836fe 2814 else if (!_tcsicmp(szParam, _T("Server.AverageStatusPollerQueueSize")))
5039dede 2815 {
35f836fe 2816 _sntprintf(szBuffer, dwBufSize, _T("%f"), g_dAvgStatusPollerQueueSize);
5039dede 2817 }
35f836fe 2818 else if (!_tcsicmp(szParam, _T("Server.AverageConfigurationPollerQueueSize")))
5039dede 2819 {
35f836fe 2820 _sntprintf(szBuffer, dwBufSize, _T("%f"), g_dAvgConfigPollerQueueSize);
5039dede 2821 }
35f836fe 2822 else if (!_tcsicmp(szParam, _T("Server.AverageDCIQueuingTime")))
5039dede 2823 {
35f836fe 2824 _sntprintf(szBuffer, dwBufSize, _T("%u"), g_dwAvgDCIQueuingTime);
5039dede 2825 }
35f836fe 2826 else if (!_tcsicmp(szParam, _T("Server.TotalEventsProcessed")))
5039dede 2827 {
35f836fe 2828 _sntprintf(szBuffer, dwBufSize, INT64_FMT, g_totalEventsProcessed);
5039dede
AK
2829 }
2830 else
2831 {
2832 dwError = DCE_NOT_SUPPORTED;
2833 }
2834 }
2835 else
2836 {
2837 dwError = DCE_NOT_SUPPORTED;
2838 }
2839
2840 return dwError;
2841}
2842
2843
2844//
adac28be
VK
2845// Translate DCI error code into RCC
2846//
2847
2848static DWORD RCCFromDCIError(DWORD error)
2849{
3188558a 2850 switch(error)
adac28be
VK
2851 {
2852 case DCE_SUCCESS:
2853 return RCC_SUCCESS;
2854 case DCE_COMM_ERROR:
2855 return RCC_COMM_FAILURE;
2856 case DCE_NOT_SUPPORTED:
2857 return RCC_DCI_NOT_SUPPORTED;
2858 default:
2859 return RCC_SYSTEM_FAILURE;
2860 }
2861}
2862
2863
2864//
5039dede
AK
2865// Get item's value for client
2866//
2867
adac28be 2868DWORD Node::getItemForClient(int iOrigin, const TCHAR *pszParam, TCHAR *pszBuffer, DWORD dwBufSize)
5039dede
AK
2869{
2870 DWORD dwResult = 0, dwRetCode;
2871
2872 // Get data from node
2873 switch(iOrigin)
2874 {
2875 case DS_INTERNAL:
2876 dwRetCode = GetInternalItem(pszParam, dwBufSize, pszBuffer);
2877 break;
2878 case DS_NATIVE_AGENT:
2879 dwRetCode = GetItemFromAgent(pszParam, dwBufSize, pszBuffer);
2880 break;
2881 case DS_SNMP_AGENT:
e320f8ce 2882 dwRetCode = getItemFromSNMP(0, pszParam, dwBufSize, pszBuffer, SNMP_RAWTYPE_NONE);
5039dede
AK
2883 break;
2884 case DS_CHECKPOINT_AGENT:
2885 dwRetCode = GetItemFromCheckPointSNMP(pszParam, dwBufSize, pszBuffer);
2886 break;
2887 default:
2888 dwResult = RCC_INVALID_ARGUMENT;
2889 break;
2890 }
2891
2892 // Translate return code to RCC
2893 if (dwResult != RCC_INVALID_ARGUMENT)
adac28be 2894 dwResult = RCCFromDCIError(dwRetCode);
5039dede
AK
2895
2896 return dwResult;
2897}
2898
2899
2900//
adac28be
VK
2901// Get table for client
2902//
2903
3188558a 2904DWORD Node::getTableForClient(const TCHAR *name, Table **table)
adac28be 2905{
3188558a 2906 DWORD dwRetCode = getTableFromAgent(name, table);
adac28be
VK
2907 return RCCFromDCIError(dwRetCode);
2908}
2909
2910
2911//
5039dede
AK
2912// Put items which requires polling into the queue
2913//
2914
7c521895 2915void Node::queueItemsForPolling(Queue *pPollerQueue)
5039dede 2916{
5039dede
AK
2917 if ((m_iStatus == STATUS_UNMANAGED) ||
2918 (m_dwFlags & NF_DISABLE_DATA_COLLECT) ||
2919 (m_bIsDeleted))
2920 return; // Do not collect data for unmanaged nodes or if data collection is disabled
2921
16d6f798 2922 time_t currTime = time(NULL);
5039dede 2923
7c521895 2924 lockDciAccess();
16d6f798 2925 for(int i = 0; i < m_dcObjects->size(); i++)
5039dede 2926 {
16d6f798
VK
2927 DCObject *object = m_dcObjects->get(i);
2928 if (object->isReadyForPolling(currTime))
5039dede 2929 {
16d6f798 2930 object->setBusyFlag(TRUE);
5039dede 2931 IncRefCount(); // Increment reference count for each queued DCI
16d6f798
VK
2932 pPollerQueue->Put(object);
2933 DbgPrintf(8, _T("Node(%s)->QueueItemsForPolling(): item %d \"%s\" added to queue"), m_szName, object->getId(), object->getName());
5039dede
AK
2934 }
2935 }
7c521895 2936 unlockDciAccess();
5039dede
AK
2937}
2938
2939
2940//
2941// Create CSCP message with object's data
2942//
2943
2944void Node::CreateMessage(CSCPMessage *pMsg)
2945{
2946 Template::CreateMessage(pMsg);
5a7d6a10 2947 pMsg->SetVariable(VID_PRIMARY_NAME, m_primaryName);
5039dede 2948 pMsg->SetVariable(VID_FLAGS, m_dwFlags);
fa6173b9 2949 pMsg->SetVariable(VID_RUNTIME_FLAGS, m_dwDynamicFlags);
5039dede
AK
2950 pMsg->SetVariable(VID_AGENT_PORT, m_wAgentPort);
2951 pMsg->SetVariable(VID_AUTH_METHOD, m_wAuthMethod);
2952 pMsg->SetVariable(VID_SHARED_SECRET, m_szSharedSecret);
35f836fe
VK
2953 pMsg->SetVariableFromMBString(VID_SNMP_AUTH_OBJECT, m_snmpSecurity->getCommunity());
2954 pMsg->SetVariableFromMBString(VID_SNMP_AUTH_PASSWORD, m_snmpSecurity->getAuthPassword());
2955 pMsg->SetVariableFromMBString(VID_SNMP_PRIV_PASSWORD, m_snmpSecurity->getPrivPassword());
4b7d8903 2956 pMsg->SetVariable(VID_SNMP_USM_METHODS, (WORD)((WORD)m_snmpSecurity->getAuthMethod() | ((WORD)m_snmpSecurity->getPrivMethod() << 8)));
5039dede 2957 pMsg->SetVariable(VID_SNMP_OID, m_szObjectId);
65e2005b 2958 pMsg->SetVariable(VID_SNMP_PORT, m_wSNMPPort);
5d2c5741 2959 pMsg->SetVariable(VID_SNMP_VERSION, (WORD)m_snmpVersion);
5039dede
AK
2960 pMsg->SetVariable(VID_AGENT_VERSION, m_szAgentVersion);
2961 pMsg->SetVariable(VID_PLATFORM_NAME, m_szPlatformName);
2962 pMsg->SetVariable(VID_POLLER_NODE_ID, m_dwPollerNode);
52c31148 2963 pMsg->SetVariable(VID_ZONE_ID, m_zoneId);
e41fced3 2964 pMsg->SetVariable(VID_AGENT_PROXY, m_dwProxyNode);
5039dede
AK
2965 pMsg->SetVariable(VID_SNMP_PROXY, m_dwSNMPProxy);
2966 pMsg->SetVariable(VID_REQUIRED_POLLS, (WORD)m_iRequiredPollCount);
0ecc2200
VK
2967 pMsg->SetVariable(VID_SYS_NAME, CHECK_NULL_EX(m_sysName));
2968 pMsg->SetVariable(VID_SYS_DESCRIPTION, CHECK_NULL_EX(m_sysDescription));
3eab63f2 2969 pMsg->SetVariable(VID_BRIDGE_BASE_ADDRESS, m_baseBridgeAddress, 6);
0ecc2200
VK
2970 if (m_lldpNodeId != NULL)
2971 pMsg->SetVariable(VID_LLDP_NODE_ID, m_lldpNodeId);
5039dede 2972 pMsg->SetVariable(VID_USE_IFXTABLE, (WORD)m_nUseIfXTable);
0d75ea88
VK
2973 if (m_vrrpInfo != NULL)
2974 {
2975 pMsg->SetVariable(VID_VRRP_VERSION, (WORD)m_vrrpInfo->getVersion());
2976 pMsg->SetVariable(VID_VRRP_VR_COUNT, (WORD)m_vrrpInfo->getSize());
2977 }
9796ce45
VK
2978 if (m_driver != NULL)
2979 {
2980 pMsg->SetVariable(VID_DRIVER_NAME, m_driver->getName());
2981 pMsg->SetVariable(VID_DRIVER_VERSION, m_driver->getVersion());
2982 }
5039dede
AK
2983}
2984
2985
2986//
2987// Modify object from message
2988//
2989
2990DWORD Node::ModifyFromMessage(CSCPMessage *pRequest, BOOL bAlreadyLocked)
2991{
2992 if (!bAlreadyLocked)
2993 LockData();
2994
2995 // Change primary IP address
2996 if (pRequest->IsVariableExist(VID_IP_ADDRESS))
2997 {
2998 DWORD i, dwIpAddr;
2999
3000 dwIpAddr = pRequest->GetVariableLong(VID_IP_ADDRESS);
3001
3002 // Check if received IP address is one of node's interface addresses
3003 LockChildList(FALSE);
3004 for(i = 0; i < m_dwChildCount; i++)
3005 if ((m_pChildList[i]->Type() == OBJECT_INTERFACE) &&
3006 (m_pChildList[i]->IpAddr() == dwIpAddr))
3007 break;
3008 UnlockChildList();
3009 if (i == m_dwChildCount)
3010 {
3011 UnlockData();
3012 return RCC_INVALID_IP_ADDR;
3013 }
3014
5039dede 3015 m_dwIpAddr = dwIpAddr;
5a7d6a10
VK
3016
3017 // Update primary name if it is not set with the same message
3018 if (!pRequest->IsVariableExist(VID_PRIMARY_NAME))
3019 {
3020 IpToStr(m_dwIpAddr, m_primaryName);
3021 }
5039dede
AK
3022 }
3023
fed33789 3024 // Change primary host name
5a7d6a10
VK
3025 if (pRequest->IsVariableExist(VID_PRIMARY_NAME))
3026 {
3027 pRequest->GetVariableStr(VID_PRIMARY_NAME, m_primaryName, MAX_DNS_NAME);
3028 m_dwDynamicFlags |= NDF_FORCE_CONFIGURATION_POLL | NDF_RECHECK_CAPABILITIES;
3029 }
3030
5039dede
AK
3031 // Poller node ID
3032 if (pRequest->IsVariableExist(VID_POLLER_NODE_ID))
3033 {
3034 DWORD dwNodeId;
3035 NetObj *pObject;
3036
3037 dwNodeId = pRequest->GetVariableLong(VID_POLLER_NODE_ID);
41f9a6dc
VK
3038 if (dwNodeId != 0)
3039 {
3040 pObject = FindObjectById(dwNodeId);
5039dede 3041
41f9a6dc
VK
3042 // Check if received id is a valid node id
3043 if (pObject == NULL)
3044 {
3045 UnlockData();
3046 return RCC_INVALID_OBJECT_ID;
3047 }
3048 if (pObject->Type() != OBJECT_NODE)
3049 {
3050 UnlockData();
3051 return RCC_INVALID_OBJECT_ID;
3052 }
3053 }
3054 m_dwPollerNode = dwNodeId;
5039dede
AK
3055 }
3056
3057 // Change listen port of native agent
3058 if (pRequest->IsVariableExist(VID_AGENT_PORT))
3059 m_wAgentPort = pRequest->GetVariableShort(VID_AGENT_PORT);
3060
3061 // Change authentication method of native agent
3062 if (pRequest->IsVariableExist(VID_AUTH_METHOD))
3063 m_wAuthMethod = pRequest->GetVariableShort(VID_AUTH_METHOD);
3064
3065 // Change shared secret of native agent
3066 if (pRequest->IsVariableExist(VID_SHARED_SECRET))
3067 pRequest->GetVariableStr(VID_SHARED_SECRET, m_szSharedSecret, MAX_SECRET_LENGTH);
3068
3069 // Change SNMP protocol version
3070 if (pRequest->IsVariableExist(VID_SNMP_VERSION))
14b98b3a 3071 {
5d2c5741 3072 m_snmpVersion = pRequest->GetVariableShort(VID_SNMP_VERSION);
14b98b3a
VK
3073 m_snmpSecurity->setSecurityModel((m_snmpVersion == SNMP_VERSION_3) ? SNMP_SECURITY_MODEL_USM : SNMP_SECURITY_MODEL_V2C);
3074 }
5039dede 3075
65e2005b
VK
3076 // Change SNMP port
3077 if (pRequest->IsVariableExist(VID_SNMP_PORT))
3078 m_wSNMPPort = pRequest->GetVariableShort(VID_SNMP_PORT);
3079
4b7d8903
VK
3080 // Change SNMP authentication data
3081 if (pRequest->IsVariableExist(VID_SNMP_AUTH_OBJECT))
5d2c5741 3082 {
35f836fe
VK
3083 char mbBuffer[256];
3084
3085 pRequest->GetVariableStrA(VID_SNMP_AUTH_OBJECT, mbBuffer, 256);
3086 m_snmpSecurity->setAuthName(mbBuffer);
4b7d8903 3087
35f836fe
VK
3088 pRequest->GetVariableStrA(VID_SNMP_AUTH_PASSWORD, mbBuffer, 256);
3089 m_snmpSecurity->setAuthPassword(mbBuffer);
4b7d8903 3090
35f836fe
VK
3091 pRequest->GetVariableStrA(VID_SNMP_PRIV_PASSWORD, mbBuffer, 256);
3092 m_snmpSecurity->setPrivPassword(mbBuffer);
4b7d8903
VK
3093
3094 WORD methods = pRequest->GetVariableShort(VID_SNMP_USM_METHODS);
3095 m_snmpSecurity->setAuthMethod((int)(methods & 0xFF));
3096 m_snmpSecurity->setPrivMethod((int)(methods >> 8));
5d2c5741 3097 }
5039dede
AK
3098
3099 // Change proxy node
e41fced3
VK
3100 if (pRequest->IsVariableExist(VID_AGENT_PROXY))
3101 m_dwProxyNode = pRequest->GetVariableLong(VID_AGENT_PROXY);
5039dede
AK
3102
3103 // Change SNMP proxy node
3104 if (pRequest->IsVariableExist(VID_SNMP_PROXY))
3105 m_dwSNMPProxy = pRequest->GetVariableLong(VID_SNMP_PROXY);
3106
3107 // Number of required polls
3108 if (pRequest->IsVariableExist(VID_REQUIRED_POLLS))
3109 m_iRequiredPollCount = (int)pRequest->GetVariableShort(VID_REQUIRED_POLLS);
3110
3111 // Enable/disable usage of ifXTable
3112 if (pRequest->IsVariableExist(VID_USE_IFXTABLE))
3113 m_nUseIfXTable = (BYTE)pRequest->GetVariableShort(VID_USE_IFXTABLE);
3114
3115 // Change flags
3116 if (pRequest->IsVariableExist(VID_FLAGS))
3117 {
3118 m_dwFlags &= NF_SYSTEM_FLAGS;
3119 m_dwFlags |= pRequest->GetVariableLong(VID_FLAGS) & NF_USER_FLAGS;
3120 }
3121
3122 return Template::ModifyFromMessage(pRequest, TRUE);
3123}
3124
3125
3126//
3127// Wakeup node using magic packet
3128//
3129
7c521895 3130DWORD Node::wakeUp()
5039dede
AK
3131{
3132 DWORD i, dwResult = RCC_NO_WOL_INTERFACES;
3133
3134 LockChildList(FALSE);
3135
3136 for(i = 0; i < m_dwChildCount; i++)
3137 if ((m_pChildList[i]->Type() == OBJECT_INTERFACE) &&
3138 (m_pChildList[i]->Status() != STATUS_UNMANAGED) &&
3139 (m_pChildList[i]->IpAddr() != 0))
3140 {
7c521895 3141 dwResult = ((Interface *)m_pChildList[i])->wakeUp();
5039dede
AK
3142 break;
3143 }
3144
3145 UnlockChildList();
3146 return dwResult;
3147}
3148
3149
3150//
3151// Get status of interface with given index from SNMP agent
3152//
3153
1e6b68a6 3154void Node::getInterfaceStatusFromSNMP(SNMP_Transport *pTransport, DWORD dwIndex, int *adminState, int *operState)
5039dede 3155{
1e6b68a6 3156 SnmpGetInterfaceStatus(m_snmpVersion, pTransport, dwIndex, adminState, operState);
5039dede
AK
3157}
3158
3159
3160//
3161// Get status of interface with given index from native agent
3162//
3163
1e6b68a6 3164void Node::getInterfaceStatusFromAgent(DWORD dwIndex, int *adminState, int *operState)
5039dede 3165{
35f836fe 3166 TCHAR szParam[128], szBuffer[32];
5039dede
AK
3167
3168 // Get administrative status
35f836fe 3169 _sntprintf(szParam, 128, _T("Net.Interface.AdminStatus(%u)"), dwIndex);
5039dede
AK
3170 if (GetItemFromAgent(szParam, 32, szBuffer) == DCE_SUCCESS)
3171 {
1e6b68a6 3172 *adminState = _tcstol(szBuffer, NULL, 0);
5039dede 3173
1e6b68a6 3174 switch(*adminState)
5039dede 3175 {
1e6b68a6
VK
3176 case IF_ADMIN_STATE_TESTING:
3177 *operState = IF_OPER_STATE_UNKNOWN;
5039dede 3178 break;
1e6b68a6 3179 case IF_ADMIN_STATE_DOWN:
5039dede 3180 case 0: // Agents before 0.2.1 may return 0 instead of 2
1e6b68a6 3181 *operState = IF_OPER_STATE_DOWN;
5039dede 3182 break;
1e6b68a6 3183 case IF_ADMIN_STATE_UP: // Interface administratively up, check link state
35f836fe 3184 _sntprintf(szParam, 128, _T("Net.Interface.Link(%u)"), dwIndex);
5039dede
AK
3185 if (GetItemFromAgent(szParam, 32, szBuffer) == DCE_SUCCESS)
3186 {
1e6b68a6
VK
3187 DWORD dwLinkState = _tcstoul(szBuffer, NULL, 0);
3188 *operState = (dwLinkState == 0) ? IF_OPER_STATE_DOWN : IF_OPER_STATE_UP;
5039dede
AK
3189 }
3190 else
3191 {
1e6b68a6 3192 *operState = IF_OPER_STATE_UNKNOWN;
5039dede
AK
3193 }
3194 break;
3195 default:
1e6b68a6
VK
3196 *adminState = IF_ADMIN_STATE_UNKNOWN;
3197 *operState = IF_OPER_STATE_UNKNOWN;
5039dede
AK
3198 break;
3199 }
3200 }
3201 else
3202 {
1e6b68a6
VK
3203 *adminState = IF_ADMIN_STATE_UNKNOWN;
3204 *operState = IF_OPER_STATE_UNKNOWN;
5039dede 3205 }
5039dede
AK
3206}
3207
3208
3209//
3210// Put list of supported parameters into CSCP message
3211//
3212
cc8ce218 3213void Node::writeParamListToMessage(CSCPMessage *pMsg)
5039dede 3214{
5039dede 3215 LockData();
cc8ce218
VK
3216
3217 if (m_paramList != NULL)
5039dede 3218 {
cc8ce218
VK
3219 pMsg->SetVariable(VID_NUM_PARAMETERS, (DWORD)m_paramList->size());
3220
3221 int i;
3222 DWORD dwId;
3223 for(i = 0, dwId = VID_PARAM_LIST_BASE; i < m_paramList->size(); i++)
5039dede 3224 {
cc8ce218
VK
3225 NXC_AGENT_PARAM *p = m_paramList->get(i);
3226 pMsg->SetVariable(dwId++, p->szName);
3227 pMsg->SetVariable(dwId++, p->szDescription);
3228 pMsg->SetVariable(dwId++, (WORD)p->iDataType);
5039dede 3229 }
cc8ce218 3230 DbgPrintf(6, _T("Node[%s]::writeParamListToMessage(): sending %d parameters"), m_szName, m_paramList->size());
5039dede
AK
3231 }
3232 else
3233 {
cc8ce218 3234 DbgPrintf(6, _T("Node[%s]::writeParamListToMessage(): m_paramList == NULL"), m_szName);
5039dede
AK
3235 pMsg->SetVariable(VID_NUM_PARAMETERS, (DWORD)0);
3236 }
cc8ce218
VK
3237
3238 if (m_tableList != NULL)
3239 {
3240 pMsg->SetVariable(VID_NUM_TABLES, (DWORD)m_tableList->size());
3241
3242 int i;
3243 DWORD dwId;
3244 for(i = 0, dwId = VID_TABLE_LIST_BASE; i < m_tableList->size(); i++)
3245 {
3246 NXC_AGENT_TABLE *t = m_tableList->get(i);
3247 pMsg->SetVariable(dwId++, t->name);
3248 pMsg->SetVariable(dwId++, t->instanceColumn);
3249 pMsg->SetVariable(dwId++, t->description);
3250 }
3251 DbgPrintf(6, _T("Node[%s]::writeParamListToMessage(): sending %d tables"), m_szName, m_tableList->size());
3252 }
3253 else
3254 {
3255 DbgPrintf(6, _T("Node[%s]::writeParamListToMessage(): m_tableList == NULL"), m_szName);
3256 pMsg->SetVariable(VID_NUM_TABLES, (DWORD)0);
3257 }
3258
3259 UnlockData();
5039dede
AK
3260}
3261
3262
3263//
3264// Open list of supported parameters for reading
3265//
3266
cc8ce218 3267void Node::openParamList(StructArray<NXC_AGENT_PARAM> **paramList)
5039dede
AK
3268{
3269 LockData();
cc8ce218 3270 *paramList = m_paramList;
5039dede
AK
3271}
3272
3273
3274//
3275// Check status of network service
3276//
3277
3278DWORD Node::CheckNetworkService(DWORD *pdwStatus, DWORD dwIpAddr, int iServiceType,
3279 WORD wPort, WORD wProto, TCHAR *pszRequest,
3280 TCHAR *pszResponse)
3281{
3282 DWORD dwError = ERR_NOT_CONNECTED;
3283
3284 if ((m_dwFlags & NF_IS_NATIVE_AGENT) &&
3285 (!(m_dwDynamicFlags & NDF_AGENT_UNREACHABLE)) &&
3286 (!(m_dwDynamicFlags & NDF_UNREACHABLE)))
3287 {
3288 AgentConnection *pConn;
3289
cd9f247e 3290 pConn = createAgentConnection();
5039dede
AK
3291 if (pConn != NULL)
3292 {
4687826e 3293 dwError = pConn->checkNetworkService(pdwStatus, dwIpAddr, iServiceType,
5039dede 3294 wPort, wProto, pszRequest, pszResponse);
7c521895 3295 pConn->disconnect();
5039dede
AK
3296 delete pConn;
3297 }
3298 }
3299 return dwError;
3300}
3301
3302
3303//
3304// Handler for object deletion
3305//
3306
3307void Node::OnObjectDelete(DWORD dwObjectId)
3308{
f5d16551 3309 LockData();
5039dede
AK
3310 if (dwObjectId == m_dwPollerNode)
3311 {
3312 // If deleted object is our poller node, change it to default
5039dede
AK
3313 m_dwPollerNode = 0;
3314 Modify();
3315 DbgPrintf(3, _T("Node \"%s\": poller node %d deleted"), m_szName, dwObjectId);
3316 }
f5d16551 3317 UnlockData();
5039dede
AK
3318}
3319
3320
3321//
3322// Check node for OSPF support
3323//
3324
3325void Node::CheckOSPFSupport(SNMP_Transport *pTransport)
3326{
3327 LONG nAdminStatus;
3328
5d2c5741 3329 if (SnmpGet(m_snmpVersion, pTransport,
f42b8099 3330 _T(".1.3.6.1.2.1.14.1.2.0"), NULL, 0, &nAdminStatus, sizeof(LONG), 0) == SNMP_ERR_SUCCESS)
5039dede
AK
3331 {
3332 LockData();
3333 if (nAdminStatus)
3334 {
3335 m_dwFlags |= NF_IS_OSPF;
3336 }
3337 else
3338 {
3339 m_dwFlags &= ~NF_IS_OSPF;
3340 }
3341 UnlockData();
3342 }
3343}
3344
3345
3346//
3347// Create ready to use agent connection
3348//
3349
cd9f247e 3350AgentConnectionEx *Node::createAgentConnection()
5039dede 3351{
45d84f8a 3352 AgentConnectionEx *conn;
5039dede
AK
3353
3354 if ((!(m_dwFlags & NF_IS_NATIVE_AGENT)) ||
6b2bb22c 3355 (m_dwFlags & NF_DISABLE_NXCP) ||
5039dede
AK
3356 (m_dwDynamicFlags & NDF_AGENT_UNREACHABLE) ||
3357 (m_dwDynamicFlags & NDF_UNREACHABLE))
3358 return NULL;
3359
1fa5706e
VK
3360 DbgPrintf(6, _T("Node::createAgentConnection(%s [%d])"), m_szName, (int)m_dwId);
3361 conn = new AgentConnectionEx(htonl(m_dwIpAddr), m_wAgentPort, m_wAuthMethod, m_szSharedSecret);
7c521895
VK
3362 setAgentProxy(conn);
3363 if (!conn->connect(g_pServerKey))
5039dede 3364 {
45d84f8a
VK
3365 delete conn;
3366 conn = NULL;
5039dede 3367 }
1fa5706e 3368 DbgPrintf(6, _T("Node::createAgentConnection(%s [%d]): conn=%p"), m_szName, (int)m_dwId, conn);
45d84f8a 3369 return conn;
5039dede
AK
3370}
3371
3372
3373//
3374// Get last collected values of all DCIs
3375//
3376
b9a8e081 3377DWORD Node::getLastValues(CSCPMessage *msg)
5039dede 3378{
7c521895 3379 lockDciAccess();
5039dede 3380
16d6f798
VK
3381 DWORD dwId = VID_DCI_VALUES_BASE, dwCount = 0;
3382 for(int i = 0; i < m_dcObjects->size(); i++)
5039dede 3383 {
16d6f798 3384 DCObject *object = m_dcObjects->get(i);
92c51b1d 3385 if (_tcsnicmp(object->getDescription(), _T("@system."), 8))
5039dede 3386 {
92c51b1d
VK
3387 if (object->getType() == DCO_TYPE_ITEM)
3388 {
3389 ((DCItem *)object)->getLastValue(msg, dwId);
3390 dwId += 50;
3391 dwCount++;
3392 }
3393 else if (object->getType() == DCO_TYPE_TABLE)
3394 {
3395 ((DCTable *)object)->getLastValueSummary(msg, dwId);
3396 dwId += 50;
3397 dwCount++;
3398 }
5039dede
AK
3399 }
3400 }
b9a8e081 3401 msg->SetVariable(VID_NUM_ITEMS, dwCount);
5039dede 3402
7c521895 3403 unlockDciAccess();
5039dede
AK
3404 return RCC_SUCCESS;
3405}
3406
3407
3408//
b9a8e081
VK
3409// Get last collected values of givel table
3410//
3411
3412DWORD Node::getTableLastValues(DWORD dciId, CSCPMessage *msg)
3413{
3414 DWORD rcc = RCC_INVALID_DCI_ID;
3415
3416 lockDciAccess();
3417
3418 for(int i = 0; i < m_dcObjects->size(); i++)
3419 {
3420 DCObject *object = m_dcObjects->get(i);
3421 if ((object->getId() == dciId) && (object->getType() == DCO_TYPE_TABLE))
3422 {
3423 ((DCTable *)object)->getLastValue(msg);
3424 rcc = RCC_SUCCESS;
3425 break;
3426 }
3427 }
3428
3429 unlockDciAccess();
3430 return rcc;
3431}
3432
3433
3434//
5039dede
AK
3435// Clean expired DCI data
3436//
3437