all pollers converted to single thread pool
[public/netxms.git] / src / server / include / nms_dcoll.h
CommitLineData
4016c0df 1/*
5039dede 2** NetXMS - Network Management System
55bdca5a 3** Copyright (C) 2003-2013 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
3cd6f508 34#define DCE_IGNORE 3
5039dede 35
674164eb
VK
36/**
37 * Threshold check results
38 */
a0dc14f9
VK
39enum ThresholdCheckResult
40{
41 ACTIVATED = 0,
42 DEACTIVATED = 1,
43 ALREADY_ACTIVE = 2,
44 ALREADY_INACTIVE = 3
45};
5039dede 46
674164eb
VK
47/**
48 * DCI value
49 */
fb05c05b 50class NXCORE_EXPORTABLE ItemValue
5039dede
AK
51{
52private:
53 double m_dFloat;
967893bb 54 INT32 m_iInt32;
5039dede 55 INT64 m_iInt64;
967893bb
VK
56 UINT32 m_dwInt32;
57 UINT64 m_qwInt64;
5039dede 58 TCHAR m_szString[MAX_DB_STRING];
967893bb 59 UINT32 m_dwTimeStamp;
5039dede
AK
60
61public:
62 ItemValue();
967893bb 63 ItemValue(const TCHAR *pszValue, UINT32 dwTimeStamp);
5039dede
AK
64 ItemValue(const ItemValue *pValue);
65 ~ItemValue();
66
967893bb
VK
67 void setTimeStamp(UINT32 dwTime) { m_dwTimeStamp = dwTime; }
68 UINT32 getTimeStamp() { return m_dwTimeStamp; }
5039dede 69
df94e0ce 70 const TCHAR *getString() { return m_szString; }
5039dede
AK
71
72 operator double() { return m_dFloat; }
967893bb
VK
73 operator UINT32() { return m_dwInt32; }
74 operator UINT64() { return m_qwInt64; }
75 operator INT32() { return m_iInt32; }
5039dede 76 operator INT64() { return m_iInt64; }
35f836fe 77 operator const TCHAR*() const { return m_szString; }
5039dede
AK
78
79 const ItemValue& operator=(const ItemValue &src);
80 const ItemValue& operator=(const TCHAR *pszStr);
81 const ItemValue& operator=(double dFloat);
967893bb 82 const ItemValue& operator=(INT32 iInt32);
5039dede 83 const ItemValue& operator=(INT64 iInt64);
967893bb
VK
84 const ItemValue& operator=(UINT32 dwInt32);
85 const ItemValue& operator=(UINT64 qwInt64);
5039dede
AK
86};
87
88
5039dede 89class DCItem;
1d34c533 90class DataCollectionTarget;
5039dede 91
674164eb
VK
92/**
93 * Threshold definition class
94 */
fb05c05b 95class NXCORE_EXPORTABLE Threshold
5039dede
AK
96{
97private:
967893bb 98 UINT32 m_id; // Unique threshold id
1d34c533
VK
99 UINT32 m_itemId; // Parent item id
100 UINT32 m_targetId; // Parent data collection target ID
967893bb
VK
101 UINT32 m_eventCode; // Event code to be generated
102 UINT32 m_rearmEventCode;
5039dede 103 ItemValue m_value;
d9ae1904
VK
104 BYTE m_function; // Function code
105 BYTE m_operation; // Comparision operation code
106 BYTE m_dataType; // Related item data type
711e5e9a 107 BYTE m_currentSeverity; // Current everity (NORMAL if threshold is inactive)
1d34c533
VK
108 int m_sampleCount; // Number of samples to calculate function on
109 TCHAR *m_scriptSource;
6b29839d 110 NXSL_VM *m_script;
d9ae1904
VK
111 BOOL m_isReached;
112 int m_numMatches; // Number of consecutive matches
113 int m_repeatInterval; // -1 = default, 0 = off, >0 = seconds between repeats
114 time_t m_lastEventTimestamp;
115
35f836fe 116 const ItemValue& value() { return m_value; }
d9ae1904 117 void calculateAverageValue(ItemValue *pResult, ItemValue &lastValue, ItemValue **ppPrevValues);
007a87e7 118 void calculateSumValue(ItemValue *pResult, ItemValue &lastValue, ItemValue **ppPrevValues);
d9ae1904
VK
119 void calculateMDValue(ItemValue *pResult, ItemValue &lastValue, ItemValue **ppPrevValues);
120 void calculateDiff(ItemValue *pResult, ItemValue &lastValue, ItemValue **ppPrevValues);
1d34c533 121 void setScript(TCHAR *script);
5039dede
AK
122
123public:
124 Threshold();
125 Threshold(DCItem *pRelatedItem);
126 Threshold(Threshold *pSrc);
127 Threshold(DB_RESULT hResult, int iRow, DCItem *pRelatedItem);
a65c1819 128 Threshold(ConfigEntry *config, DCItem *parentItem);
5039dede
AK
129 ~Threshold();
130
1d34c533 131 void bindToItem(UINT32 itemId, UINT32 targetId) { m_itemId = itemId; m_targetId = targetId; }
5039dede 132
967893bb
VK
133 UINT32 getId() { return m_id; }
134 UINT32 getEventCode() { return m_eventCode; }
135 UINT32 getRearmEventCode() { return m_rearmEventCode; }
071fd171
VK
136 int getFunction() { return m_function; }
137 int getOperation() { return m_operation; }
1d34c533 138 int getSampleCount() { return m_sampleCount; }
df94e0ce 139 const TCHAR *getStringValue() { return m_value.getString(); }
d9ae1904 140 BOOL isReached() { return m_isReached; }
4016c0df 141
d9ae1904
VK
142 int getRepeatInterval() { return m_repeatInterval; }
143 time_t getLastEventTimestamp() { return m_lastEventTimestamp; }
711e5e9a
VK
144 int getCurrentSeverity() { return m_currentSeverity; }
145 void markLastEvent(int severity);
5039dede 146
967893bb 147 BOOL saveToDB(DB_HANDLE hdb, UINT32 dwIndex);
1d34c533 148 ThresholdCheckResult check(ItemValue &value, ItemValue **ppPrevValues, ItemValue &fvalue, NetObj *target, DCItem *dci);
a0dc14f9 149 ThresholdCheckResult checkError(UINT32 dwErrorCount);
5039dede 150
b368969c
VK
151 void createMessage(NXCPMessage *msg, UINT32 baseId);
152 void updateFromMessage(NXCPMessage *msg, UINT32 baseId);
5039dede 153
35f836fe 154 void createId();
1d34c533 155 UINT32 getRequiredCacheSize() { return ((m_function == F_LAST) || (m_function == F_ERROR)) ? 0 : m_sampleCount; }
5039dede 156
d9ae1904 157 BOOL compare(Threshold *pThr);
5039dede 158
a65c1819 159 void createNXMPRecord(String &str, int index);
5039dede 160
d9ae1904
VK
161 void associate(DCItem *pItem);
162 void setFunction(int nFunc) { m_function = nFunc; }
163 void setOperation(int nOp) { m_operation = nOp; }
967893bb
VK
164 void setEvent(UINT32 dwEvent) { m_eventCode = dwEvent; }
165 void setRearmEvent(UINT32 dwEvent) { m_rearmEventCode = dwEvent; }
1d34c533 166 void setSampleCount(int nVal) { m_sampleCount = nVal; }
d9ae1904 167 void setValue(const TCHAR *value) { m_value = value; }
5039dede
AK
168};
169
5039dede
AK
170class Template;
171
674164eb
VK
172/**
173 * Generic data collection object
174 */
cc8ce218 175class NXCORE_EXPORTABLE DCObject
5039dede 176{
cc8ce218 177protected:
c42b4551
VK
178 UINT32 m_id;
179 TCHAR m_name[MAX_ITEM_NAME];
54bb335a 180 TCHAR m_szDescription[MAX_DB_STRING];
54bb335a 181 TCHAR m_systemTag[MAX_DB_STRING];
ced80327
VK
182 time_t m_tLastPoll; // Last poll time
183 int m_iPollingInterval; // Polling interval in seconds
e9902466 184 int m_iRetentionTime; // Retention time in days
cc8ce218 185 BYTE m_source; // origin: SNMP, agent, etc.
ced80327
VK
186 BYTE m_status; // Item status: active, disabled or not supported
187 BYTE m_busy; // 1 when item is queued for polling, 0 if not
188 BYTE m_scheduledForDeletion; // 1 when item is scheduled for deletion, 0 if not
50ea73e3 189 UINT16 m_flags;
967893bb
VK
190 UINT32 m_dwTemplateId; // Related template's id
191 UINT32 m_dwTemplateItemId; // Related template item's id
5039dede 192 Template *m_pNode; // Pointer to node or template object this item related to
5039dede 193 MUTEX m_hMutex;
967893bb 194 UINT32 m_dwNumSchedules;
5039dede 195 TCHAR **m_ppScheduleList;
ced80327 196 time_t m_tLastCheck; // Last schedule checking time
967893bb
VK
197 UINT32 m_dwErrorCount; // Consequtive collection error count
198 UINT32 m_dwResourceId; // Associated cluster resource ID
199 UINT32 m_dwProxyNode; // Proxy node ID or 0 to disable
65e2005b 200 WORD m_snmpPort; // Custom SNMP port or 0 for node default
cc8ce218 201 TCHAR *m_pszPerfTabSettings;
55bdca5a 202 TCHAR *m_transformationScriptSource; // Transformation script (source code)
6b29839d 203 NXSL_VM *m_transformationScript; // Compiled transformation script
4016c0df 204 TCHAR *m_comments;
5039dede 205
c17f6cbc 206 void lock() { MutexLock(m_hMutex); }
fb05c05b 207 void unlock() { MutexUnlock(m_hMutex); }
5039dede 208
ba89fed2 209 BOOL loadCustomSchedules();
55bdca5a 210 bool matchSchedule(struct tm *pCurrTime, TCHAR *pszSchedule, BOOL *bWithSeconds, time_t currTimestamp);
5039dede 211
fb05c05b 212 void expandMacros(const TCHAR *src, TCHAR *dst, size_t dstLen);
5039dede 213
cc8ce218 214 virtual bool isCacheLoaded();
5039dede 215
cc8ce218
VK
216 // --- constructors ---
217 DCObject();
218 DCObject(const DCObject *src);
967893bb 219 DCObject(UINT32 dwId, const TCHAR *szName, int iSource, int iPollingInterval, int iRetentionTime, Template *pNode,
cc8ce218
VK
220 const TCHAR *pszDescription = NULL, const TCHAR *systemTag = NULL);
221 DCObject(ConfigEntry *config, Template *owner);
fb05c05b 222
cc8ce218
VK
223public:
224 virtual ~DCObject();
fb05c05b 225
16d6f798
VK
226 virtual int getType() const { return DCO_TYPE_GENERIC; }
227
228 virtual void updateFromTemplate(DCObject *dcObject);
229
cc8ce218 230 virtual BOOL saveToDB(DB_HANDLE hdb);
c42b4551 231 virtual void deleteFromDatabase();
16d6f798 232 virtual bool loadThresholdsFromDB();
fb05c05b 233
e63fca40 234 virtual bool processNewValue(time_t nTimeStamp, const void *value, bool *updateStatus);
df94e0ce
VK
235 virtual void processNewError();
236
d51f2182
VK
237 virtual bool hasValue();
238
c42b4551 239 UINT32 getId() { return m_id; }
ced80327 240 int getDataSource() { return m_source; }
ced80327 241 int getStatus() { return m_status; }
c42b4551 242 const TCHAR *getName() { return m_name; }
fb05c05b 243 const TCHAR *getDescription() { return m_szDescription; }
54bb335a 244 const TCHAR *getSystemTag() { return m_systemTag; }
74526d25 245 const TCHAR *getPerfTabSettings() { return m_pszPerfTabSettings; }
e9902466 246 int getPollingInterval() { return m_iPollingInterval; }
6fd6de0a 247 Template *getTarget() { return m_pNode; }
967893bb
VK
248 UINT32 getTemplateId() { return m_dwTemplateId; }
249 UINT32 getTemplateItemId() { return m_dwTemplateItemId; }
250 UINT32 getResourceId() { return m_dwResourceId; }
251 UINT32 getProxyNode() { return m_dwProxyNode; }
bb51576f 252 time_t getLastPollTime() { return m_tLastPoll; }
967893bb 253 UINT32 getErrorCount() { return m_dwErrorCount; }
65e2005b 254 WORD getSnmpPort() { return m_snmpPort; }
f6d71b18 255 bool isShowOnObjectTooltip() { return (m_flags & DCF_SHOW_ON_OBJECT_TOOLTIP) ? true : false; }
1fa2ca0e 256 bool isShowInObjectOverview() { return (m_flags & DCF_SHOW_IN_OBJECT_OVERVIEW) ? true : false; }
9ad921e5 257 bool isAggregateOnCluster() { return (m_flags & DCF_AGGREGATE_ON_CLUSTER) ? true : false; }
1fa2ca0e 258 bool isStatusDCO() {return (m_flags & DCF_CALCULATE_NODE_STATUS) ? true : false; }
85ae39bc 259 int getAggregationFunction() { return DCF_GET_AGGREGATION_FUNCTION(m_flags); }
400e55c4 260 Template *getNode() { return m_pNode; }
5f648670 261 int getRetentionTime() { return m_iRetentionTime; }
7f16667f 262 const TCHAR *getComments() { return m_comments; }
e9902466 263 INT16 getAgentCacheMode();
fb05c05b 264
f4e4e1d0 265 bool matchClusterResource();
ced80327
VK
266 bool isReadyForPolling(time_t currTime);
267 bool isScheduledForDeletion() { return m_scheduledForDeletion ? true : false; }
fb05c05b 268 void setLastPollTime(time_t tLastPoll) { m_tLastPoll = tLastPoll; }
a8f60046 269 void setStatus(int status, bool generateEvent);
ced80327 270 void setBusyFlag(BOOL busy) { m_busy = (BYTE)busy; }
4016c0df 271 void setTemplateId(UINT32 dwTemplateId, UINT32 dwItemId)
5039dede 272 { m_dwTemplateId = dwTemplateId; m_dwTemplateItemId = dwItemId; }
cc8ce218 273
b368969c
VK
274 virtual void createMessage(NXCPMessage *pMsg);
275 virtual void updateFromMessage(NXCPMessage *pMsg);
cc8ce218 276
967893bb 277 virtual void changeBinding(UINT32 dwNewId, Template *pNode, BOOL doMacroExpansion);
cc8ce218
VK
278
279 virtual void deleteExpiredData();
280 virtual bool deleteAllData();
281
967893bb 282 virtual void getEventList(UINT32 **ppdwList, UINT32 *pdwSize);
cc8ce218
VK
283 virtual void createNXMPRecord(String &str);
284
c42b4551 285 void setName(const TCHAR *pszName) { nx_strncpy(m_name, pszName, MAX_ITEM_NAME); }
cc8ce218
VK
286 void setDescription(const TCHAR *pszDescr) { nx_strncpy(m_szDescription, pszDescr, MAX_DB_STRING); }
287 void setOrigin(int origin) { m_source = origin; }
288 void setRetentionTime(int nTime) { m_iRetentionTime = nTime; }
289 void setInterval(int nInt) { m_iPollingInterval = nInt; }
290 void setAdvScheduleFlag(BOOL bFlag) { if (bFlag) m_flags |= DCF_ADVANCED_SCHEDULE; else m_flags &= ~DCF_ADVANCED_SCHEDULE; }
291 void addSchedule(const TCHAR *pszSchedule);
55bdca5a 292 void setTransformationScript(const TCHAR *pszScript);
cc8ce218
VK
293
294 bool prepareForDeletion();
295};
296
674164eb
VK
297/**
298 * Data collection item class
299 */
cc8ce218
VK
300class NXCORE_EXPORTABLE DCItem : public DCObject
301{
302protected:
6b8e9f96 303 TCHAR m_instance[MAX_DB_STRING];
cc8ce218
VK
304 BYTE m_deltaCalculation; // Delta calculation method
305 BYTE m_dataType;
d02f6b92 306 int m_sampleCount; // Number of samples required to calculate value
6b8e9f96 307 ObjectArray<Threshold> *m_thresholds;
d140955e
VK
308 UINT32 m_cacheSize; // Number of items in cache
309 UINT32 m_requiredCacheSize;
cc8ce218
VK
310 ItemValue **m_ppValueCache;
311 ItemValue m_prevRawValue; // Previous raw value (used for delta calculation)
312 time_t m_tPrevValueTimeStamp;
313 bool m_bCacheLoaded;
314 int m_nBaseUnits;
315 int m_nMultiplier;
6b8e9f96 316 TCHAR *m_customUnitName;
cc8ce218 317 WORD m_snmpRawValueType; // Actual SNMP raw value type for input transformation
6b8e9f96
VK
318 WORD m_instanceDiscoveryMethod;
319 TCHAR *m_instanceDiscoveryData;
320 TCHAR *m_instanceFilterSource;
6b29839d 321 NXSL_VM *m_instanceFilter;
cc8ce218 322
0156d6b2 323 bool transform(ItemValue &value, time_t nElapsedTime);
cc8ce218
VK
324 void checkThresholds(ItemValue &value);
325 void clearCache();
326
327 virtual bool isCacheLoaded();
328
329public:
330 DCItem();
331 DCItem(const DCItem *pItem);
332 DCItem(DB_RESULT hResult, int iRow, Template *pNode);
4016c0df 333 DCItem(UINT32 dwId, const TCHAR *szName, int iSource, int iDataType,
cc8ce218
VK
334 int iPollingInterval, int iRetentionTime, Template *pNode,
335 const TCHAR *pszDescription = NULL, const TCHAR *systemTag = NULL);
336 DCItem(ConfigEntry *config, Template *owner);
337 virtual ~DCItem();
338
16d6f798
VK
339 virtual int getType() const { return DCO_TYPE_ITEM; }
340
341 virtual void updateFromTemplate(DCObject *dcObject);
cc8ce218
VK
342
343 virtual BOOL saveToDB(DB_HANDLE hdb);
c42b4551 344 virtual void deleteFromDatabase();
16d6f798 345 virtual bool loadThresholdsFromDB();
cc8ce218 346
967893bb 347 void updateCacheSize(UINT32 dwCondId = 0);
d140955e 348 void reloadCache();
cc8ce218
VK
349
350 int getDataType() { return m_dataType; }
351 bool isInterpretSnmpRawValue() { return (m_flags & DCF_RAW_VALUE_OCTET_STRING) ? true : false; }
352 WORD getSnmpRawValueType() { return m_snmpRawValueType; }
711e5e9a 353 bool hasActiveThreshold();
3f4c195f 354 int getThresholdSeverity();
d51f2182
VK
355 WORD getInstanceDiscoveryMethod() { return m_instanceDiscoveryMethod; }
356 const TCHAR *getInstanceDiscoveryData() { return m_instanceDiscoveryData; }
6b29839d 357 NXSL_VM *getInstanceFilter() { return m_instanceFilter; }
d51f2182 358 const TCHAR *getInstance() { return m_instance; }
d02f6b92 359 int getSampleCount() { return m_sampleCount; }
d51f2182 360
a6312bd6 361 void filterInstanceList(StringMap *instances);
d51f2182 362 void expandInstance();
cc8ce218 363
e63fca40 364 virtual bool processNewValue(time_t nTimeStamp, const void *value, bool *updateStatus);
0156d6b2 365 virtual void processNewError();
5039dede 366
d51f2182
VK
367 virtual bool hasValue();
368
b368969c 369 void fillLastValueMessage(NXCPMessage *pMsg, UINT32 dwId);
fb05c05b 370 NXSL_Value *getValueForNXSL(int nFunction, int nPolls);
42c782b1 371 NXSL_Value *getRawValueForNXSL();
4a435beb 372 const TCHAR *getLastValue();
85ae39bc 373 ItemValue *getInternalLastValue();
2852ef09 374 TCHAR *getAggregateValue(AggregationFunction func, time_t periodStart, time_t periodEnd);
5039dede 375
b368969c 376 virtual void createMessage(NXCPMessage *pMsg);
0c364581 377#if defined(__SUNPRO_CC) || defined(__HP_aCC)
9113e749
VK
378 using DCObject::updateFromMessage;
379#endif
b368969c
VK
380 void updateFromMessage(NXCPMessage *pMsg, UINT32 *pdwNumMaps, UINT32 **ppdwMapIndex, UINT32 **ppdwMapId);
381 void fillMessageWithThresholds(NXCPMessage *msg);
5039dede 382
967893bb 383 virtual void changeBinding(UINT32 dwNewId, Template *pNode, BOOL doMacroExpansion);
cc8ce218
VK
384
385 virtual void deleteExpiredData();
386 virtual bool deleteAllData();
5039dede 387
967893bb 388 virtual void getEventList(UINT32 **ppdwList, UINT32 *pdwSize);
cc8ce218 389 virtual void createNXMPRecord(String &str);
4262c0dc 390
6b8e9f96 391 int getThresholdCount() const { return (m_thresholds != NULL) ? m_thresholds->size() : 0; }
967893bb 392 BOOL enumThresholds(BOOL (* pfCallback)(Threshold *, UINT32, void *), void *pArg);
fb05c05b 393
6b8e9f96 394 void setInstance(const TCHAR *instance) { nx_strncpy(m_instance, instance, MAX_DB_STRING); }
ced80327 395 void setDataType(int dataType) { m_dataType = dataType; }
ced80327 396 void setDeltaCalcMethod(int method) { m_deltaCalculation = method; }
e320f8ce 397 void setAllThresholdsFlag(BOOL bFlag) { if (bFlag) m_flags |= DCF_ALL_THRESHOLDS; else m_flags &= ~DCF_ALL_THRESHOLDS; }
fb05c05b
VK
398 void addThreshold(Threshold *pThreshold);
399 void deleteAllThresholds();
d51f2182 400 void setInstanceDiscoveryMethod(WORD method) { m_instanceDiscoveryMethod = method; }
a6312bd6 401 void setInstanceDiscoveryData(const TCHAR *data) { safe_free(m_instanceDiscoveryData); m_instanceDiscoveryData = _tcsdup_ex(data); }
afbe5388 402 void setInstanceFilter(const TCHAR *pszScript);
fb05c05b 403
75f78aef 404 static bool testTransformation(DataCollectionTarget *object, const TCHAR *script, const TCHAR *value, TCHAR *buffer, size_t bufSize);
5039dede
AK
405};
406
674164eb
VK
407/**
408 * Table column definition
409 */
df94e0ce
VK
410class NXCORE_EXPORTABLE DCTableColumn
411{
412private:
413 TCHAR m_name[MAX_COLUMN_NAME];
0b77b73e 414 TCHAR *m_displayName;
df94e0ce 415 SNMP_ObjectId *m_snmpOid;
22aaa779 416 UINT16 m_flags;
df94e0ce
VK
417
418public:
419 DCTableColumn(const DCTableColumn *src);
b368969c 420 DCTableColumn(NXCPMessage *msg, UINT32 baseId);
b4369830 421 DCTableColumn(DB_RESULT hResult, int row);
9387bc59 422 DCTableColumn(ConfigEntry *e);
df94e0ce
VK
423 ~DCTableColumn();
424
ba89fed2 425 const TCHAR *getName() { return m_name; }
0b77b73e 426 const TCHAR *getDisplayName() { return (m_displayName != NULL) ? m_displayName : m_name; }
22aaa779
VK
427 UINT16 getFlags() { return m_flags; }
428 int getDataType() { return TCF_GET_DATA_TYPE(m_flags); }
429 int getAggregationFunction() { return TCF_GET_AGGREGATION_FUNCTION(m_flags); }
ba89fed2 430 SNMP_ObjectId *getSnmpOid() { return m_snmpOid; }
22aaa779 431 bool isInstanceColumn() { return (m_flags & TCF_INSTANCE_COLUMN) != 0; }
6e102d6e
VK
432
433 void createNXMPRecord(String &str, int id);
ba89fed2
VK
434};
435
a567a02a
VK
436/**
437 * Table column ID hash entry
438 */
ba89fed2
VK
439struct TC_ID_MAP_ENTRY
440{
967893bb 441 INT32 id;
ba89fed2 442 TCHAR name[MAX_COLUMN_NAME];
df94e0ce
VK
443};
444
9098ad59
VK
445/**
446 * Condition for table DCI threshold
447 */
448class NXCORE_EXPORTABLE DCTableCondition
449{
450private:
451 TCHAR *m_column;
452 int m_operation;
453 ItemValue m_value;
454
455public:
456 DCTableCondition(const TCHAR *column, int operation, const TCHAR *value);
457 DCTableCondition(DCTableCondition *src);
458 ~DCTableCondition();
459
460 bool check(Table *value, int row);
461
462 const TCHAR *getColumn() { return m_column; }
463 int getOperation() { return m_operation; }
464 const TCHAR *getValue() { return m_value.getString(); }
465};
466
467/**
468 * Condition group for table DCI threshold
469 */
470class NXCORE_EXPORTABLE DCTableConditionGroup
471{
472private:
473 ObjectArray<DCTableCondition> *m_conditions;
474
475public:
476 DCTableConditionGroup();
b368969c 477 DCTableConditionGroup(NXCPMessage *msg, UINT32 *baseId);
9098ad59 478 DCTableConditionGroup(DCTableConditionGroup *src);
9387bc59 479 DCTableConditionGroup(ConfigEntry *e);
9098ad59
VK
480 ~DCTableConditionGroup();
481
482 bool check(Table *value, int row);
483
b368969c 484 UINT32 fillMessage(NXCPMessage *msg, UINT32 baseId);
9098ad59
VK
485
486 ObjectArray<DCTableCondition> *getConditions() { return m_conditions; }
487};
488
489/**
490 * Threshold definition for tabe DCI
491 */
492class NXCORE_EXPORTABLE DCTableThreshold
493{
494private:
495 UINT32 m_id;
496 ObjectArray<DCTableConditionGroup> *m_groups;
497 UINT32 m_activationEvent;
498 UINT32 m_deactivationEvent;
a58501c0 499 StringSet *m_activeKeys;
9098ad59
VK
500
501 void loadConditions();
502
503public:
504 DCTableThreshold();
505 DCTableThreshold(DB_RESULT hResult, int row);
b368969c 506 DCTableThreshold(NXCPMessage *msg, UINT32 *baseId);
9098ad59 507 DCTableThreshold(DCTableThreshold *src);
9387bc59 508 DCTableThreshold(ConfigEntry *e);
9098ad59
VK
509 ~DCTableThreshold();
510
fa493140
VK
511 void copyState(DCTableThreshold *src);
512
a0dc14f9 513 ThresholdCheckResult check(Table *value, int row, const TCHAR *instance);
9098ad59
VK
514
515 bool saveToDatabase(DB_HANDLE hdb, UINT32 tableId, int seq);
b368969c 516 UINT32 fillMessage(NXCPMessage *msg, UINT32 baseId);
6e102d6e
VK
517 void createNXMPRecord(String &str, int id);
518
9098ad59 519 UINT32 getId() { return m_id; }
a0dc14f9
VK
520 UINT32 getActivationEvent() { return m_activationEvent; }
521 UINT32 getDeactivationEvent() { return m_deactivationEvent; }
9098ad59
VK
522};
523
4a435beb
VK
524/**
525 * Tabular data collection object
526 */
16d6f798
VK
527class NXCORE_EXPORTABLE DCTable : public DCObject
528{
df94e0ce 529protected:
df94e0ce 530 ObjectArray<DCTableColumn> *m_columns;
9098ad59 531 ObjectArray<DCTableThreshold> *m_thresholds;
b9a8e081 532 Table *m_lastValue;
df94e0ce 533
ba89fed2
VK
534 static TC_ID_MAP_ENTRY *m_cache;
535 static int m_cacheSize;
536 static int m_cacheAllocated;
537 static MUTEX m_cacheMutex;
538
0156d6b2 539 bool transform(Table *value);
50633e41 540 void checkThresholds(Table *value);
4016c0df 541
9098ad59
VK
542 bool loadThresholds();
543 bool saveThresholds(DB_HANDLE hdb);
7de1151b 544
16d6f798
VK
545public:
546 DCTable();
547 DCTable(const DCTable *src);
967893bb 548 DCTable(UINT32 id, const TCHAR *name, int source, int pollingInterval, int retentionTime,
22aaa779 549 Template *node, const TCHAR *description = NULL, const TCHAR *systemTag = NULL);
ba89fed2 550 DCTable(DB_RESULT hResult, int iRow, Template *pNode);
9387bc59 551 DCTable(ConfigEntry *config, Template *owner);
16d6f798
VK
552 virtual ~DCTable();
553
554 virtual int getType() const { return DCO_TYPE_TABLE; }
555
a0ddfb29
VK
556 virtual void updateFromTemplate(DCObject *dcObject);
557
ba89fed2 558 virtual BOOL saveToDB(DB_HANDLE hdb);
c42b4551 559 virtual void deleteFromDatabase();
ba89fed2 560
e63fca40 561 virtual bool processNewValue(time_t nTimeStamp, const void *value, bool *updateStatus);
533ce8c0 562 virtual void processNewError();
ba89fed2 563
b368969c
VK
564 virtual void createMessage(NXCPMessage *pMsg);
565 virtual void updateFromMessage(NXCPMessage *pMsg);
b4369830 566
ba89fed2
VK
567 virtual void deleteExpiredData();
568 virtual bool deleteAllData();
b9a8e081 569
6e102d6e
VK
570 virtual void createNXMPRecord(String &str);
571
b368969c
VK
572 void fillLastValueMessage(NXCPMessage *msg);
573 void fillLastValueSummaryMessage(NXCPMessage *pMsg, UINT32 dwId);
92c51b1d 574
a567a02a 575 int getColumnDataType(const TCHAR *name);
db117859 576 ObjectArray<DCTableColumn> *getColumns() { return m_columns; }
a0ddfb29
VK
577 Table *getLastValue();
578
579 void mergeValues(Table *dest, Table *src, int count);
580
581 void updateResultColumns(Table *t);
92c51b1d 582
967893bb 583 static INT32 columnIdFromName(const TCHAR *name);
16d6f798
VK
584};
585
4a435beb
VK
586/**
587 * Functions
588 */
ced80327 589BOOL InitDataCollector();
967893bb 590void DeleteAllItemsForNode(UINT32 dwNodeId);
b368969c 591void WriteFullParamListToMessage(NXCPMessage *pMsg, WORD flags);
9fddfb91 592int GetDCObjectType(UINT32 nodeId, UINT32 dciId);
5039dede 593
85ae39bc
VK
594void CalculateItemValueDiff(ItemValue &result, int nDataType, ItemValue &value1, ItemValue &value2);
595void CalculateItemValueAverage(ItemValue &result, int nDataType, int nNumValues, ItemValue **ppValueList);
596void CalculateItemValueMD(ItemValue &result, int nDataType, int nNumValues, ItemValue **ppValueList);
597void CalculateItemValueTotal(ItemValue &result, int nDataType, int nNumValues, ItemValue **ppValueList);
598void CalculateItemValueMin(ItemValue &result, int nDataType, int nNumValues, ItemValue **ppValueList);
599void CalculateItemValueMax(ItemValue &result, int nDataType, int nNumValues, ItemValue **ppValueList);
5039dede 600
85ae39bc
VK
601/**
602 * Global variables
603 */
5039dede
AK
604extern double g_dAvgPollerQueueSize;
605extern double g_dAvgDBWriterQueueSize;
cf084617 606extern double g_dAvgIDataWriterQueueSize;
76fcb995 607extern double g_dAvgRawDataWriterQueueSize;
cf084617 608extern double g_dAvgDBAndIDataWriterQueueSize;
f1784ab6
VK
609extern double g_dAvgSyslogProcessingQueueSize;
610extern double g_dAvgSyslogWriterQueueSize;
967893bb 611extern UINT32 g_dwAvgDCIQueuingTime;
5039dede
AK
612
613
614#endif /* _nms_dcoll_h_ */