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