code plementation preparation for instance discovery implementation
[public/netxms.git] / src / server / include / nms_dcoll.h
CommitLineData
5039dede
AK
1/*
2** NetXMS - Network Management System
50d0de67 3** Copyright (C) 2003-2012 Victor Kirhenshtein
5039dede
AK
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18**
19** File: 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
674164eb
VK
35/**
36 * Threshold check results
37 */
5039dede
AK
38#define THRESHOLD_REACHED 0
39#define THRESHOLD_REARMED 1
40#define NO_ACTION 2
41
674164eb
VK
42/**
43 * DCI value
44 */
fb05c05b 45class NXCORE_EXPORTABLE ItemValue
5039dede
AK
46{
47private:
48 double m_dFloat;
49 LONG m_iInt32;
50 INT64 m_iInt64;
51 DWORD m_dwInt32;
52 QWORD m_qwInt64;
53 TCHAR m_szString[MAX_DB_STRING];
54 DWORD m_dwTimeStamp;
55
56public:
57 ItemValue();
58 ItemValue(const TCHAR *pszValue, DWORD dwTimeStamp);
59 ItemValue(const ItemValue *pValue);
60 ~ItemValue();
61
df94e0ce
VK
62 void setTimeStamp(DWORD dwTime) { m_dwTimeStamp = dwTime; }
63 DWORD getTimeStamp() { return m_dwTimeStamp; }
5039dede 64
df94e0ce 65 const TCHAR *getString() { return m_szString; }
5039dede
AK
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; }
35f836fe 72 operator const TCHAR*() const { return m_szString; }
5039dede
AK
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
5039dede
AK
84class DCItem;
85
674164eb
VK
86/**
87 * Threshold definition class
88 */
fb05c05b 89class NXCORE_EXPORTABLE Threshold
5039dede
AK
90{
91private:
d9ae1904
VK
92 DWORD m_id; // Unique threshold id
93 DWORD m_itemId; // Related item id
94 DWORD m_eventCode; // Event code to be generated
95 DWORD m_rearmEventCode;
5039dede 96 ItemValue m_value;
d9ae1904
VK
97 BYTE m_function; // Function code
98 BYTE m_operation; // Comparision operation code
99 BYTE m_dataType; // Related item data type
711e5e9a 100 BYTE m_currentSeverity; // Current everity (NORMAL if threshold is inactive)
d9ae1904
VK
101 int m_param1; // Function's parameter #1
102 int m_param2; // Function's parameter #2
103 BOOL m_isReached;
104 int m_numMatches; // Number of consecutive matches
105 int m_repeatInterval; // -1 = default, 0 = off, >0 = seconds between repeats
106 time_t m_lastEventTimestamp;
107
35f836fe 108 const ItemValue& value() { return m_value; }
d9ae1904 109 void calculateAverageValue(ItemValue *pResult, ItemValue &lastValue, ItemValue **ppPrevValues);
007a87e7 110 void calculateSumValue(ItemValue *pResult, ItemValue &lastValue, ItemValue **ppPrevValues);
d9ae1904
VK
111 void calculateMDValue(ItemValue *pResult, ItemValue &lastValue, ItemValue **ppPrevValues);
112 void calculateDiff(ItemValue *pResult, ItemValue &lastValue, ItemValue **ppPrevValues);
5039dede
AK
113
114public:
115 Threshold();
116 Threshold(DCItem *pRelatedItem);
117 Threshold(Threshold *pSrc);
118 Threshold(DB_RESULT hResult, int iRow, DCItem *pRelatedItem);
a65c1819 119 Threshold(ConfigEntry *config, DCItem *parentItem);
5039dede
AK
120 ~Threshold();
121
d9ae1904 122 void bindToItem(DWORD dwItemId) { m_itemId = dwItemId; }
5039dede 123
d9ae1904
VK
124 DWORD getId() { return m_id; }
125 DWORD getEventCode() { return m_eventCode; }
126 DWORD getRearmEventCode() { return m_rearmEventCode; }
071fd171
VK
127 int getFunction() { return m_function; }
128 int getOperation() { return m_operation; }
fee65d26
VK
129 int getParam1() { return m_param1; }
130 int getParam2() { return m_param2; }
df94e0ce 131 const TCHAR *getStringValue() { return m_value.getString(); }
d9ae1904 132 BOOL isReached() { return m_isReached; }
5039dede 133
d9ae1904
VK
134 int getRepeatInterval() { return m_repeatInterval; }
135 time_t getLastEventTimestamp() { return m_lastEventTimestamp; }
711e5e9a
VK
136 int getCurrentSeverity() { return m_currentSeverity; }
137 void markLastEvent(int severity);
5039dede 138
d9ae1904
VK
139 BOOL saveToDB(DB_HANDLE hdb, DWORD dwIndex);
140 int check(ItemValue &value, ItemValue **ppPrevValues, ItemValue &fvalue);
141 int checkError(DWORD dwErrorCount);
5039dede 142
d9ae1904
VK
143 void createMessage(CSCPMessage *msg, DWORD baseId);
144 void updateFromMessage(CSCPMessage *msg, DWORD baseId);
5039dede 145
35f836fe
VK
146 void createId();
147 DWORD getRequiredCacheSize() { return ((m_function == F_LAST) || (m_function == F_ERROR)) ? 0 : m_param1; }
5039dede 148
d9ae1904 149 BOOL compare(Threshold *pThr);
5039dede 150
a65c1819 151 void createNXMPRecord(String &str, int index);
5039dede 152
d9ae1904
VK
153 void associate(DCItem *pItem);
154 void setFunction(int nFunc) { m_function = nFunc; }
155 void setOperation(int nOp) { m_operation = nOp; }
156 void setEvent(DWORD dwEvent) { m_eventCode = dwEvent; }
157 void setRearmEvent(DWORD dwEvent) { m_rearmEventCode = dwEvent; }
158 void setParam1(int nVal) { m_param1 = nVal; }
159 void setParam2(int nVal) { m_param2 = nVal; }
160 void setValue(const TCHAR *value) { m_value = value; }
5039dede
AK
161};
162
5039dede
AK
163class Template;
164
674164eb
VK
165/**
166 * Generic data collection object
167 */
cc8ce218 168class NXCORE_EXPORTABLE DCObject
5039dede 169{
cc8ce218 170protected:
5039dede 171 DWORD m_dwId;
54bb335a
VK
172 TCHAR m_szName[MAX_ITEM_NAME];
173 TCHAR m_szDescription[MAX_DB_STRING];
54bb335a 174 TCHAR m_systemTag[MAX_DB_STRING];
ced80327
VK
175 time_t m_tLastPoll; // Last poll time
176 int m_iPollingInterval; // Polling interval in seconds
177 int m_iRetentionTime; // Retention time in seconds
cc8ce218 178 BYTE m_source; // origin: SNMP, agent, etc.
ced80327
VK
179 BYTE m_status; // Item status: active, disabled or not supported
180 BYTE m_busy; // 1 when item is queued for polling, 0 if not
181 BYTE m_scheduledForDeletion; // 1 when item is scheduled for deletion, 0 if not
e320f8ce 182 WORD m_flags;
ced80327
VK
183 DWORD m_dwTemplateId; // Related template's id
184 DWORD m_dwTemplateItemId; // Related template item's id
5039dede 185 Template *m_pNode; // Pointer to node or template object this item related to
5039dede 186 MUTEX m_hMutex;
5039dede
AK
187 DWORD m_dwNumSchedules;
188 TCHAR **m_ppScheduleList;
ced80327
VK
189 time_t m_tLastCheck; // Last schedule checking time
190 DWORD m_dwErrorCount; // Consequtive collection error count
191 DWORD m_dwResourceId; // Associated cluster resource ID
192 DWORD m_dwProxyNode; // Proxy node ID or 0 to disable
65e2005b 193 WORD m_snmpPort; // Custom SNMP port or 0 for node default
cc8ce218 194 TCHAR *m_pszPerfTabSettings;
5039dede 195
c17f6cbc 196 void lock() { MutexLock(m_hMutex); }
fb05c05b 197 void unlock() { MutexUnlock(m_hMutex); }
5039dede 198
ba89fed2
VK
199 BOOL loadCustomSchedules();
200
cc8ce218 201 bool matchClusterResource();
5039dede 202
fb05c05b 203 void expandMacros(const TCHAR *src, TCHAR *dst, size_t dstLen);
5039dede 204
cc8ce218 205 virtual bool isCacheLoaded();
5039dede 206
cc8ce218
VK
207 // --- constructors ---
208 DCObject();
209 DCObject(const DCObject *src);
210 DCObject(DWORD dwId, const TCHAR *szName, int iSource, int iPollingInterval, int iRetentionTime, Template *pNode,
211 const TCHAR *pszDescription = NULL, const TCHAR *systemTag = NULL);
212 DCObject(ConfigEntry *config, Template *owner);
fb05c05b 213
cc8ce218
VK
214public:
215 virtual ~DCObject();
fb05c05b 216
16d6f798
VK
217 virtual int getType() const { return DCO_TYPE_GENERIC; }
218
219 virtual void updateFromTemplate(DCObject *dcObject);
220
cc8ce218
VK
221 virtual BOOL saveToDB(DB_HANDLE hdb);
222 virtual void deleteFromDB();
16d6f798 223 virtual bool loadThresholdsFromDB();
fb05c05b 224
df94e0ce
VK
225 virtual void processNewValue(time_t nTimeStamp, void *value);
226 virtual void processNewError();
227
cc8ce218 228 DWORD getId() { return m_dwId; }
ced80327 229 int getDataSource() { return m_source; }
ced80327 230 int getStatus() { return m_status; }
fb05c05b
VK
231 const TCHAR *getName() { return m_szName; }
232 const TCHAR *getDescription() { return m_szDescription; }
54bb335a 233 const TCHAR *getSystemTag() { return m_systemTag; }
74526d25 234 const TCHAR *getPerfTabSettings() { return m_pszPerfTabSettings; }
6fd6de0a 235 Template *getTarget() { return m_pNode; }
fb05c05b
VK
236 DWORD getTemplateId() { return m_dwTemplateId; }
237 DWORD getTemplateItemId() { return m_dwTemplateItemId; }
238 DWORD getResourceId() { return m_dwResourceId; }
239 DWORD getProxyNode() { return m_dwProxyNode; }
bb51576f 240 time_t getLastPollTime() { return m_tLastPoll; }
d7877de4 241 DWORD getErrorCount() { return m_dwErrorCount; }
65e2005b 242 WORD getSnmpPort() { return m_snmpPort; }
fb05c05b 243
ced80327
VK
244 bool isReadyForPolling(time_t currTime);
245 bool isScheduledForDeletion() { return m_scheduledForDeletion ? true : false; }
fb05c05b 246 void setLastPollTime(time_t tLastPoll) { m_tLastPoll = tLastPoll; }
a8f60046 247 void setStatus(int status, bool generateEvent);
ced80327 248 void setBusyFlag(BOOL busy) { m_busy = (BYTE)busy; }
fb05c05b 249 void setTemplateId(DWORD dwTemplateId, DWORD dwItemId)
5039dede 250 { m_dwTemplateId = dwTemplateId; m_dwTemplateItemId = dwItemId; }
cc8ce218
VK
251
252 virtual void createMessage(CSCPMessage *pMsg);
253 virtual void updateFromMessage(CSCPMessage *pMsg);
254
255 virtual void changeBinding(DWORD dwNewId, Template *pNode, BOOL doMacroExpansion);
256
257 virtual void deleteExpiredData();
258 virtual bool deleteAllData();
259
260 virtual void getEventList(DWORD **ppdwList, DWORD *pdwSize);
261 virtual void createNXMPRecord(String &str);
262
263 void setName(const TCHAR *pszName) { nx_strncpy(m_szName, pszName, MAX_ITEM_NAME); }
264 void setDescription(const TCHAR *pszDescr) { nx_strncpy(m_szDescription, pszDescr, MAX_DB_STRING); }
265 void setOrigin(int origin) { m_source = origin; }
266 void setRetentionTime(int nTime) { m_iRetentionTime = nTime; }
267 void setInterval(int nInt) { m_iPollingInterval = nInt; }
268 void setAdvScheduleFlag(BOOL bFlag) { if (bFlag) m_flags |= DCF_ADVANCED_SCHEDULE; else m_flags &= ~DCF_ADVANCED_SCHEDULE; }
269 void addSchedule(const TCHAR *pszSchedule);
270
271 bool prepareForDeletion();
3f0e1087
AK
272
273private:
274 BOOL matchSchedule(struct tm *pCurrTime, TCHAR *pszSchedule, BOOL *bWithSeconds, time_t currTimestamp);
cc8ce218
VK
275};
276
674164eb
VK
277/**
278 * Data collection item class
279 */
cc8ce218
VK
280class NXCORE_EXPORTABLE DCItem : public DCObject
281{
282protected:
6b8e9f96 283 TCHAR m_instance[MAX_DB_STRING];
cc8ce218
VK
284 BYTE m_deltaCalculation; // Delta calculation method
285 BYTE m_dataType;
6b8e9f96 286 ObjectArray<Threshold> *m_thresholds;
afbe5388
VK
287 TCHAR *m_transformerSource; // Transformation script (source code)
288 NXSL_Program *m_transformer; // Compiled transformation script
cc8ce218
VK
289 DWORD m_dwCacheSize; // Number of items in cache
290 ItemValue **m_ppValueCache;
291 ItemValue m_prevRawValue; // Previous raw value (used for delta calculation)
292 time_t m_tPrevValueTimeStamp;
293 bool m_bCacheLoaded;
294 int m_nBaseUnits;
295 int m_nMultiplier;
6b8e9f96 296 TCHAR *m_customUnitName;
cc8ce218 297 WORD m_snmpRawValueType; // Actual SNMP raw value type for input transformation
6b8e9f96
VK
298 WORD m_instanceDiscoveryMethod;
299 TCHAR *m_instanceDiscoveryData;
300 TCHAR *m_instanceFilterSource;
301 NXSL_Program *m_instanceFilter;
cc8ce218
VK
302
303 void transform(ItemValue &value, time_t nElapsedTime);
304 void checkThresholds(ItemValue &value);
305 void clearCache();
306
307 virtual bool isCacheLoaded();
308
309public:
310 DCItem();
311 DCItem(const DCItem *pItem);
312 DCItem(DB_RESULT hResult, int iRow, Template *pNode);
313 DCItem(DWORD dwId, const TCHAR *szName, int iSource, int iDataType,
314 int iPollingInterval, int iRetentionTime, Template *pNode,
315 const TCHAR *pszDescription = NULL, const TCHAR *systemTag = NULL);
316 DCItem(ConfigEntry *config, Template *owner);
317 virtual ~DCItem();
318
16d6f798
VK
319 virtual int getType() const { return DCO_TYPE_ITEM; }
320
321 virtual void updateFromTemplate(DCObject *dcObject);
cc8ce218
VK
322
323 virtual BOOL saveToDB(DB_HANDLE hdb);
324 virtual void deleteFromDB();
16d6f798 325 virtual bool loadThresholdsFromDB();
cc8ce218
VK
326
327 void updateCacheSize(DWORD dwCondId = 0);
328
329 int getDataType() { return m_dataType; }
330 bool isInterpretSnmpRawValue() { return (m_flags & DCF_RAW_VALUE_OCTET_STRING) ? true : false; }
331 WORD getSnmpRawValueType() { return m_snmpRawValueType; }
711e5e9a 332 bool hasActiveThreshold();
cc8ce218 333
df94e0ce 334 void processNewValue(time_t nTimeStamp, void *value);
fb05c05b 335 void processNewError();
5039dede 336
fb05c05b
VK
337 void getLastValue(CSCPMessage *pMsg, DWORD dwId);
338 NXSL_Value *getValueForNXSL(int nFunction, int nPolls);
5039dede 339
cc8ce218 340 virtual void createMessage(CSCPMessage *pMsg);
fb05c05b 341 void updateFromMessage(CSCPMessage *pMsg, DWORD *pdwNumMaps, DWORD **ppdwMapIndex, DWORD **ppdwMapId);
071fd171 342 void fillMessageWithThresholds(CSCPMessage *msg);
5039dede 343
cc8ce218
VK
344 virtual void changeBinding(DWORD dwNewId, Template *pNode, BOOL doMacroExpansion);
345
346 virtual void deleteExpiredData();
347 virtual bool deleteAllData();
5039dede 348
cc8ce218
VK
349 virtual void getEventList(DWORD **ppdwList, DWORD *pdwSize);
350 virtual void createNXMPRecord(String &str);
4262c0dc 351
6b8e9f96 352 int getThresholdCount() const { return (m_thresholds != NULL) ? m_thresholds->size() : 0; }
fb05c05b
VK
353 BOOL enumThresholds(BOOL (* pfCallback)(Threshold *, DWORD, void *), void *pArg);
354
6b8e9f96 355 void setInstance(const TCHAR *instance) { nx_strncpy(m_instance, instance, MAX_DB_STRING); }
ced80327 356 void setDataType(int dataType) { m_dataType = dataType; }
ced80327 357 void setDeltaCalcMethod(int method) { m_deltaCalculation = method; }
e320f8ce 358 void setAllThresholdsFlag(BOOL bFlag) { if (bFlag) m_flags |= DCF_ALL_THRESHOLDS; else m_flags &= ~DCF_ALL_THRESHOLDS; }
fb05c05b
VK
359 void addThreshold(Threshold *pThreshold);
360 void deleteAllThresholds();
a65c1819 361 void setTransformationScript(const TCHAR *pszScript);
afbe5388 362 void setInstanceFilter(const TCHAR *pszScript);
fb05c05b
VK
363
364 BOOL testTransformation(const TCHAR *script, const TCHAR *value, TCHAR *buffer, size_t bufSize);
5039dede
AK
365};
366
674164eb
VK
367/**
368 * Table column definition
369 */
df94e0ce
VK
370class NXCORE_EXPORTABLE DCTableColumn
371{
372private:
373 TCHAR m_name[MAX_COLUMN_NAME];
374 int m_dataType;
375 SNMP_ObjectId *m_snmpOid;
376 TCHAR *m_scriptSource;
377 NXSL_Program *m_script;
378
379public:
380 DCTableColumn(const DCTableColumn *src);
b4369830
VK
381 DCTableColumn(CSCPMessage *msg, DWORD baseId);
382 DCTableColumn(DB_RESULT hResult, int row);
df94e0ce
VK
383 ~DCTableColumn();
384
385 void setTransformationScript(const TCHAR *script);
ba89fed2
VK
386
387 const TCHAR *getName() { return m_name; }
388 int getDataType() { return m_dataType; }
389 SNMP_ObjectId *getSnmpOid() { return m_snmpOid; }
390 const TCHAR *getScriptSource() { return m_scriptSource; }
391};
392
393
394//
395// Table column ID hash entry
396//
397
398struct TC_ID_MAP_ENTRY
399{
400 LONG id;
401 TCHAR name[MAX_COLUMN_NAME];
df94e0ce
VK
402};
403
404
16d6f798
VK
405//
406// Tabular data collection object
407//
408
409class NXCORE_EXPORTABLE DCTable : public DCObject
410{
df94e0ce
VK
411protected:
412 TCHAR m_instanceColumn[MAX_COLUMN_NAME];
413 ObjectArray<DCTableColumn> *m_columns;
b9a8e081 414 Table *m_lastValue;
df94e0ce 415
ba89fed2
VK
416 static TC_ID_MAP_ENTRY *m_cache;
417 static int m_cacheSize;
418 static int m_cacheAllocated;
419 static MUTEX m_cacheMutex;
420
16d6f798
VK
421public:
422 DCTable();
423 DCTable(const DCTable *src);
ba89fed2
VK
424 DCTable(DWORD id, const TCHAR *name, int source, int pollingInterval, int retentionTime,
425 Template *node, const TCHAR *instanceColumn = NULL, const TCHAR *description = NULL, const TCHAR *systemTag = NULL);
426 DCTable(DB_RESULT hResult, int iRow, Template *pNode);
16d6f798
VK
427 virtual ~DCTable();
428
429 virtual int getType() const { return DCO_TYPE_TABLE; }
430
ba89fed2
VK
431 virtual BOOL saveToDB(DB_HANDLE hdb);
432 virtual void deleteFromDB();
433
533ce8c0
VK
434 virtual void processNewValue(time_t nTimeStamp, void *value);
435 virtual void processNewError();
ba89fed2 436
b4369830
VK
437 virtual void createMessage(CSCPMessage *pMsg);
438 virtual void updateFromMessage(CSCPMessage *pMsg);
439
ba89fed2
VK
440 virtual void deleteExpiredData();
441 virtual bool deleteAllData();
b9a8e081
VK
442
443 void getLastValue(CSCPMessage *msg);
92c51b1d
VK
444 void getLastValueSummary(CSCPMessage *pMsg, DWORD dwId);
445
446 LONG getInstanceColumnId();
447
448 static LONG columnIdFromName(const TCHAR *name);
16d6f798
VK
449};
450
451
5039dede
AK
452//
453// Functions
454//
455
ced80327 456BOOL InitDataCollector();
5039dede 457void DeleteAllItemsForNode(DWORD dwNodeId);
074498ac 458void WriteFullParamListToMessage(CSCPMessage *pMsg, WORD flags);
5039dede
AK
459
460void CalculateItemValueDiff(ItemValue &result, int nDataType,
461 ItemValue &value1, ItemValue &value2);
462void CalculateItemValueAverage(ItemValue &result, int nDataType,
463 int nNumValues, ItemValue **ppValueList);
464void CalculateItemValueMD(ItemValue &result, int nDataType,
465 int nNumValues, ItemValue **ppValueList);
466
467
468//
469// Variables
470//
471
472extern double g_dAvgPollerQueueSize;
473extern double g_dAvgDBWriterQueueSize;
cf084617
VK
474extern double g_dAvgIDataWriterQueueSize;
475extern double g_dAvgDBAndIDataWriterQueueSize;
5039dede
AK
476extern double g_dAvgStatusPollerQueueSize;
477extern double g_dAvgConfigPollerQueueSize;
478extern DWORD g_dwAvgDCIQueuingTime;
479
480
481#endif /* _nms_dcoll_h_ */