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