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