Intermediate commit:
[public/netxms.git] / src / server / core / np.cpp
CommitLineData
5039dede
AK
1/*
2** NetXMS - Network Management System
a749a70a 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: np.cpp
20**
21**/
22
23#include "nxcore.h"
24
25
26//
27// Externals
28//
29
30extern Queue g_nodePollerQueue;
31
32
33//
34// Discovery class
35//
36
37class NXSL_DiscoveryClass : public NXSL_Class
38{
39public:
40 NXSL_DiscoveryClass();
41
bb51576f 42 virtual NXSL_Value *getAttr(NXSL_Object *pObject, const TCHAR *pszAttr);
5039dede
AK
43};
44
45
46//
47// Implementation of discovery class
48//
49
50NXSL_DiscoveryClass::NXSL_DiscoveryClass()
51 :NXSL_Class()
52{
35f836fe 53 _tcscpy(m_szName, _T("NewNode"));
5039dede
AK
54}
55
bb51576f 56NXSL_Value *NXSL_DiscoveryClass::getAttr(NXSL_Object *pObject, const TCHAR *pszAttr)
5039dede
AK
57{
58 DISCOVERY_FILTER_DATA *pData;
59 NXSL_Value *pValue = NULL;
35f836fe 60 TCHAR szBuffer[256];
5039dede 61
bb5365ed 62 pData = (DISCOVERY_FILTER_DATA *)pObject->getData();
35f836fe 63 if (!_tcscmp(pszAttr, _T("ipAddr")))
5039dede
AK
64 {
65 IpToStr(pData->dwIpAddr, szBuffer);
66 pValue = new NXSL_Value(szBuffer);
67 }
35f836fe 68 else if (!_tcscmp(pszAttr, _T("netMask")))
5039dede
AK
69 {
70 IpToStr(pData->dwNetMask, szBuffer);
71 pValue = new NXSL_Value(szBuffer);
72 }
35f836fe 73 else if (!_tcscmp(pszAttr, _T("subnet")))
5039dede
AK
74 {
75 IpToStr(pData->dwSubnetAddr, szBuffer);
76 pValue = new NXSL_Value(szBuffer);
77 }
35f836fe 78 else if (!_tcscmp(pszAttr, _T("isAgent")))
5039dede
AK
79 {
80 pValue = new NXSL_Value((LONG)((pData->dwFlags & NNF_IS_AGENT) ? 1 : 0));
81 }
35f836fe 82 else if (!_tcscmp(pszAttr, _T("isSNMP")))
5039dede
AK
83 {
84 pValue = new NXSL_Value((LONG)((pData->dwFlags & NNF_IS_SNMP) ? 1 : 0));
85 }
35f836fe 86 else if (!_tcscmp(pszAttr, _T("isBridge")))
5039dede
AK
87 {
88 pValue = new NXSL_Value((LONG)((pData->dwFlags & NNF_IS_BRIDGE) ? 1 : 0));
89 }
35f836fe 90 else if (!_tcscmp(pszAttr, _T("isRouter")))
5039dede
AK
91 {
92 pValue = new NXSL_Value((LONG)((pData->dwFlags & NNF_IS_ROUTER) ? 1 : 0));
93 }
35f836fe 94 else if (!_tcscmp(pszAttr, _T("isPrinter")))
5039dede
AK
95 {
96 pValue = new NXSL_Value((LONG)((pData->dwFlags & NNF_IS_PRINTER) ? 1 : 0));
97 }
35f836fe 98 else if (!_tcscmp(pszAttr, _T("isCDP")))
5039dede
AK
99 {
100 pValue = new NXSL_Value((LONG)((pData->dwFlags & NNF_IS_CDP) ? 1 : 0));
101 }
35f836fe 102 else if (!_tcscmp(pszAttr, _T("isSONMP")))
5039dede
AK
103 {
104 pValue = new NXSL_Value((LONG)((pData->dwFlags & NNF_IS_SONMP) ? 1 : 0));
105 }
35f836fe 106 else if (!_tcscmp(pszAttr, _T("isLLDP")))
5039dede
AK
107 {
108 pValue = new NXSL_Value((LONG)((pData->dwFlags & NNF_IS_LLDP) ? 1 : 0));
109 }
35f836fe 110 else if (!_tcscmp(pszAttr, _T("snmpVersion")))
5039dede
AK
111 {
112 pValue = new NXSL_Value((LONG)pData->nSNMPVersion);
113 }
35f836fe 114 else if (!_tcscmp(pszAttr, _T("snmpOID")))
5039dede
AK
115 {
116 pValue = new NXSL_Value(pData->szObjectId);
117 }
35f836fe 118 else if (!_tcscmp(pszAttr, _T("agentVersion")))
5039dede
AK
119 {
120 pValue = new NXSL_Value(pData->szAgentVersion);
121 }
35f836fe 122 else if (!_tcscmp(pszAttr, _T("platformName")))
5039dede
AK
123 {
124 pValue = new NXSL_Value(pData->szPlatform);
125 }
126 return pValue;
127}
128
129
130//
131// Discovery class object
132//
133
134static NXSL_DiscoveryClass m_nxslDiscoveryClass;
135
136
137//
138// Poll new node for configuration
139// Returns pointer to new node object on success or NULL on failure
140//
141
07f6978d 142Node *PollNewNode(DWORD dwIpAddr, DWORD dwNetMask, DWORD dwCreationFlags,
f1b6417b 143 WORD agentPort, WORD snmpPort, TCHAR *pszName, DWORD dwProxyNode, DWORD dwSNMPProxy,
a749a70a 144 Cluster *pCluster, DWORD zoneId, bool doConfPoll, bool discoveredNode)
5039dede
AK
145{
146 Node *pNode;
35f836fe 147 TCHAR szIpAddr1[32], szIpAddr2[32];
5039dede
AK
148 DWORD dwFlags = 0;
149
dc25b21c 150 DbgPrintf(4, _T("PollNode(%s,%s) zone %d"), IpToStr(dwIpAddr, szIpAddr1), IpToStr(dwNetMask, szIpAddr2), (int)zoneId);
5039dede 151 // Check for node existence
89135050
VK
152 if ((FindNodeByIP(zoneId, dwIpAddr) != NULL) ||
153 (FindSubnetByIP(zoneId, dwIpAddr) != NULL))
5039dede 154 {
35f836fe 155 DbgPrintf(4, _T("PollNode: Node %s already exist in database"),
5039dede
AK
156 IpToStr(dwIpAddr, szIpAddr1));
157 return NULL;
158 }
159
160 if (dwCreationFlags & NXC_NCF_DISABLE_ICMP)
161 dwFlags |= NF_DISABLE_ICMP;
162 if (dwCreationFlags & NXC_NCF_DISABLE_SNMP)
163 dwFlags |= NF_DISABLE_SNMP;
164 if (dwCreationFlags & NXC_NCF_DISABLE_NXCP)
165 dwFlags |= NF_DISABLE_NXCP;
dc25b21c 166 pNode = new Node(dwIpAddr, dwFlags, dwProxyNode, dwSNMPProxy, zoneId);
f1b6417b
VK
167 if (agentPort != 0)
168 pNode->setAgentPort(agentPort);
169 if (snmpPort != 0)
170 pNode->setSnmpPort(snmpPort);
5039dede
AK
171 NetObjInsert(pNode, TRUE);
172 if (pszName != NULL)
eec253a8 173 pNode->setName(pszName);
5039dede 174
a749a70a
VK
175 // Use DNS name as primary name if required
176 if (discoveredNode && ConfigReadInt(_T("UseDNSNameForDiscoveredNodes"), 0))
177 {
178 DWORD ip = htonl(dwIpAddr);
179 struct hostent *hs = gethostbyaddr((char *)&ip, 4, AF_INET);
180 if (hs != NULL)
181 {
182 TCHAR dnsName[MAX_DNS_NAME];
183#ifdef UNICODE
184 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, hs->h_name, -1, dnsName, MAX_DNS_NAME);
185 dnsName[MAX_DNS_NAME - 1] = 0;
186#else
187 nx_strncpy(dnsName, hs->h_name, MAX_DNS_NAME);
188#endif
189 if (ntohl(ResolveHostName(dnsName)) == dwIpAddr)
190 {
191 // We have valid DNS name which resolves back to node's IP address, use it as primary name
192 pNode->setPrimaryName(dnsName);
193 DbgPrintf(4, _T("PollNode: Using DNS name %s as primary name for node %s"), dnsName, IpToStr(dwIpAddr, szIpAddr1));
194 }
195 }
196 }
197
5039dede
AK
198 // Bind node to cluster before first configuration poll
199 if (pCluster != NULL)
200 {
1da08bc6 201 pCluster->ApplyToNode(pNode);
5039dede
AK
202 }
203
204 if (dwCreationFlags & NXC_NCF_CREATE_UNMANAGED)
205 {
eec253a8 206 pNode->setMgmtStatus(FALSE);
5039dede 207 }
5039dede
AK
208
209 // Add default DCIs
16d6f798 210 pNode->addDCObject(new DCItem(CreateUniqueId(IDG_ITEM), _T("Status"), DS_INTERNAL, DCI_DT_INT, 60, 30, pNode));
588f7351
VK
211
212 if (doConfPoll)
213 pNode->configurationPoll(NULL, 0, -1, dwNetMask);
214
215 pNode->unhide();
216 PostEvent(EVENT_NODE_ADDED, pNode->Id(), NULL);
217
5039dede
AK
218 return pNode;
219}
220
baa5324c
AK
221static Node *GetOldNodeWithNewIP(DWORD dwIpAddr, DWORD dwNetMask, DWORD dwZoneId, BYTE *bMacAddr)
222{
223 Subnet *subnet;
224 BYTE nodeMacAddr[MAC_ADDR_LENGTH];
225 TCHAR szIpAddr[16], szMacAddr[20];
226
227 IpToStr(dwIpAddr, szIpAddr);
228 MACToStr(bMacAddr, szMacAddr);
229 DbgPrintf(6, _T("GetOldNodeWithNewIP: ip=%s mac=%s"), szIpAddr, szMacAddr);
230
231 if (bMacAddr == NULL)
232 {
233 subnet = FindSubnetByIP(dwZoneId, dwIpAddr);
234 if (subnet != NULL)
235 {
236 BOOL found = subnet->findMacAddress(dwIpAddr, nodeMacAddr);
237 if (!found)
238 {
239 DbgPrintf(6, _T("GetOldNodeWithNewIP: subnet not found"));
240 return NULL;
241 }
242 }
243 }
244 else
245 {
246 memcpy(nodeMacAddr, bMacAddr, MAC_ADDR_LENGTH);
247 }
248
249 Node *node = FindNodeByMAC(nodeMacAddr);
250
251 if (node == NULL)
252 DbgPrintf(6, _T("GetOldNodeWithNewIP: returning null (FindNodeByMac!)"));
253
254 return node;
255}
5039dede
AK
256
257//
258// Check if newly discovered node should be added
259//
260
baa5324c 261static BOOL AcceptNewNode(DWORD dwIpAddr, DWORD dwNetMask, DWORD zoneId, BYTE *macAddr)
5039dede
AK
262{
263 DISCOVERY_FILTER_DATA data;
df8a4ca2 264 TCHAR szFilter[MAX_DB_STRING], szBuffer[256], szIpAddr[16];
5039dede
AK
265 DWORD dwTemp;
266 AgentConnection *pAgentConn;
267 NXSL_Program *pScript;
268 NXSL_Value *pValue;
269 BOOL bResult = FALSE;
270 SNMP_UDPTransport *pTransport;
271
272 IpToStr(dwIpAddr, szIpAddr);
89135050
VK
273 if ((FindNodeByIP(zoneId, dwIpAddr) != NULL) ||
274 (FindSubnetByIP(zoneId, dwIpAddr) != NULL))
5039dede 275 {
35f836fe 276 DbgPrintf(4, _T("AcceptNewNode(%s): node already exist in database"), szIpAddr);
5039dede
AK
277 return FALSE; // Node already exist in database
278 }
279
baa5324c
AK
280 Node *oldNode;
281 if ((oldNode = GetOldNodeWithNewIP(dwIpAddr, dwNetMask, zoneId, macAddr)) != NULL)
282 {
283 TCHAR szOldIpAddr[16];
284 IpToStr(oldNode->IpAddr(), szOldIpAddr);
285 DbgPrintf(4, _T("AcceptNewNode(%s): node already exist in database with ip %s, will change to new"),
286 szIpAddr, szOldIpAddr);
287 oldNode->changeIPAddress(dwIpAddr);
288 return FALSE;
289 }
290
5039dede 291 // Read configuration
35f836fe 292 ConfigReadStr(_T("DiscoveryFilter"), szFilter, MAX_DB_STRING, _T(""));
5039dede
AK
293 StrStrip(szFilter);
294
295 // Run filter script
35f836fe 296 if ((szFilter[0] == 0) || (!_tcsicmp(szFilter, _T("none"))))
5039dede 297 {
35f836fe 298 DbgPrintf(4, _T("AcceptNewNode(%s): no filtering, node accepted"), szIpAddr);
5039dede
AK
299 return TRUE; // No filtering
300 }
301
302 // Initialize new node data
303 memset(&data, 0, sizeof(DISCOVERY_FILTER_DATA));
304 data.dwIpAddr = dwIpAddr;
305 data.dwNetMask = dwNetMask;
306 data.dwSubnetAddr = dwIpAddr & dwNetMask;
307
308 // Check SNMP support
35f836fe 309 DbgPrintf(4, _T("AcceptNewNode(%s): checking SNMP support"), szIpAddr);
5039dede 310 pTransport = new SNMP_UDPTransport;
c4366266 311 pTransport->createUDPTransport(NULL, htonl(dwIpAddr), 161);
df8a4ca2
VK
312 SNMP_SecurityContext *ctx = SnmpCheckCommSettings(pTransport, &data.nSNMPVersion, NULL);
313 if (ctx != NULL)
5039dede
AK
314 {
315 data.dwFlags |= NNF_IS_SNMP;
df8a4ca2 316 delete ctx;
5039dede
AK
317 }
318
319 // Check NetXMS agent support
35f836fe 320 DbgPrintf(4, _T("AcceptNewNode(%s): checking NetXMS agent"), szIpAddr);
5039dede 321 pAgentConn = new AgentConnection(htonl(dwIpAddr), AGENT_LISTEN_PORT,
35f836fe 322 AUTH_NONE, _T(""));
7c521895 323 if (pAgentConn->connect(g_pServerKey))
5039dede
AK
324 {
325 data.dwFlags |= NNF_IS_AGENT;
4687826e
VK
326 pAgentConn->getParameter(_T("Agent.Version"), MAX_AGENT_VERSION_LEN, data.szAgentVersion);
327 pAgentConn->getParameter(_T("System.PlatformName"), MAX_PLATFORM_NAME_LEN, data.szPlatform);
5039dede
AK
328 }
329
330 // Check if node is a router
331 if (data.dwFlags & NNF_IS_SNMP)
332 {
5d2c5741 333 if (SnmpGet(data.nSNMPVersion, pTransport,
f42b8099 334 _T(".1.3.6.1.2.1.4.1.0"), NULL, 0, &dwTemp, sizeof(DWORD), 0) == SNMP_ERR_SUCCESS)
5039dede
AK
335 {
336 if (dwTemp == 1)
337 data.dwFlags |= NNF_IS_ROUTER;
338 }
339 }
340 else if (data.dwFlags & NNF_IS_AGENT)
341 {
342 // Check IP forwarding status
4687826e 343 if (pAgentConn->getParameter(_T("Net.IP.Forwarding"), 16, szBuffer) == ERR_SUCCESS)
5039dede
AK
344 {
345 if (_tcstoul(szBuffer, NULL, 10) != 0)
346 data.dwFlags |= NNF_IS_ROUTER;
347 }
348 }
349
350 // Check various SNMP device capabilities
351 if (data.dwFlags & NNF_IS_SNMP)
352 {
353 // Get SNMP OID
5d2c5741 354 SnmpGet(data.nSNMPVersion, pTransport,
f42b8099 355 _T(".1.3.6.1.2.1.1.2.0"), NULL, 0, data.szObjectId, MAX_OID_LEN * 4, 0);
5039dede
AK
356
357 // Check if node is a bridge
5d2c5741 358 if (SnmpGet(data.nSNMPVersion, pTransport,
f42b8099 359 _T(".1.3.6.1.2.1.17.1.1.0"), NULL, 0, szBuffer, 256, 0) == SNMP_ERR_SUCCESS)
5039dede
AK
360 {
361 data.dwFlags |= NNF_IS_BRIDGE;
362 }
363
364 // Check for CDP (Cisco Discovery Protocol) support
5d2c5741 365 if (SnmpGet(data.nSNMPVersion, pTransport,
f42b8099 366 _T(".1.3.6.1.4.1.9.9.23.1.3.1.0"), NULL, 0, &dwTemp, sizeof(DWORD), 0) == SNMP_ERR_SUCCESS)
5039dede
AK
367 {
368 if (dwTemp == 1)
369 data.dwFlags |= NNF_IS_CDP;
370 }
371
372 // Check for SONMP (Nortel topology discovery protocol) support
5d2c5741 373 if (SnmpGet(data.nSNMPVersion, pTransport,
f42b8099 374 _T(".1.3.6.1.4.1.45.1.6.13.1.2.0"), NULL, 0, &dwTemp, sizeof(DWORD), 0) == SNMP_ERR_SUCCESS)
5039dede
AK
375 {
376 if (dwTemp == 1)
377 data.dwFlags |= NNF_IS_SONMP;
378 }
379
380 // Check for LLDP (Link Layer Discovery Protocol) support
5d2c5741 381 if (SnmpGet(data.nSNMPVersion, pTransport,
f42b8099 382 _T(".1.0.8802.1.1.2.1.3.2.0"), NULL, 0, szBuffer, 256, 0) == SNMP_ERR_SUCCESS)
5039dede
AK
383 {
384 data.dwFlags |= NNF_IS_LLDP;
385 }
386 }
387
388 // Cleanup
389 delete pAgentConn;
390 delete pTransport;
391
392 // Check if we use simple filter instead of script
35f836fe 393 if (!_tcsicmp(szFilter, _T("auto")))
5039dede
AK
394 {
395 DWORD dwFlags;
396 DB_RESULT hResult;
397 int i, nRows, nType;
398
399 dwFlags = ConfigReadULong(_T("DiscoveryFilterFlags"), DFF_ALLOW_AGENT | DFF_ALLOW_SNMP);
35f836fe 400 DbgPrintf(4, _T("AcceptNewNode(%s): auto filter, dwFlags=%04X"), szIpAddr, dwFlags);
5039dede
AK
401
402 if ((dwFlags & (DFF_ALLOW_AGENT | DFF_ALLOW_SNMP)) == 0)
403 {
404 bResult = TRUE;
405 }
406 else
407 {
408 if (dwFlags & DFF_ALLOW_AGENT)
409 {
410 if (data.dwFlags & NNF_IS_AGENT)
411 bResult = TRUE;
412 }
413
414 if (dwFlags & DFF_ALLOW_SNMP)
415 {
416 if (data.dwFlags & NNF_IS_SNMP)
417 bResult = TRUE;
418 }
419 }
420
421 // Check range
422 if ((dwFlags & DFF_ONLY_RANGE) && bResult)
423 {
35f836fe 424 DbgPrintf(4, _T("AcceptNewNode(%s): auto filter - checking range"), szIpAddr);
5039dede
AK
425 hResult = DBSelect(g_hCoreDB, _T("SELECT addr_type,addr1,addr2 FROM address_lists WHERE list_type=2"));
426 if (hResult != NULL)
427 {
428 nRows = DBGetNumRows(hResult);
429 for(i = 0, bResult = FALSE; (i < nRows) && (!bResult); i++)
430 {
431 nType = DBGetFieldLong(hResult, i, 0);
432 if (nType == 0)
433 {
434 // Subnet
435 bResult = (data.dwIpAddr & DBGetFieldIPAddr(hResult, i, 2)) == DBGetFieldIPAddr(hResult, i, 1);
436 }
437 else
438 {
439 // Range
440 bResult = ((data.dwIpAddr >= DBGetFieldIPAddr(hResult, i, 1)) &&
441 (data.dwIpAddr <= DBGetFieldIPAddr(hResult, i, 2)));
442 }
443 }
444 DBFreeResult(hResult);
445 }
446 }
35f836fe 447 DbgPrintf(4, _T("AcceptNewNode(%s): auto filter - bResult=%d"), szIpAddr, bResult);
5039dede
AK
448 }
449 else
450 {
bb5365ed
VK
451 g_pScriptLibrary->lock();
452 pScript = g_pScriptLibrary->findScript(szFilter);
5039dede
AK
453 if (pScript != NULL)
454 {
35f836fe 455 DbgPrintf(4, _T("AcceptNewNode(%s): Running filter script %s"), szIpAddr, szFilter);
5039dede 456 pValue = new NXSL_Value(new NXSL_Object(&m_nxslDiscoveryClass, &data));
13ceb0fb 457 if (pScript->run(new NXSL_ServerEnv, 1, &pValue) == 0)
5039dede 458 {
bb5365ed 459 bResult = (pScript->getResult()->getValueAsInt32() != 0) ? TRUE : FALSE;
35f836fe 460 DbgPrintf(4, _T("AcceptNewNode(%s): Filter script result: %d"), szIpAddr, bResult);
5039dede
AK
461 }
462 else
463 {
35f836fe 464 DbgPrintf(4, _T("AcceptNewNode(%s): Filter script execution error: %s"),
bb5365ed 465 szIpAddr, pScript->getErrorText());
35f836fe 466 PostEvent(EVENT_SCRIPT_ERROR, g_dwMgmtNode, "ssd", szFilter,
bb5365ed 467 pScript->getErrorText(), 0);
5039dede
AK
468 }
469 }
470 else
471 {
35f836fe 472 DbgPrintf(4, _T("AcceptNewNode(%s): Cannot find filter script %s"), szIpAddr, szFilter);
5039dede 473 }
bb5365ed 474 g_pScriptLibrary->unlock();
5039dede
AK
475 }
476
477 return bResult;
478}
479
480
481//
482// Node poller thread (poll new nodes and put them into the database)
483//
484
485THREAD_RESULT THREAD_CALL NodePoller(void *arg)
486{
487 NEW_NODE *pInfo;
488 TCHAR szIpAddr[16], szNetMask[16];
489
35f836fe 490 DbgPrintf(1, _T("Node poller started"));
5039dede 491
89135050 492 while(!IsShutdownInProgress())
5039dede
AK
493 {
494 pInfo = (NEW_NODE *)g_nodePollerQueue.GetOrBlock();
495 if (pInfo == INVALID_POINTER_VALUE)
496 break; // Shutdown indicator received
497
dc25b21c
VK
498 DbgPrintf(4, _T("NodePoller: processing node %s/%s in zone %d"),
499 IpToStr(pInfo->dwIpAddr, szIpAddr), IpToStr(pInfo->dwNetMask, szNetMask), (int)pInfo->zoneId);
baa5324c 500 if (AcceptNewNode(pInfo->dwIpAddr, pInfo->dwNetMask, pInfo->zoneId, pInfo->bMacAddr))
07f6978d 501 {
f1b6417b 502 Node *node = PollNewNode(pInfo->dwIpAddr, pInfo->dwNetMask, 0, 0, 0, NULL, 0, 0, NULL, pInfo->zoneId, true, true);
07f6978d 503 }
5039dede
AK
504 free(pInfo);
505 }
35f836fe 506 DbgPrintf(1, _T("Node poller thread terminated"));
5039dede
AK
507 return THREAD_OK;
508}