969d2e723c89eabdfa7ee03042860aef92776cdf
[public/netxms.git] / src / server / include / nms_objects.h
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003, 2004, 2005, 2006, 2007 Victor Kirhenshtein
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: nms_objects.h
20 **
21 **/
22
23 #ifndef _nms_objects_h_
24 #define _nms_objects_h_
25
26 #include <nms_agent.h>
27 #include <nxnt.h>
28 #include <netxms_maps.h>
29
30
31 //
32 // Forward declarations of classes
33 //
34
35 class AgentConnection;
36 class AgentConnectionEx;
37 class ClientSession;
38 class Queue;
39
40
41 //
42 // Global variables used by inline functions
43 //
44
45 extern DWORD g_dwDiscoveryPollingInterval;
46 extern DWORD g_dwStatusPollingInterval;
47 extern DWORD g_dwConfigurationPollingInterval;
48 extern DWORD g_dwRoutingTableUpdateInterval;
49 extern DWORD g_dwConditionPollingInterval;
50
51
52 //
53 // Constants
54 //
55
56 #define MAX_INTERFACES 4096
57 #define INVALID_INDEX 0xFFFFFFFF
58
59
60 //
61 // Last events
62 //
63
64 #define MAX_LAST_EVENTS 8
65
66 #define LAST_EVENT_NODE_DOWN 0
67
68
69 //
70 // Built-in object IDs
71 //
72
73 #define BUILTIN_OID_NETWORK 1
74 #define BUILTIN_OID_SERVICEROOT 2
75 #define BUILTIN_OID_TEMPLATEROOT 3
76 #define BUILTIN_OID_ZONE0 4
77
78
79 //
80 // Node runtime (dynamic) flags
81 //
82
83 #define NDF_QUEUED_FOR_STATUS_POLL 0x0001
84 #define NDF_QUEUED_FOR_CONFIG_POLL 0x0002
85 #define NDF_UNREACHABLE 0x0004
86 #define NDF_AGENT_UNREACHABLE 0x0008
87 #define NDF_SNMP_UNREACHABLE 0x0010
88 #define NDF_QUEUED_FOR_DISCOVERY_POLL 0x0020
89 #define NDF_FORCE_STATUS_POLL 0x0040
90 #define NDF_FORCE_CONFIGURATION_POLL 0x0080
91 #define NDF_QUEUED_FOR_ROUTE_POLL 0x0100
92 #define NDF_CPSNMP_UNREACHABLE 0x0200
93 #define NDF_RECHECK_CAPABILITIES 0x0400
94 #define NDF_POLLING_DISABLED 0x0800
95
96
97 //
98 // Cluster runtime flags
99 //
100
101 #define CLF_QUEUED_FOR_STATUS_POLL 0x0001
102 #define CLF_DOWN 0x0002
103
104
105 //
106 // Status poll types
107 //
108
109 #define POLL_ICMP_PING 0
110 #define POLL_SNMP 1
111 #define POLL_NATIVE_AGENT 2
112
113
114 //
115 // Zone types
116 //
117
118 #define ZONE_TYPE_PASSIVE 0
119 #define ZONE_TYPE_ACTIVE 1
120
121
122 //
123 // Template update types
124 //
125
126 #define APPLY_TEMPLATE 0
127 #define REMOVE_TEMPLATE 1
128
129
130 //
131 // Queued template update information
132 //
133
134 struct TEMPLATE_UPDATE_INFO
135 {
136 int iUpdateType;
137 Template *pTemplate;
138 DWORD dwNodeId;
139 BOOL bRemoveDCI;
140 };
141
142
143 //
144 // DCI configuration for system templates
145 //
146
147 typedef struct
148 {
149 const TCHAR *pszName;
150 const TCHAR *pszParam;
151 int nInterval;
152 int nRetention;
153 int nDataType;
154 int nOrigin;
155 int nFound;
156 } DCI_CFG;
157
158
159 //
160 // Base class for network objects
161 //
162
163 class NXCORE_EXPORTABLE NetObj
164 {
165 protected:
166 DWORD m_dwId;
167 DWORD m_dwTimeStamp; // Last change time stamp
168 DWORD m_dwRefCount; // Number of references. Object can be destroyed only when this counter is zero
169 TCHAR m_szName[MAX_OBJECT_NAME];
170 TCHAR *m_pszComments; // User comments
171 int m_iStatus;
172 int m_iStatusCalcAlg; // Status calculation algorithm
173 int m_iStatusPropAlg; // Status propagation algorithm
174 int m_iFixedStatus; // Status if propagation is "Fixed"
175 int m_iStatusShift; // Shift value for "shifted" status propagation
176 int m_iStatusTranslation[4];
177 int m_iStatusSingleThreshold;
178 int m_iStatusThresholds[4];
179 BOOL m_bIsModified;
180 BOOL m_bIsDeleted;
181 BOOL m_bIsHidden;
182 BOOL m_bIsSystem;
183 MUTEX m_mutexData; // Object data access mutex
184 MUTEX m_mutexRefCount; // Reference counter access mutex
185 RWLOCK m_rwlockParentList; // Lock for parent list
186 RWLOCK m_rwlockChildList; // Lock for child list
187 DWORD m_dwIpAddr; // Every object should have an IP address
188 DWORD m_dwImageId; // Custom image id or 0 if object has default image
189 ClientSession *m_pPollRequestor;
190
191 DWORD m_dwChildCount; // Number of child objects
192 NetObj **m_pChildList; // Array of pointers to child objects
193
194 DWORD m_dwParentCount; // Number of parent objects
195 NetObj **m_pParentList; // Array of pointers to parent objects
196
197 AccessList *m_pAccessList;
198 BOOL m_bInheritAccessRights;
199 MUTEX m_mutexACL;
200
201 void LockData(void) { MutexLock(m_mutexData, INFINITE); }
202 void UnlockData(void) { MutexUnlock(m_mutexData); }
203 void LockACL(void) { MutexLock(m_mutexACL, INFINITE); }
204 void UnlockACL(void) { MutexUnlock(m_mutexACL); }
205 void LockParentList(BOOL bWrite)
206 {
207 if (bWrite)
208 RWLockWriteLock(m_rwlockParentList, INFINITE);
209 else
210 RWLockReadLock(m_rwlockParentList, INFINITE);
211 }
212 void UnlockParentList(void) { RWLockUnlock(m_rwlockParentList); }
213 void LockChildList(BOOL bWrite)
214 {
215 if (bWrite)
216 RWLockWriteLock(m_rwlockChildList, INFINITE);
217 else
218 RWLockReadLock(m_rwlockChildList, INFINITE);
219 }
220 void UnlockChildList(void) { RWLockUnlock(m_rwlockChildList); }
221
222 void Modify(void); // Used to mark object as modified
223
224 BOOL LoadACLFromDB(void);
225 BOOL SaveACLToDB(DB_HANDLE hdb);
226 BOOL LoadCommonProperties(void);
227 BOOL SaveCommonProperties(DB_HANDLE hdb);
228
229 void SendPollerMsg(DWORD dwRqId, const TCHAR *pszFormat, ...);
230
231 virtual void PrepareForDeletion(void);
232 virtual void OnObjectDelete(DWORD dwObjectId);
233
234 public:
235 NetObj();
236 virtual ~NetObj();
237
238 virtual int Type(void) { return OBJECT_GENERIC; }
239
240 DWORD IpAddr(void) { return m_dwIpAddr; }
241 DWORD Id(void) { return m_dwId; }
242 const TCHAR *Name(void) { return m_szName; }
243 int Status(void) { return m_iStatus; }
244 int PropagatedStatus(void);
245 DWORD TimeStamp(void) { return m_dwTimeStamp; }
246
247 BOOL IsModified(void) { return m_bIsModified; }
248 BOOL IsDeleted(void) { return m_bIsDeleted; }
249 BOOL IsOrphaned(void) { return m_dwParentCount == 0 ? TRUE : FALSE; }
250 BOOL IsEmpty(void) { return m_dwChildCount == 0 ? TRUE : FALSE; }
251
252 BOOL IsSystem(void) { return m_bIsSystem; }
253 void SetSystemFlag(BOOL bFlag) { m_bIsSystem = bFlag; }
254
255 DWORD RefCount(void);
256 void IncRefCount(void);
257 void DecRefCount(void);
258
259 BOOL IsChild(DWORD dwObjectId);
260
261 void AddChild(NetObj *pObject); // Add reference to child object
262 void AddParent(NetObj *pObject); // Add reference to parent object
263
264 void DeleteChild(NetObj *pObject); // Delete reference to child object
265 void DeleteParent(NetObj *pObject); // Delete reference to parent object
266
267 void Delete(BOOL bIndexLocked); // Prepare object for deletion
268
269 BOOL IsHidden(void) { return m_bIsHidden; }
270 void Hide(void);
271 void Unhide(void);
272
273 virtual BOOL SaveToDB(DB_HANDLE hdb);
274 virtual BOOL DeleteFromDB(void);
275 virtual BOOL CreateFromDB(DWORD dwId);
276
277 void SetId(DWORD dwId) { m_dwId = dwId; Modify(); }
278 void SetMgmtStatus(BOOL bIsManaged);
279 void SetName(const TCHAR *pszName) { nx_strncpy(m_szName, pszName, MAX_OBJECT_NAME); Modify(); }
280 void ResetStatus(void) { m_iStatus = STATUS_UNKNOWN; Modify(); }
281 void SetComments(TCHAR *pszText);
282
283 virtual void CalculateCompoundStatus(BOOL bForcedRecalc = FALSE);
284
285 virtual void CreateMessage(CSCPMessage *pMsg);
286 virtual DWORD ModifyFromMessage(CSCPMessage *pRequest, BOOL bAlreadyLocked = FALSE);
287
288 void CommentsToMessage(CSCPMessage *pMsg);
289
290 DWORD GetUserRights(DWORD dwUserId);
291 BOOL CheckAccessRights(DWORD dwUserId, DWORD dwRequiredRights);
292 void DropUserAccess(DWORD dwUserId);
293
294 void AddChildNodesToList(DWORD *pdwNumNodes, Node ***pppNodeList, DWORD dwUserId);
295
296 // Debug methods
297 const char *ParentList(char *szBuffer);
298 const char *ChildList(char *szBuffer);
299 const char *TimeStampAsText(void) { return ctime((time_t *)&m_dwTimeStamp); }
300 };
301
302
303 //
304 // Inline functions of NetObj class
305 //
306
307 inline DWORD NetObj::RefCount(void)
308 {
309 DWORD dwRefCount;
310
311 MutexLock(m_mutexRefCount, INFINITE);
312 dwRefCount = m_dwRefCount;
313 MutexUnlock(m_mutexRefCount);
314 return dwRefCount;
315 }
316
317 inline void NetObj::IncRefCount(void)
318 {
319 MutexLock(m_mutexRefCount, INFINITE);
320 m_dwRefCount++;
321 MutexUnlock(m_mutexRefCount);
322 }
323
324 inline void NetObj::DecRefCount(void)
325 {
326 MutexLock(m_mutexRefCount, INFINITE);
327 if (m_dwRefCount > 0)
328 m_dwRefCount--;
329 MutexUnlock(m_mutexRefCount);
330 }
331
332
333 //
334 // Node template class
335 //
336
337 class NXCORE_EXPORTABLE Template : public NetObj
338 {
339 protected:
340 DWORD m_dwNumItems; // Number of data collection items
341 DCItem **m_ppItems; // Data collection items
342 DWORD m_dwDCILockStatus;
343 DWORD m_dwVersion;
344 BOOL m_bDCIListModified;
345 TCHAR m_szCurrDCIOwner[MAX_SESSION_NAME];
346
347 void LoadItemsFromDB(void);
348 void DestroyItems(void);
349 void ValidateDCIList(DCI_CFG *cfg);
350
351 public:
352 Template();
353 Template(const TCHAR *pszName);
354 virtual ~Template();
355
356 virtual int Type(void) { return OBJECT_TEMPLATE; }
357
358 virtual BOOL SaveToDB(DB_HANDLE hdb);
359 virtual BOOL DeleteFromDB(void);
360 virtual BOOL CreateFromDB(DWORD dwId);
361
362 virtual void CreateMessage(CSCPMessage *pMsg);
363 virtual DWORD ModifyFromMessage(CSCPMessage *pRequest, BOOL bAlreadyLocked = FALSE);
364
365 virtual void CalculateCompoundStatus(BOOL bForcedRecalc = FALSE);
366
367 int VersionMajor(void) { return m_dwVersion >> 16; }
368 int VersionMinor(void) { return m_dwVersion & 0xFFFF; }
369
370 DWORD GetItemCount(void) { return m_dwNumItems; }
371 BOOL AddItem(DCItem *pItem, BOOL bLocked = FALSE);
372 BOOL UpdateItem(DWORD dwItemId, CSCPMessage *pMsg, DWORD *pdwNumMaps,
373 DWORD **ppdwMapIndex, DWORD **ppdwMapId);
374 BOOL DeleteItem(DWORD dwItemId, BOOL bNeedLock);
375 BOOL SetItemStatus(DWORD dwNumItems, DWORD *pdwItemList, int iStatus);
376 int GetItemType(DWORD dwItemId);
377 DCItem *GetItemById(DWORD dwItemId);
378 DCItem *GetItemByIndex(DWORD dwIndex);
379 DCItem *GetItemByName(TCHAR *pszName);
380 BOOL LockDCIList(DWORD dwSessionId, const TCHAR *pszNewOwner, TCHAR *pszCurrOwner);
381 BOOL UnlockDCIList(DWORD dwSessionId);
382 void SetDCIModificationFlag(void) { m_bDCIListModified = TRUE; }
383 void SendItemsToClient(ClientSession *pSession, DWORD dwRqId);
384 BOOL IsLockedBySession(DWORD dwSessionId) { return m_dwDCILockStatus == dwSessionId; }
385 DWORD *GetDCIEventsList(DWORD *pdwCount);
386 void ValidateSystemTemplate(void);
387
388 BOOL ApplyToNode(Node *pNode);
389 void QueueUpdate(void);
390 void QueueRemoveFromNode(DWORD dwNodeId, BOOL bRemoveDCI);
391
392 void CreateNXMPRecord(String &str);
393
394 BOOL EnumDCI(BOOL (* pfCallback)(DCItem *, DWORD, void *), void *pArg);
395 void AssociateItems(void);
396 };
397
398
399 //
400 // Cluster class
401 //
402
403 class NXCORE_EXPORTABLE Cluster : public Template
404 {
405 protected:
406 DWORD m_dwClusterType;
407 DWORD m_dwNumSyncNets;
408 IP_NETWORK *m_pSyncNetList;
409 DWORD m_dwNumResources;
410 CLUSTER_RESOURCE *m_pResourceList;
411 DWORD m_dwFlags;
412 time_t m_tmLastPoll;
413
414 public:
415 Cluster();
416 Cluster(const TCHAR *pszName);
417 virtual ~Cluster();
418
419 virtual int Type(void) { return OBJECT_CLUSTER; }
420 virtual BOOL SaveToDB(DB_HANDLE hdb);
421 virtual BOOL DeleteFromDB(void);
422 virtual BOOL CreateFromDB(DWORD dwId);
423
424 virtual void CreateMessage(CSCPMessage *pMsg);
425 virtual DWORD ModifyFromMessage(CSCPMessage *pRequest, BOOL bAlreadyLocked = FALSE);
426
427 virtual void CalculateCompoundStatus(BOOL bForcedRecalc = FALSE);
428
429 BOOL IsSyncAddr(DWORD dwAddr);
430 BOOL IsVirtualAddr(DWORD dwAddr);
431 BOOL IsResourceOnNode(DWORD dwResource, DWORD dwNode);
432
433 void StatusPoll(ClientSession *pSession, DWORD dwRqId, int nPoller);
434 void LockForStatusPoll(void) { m_dwFlags |= CLF_QUEUED_FOR_STATUS_POLL; }
435 BOOL ReadyForStatusPoll(void)
436 {
437 return ((m_iStatus != STATUS_UNMANAGED) &&
438 (!(m_dwFlags & CLF_QUEUED_FOR_STATUS_POLL)) &&
439 ((DWORD)time(NULL) - (DWORD)m_tmLastPoll > g_dwStatusPollingInterval))
440 ? TRUE : FALSE;
441 }
442 };
443
444
445 //
446 // Interface class
447 //
448
449 class NXCORE_EXPORTABLE Interface : public NetObj
450 {
451 protected:
452 DWORD m_dwIfIndex;
453 DWORD m_dwIfType;
454 DWORD m_dwIpNetMask;
455 BYTE m_bMacAddr[MAC_ADDR_LENGTH];
456 QWORD m_qwLastDownEventId;
457 BOOL m_bSyntheticMask;
458 int m_iPendingStatus;
459 int m_iPollCount;
460 int m_iRequiredPollCount;
461
462 public:
463 Interface();
464 Interface(DWORD dwAddr, DWORD dwNetMask, BOOL bSyntheticMask);
465 Interface(const char *szName, DWORD dwIndex, DWORD dwAddr, DWORD dwNetMask, DWORD dwType);
466 virtual ~Interface();
467
468 virtual int Type(void) { return OBJECT_INTERFACE; }
469 virtual BOOL SaveToDB(DB_HANDLE hdb);
470 virtual BOOL DeleteFromDB(void);
471 virtual BOOL CreateFromDB(DWORD dwId);
472
473 Node *GetParentNode(void);
474
475 void SetMacAddr(BYTE *pbNewMac) { memcpy(m_bMacAddr, pbNewMac, MAC_ADDR_LENGTH); Modify(); }
476
477 DWORD IpNetMask(void) { return m_dwIpNetMask; }
478 DWORD IfIndex(void) { return m_dwIfIndex; }
479 DWORD IfType(void) { return m_dwIfType; }
480 const BYTE *MacAddr(void) { return m_bMacAddr; }
481 BOOL IsSyntheticMask(void) { return m_bSyntheticMask; }
482
483 QWORD GetLastDownEventId(void) { return m_qwLastDownEventId; }
484 void SetLastDownEventId(QWORD qwId) { m_qwLastDownEventId = qwId; }
485
486 BOOL IsFake(void) { return (m_dwIfIndex == 1) &&
487 (m_dwIfType == IFTYPE_OTHER) &&
488 (!_tcscmp(m_szName, _T("lan0"))) &&
489 (!memcmp(m_bMacAddr, "\x00\x00\x00\x00\x00\x00", 6)); }
490 void SetIpAddr(DWORD dwNewAddr);
491
492 void StatusPoll(ClientSession *pSession, DWORD dwRqId, Queue *pEventQueue,
493 BOOL bClusterSync, SNMP_Transport *pTransport);
494
495 virtual void CreateMessage(CSCPMessage *pMsg);
496 virtual DWORD ModifyFromMessage(CSCPMessage *pRequest, BOOL bAlreadyLocked = FALSE);
497
498 DWORD WakeUp(void);
499 };
500
501
502 //
503 // Network service class
504 //
505
506 class NetworkService : public NetObj
507 {
508 protected:
509 int m_iServiceType; // SSH, POP3, etc.
510 Node *m_pHostNode; // Pointer to node object which hosts this service
511 DWORD m_dwPollerNode; // ID of node object which is used for polling
512 // If 0, m_pHostNode->m_dwPollerNode will be used
513 WORD m_wProto; // Protocol (TCP, UDP, etc.)
514 WORD m_wPort; // TCP or UDP port number
515 TCHAR *m_pszRequest; // Service-specific request
516 TCHAR *m_pszResponse; // Service-specific expected response
517 int m_iPendingStatus;
518 int m_iPollCount;
519 int m_iRequiredPollCount;
520
521 virtual void OnObjectDelete(DWORD dwObjectId);
522
523 public:
524 NetworkService();
525 NetworkService(int iServiceType, WORD wProto, WORD wPort,
526 TCHAR *pszRequest, TCHAR *pszResponse,
527 Node *pHostNode = NULL, DWORD dwPollerNode = 0);
528 virtual ~NetworkService();
529
530 virtual int Type(void) { return OBJECT_NETWORKSERVICE; }
531
532 virtual BOOL SaveToDB(DB_HANDLE hdb);
533 virtual BOOL DeleteFromDB(void);
534 virtual BOOL CreateFromDB(DWORD dwId);
535
536 void StatusPoll(ClientSession *pSession, DWORD dwRqId, Node *pPollerNode, Queue *pEventQueue);
537
538 virtual void CreateMessage(CSCPMessage *pMsg);
539 virtual DWORD ModifyFromMessage(CSCPMessage *pRequest, BOOL bAlreadyLocked = FALSE);
540 };
541
542
543 //
544 // VPN connector class
545 //
546
547 class VPNConnector : public NetObj
548 {
549 protected:
550 DWORD m_dwPeerGateway; // Object ID of peer gateway
551 DWORD m_dwNumLocalNets;
552 IP_NETWORK *m_pLocalNetList;
553 DWORD m_dwNumRemoteNets;
554 IP_NETWORK *m_pRemoteNetList;
555
556 Node *GetParentNode(void);
557
558 public:
559 VPNConnector();
560 VPNConnector(BOOL bIsHidden);
561 virtual ~VPNConnector();
562
563 virtual int Type(void) { return OBJECT_VPNCONNECTOR; }
564
565 virtual BOOL SaveToDB(DB_HANDLE hdb);
566 virtual BOOL DeleteFromDB(void);
567 virtual BOOL CreateFromDB(DWORD dwId);
568
569 virtual void CreateMessage(CSCPMessage *pMsg);
570 virtual DWORD ModifyFromMessage(CSCPMessage *pRequest, BOOL bAlreadyLocked = FALSE);
571
572 BOOL IsLocalAddr(DWORD dwIpAddr);
573 BOOL IsRemoteAddr(DWORD dwIpAddr);
574 DWORD GetPeerGatewayAddr(void);
575 };
576
577
578 //
579 // Node
580 //
581
582 class NXCORE_EXPORTABLE Node : public Template
583 {
584 protected:
585 DWORD m_dwFlags;
586 DWORD m_dwDynamicFlags; // Flags used at runtime by server
587 int m_iPendingStatus;
588 int m_iPollCount;
589 int m_iRequiredPollCount;
590 DWORD m_dwZoneGUID;
591 WORD m_wAgentPort;
592 WORD m_wAuthMethod;
593 DWORD m_dwNodeType;
594 char m_szSharedSecret[MAX_SECRET_LENGTH];
595 int m_iStatusPollType;
596 int m_iSNMPVersion;
597 WORD m_wSNMPPort;
598 char m_szCommunityString[MAX_COMMUNITY_LENGTH];
599 char m_szObjectId[MAX_OID_LEN * 4];
600 char m_szAgentVersion[MAX_AGENT_VERSION_LEN];
601 char m_szPlatformName[MAX_PLATFORM_NAME_LEN];
602 char m_szSysDescription[MAX_DB_STRING];
603 DWORD m_dwNumParams; // Number of elements in supported parameters list
604 NXC_AGENT_PARAM *m_pParamList; // List of supported parameters
605 time_t m_tLastDiscoveryPoll;
606 time_t m_tLastStatusPoll;
607 time_t m_tLastConfigurationPoll;
608 time_t m_tLastRTUpdate;
609 time_t m_tFailTimeSNMP;
610 time_t m_tFailTimeAgent;
611 MUTEX m_hPollerMutex;
612 MUTEX m_hAgentAccessMutex;
613 MUTEX m_mutexRTAccess;
614 MUTEX m_mutexTopoAccess;
615 AgentConnectionEx *m_pAgentConnection;
616 DWORD m_dwPollerNode; // Node used for network service polling
617 DWORD m_dwProxyNode; // Node used as proxy for agent connection
618 DWORD m_dwSNMPProxy; // Node used as proxy for SNMP requests
619 QWORD m_qwLastEvents[MAX_LAST_EVENTS];
620 ROUTING_TABLE *m_pRoutingTable;
621 nxObjList *m_pTopology; // Layer 2 topology
622 time_t m_tLastTopologyPoll;
623
624 void PollerLock(void) { MutexLock(m_hPollerMutex, INFINITE); }
625 void PollerUnlock(void) { MutexUnlock(m_hPollerMutex); }
626
627 void AgentLock(void) { MutexLock(m_hAgentAccessMutex, INFINITE); }
628 void AgentUnlock(void) { MutexUnlock(m_hAgentAccessMutex); }
629
630 void RTLock(void) { MutexLock(m_mutexRTAccess, INFINITE); }
631 void RTUnlock(void) { MutexUnlock(m_mutexRTAccess); }
632
633 BOOL CheckSNMPIntegerValue(SNMP_Transport *pTransport, const char *pszOID, int nValue);
634 void CheckOSPFSupport(SNMP_Transport *pTransport);
635 BOOL ResolveName(BOOL useOnlyDNS);
636 void SetAgentProxy(AgentConnection *pConn);
637
638 DWORD GetInterfaceCount(Interface **ppInterface);
639
640 void CheckInterfaceNames(INTERFACE_LIST *pIfList);
641 void CheckSubnetBinding(INTERFACE_LIST *pIfList);
642
643 virtual void PrepareForDeletion(void);
644 virtual void OnObjectDelete(DWORD dwObjectId);
645
646 public:
647 Node();
648 Node(DWORD dwAddr, DWORD dwFlags, DWORD dwProxyNode, DWORD dwSNMPProxy, DWORD dwZone);
649 virtual ~Node();
650
651 virtual int Type(void) { return OBJECT_NODE; }
652
653 virtual BOOL SaveToDB(DB_HANDLE hdb);
654 virtual BOOL DeleteFromDB(void);
655 virtual BOOL CreateFromDB(DWORD dwId);
656
657 Cluster *GetMyCluster(void);
658
659 DWORD Flags(void) { return m_dwFlags; }
660 DWORD RuntimeFlags(void) { return m_dwDynamicFlags; }
661 DWORD ZoneGUID(void) { return m_dwZoneGUID; }
662 void SetLocalMgmtFlag(void) { m_dwFlags |= NF_IS_LOCAL_MGMT; }
663
664 BOOL IsSNMPSupported(void) { return m_dwFlags & NF_IS_SNMP ? TRUE : FALSE; }
665 BOOL IsNativeAgent(void) { return m_dwFlags & NF_IS_NATIVE_AGENT ? TRUE : FALSE; }
666 BOOL IsBridge(void) { return m_dwFlags & NF_IS_BRIDGE ? TRUE : FALSE; }
667 BOOL IsRouter(void) { return m_dwFlags & NF_IS_ROUTER ? TRUE : FALSE; }
668 BOOL IsLocalManagement(void) { return m_dwFlags & NF_IS_LOCAL_MGMT ? TRUE : FALSE; }
669
670 LONG GetSNMPVersion() { return m_iSNMPVersion; }
671 const TCHAR *GetSNMPObjectId() { return m_szObjectId; }
672 const TCHAR *GetAgentVersion() { return m_szAgentVersion; }
673 const TCHAR *GetPlatformName() { return m_szPlatformName; }
674
675 BOOL IsDown(void) { return m_dwDynamicFlags & NDF_UNREACHABLE ? TRUE : FALSE; }
676
677 const char *ObjectId(void) { return m_szObjectId; }
678
679 void AddInterface(Interface *pInterface) { AddChild(pInterface); pInterface->AddParent(this); }
680 void CreateNewInterface(DWORD dwAddr, DWORD dwNetMask, char *szName = NULL,
681 DWORD dwIndex = 0, DWORD dwType = 0, BYTE *pbMacAddr = NULL);
682 void DeleteInterface(Interface *pInterface);
683
684 void ChangeIPAddress(DWORD dwIpAddr);
685
686 ARP_CACHE *GetArpCache(void);
687 INTERFACE_LIST *GetInterfaceList(void);
688 Interface *FindInterface(DWORD dwIndex, DWORD dwHostAddr);
689 BOOL IsMyIP(DWORD dwIpAddr);
690 int GetInterfaceStatusFromSNMP(SNMP_Transport *pTransport, DWORD dwIndex);
691 int GetInterfaceStatusFromAgent(DWORD dwIndex);
692 ROUTING_TABLE *GetRoutingTable(void);
693 ROUTING_TABLE *GetCachedRoutingTable(void) { return m_pRoutingTable; }
694 BOOL GetNextHop(DWORD dwSrcAddr, DWORD dwDestAddr, DWORD *pdwNextHop,
695 DWORD *pdwIfIndex, BOOL *pbIsVPN);
696
697 void SetRecheckCapsFlag(void) { m_dwDynamicFlags |= NDF_RECHECK_CAPABILITIES; }
698 void SetDiscoveryPollTimeStamp(void);
699 void StatusPoll(ClientSession *pSession, DWORD dwRqId, int nPoller);
700 void ConfigurationPoll(ClientSession *pSession, DWORD dwRqId, int nPoller, DWORD dwNetMask);
701 void UpdateInterfaceNames(ClientSession *pSession, DWORD dwRqId);
702 void UpdateRoutingTable(void);
703 BOOL ReadyForStatusPoll(void);
704 BOOL ReadyForConfigurationPoll(void);
705 BOOL ReadyForDiscoveryPoll(void);
706 BOOL ReadyForRoutePoll(void);
707 void LockForStatusPoll(void);
708 void LockForConfigurationPoll(void);
709 void LockForDiscoveryPoll(void);
710 void LockForRoutePoll(void);
711
712 virtual void CalculateCompoundStatus(BOOL bForcedRecalc = FALSE);
713
714 BOOL ConnectToAgent(void);
715 DWORD GetItemFromSNMP(const char *szParam, DWORD dwBufSize, char *szBuffer);
716 DWORD GetItemFromCheckPointSNMP(const char *szParam, DWORD dwBufSize, char *szBuffer);
717 DWORD GetItemFromAgent(const char *szParam, DWORD dwBufSize, char *szBuffer);
718 DWORD GetInternalItem(const char *szParam, DWORD dwBufSize, char *szBuffer);
719 void QueueItemsForPolling(Queue *pPollerQueue);
720 DWORD GetItemForClient(int iOrigin, const char *pszParam, char *pszBuffer, DWORD dwBufSize);
721 DWORD GetLastValues(CSCPMessage *pMsg);
722 void CleanDCIData(void);
723 BOOL ApplyTemplateItem(DWORD dwTemplateId, DCItem *pItem);
724 void CleanDeletedTemplateItems(DWORD dwTemplateId, DWORD dwNumItems, DWORD *pdwItemList);
725 void UnbindFromTemplate(DWORD dwTemplateId, BOOL bRemoveDCI);
726 void UpdateDCICache(void);
727 DWORD GetSystemDCIList(CSCPMessage *pMsg);
728
729 void OpenParamList(DWORD *pdwNumParams, NXC_AGENT_PARAM **ppParamList);
730 void CloseParamList(void) { UnlockData(); }
731
732 AgentConnection *CreateAgentConnection(void);
733 SNMP_Transport *CreateSNMPTransport(void);
734
735 virtual void CreateMessage(CSCPMessage *pMsg);
736 virtual DWORD ModifyFromMessage(CSCPMessage *pRequest, BOOL bAlreadyLocked = FALSE);
737 void WriteParamListToMessage(CSCPMessage *pMsg);
738
739 DWORD WakeUp(void);
740
741 void AddService(NetworkService *pNetSrv) { AddChild(pNetSrv); pNetSrv->AddParent(this); }
742 DWORD CheckNetworkService(DWORD *pdwStatus, DWORD dwIpAddr, int iServiceType, WORD wPort = 0,
743 WORD wProto = 0, TCHAR *pszRequest = NULL, TCHAR *pszResponse = NULL);
744
745 QWORD GetLastEventId(int nIndex) { return ((nIndex >= 0) && (nIndex < MAX_LAST_EVENTS)) ? m_qwLastEvents[nIndex] : 0; }
746 void SetLastEventId(int nIndex, QWORD qwId) { if ((nIndex >= 0) && (nIndex < MAX_LAST_EVENTS)) m_qwLastEvents[nIndex] = qwId; }
747
748 DWORD CallSnmpEnumerate(const char *pszRootOid,
749 DWORD (* pHandler)(DWORD, const char *, SNMP_Variable *, SNMP_Transport *, void *), void *pArg);
750
751 nxObjList *GetL2Topology(void);
752 nxObjList *BuildL2Topology(DWORD *pdwStatus);
753 };
754
755
756 //
757 // Inline functions for Node class
758 //
759
760 inline void Node::SetDiscoveryPollTimeStamp(void)
761 {
762 m_tLastDiscoveryPoll = time(NULL);
763 m_dwDynamicFlags &= ~NDF_QUEUED_FOR_DISCOVERY_POLL;
764 }
765
766 inline BOOL Node::ReadyForStatusPoll(void)
767 {
768 if (m_dwDynamicFlags & NDF_FORCE_STATUS_POLL)
769 {
770 m_dwDynamicFlags &= ~NDF_FORCE_STATUS_POLL;
771 return TRUE;
772 }
773 return ((m_iStatus != STATUS_UNMANAGED) &&
774 (!(m_dwFlags & NF_DISABLE_STATUS_POLL)) &&
775 (!(m_dwDynamicFlags & NDF_QUEUED_FOR_STATUS_POLL)) &&
776 (!(m_dwDynamicFlags & NDF_POLLING_DISABLED)) &&
777 (GetMyCluster() == NULL) &&
778 ((DWORD)time(NULL) - (DWORD)m_tLastStatusPoll > g_dwStatusPollingInterval))
779 ? TRUE : FALSE;
780 }
781
782 inline BOOL Node::ReadyForConfigurationPoll(void)
783 {
784 if (m_dwDynamicFlags & NDF_FORCE_CONFIGURATION_POLL)
785 {
786 m_dwDynamicFlags &= ~NDF_FORCE_CONFIGURATION_POLL;
787 return TRUE;
788 }
789 return ((m_iStatus != STATUS_UNMANAGED) &&
790 (!(m_dwFlags & NF_DISABLE_CONF_POLL)) &&
791 (!(m_dwDynamicFlags & NDF_QUEUED_FOR_CONFIG_POLL)) &&
792 (!(m_dwDynamicFlags & NDF_POLLING_DISABLED)) &&
793 ((DWORD)time(NULL) - (DWORD)m_tLastConfigurationPoll > g_dwConfigurationPollingInterval))
794 ? TRUE : FALSE;
795 }
796
797 inline BOOL Node::ReadyForDiscoveryPoll(void)
798 {
799 return ((g_dwFlags & AF_ENABLE_NETWORK_DISCOVERY) &&
800 (m_iStatus != STATUS_UNMANAGED) &&
801 (!(m_dwDynamicFlags & NDF_QUEUED_FOR_DISCOVERY_POLL)) &&
802 (!(m_dwDynamicFlags & NDF_POLLING_DISABLED)) &&
803 ((DWORD)time(NULL) - (DWORD)m_tLastDiscoveryPoll > g_dwDiscoveryPollingInterval))
804 ? TRUE : FALSE;
805 }
806
807 inline BOOL Node::ReadyForRoutePoll(void)
808 {
809 return ((m_iStatus != STATUS_UNMANAGED) &&
810 (!(m_dwFlags & NF_DISABLE_ROUTE_POLL)) &&
811 (!(m_dwDynamicFlags & NDF_QUEUED_FOR_ROUTE_POLL)) &&
812 (!(m_dwDynamicFlags & NDF_POLLING_DISABLED)) &&
813 ((DWORD)time(NULL) - (DWORD)m_tLastRTUpdate > g_dwRoutingTableUpdateInterval))
814 ? TRUE : FALSE;
815 }
816
817 inline void Node::LockForStatusPoll(void)
818 {
819 LockData();
820 m_dwDynamicFlags |= NDF_QUEUED_FOR_STATUS_POLL;
821 UnlockData();
822 }
823
824 inline void Node::LockForConfigurationPoll(void)
825 {
826 LockData();
827 m_dwDynamicFlags |= NDF_QUEUED_FOR_CONFIG_POLL;
828 UnlockData();
829 }
830
831 inline void Node::LockForDiscoveryPoll(void)
832 {
833 LockData();
834 m_dwDynamicFlags |= NDF_QUEUED_FOR_DISCOVERY_POLL;
835 UnlockData();
836 }
837
838 inline void Node::LockForRoutePoll(void)
839 {
840 LockData();
841 m_dwDynamicFlags |= NDF_QUEUED_FOR_ROUTE_POLL;
842 UnlockData();
843 }
844
845
846 //
847 // Subnet
848 //
849
850 class NXCORE_EXPORTABLE Subnet : public NetObj
851 {
852 protected:
853 DWORD m_dwIpNetMask;
854 DWORD m_dwZoneGUID;
855 BOOL m_bSyntheticMask;
856
857 public:
858 Subnet();
859 Subnet(DWORD dwAddr, DWORD dwNetMask, DWORD dwZone, BOOL bSyntheticMask);
860 virtual ~Subnet();
861
862 virtual int Type(void) { return OBJECT_SUBNET; }
863
864 virtual BOOL SaveToDB(DB_HANDLE hdb);
865 virtual BOOL DeleteFromDB(void);
866 virtual BOOL CreateFromDB(DWORD dwId);
867
868 void AddNode(Node *pNode) { AddChild(pNode); pNode->AddParent(this); }
869 virtual void CreateMessage(CSCPMessage *pMsg);
870
871 DWORD IpNetMask(void) { return m_dwIpNetMask; }
872 DWORD ZoneGUID(void) { return m_dwZoneGUID; }
873 BOOL IsSyntheticMask(void) { return m_bSyntheticMask; }
874
875 void SetCorrectMask(DWORD dwAddr, DWORD dwMask);
876 };
877
878
879 //
880 // Universal root object
881 //
882
883 class NXCORE_EXPORTABLE UniversalRoot : public NetObj
884 {
885 public:
886 UniversalRoot();
887 virtual ~UniversalRoot();
888
889 virtual BOOL SaveToDB(DB_HANDLE hdb);
890 virtual void LoadFromDB(void);
891 virtual const char *DefaultName(void) { return "Root Object"; }
892
893 void LinkChildObjects(void);
894 void LinkObject(NetObj *pObject) { AddChild(pObject); pObject->AddParent(this); }
895 };
896
897
898 //
899 // Service root
900 //
901
902 class NXCORE_EXPORTABLE ServiceRoot : public UniversalRoot
903 {
904 public:
905 ServiceRoot();
906 virtual ~ServiceRoot();
907
908 virtual int Type(void) { return OBJECT_SERVICEROOT; }
909 virtual const char *DefaultName(void) { return "All Services"; }
910 };
911
912
913 //
914 // Template root
915 //
916
917 class NXCORE_EXPORTABLE TemplateRoot : public UniversalRoot
918 {
919 public:
920 TemplateRoot();
921 virtual ~TemplateRoot();
922
923 virtual int Type(void) { return OBJECT_TEMPLATEROOT; }
924 virtual const char *DefaultName(void) { return "Templates"; }
925 virtual void CalculateCompoundStatus(BOOL bForcedRecalc = FALSE);
926 };
927
928
929 //
930 // Generic container object
931 //
932
933 class NXCORE_EXPORTABLE Container : public NetObj
934 {
935 private:
936 DWORD *m_pdwChildIdList;
937 DWORD m_dwChildIdListSize;
938
939 protected:
940 DWORD m_dwCategory;
941
942 public:
943 Container();
944 Container(TCHAR *pszName, DWORD dwCategory);
945 virtual ~Container();
946
947 virtual int Type(void) { return OBJECT_CONTAINER; }
948
949 virtual BOOL SaveToDB(DB_HANDLE hdb);
950 virtual BOOL DeleteFromDB(void);
951 virtual BOOL CreateFromDB(DWORD dwId);
952
953 virtual void CreateMessage(CSCPMessage *pMsg);
954
955 DWORD Category(void) { return m_dwCategory; }
956
957 void LinkChildObjects(void);
958 void LinkObject(NetObj *pObject) { AddChild(pObject); pObject->AddParent(this); }
959 };
960
961
962 //
963 // Template group object
964 //
965
966 class NXCORE_EXPORTABLE TemplateGroup : public Container
967 {
968 public:
969 TemplateGroup() : Container() { }
970 TemplateGroup(TCHAR *pszName) : Container(pszName, 0) { }
971 virtual ~TemplateGroup() { }
972
973 virtual int Type(void) { return OBJECT_TEMPLATEGROUP; }
974 };
975
976
977 //
978 // Zone object
979 //
980
981 class Zone : public NetObj
982 {
983 protected:
984 DWORD m_dwZoneGUID;
985 int m_iZoneType;
986 DWORD m_dwControllerIpAddr;
987 DWORD m_dwAddrListSize;
988 DWORD *m_pdwIpAddrList;
989
990 public:
991 Zone();
992 virtual ~Zone();
993
994 virtual int Type(void) { return OBJECT_ZONE; }
995
996 virtual BOOL SaveToDB(DB_HANDLE hdb);
997 virtual BOOL DeleteFromDB(void);
998 virtual BOOL CreateFromDB(DWORD dwId);
999
1000 virtual void CreateMessage(CSCPMessage *pMsg);
1001 virtual DWORD ModifyFromMessage(CSCPMessage *pRequest, BOOL bAlreadyLocked = FALSE);
1002
1003 DWORD GUID(void) { return m_dwZoneGUID; }
1004
1005 void AddSubnet(Subnet *pSubnet) { AddChild(pSubnet); pSubnet->AddParent(this); }
1006 };
1007
1008
1009 //
1010 // Entire network
1011 //
1012
1013 class NXCORE_EXPORTABLE Network : public NetObj
1014 {
1015 public:
1016 Network();
1017 virtual ~Network();
1018
1019 virtual int Type(void) { return OBJECT_NETWORK; }
1020 virtual BOOL SaveToDB(DB_HANDLE hdb);
1021
1022 void AddSubnet(Subnet *pSubnet) { AddChild(pSubnet); pSubnet->AddParent(this); }
1023 void AddZone(Zone *pZone) { AddChild(pZone); pZone->AddParent(this); }
1024 void LoadFromDB(void);
1025 };
1026
1027
1028 //
1029 // Condition
1030 //
1031
1032 class NXCORE_EXPORTABLE Condition : public NetObj
1033 {
1034 protected:
1035 DWORD m_dwDCICount;
1036 INPUT_DCI *m_pDCIList;
1037 TCHAR *m_pszScript;
1038 NXSL_Program *m_pCompiledScript;
1039 DWORD m_dwActivationEventCode;
1040 DWORD m_dwDeactivationEventCode;
1041 DWORD m_dwSourceObject;
1042 int m_nActiveStatus;
1043 int m_nInactiveStatus;
1044 BOOL m_bIsActive;
1045 time_t m_tmLastPoll;
1046 BOOL m_bQueuedForPolling;
1047
1048 public:
1049
1050 Condition();
1051 Condition(BOOL bHidden);
1052 virtual ~Condition();
1053
1054 virtual int Type(void) { return OBJECT_CONDITION; }
1055
1056 virtual BOOL SaveToDB(DB_HANDLE hdb);
1057 virtual BOOL DeleteFromDB(void);
1058 virtual BOOL CreateFromDB(DWORD dwId);
1059
1060 virtual void CreateMessage(CSCPMessage *pMsg);
1061 virtual DWORD ModifyFromMessage(CSCPMessage *pRequest, BOOL bAlreadyLocked = FALSE);
1062
1063 void Check(void);
1064
1065 void LockForPoll(void);
1066 void EndPoll(void);
1067
1068 BOOL ReadyForPoll(void)
1069 {
1070 return ((m_iStatus != STATUS_UNMANAGED) &&
1071 (!m_bQueuedForPolling) &&
1072 ((DWORD)time(NULL) - (DWORD)m_tmLastPoll > g_dwConditionPollingInterval))
1073 ? TRUE : FALSE;
1074 }
1075
1076 int GetCacheSizeForDCI(DWORD dwItemId, BOOL bNoLock);
1077 };
1078
1079
1080 //
1081 // Object index structure
1082 //
1083
1084 struct INDEX
1085 {
1086 DWORD dwKey;
1087 void *pObject;
1088 };
1089
1090
1091 //
1092 // Container category information
1093 //
1094
1095 struct CONTAINER_CATEGORY
1096 {
1097 DWORD dwCatId;
1098 char szName[MAX_OBJECT_NAME];
1099 char *pszDescription;
1100 DWORD dwImageId;
1101 };
1102
1103
1104 //
1105 // Functions
1106 //
1107
1108 void ObjectsInit(void);
1109
1110 void NetObjInsert(NetObj *pObject, BOOL bNewObject);
1111 void NetObjDeleteFromIndexes(NetObj *pObject);
1112 void NetObjDelete(NetObj *pObject);
1113
1114 void UpdateNodeIndex(DWORD dwOldIpAddr, DWORD dwNewIpAddr, NetObj *pObject);
1115 void UpdateInterfaceIndex(DWORD dwOldIpAddr, DWORD dwNewIpAddr, NetObj *pObject);
1116
1117 NetObj NXCORE_EXPORTABLE *FindObjectById(DWORD dwId);
1118 NetObj NXCORE_EXPORTABLE *FindObjectByName(const TCHAR *pszName);
1119 Template NXCORE_EXPORTABLE *FindTemplateByName(const TCHAR *pszName);
1120 Node NXCORE_EXPORTABLE *FindNodeByIP(DWORD dwAddr);
1121 Subnet NXCORE_EXPORTABLE *FindSubnetByIP(DWORD dwAddr);
1122 Subnet NXCORE_EXPORTABLE *FindSubnetForNode(DWORD dwNodeAddr);
1123 DWORD NXCORE_EXPORTABLE FindLocalMgmtNode(void);
1124 CONTAINER_CATEGORY NXCORE_EXPORTABLE *FindContainerCategory(DWORD dwId);
1125 Zone NXCORE_EXPORTABLE *FindZoneByGUID(DWORD dwZoneGUID);
1126
1127 BOOL LoadObjects(void);
1128 void DumpObjects(CONSOLE_CTX pCtx);
1129
1130 void DeleteUserFromAllObjects(DWORD dwUserId);
1131
1132 BOOL IsValidParentClass(int iChildClass, int iParentClass);
1133
1134 int DefaultPropagatedStatus(int iObjectStatus);
1135 int GetDefaultStatusCalculation(int *pnSingleThreshold, int **ppnThresholds);
1136
1137
1138 //
1139 // Global variables
1140 //
1141
1142 extern Network *g_pEntireNet;
1143 extern ServiceRoot *g_pServiceRoot;
1144 extern TemplateRoot *g_pTemplateRoot;
1145
1146 extern DWORD g_dwMgmtNode;
1147 extern INDEX *g_pIndexById;
1148 extern DWORD g_dwIdIndexSize;
1149 extern INDEX *g_pSubnetIndexByAddr;
1150 extern DWORD g_dwSubnetAddrIndexSize;
1151 extern INDEX *g_pNodeIndexByAddr;
1152 extern DWORD g_dwNodeAddrIndexSize;
1153 extern INDEX *g_pInterfaceIndexByAddr;
1154 extern DWORD g_dwInterfaceAddrIndexSize;
1155 extern INDEX *g_pZoneIndexByGUID;
1156 extern DWORD g_dwZoneGUIDIndexSize;
1157 extern INDEX *g_pConditionIndex;
1158 extern DWORD g_dwConditionIndexSize;
1159 extern RWLOCK g_rwlockIdIndex;
1160 extern RWLOCK g_rwlockNodeIndex;
1161 extern RWLOCK g_rwlockSubnetIndex;
1162 extern RWLOCK g_rwlockInterfaceIndex;
1163 extern RWLOCK g_rwlockZoneIndex;
1164 extern RWLOCK g_rwlockConditionIndex;
1165 extern DWORD g_dwNumCategories;
1166 extern CONTAINER_CATEGORY *g_pContainerCatList;
1167 extern const char *g_szClassName[];
1168 extern BOOL g_bModificationsLocked;
1169 extern Queue *g_pTemplateUpdateQueue;
1170
1171
1172 #endif /* _nms_objects_h_ */