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