- Fixed issue #0000017 (server crash after DCI copy)
[public/netxms.git] / src / server / include / nms_dcoll.h
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003, 2004 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_dcoll.h
20 **
21 **/
22
23 #ifndef _nms_dcoll_h_
24 #define _nms_dcoll_h_
25
26
27 //
28 // Data collection errors
29 //
30
31 #define DCE_SUCCESS 0
32 #define DCE_COMM_ERROR 1
33 #define DCE_NOT_SUPPORTED 2
34
35
36 //
37 // Threshold check results
38 //
39
40 #define THRESHOLD_REACHED 0
41 #define THRESHOLD_REARMED 1
42 #define NO_ACTION 2
43
44
45 //
46 // DCI value
47 //
48
49 class ItemValue
50 {
51 private:
52 double m_dFloat;
53 long m_iInt32;
54 INT64 m_iInt64;
55 DWORD m_dwInt32;
56 QWORD m_qwInt64;
57 TCHAR m_szString[MAX_DB_STRING];
58
59 public:
60 ItemValue();
61 ItemValue(const TCHAR *pszValue);
62 ItemValue(const ItemValue *pValue);
63 ~ItemValue();
64
65 const TCHAR *String(void) { return m_szString; }
66
67 operator double() { return m_dFloat; }
68 operator DWORD() { return m_dwInt32; }
69 operator QWORD() { return m_qwInt64; }
70 operator long() { return m_iInt32; }
71 operator INT64() { return m_iInt64; }
72 operator const char*() const { return m_szString; }
73
74 const ItemValue& operator=(const ItemValue &src);
75 const ItemValue& operator=(const TCHAR *pszStr);
76 const ItemValue& operator=(double dFloat);
77 const ItemValue& operator=(long iInt32);
78 const ItemValue& operator=(INT64 iInt64);
79 const ItemValue& operator=(DWORD dwInt32);
80 const ItemValue& operator=(QWORD qwInt64);
81 };
82
83
84 //
85 // Threshold definition class
86 //
87
88 class DCItem;
89
90 class Threshold
91 {
92 private:
93 DWORD m_dwId; // Unique threshold id
94 DWORD m_dwItemId; // Related item id
95 DWORD m_dwEventCode; // Event code to be generated
96 ItemValue m_value;
97 BYTE m_iFunction; // Function code
98 BYTE m_iOperation; // Comparision operation code
99 BYTE m_iDataType; // Related item data type
100 int m_iParam1; // Function's parameter #1
101 int m_iParam2; // Function's parameter #2
102 BOOL m_bIsReached;
103
104 const ItemValue& Value(void) { return m_value; }
105 void CalculateAverageValue(ItemValue *pResult, ItemValue &lastValue, ItemValue **ppPrevValues);
106
107 public:
108 Threshold(DCItem *pRelatedItem);
109 Threshold(Threshold *pSrc);
110 Threshold(DB_RESULT hResult, int iRow, DCItem *pRelatedItem);
111 ~Threshold();
112
113 void BindToItem(DWORD dwItemId) { m_dwItemId = dwItemId; }
114
115 DWORD Id(void) { return m_dwId; }
116 DWORD EventCode(void) { return m_dwEventCode; }
117 const char *StringValue(void) { return m_value.String(); }
118 BOOL IsReached(void) { return m_bIsReached; }
119
120 BOOL SaveToDB(DWORD dwIndex);
121 int Check(ItemValue &value, ItemValue **ppPrevValues, ItemValue &fvalue);
122
123 void CreateMessage(DCI_THRESHOLD *pData);
124 void UpdateFromMessage(DCI_THRESHOLD *pData);
125
126 void CreateId(void);
127 DWORD RequiredCacheSize(void) { return (m_iFunction == F_LAST) ? 0 : m_iParam1; }
128 };
129
130
131 //
132 // Data collection item class
133 //
134
135 class Template;
136
137 class DCItem
138 {
139 private:
140 DWORD m_dwId;
141 char m_szName[MAX_ITEM_NAME];
142 char m_szDescription[MAX_DB_STRING];
143 char m_szInstance[MAX_DB_STRING];
144 time_t m_tLastPoll; // Last poll time
145 int m_iPollingInterval; // Polling interval in seconds
146 int m_iRetentionTime; // Retention time in seconds
147 BYTE m_iDeltaCalculation; // Delta calculation method
148 BYTE m_iSource; // SNMP or native agent?
149 BYTE m_iDataType;
150 BYTE m_iStatus; // Item status: active, disabled or not supported
151 BYTE m_iBusy; // 1 when item is queued for polling, 0 if not
152 DWORD m_dwTemplateId; // Related template's id
153 DWORD m_dwNumThresholds;
154 Threshold **m_ppThresholdList;
155 Template *m_pNode; // Pointer to node or template object this item related to
156 char *m_pszFormula; // Transformation formula
157 MUTEX m_hMutex;
158 DWORD m_dwCacheSize; // Number of items in cache
159 ItemValue **m_ppValueCache;
160 ItemValue m_prevRawValue; // Previous raw value (used for delta calculation)
161
162 void Lock(void) { MutexLock(m_hMutex, INFINITE); }
163 void Unlock(void) { MutexUnlock(m_hMutex); }
164
165 void Transform(ItemValue &value, long nElapsedTime);
166 void CheckThresholds(ItemValue &value);
167 void UpdateCacheSize(void);
168 void ClearCache(void);
169
170 public:
171 DCItem();
172 DCItem(const DCItem *pItem);
173 DCItem(DB_RESULT hResult, int iRow, Template *pNode);
174 DCItem(DWORD dwId, char *szName, int iSource, int iDataType,
175 int iPollingInterval, int iRetentionTime, Template *pNode,
176 char *pszDescription = NULL);
177 ~DCItem();
178
179 void PrepareForDeletion(void);
180
181 BOOL SaveToDB(void);
182 BOOL LoadThresholdsFromDB(void);
183 void DeleteFromDB(void);
184
185 DWORD Id(void) { return m_dwId; }
186 int DataSource(void) { return m_iSource; }
187 int DataType(void) { return m_iDataType; }
188 const char *Name(void) { return m_szName; }
189 Template *RelatedNode(void) { return m_pNode; }
190
191 BOOL ReadyForPolling(time_t currTime)
192 {
193 BOOL bResult;
194
195 Lock();
196 bResult = ((m_iStatus == ITEM_STATUS_ACTIVE) && (!m_iBusy) &&
197 (m_tLastPoll + m_iPollingInterval <= currTime));
198 Unlock();
199 return bResult;
200 }
201
202 void SetLastPollTime(time_t tLastPoll) { m_tLastPoll = tLastPoll; }
203 void SetStatus(int iStatus) { m_iStatus = (BYTE)iStatus; }
204 void SetBusyFlag(BOOL bIsBusy) { m_iBusy = (BYTE)bIsBusy; }
205 void ChangeBinding(DWORD dwNewId, Template *pNode);
206
207 void NewValue(DWORD dwTimeStamp, const char *pszValue);
208
209 void GetLastValue(CSCPMessage *pMsg, DWORD dwId);
210
211 void CreateMessage(CSCPMessage *pMsg);
212 void UpdateFromMessage(CSCPMessage *pMsg, DWORD *pdwNumMaps, DWORD **ppdwMapIndex, DWORD **ppdwMapId);
213
214 void CleanData(void);
215 };
216
217
218 //
219 // Functions
220 //
221
222 BOOL InitDataCollector(void);
223 void DeleteAllItemsForNode(DWORD dwNodeId);
224
225
226 //
227 // Variables
228 //
229
230 extern double g_dAvgPollerQueueSize;
231 extern double g_dAvgDBWriterQueueSize;
232 extern double g_dAvgStatusPollerQueueSize;
233 extern double g_dAvgConfigPollerQueueSize;
234 extern DWORD g_dwAvgDCIQueuingTime;
235
236
237 #endif /* _nms_dcoll_h_ */