minor changes in installers
[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
221
222//
223// Check if newly discovered node should be added
224//
225
89135050 226static BOOL AcceptNewNode(DWORD dwIpAddr, DWORD dwNetMask, DWORD zoneId)
5039dede
AK
227{
228 DISCOVERY_FILTER_DATA data;
df8a4ca2 229 TCHAR szFilter[MAX_DB_STRING], szBuffer[256], szIpAddr[16];
5039dede
AK
230 DWORD dwTemp;
231 AgentConnection *pAgentConn;
232 NXSL_Program *pScript;
233 NXSL_Value *pValue;
234 BOOL bResult = FALSE;
235 SNMP_UDPTransport *pTransport;
236
237 IpToStr(dwIpAddr, szIpAddr);
89135050
VK
238 if ((FindNodeByIP(zoneId, dwIpAddr) != NULL) ||
239 (FindSubnetByIP(zoneId, dwIpAddr) != NULL))
5039dede 240 {
35f836fe 241 DbgPrintf(4, _T("AcceptNewNode(%s): node already exist in database"), szIpAddr);
5039dede
AK
242 return FALSE; // Node already exist in database
243 }
244
245 // Read configuration
35f836fe 246 ConfigReadStr(_T("DiscoveryFilter"), szFilter, MAX_DB_STRING, _T(""));
5039dede
AK
247 StrStrip(szFilter);
248
249 // Run filter script
35f836fe 250 if ((szFilter[0] == 0) || (!_tcsicmp(szFilter, _T("none"))))
5039dede 251 {
35f836fe 252 DbgPrintf(4, _T("AcceptNewNode(%s): no filtering, node accepted"), szIpAddr);
5039dede
AK
253 return TRUE; // No filtering
254 }
255
256 // Initialize new node data
257 memset(&data, 0, sizeof(DISCOVERY_FILTER_DATA));
258 data.dwIpAddr = dwIpAddr;
259 data.dwNetMask = dwNetMask;
260 data.dwSubnetAddr = dwIpAddr & dwNetMask;
261
262 // Check SNMP support
35f836fe 263 DbgPrintf(4, _T("AcceptNewNode(%s): checking SNMP support"), szIpAddr);
5039dede 264 pTransport = new SNMP_UDPTransport;
c4366266 265 pTransport->createUDPTransport(NULL, htonl(dwIpAddr), 161);
df8a4ca2
VK
266 SNMP_SecurityContext *ctx = SnmpCheckCommSettings(pTransport, &data.nSNMPVersion, NULL);
267 if (ctx != NULL)
5039dede
AK
268 {
269 data.dwFlags |= NNF_IS_SNMP;
df8a4ca2 270 delete ctx;
5039dede
AK
271 }
272
273 // Check NetXMS agent support
35f836fe 274 DbgPrintf(4, _T("AcceptNewNode(%s): checking NetXMS agent"), szIpAddr);
5039dede 275 pAgentConn = new AgentConnection(htonl(dwIpAddr), AGENT_LISTEN_PORT,
35f836fe 276 AUTH_NONE, _T(""));
7c521895 277 if (pAgentConn->connect(g_pServerKey))
5039dede
AK
278 {
279 data.dwFlags |= NNF_IS_AGENT;
4687826e
VK
280 pAgentConn->getParameter(_T("Agent.Version"), MAX_AGENT_VERSION_LEN, data.szAgentVersion);
281 pAgentConn->getParameter(_T("System.PlatformName"), MAX_PLATFORM_NAME_LEN, data.szPlatform);
5039dede
AK
282 }
283
284 // Check if node is a router
285 if (data.dwFlags & NNF_IS_SNMP)
286 {
5d2c5741 287 if (SnmpGet(data.nSNMPVersion, pTransport,
f42b8099 288 _T(".1.3.6.1.2.1.4.1.0"), NULL, 0, &dwTemp, sizeof(DWORD), 0) == SNMP_ERR_SUCCESS)
5039dede
AK
289 {
290 if (dwTemp == 1)
291 data.dwFlags |= NNF_IS_ROUTER;
292 }
293 }
294 else if (data.dwFlags & NNF_IS_AGENT)
295 {
296 // Check IP forwarding status
4687826e 297 if (pAgentConn->getParameter(_T("Net.IP.Forwarding"), 16, szBuffer) == ERR_SUCCESS)
5039dede
AK
298 {
299 if (_tcstoul(szBuffer, NULL, 10) != 0)
300 data.dwFlags |= NNF_IS_ROUTER;
301 }
302 }
303
304 // Check various SNMP device capabilities
305 if (data.dwFlags & NNF_IS_SNMP)
306 {
307 // Get SNMP OID
5d2c5741 308 SnmpGet(data.nSNMPVersion, pTransport,
f42b8099 309 _T(".1.3.6.1.2.1.1.2.0"), NULL, 0, data.szObjectId, MAX_OID_LEN * 4, 0);
5039dede
AK
310
311 // Check if node is a bridge
5d2c5741 312 if (SnmpGet(data.nSNMPVersion, pTransport,
f42b8099 313 _T(".1.3.6.1.2.1.17.1.1.0"), NULL, 0, szBuffer, 256, 0) == SNMP_ERR_SUCCESS)
5039dede
AK
314 {
315 data.dwFlags |= NNF_IS_BRIDGE;
316 }
317
318 // Check for CDP (Cisco Discovery Protocol) support
5d2c5741 319 if (SnmpGet(data.nSNMPVersion, pTransport,
f42b8099 320 _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
321 {
322 if (dwTemp == 1)
323 data.dwFlags |= NNF_IS_CDP;
324 }
325
326 // Check for SONMP (Nortel topology discovery protocol) support
5d2c5741 327 if (SnmpGet(data.nSNMPVersion, pTransport,
f42b8099 328 _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
329 {
330 if (dwTemp == 1)
331 data.dwFlags |= NNF_IS_SONMP;
332 }
333
334 // Check for LLDP (Link Layer Discovery Protocol) support
5d2c5741 335 if (SnmpGet(data.nSNMPVersion, pTransport,
f42b8099 336 _T(".1.0.8802.1.1.2.1.3.2.0"), NULL, 0, szBuffer, 256, 0) == SNMP_ERR_SUCCESS)
5039dede
AK
337 {
338 data.dwFlags |= NNF_IS_LLDP;
339 }
340 }
341
342 // Cleanup
343 delete pAgentConn;
344 delete pTransport;
345
346 // Check if we use simple filter instead of script
35f836fe 347 if (!_tcsicmp(szFilter, _T("auto")))
5039dede
AK
348 {
349 DWORD dwFlags;
350 DB_RESULT hResult;
351 int i, nRows, nType;
352
353 dwFlags = ConfigReadULong(_T("DiscoveryFilterFlags"), DFF_ALLOW_AGENT | DFF_ALLOW_SNMP);
35f836fe 354 DbgPrintf(4, _T("AcceptNewNode(%s): auto filter, dwFlags=%04X"), szIpAddr, dwFlags);
5039dede
AK
355
356 if ((dwFlags & (DFF_ALLOW_AGENT | DFF_ALLOW_SNMP)) == 0)
357 {
358 bResult = TRUE;
359 }
360 else
361 {
362 if (dwFlags & DFF_ALLOW_AGENT)
363 {
364 if (data.dwFlags & NNF_IS_AGENT)
365 bResult = TRUE;
366 }
367
368 if (dwFlags & DFF_ALLOW_SNMP)
369 {
370 if (data.dwFlags & NNF_IS_SNMP)
371 bResult = TRUE;
372 }
373 }
374
375 // Check range
376 if ((dwFlags & DFF_ONLY_RANGE) && bResult)
377 {
35f836fe 378 DbgPrintf(4, _T("AcceptNewNode(%s): auto filter - checking range"), szIpAddr);
5039dede
AK
379 hResult = DBSelect(g_hCoreDB, _T("SELECT addr_type,addr1,addr2 FROM address_lists WHERE list_type=2"));
380 if (hResult != NULL)
381 {
382 nRows = DBGetNumRows(hResult);
383 for(i = 0, bResult = FALSE; (i < nRows) && (!bResult); i++)
384 {
385 nType = DBGetFieldLong(hResult, i, 0);
386 if (nType == 0)
387 {
388 // Subnet
389 bResult = (data.dwIpAddr & DBGetFieldIPAddr(hResult, i, 2)) == DBGetFieldIPAddr(hResult, i, 1);
390 }
391 else
392 {
393 // Range
394 bResult = ((data.dwIpAddr >= DBGetFieldIPAddr(hResult, i, 1)) &&
395 (data.dwIpAddr <= DBGetFieldIPAddr(hResult, i, 2)));
396 }
397 }
398 DBFreeResult(hResult);
399 }
400 }
35f836fe 401 DbgPrintf(4, _T("AcceptNewNode(%s): auto filter - bResult=%d"), szIpAddr, bResult);
5039dede
AK
402 }
403 else
404 {
bb5365ed
VK
405 g_pScriptLibrary->lock();
406 pScript = g_pScriptLibrary->findScript(szFilter);
5039dede
AK
407 if (pScript != NULL)
408 {
35f836fe 409 DbgPrintf(4, _T("AcceptNewNode(%s): Running filter script %s"), szIpAddr, szFilter);
5039dede 410 pValue = new NXSL_Value(new NXSL_Object(&m_nxslDiscoveryClass, &data));
13ceb0fb 411 if (pScript->run(new NXSL_ServerEnv, 1, &pValue) == 0)
5039dede 412 {
bb5365ed 413 bResult = (pScript->getResult()->getValueAsInt32() != 0) ? TRUE : FALSE;
35f836fe 414 DbgPrintf(4, _T("AcceptNewNode(%s): Filter script result: %d"), szIpAddr, bResult);
5039dede
AK
415 }
416 else
417 {
35f836fe 418 DbgPrintf(4, _T("AcceptNewNode(%s): Filter script execution error: %s"),
bb5365ed 419 szIpAddr, pScript->getErrorText());
35f836fe 420 PostEvent(EVENT_SCRIPT_ERROR, g_dwMgmtNode, "ssd", szFilter,
bb5365ed 421 pScript->getErrorText(), 0);
5039dede
AK
422 }
423 }
424 else
425 {
35f836fe 426 DbgPrintf(4, _T("AcceptNewNode(%s): Cannot find filter script %s"), szIpAddr, szFilter);
5039dede 427 }
bb5365ed 428 g_pScriptLibrary->unlock();
5039dede
AK
429 }
430
431 return bResult;
432}
433
434
435//
436// Node poller thread (poll new nodes and put them into the database)
437//
438
439THREAD_RESULT THREAD_CALL NodePoller(void *arg)
440{
441 NEW_NODE *pInfo;
442 TCHAR szIpAddr[16], szNetMask[16];
443
35f836fe 444 DbgPrintf(1, _T("Node poller started"));
5039dede 445
89135050 446 while(!IsShutdownInProgress())
5039dede
AK
447 {
448 pInfo = (NEW_NODE *)g_nodePollerQueue.GetOrBlock();
449 if (pInfo == INVALID_POINTER_VALUE)
450 break; // Shutdown indicator received
451
dc25b21c
VK
452 DbgPrintf(4, _T("NodePoller: processing node %s/%s in zone %d"),
453 IpToStr(pInfo->dwIpAddr, szIpAddr), IpToStr(pInfo->dwNetMask, szNetMask), (int)pInfo->zoneId);
89135050 454 if (AcceptNewNode(pInfo->dwIpAddr, pInfo->dwNetMask, pInfo->zoneId))
07f6978d 455 {
f1b6417b 456 Node *node = PollNewNode(pInfo->dwIpAddr, pInfo->dwNetMask, 0, 0, 0, NULL, 0, 0, NULL, pInfo->zoneId, true, true);
07f6978d 457 }
5039dede
AK
458 free(pInfo);
459 }
35f836fe 460 DbgPrintf(1, _T("Node poller thread terminated"));
5039dede
AK
461 return THREAD_OK;
462}