Fixed force poll on DCI with custom schedule Fixes #1293
[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
37 /**
38 * Threshold check results
39 */
40 enum ThresholdCheckResult
41 {
42 ACTIVATED = 0,
43 DEACTIVATED = 1,
44 ALREADY_ACTIVE = 2,
45 ALREADY_INACTIVE = 3
46 };
47
48 /**
49 * DCI value
50 */
51 class NXCORE_EXPORTABLE ItemValue
52 {
53 private:
54 double m_dFloat;
55 INT32 m_iInt32;
56 INT64 m_iInt64;
57 UINT32 m_dwInt32;
58 UINT64 m_qwInt64;
59 TCHAR m_szString[MAX_DB_STRING];
60 UINT32 m_dwTimeStamp;
61
62 public:
63 ItemValue();
64 ItemValue(const TCHAR *pszValue, UINT32 dwTimeStamp);
65 ItemValue(const ItemValue *pValue);
66 ~ItemValue();
67
68 void setTimeStamp(UINT32 dwTime) { m_dwTimeStamp = dwTime; }
69 UINT32 getTimeStamp() { return m_dwTimeStamp; }
70
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; }
76 const TCHAR *getString() { return m_szString; }
77
78 operator double() { return m_dFloat; }
79 operator UINT32() { return m_dwInt32; }
80 operator UINT64() { return m_qwInt64; }
81 operator INT32() { return m_iInt32; }
82 operator INT64() { return m_iInt64; }
83 operator const TCHAR*() const { return m_szString; }
84
85 const ItemValue& operator=(const ItemValue &src);
86 const ItemValue& operator=(const TCHAR *pszStr);
87 const ItemValue& operator=(double dFloat);
88 const ItemValue& operator=(INT32 iInt32);
89 const ItemValue& operator=(INT64 iInt64);
90 const ItemValue& operator=(UINT32 dwInt32);
91 const ItemValue& operator=(UINT64 qwInt64);
92 };
93
94
95 class DCItem;
96 class DataCollectionTarget;
97
98 /**
99 * Threshold definition class
100 */
101 class NXCORE_EXPORTABLE Threshold
102 {
103 private:
104 UINT32 m_id; // Unique threshold id
105 UINT32 m_itemId; // Parent item id
106 UINT32 m_targetId; // Parent data collection target ID
107 UINT32 m_eventCode; // Event code to be generated
108 UINT32 m_rearmEventCode;
109 ItemValue m_value;
110 BYTE m_function; // Function code
111 BYTE m_operation; // Comparision operation code
112 BYTE m_dataType; // Related item data type
113 BYTE m_currentSeverity; // Current everity (NORMAL if threshold is inactive)
114 int m_sampleCount; // Number of samples to calculate function on
115 TCHAR *m_scriptSource;
116 NXSL_Program *m_script;
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
122 const ItemValue& value() { return m_value; }
123 void calculateAverageValue(ItemValue *pResult, ItemValue &lastValue, ItemValue **ppPrevValues);
124 void calculateSumValue(ItemValue *pResult, ItemValue &lastValue, ItemValue **ppPrevValues);
125 void calculateMDValue(ItemValue *pResult, ItemValue &lastValue, ItemValue **ppPrevValues);
126 void calculateDiff(ItemValue *pResult, ItemValue &lastValue, ItemValue **ppPrevValues);
127 void setScript(TCHAR *script);
128
129 public:
130 Threshold();
131 Threshold(DCItem *pRelatedItem);
132 Threshold(Threshold *pSrc);
133 Threshold(DB_RESULT hResult, int iRow, DCItem *pRelatedItem);
134 Threshold(ConfigEntry *config, DCItem *parentItem);
135 ~Threshold();
136
137 void bindToItem(UINT32 itemId, UINT32 targetId) { m_itemId = itemId; m_targetId = targetId; }
138
139 UINT32 getId() { return m_id; }
140 UINT32 getEventCode() { return m_eventCode; }
141 UINT32 getRearmEventCode() { return m_rearmEventCode; }
142 int getFunction() { return m_function; }
143 int getOperation() { return m_operation; }
144 int getSampleCount() { return m_sampleCount; }
145 const TCHAR *getStringValue() { return m_value.getString(); }
146 BOOL isReached() { return m_isReached; }
147
148 int getRepeatInterval() { return m_repeatInterval; }
149 time_t getLastEventTimestamp() { return m_lastEventTimestamp; }
150 int getCurrentSeverity() { return m_currentSeverity; }
151 void markLastEvent(int severity);
152
153 BOOL saveToDB(DB_HANDLE hdb, UINT32 dwIndex);
154 ThresholdCheckResult check(ItemValue &value, ItemValue **ppPrevValues, ItemValue &fvalue, NetObj *target, DCItem *dci);
155 ThresholdCheckResult checkError(UINT32 dwErrorCount);
156
157 void createMessage(NXCPMessage *msg, UINT32 baseId);
158 void updateFromMessage(NXCPMessage *msg, UINT32 baseId);
159
160 void createId();
161 UINT32 getRequiredCacheSize() { return ((m_function == F_LAST) || (m_function == F_ERROR)) ? 0 : m_sampleCount; }
162
163 BOOL compare(Threshold *pThr);
164
165 void createNXMPRecord(String &str, int index);
166
167 void associate(DCItem *pItem);
168 void setFunction(int nFunc) { m_function = nFunc; }
169 void setOperation(int nOp) { m_operation = nOp; }
170 void setEvent(UINT32 dwEvent) { m_eventCode = dwEvent; }
171 void setRearmEvent(UINT32 dwEvent) { m_rearmEventCode = dwEvent; }
172 void setSampleCount(int nVal) { m_sampleCount = nVal; }
173 void setValue(const TCHAR *value) { m_value = value; }
174 void setDataType(BYTE type) { m_dataType = type; }
175 };
176
177 class Template;
178
179 /**
180 * Generic data collection object
181 */
182 class NXCORE_EXPORTABLE DCObject
183 {
184 protected:
185 UINT32 m_id;
186 uuid m_guid;
187 TCHAR m_name[MAX_ITEM_NAME];
188 TCHAR m_description[MAX_DB_STRING];
189 TCHAR m_systemTag[MAX_DB_STRING];
190 time_t m_tLastPoll; // Last poll time
191 int m_iPollingInterval; // Polling interval in seconds
192 int m_iRetentionTime; // Retention time in days
193 BYTE m_source; // origin: SNMP, agent, etc.
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
197 UINT16 m_flags;
198 UINT32 m_dwTemplateId; // Related template's id
199 UINT32 m_dwTemplateItemId; // Related template item's id
200 Template *m_owner; // Pointer to node or template object this item related to
201 MUTEX m_hMutex;
202 StringList *m_schedules;
203 time_t m_tLastCheck; // Last schedule checking time
204 UINT32 m_dwErrorCount; // Consequtive collection error count
205 UINT32 m_dwResourceId; // Associated cluster resource ID
206 UINT32 m_sourceNode; // Source node ID or 0 to disable
207 WORD m_snmpPort; // Custom SNMP port or 0 for node default
208 TCHAR *m_pszPerfTabSettings;
209 TCHAR *m_transformationScriptSource; // Transformation script (source code)
210 NXSL_Program *m_transformationScript; // Compiled transformation script
211 TCHAR *m_comments;
212 bool m_forcePoll;
213
214 void lock() { MutexLock(m_hMutex); }
215 void unlock() { MutexUnlock(m_hMutex); }
216
217 bool loadCustomSchedules(DB_HANDLE hdb);
218 bool matchSchedule(struct tm *pCurrTime, const TCHAR *pszSchedule, BOOL *bWithSeconds, time_t currTimestamp);
219
220 void expandMacros(const TCHAR *src, TCHAR *dst, size_t dstLen);
221
222 virtual bool isCacheLoaded();
223
224 // --- constructors ---
225 DCObject();
226 DCObject(const DCObject *src);
227 DCObject(UINT32 dwId, const TCHAR *szName, int iSource, int iPollingInterval, int iRetentionTime, Template *pNode,
228 const TCHAR *pszDescription = NULL, const TCHAR *systemTag = NULL);
229 DCObject(ConfigEntry *config, Template *owner);
230
231 public:
232 virtual ~DCObject();
233
234 virtual int getType() const { return DCO_TYPE_GENERIC; }
235
236 virtual void updateFromTemplate(DCObject *dcObject);
237 virtual void updateFromImport(ConfigEntry *config);
238
239 virtual bool saveToDatabase(DB_HANDLE hdb);
240 virtual void deleteFromDatabase();
241 virtual bool loadThresholdsFromDB(DB_HANDLE hdb);
242
243 virtual bool processNewValue(time_t nTimeStamp, const void *value, bool *updateStatus);
244 void processNewError(bool noInstance);
245 virtual void processNewError(bool noInstance, time_t now);
246
247 virtual bool hasValue();
248
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; }
273 bool isAggregateWithErrors() const { return (m_flags & DCF_AGGREGATE_WITH_ERRORS) ? true : false; }
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; }
278 INT16 getAgentCacheMode();
279
280 bool matchClusterResource();
281 bool isReadyForPolling(time_t currTime);
282 bool isScheduledForDeletion() { return m_scheduledForDeletion ? true : false; }
283 void setLastPollTime(time_t tLastPoll) { m_tLastPoll = tLastPoll; }
284 void setStatus(int status, bool generateEvent);
285 void setBusyFlag(BOOL busy) { m_busy = (BYTE)busy; }
286 void setTemplateId(UINT32 dwTemplateId, UINT32 dwItemId) { m_dwTemplateId = dwTemplateId; m_dwTemplateItemId = dwItemId; }
287
288 virtual void createMessage(NXCPMessage *pMsg);
289 virtual void updateFromMessage(NXCPMessage *pMsg);
290
291 virtual void changeBinding(UINT32 dwNewId, Template *newOwner, BOOL doMacroExpansion);
292
293 virtual void deleteExpiredData();
294 virtual bool deleteAllData();
295
296 virtual void getEventList(UINT32 **ppdwList, UINT32 *pdwSize);
297 virtual void createExportRecord(String &str);
298
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); }
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);
308 void setTransformationScript(const TCHAR *source);
309 void setForcePoll(bool poll) { m_forcePoll = poll; }
310
311 bool prepareForDeletion();
312
313 static int m_defaultRetentionTime;
314 static int m_defaultPollingInterval;
315 };
316
317 /**
318 * Data collection item class
319 */
320 class NXCORE_EXPORTABLE DCItem : public DCObject
321 {
322 protected:
323 TCHAR m_instance[MAX_DB_STRING];
324 BYTE m_deltaCalculation; // Delta calculation method
325 BYTE m_dataType;
326 int m_sampleCount; // Number of samples required to calculate value
327 ObjectArray<Threshold> *m_thresholds;
328 UINT32 m_cacheSize; // Number of items in cache
329 UINT32 m_requiredCacheSize;
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;
336 TCHAR *m_customUnitName;
337 WORD m_snmpRawValueType; // Actual SNMP raw value type for input transformation
338 WORD m_instanceDiscoveryMethod;
339 TCHAR *m_instanceDiscoveryData;
340 TCHAR *m_instanceFilterSource;
341 NXSL_Program *m_instanceFilter;
342
343 bool transform(ItemValue &value, time_t nElapsedTime);
344 void checkThresholds(ItemValue &value);
345 void updateCacheSizeInternal(UINT32 conditionId = 0);
346 void clearCache();
347
348 virtual bool isCacheLoaded();
349
350 using DCObject::updateFromMessage;
351
352 public:
353 DCItem();
354 DCItem(const DCItem *pItem);
355 DCItem(DB_HANDLE hdb, DB_RESULT hResult, int iRow, Template *pNode);
356 DCItem(UINT32 dwId, const TCHAR *szName, int iSource, int iDataType,
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
362 virtual int getType() const { return DCO_TYPE_ITEM; }
363
364 virtual void updateFromTemplate(DCObject *dcObject);
365 virtual void updateFromImport(ConfigEntry *config);
366
367 virtual bool saveToDatabase(DB_HANDLE hdb);
368 virtual void deleteFromDatabase();
369 virtual bool loadThresholdsFromDB(DB_HANDLE hdb);
370
371 void updateCacheSize(UINT32 conditionId = 0) { lock(); updateCacheSizeInternal(conditionId); unlock(); }
372 void reloadCache();
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; }
377 bool hasActiveThreshold();
378 int getThresholdSeverity();
379 WORD getInstanceDiscoveryMethod() { return m_instanceDiscoveryMethod; }
380 const TCHAR *getInstanceDiscoveryData() { return m_instanceDiscoveryData; }
381 const TCHAR *getInstance() { return m_instance; }
382 int getSampleCount() { return m_sampleCount; }
383
384 void filterInstanceList(StringMap *instances);
385 void expandInstance();
386
387 virtual bool processNewValue(time_t nTimeStamp, const void *value, bool *updateStatus);
388 virtual void processNewError(bool noInstance, time_t now);
389
390 virtual bool hasValue();
391
392 void fillLastValueMessage(NXCPMessage *pMsg, UINT32 dwId);
393 NXSL_Value *getValueForNXSL(int nFunction, int nPolls);
394 NXSL_Value *getRawValueForNXSL();
395 const TCHAR *getLastValue();
396 ItemValue *getInternalLastValue();
397 TCHAR *getAggregateValue(AggregationFunction func, time_t periodStart, time_t periodEnd);
398
399 virtual void createMessage(NXCPMessage *pMsg);
400 void updateFromMessage(NXCPMessage *pMsg, UINT32 *pdwNumMaps, UINT32 **ppdwMapIndex, UINT32 **ppdwMapId);
401 void fillMessageWithThresholds(NXCPMessage *msg);
402
403 virtual void changeBinding(UINT32 dwNewId, Template *pNode, BOOL doMacroExpansion);
404
405 virtual void deleteExpiredData();
406 virtual bool deleteAllData();
407
408 virtual void getEventList(UINT32 **ppdwList, UINT32 *pdwSize);
409 virtual void createExportRecord(String &str);
410
411 int getThresholdCount() const { return (m_thresholds != NULL) ? m_thresholds->size() : 0; }
412 BOOL enumThresholds(BOOL (* pfCallback)(Threshold *, UINT32, void *), void *pArg);
413
414 void setInstance(const TCHAR *instance) { nx_strncpy(m_instance, instance, MAX_DB_STRING); }
415 void setDataType(int dataType) { m_dataType = dataType; }
416 void setDeltaCalcMethod(int method) { m_deltaCalculation = method; }
417 void setAllThresholdsFlag(BOOL bFlag) { if (bFlag) m_flags |= DCF_ALL_THRESHOLDS; else m_flags &= ~DCF_ALL_THRESHOLDS; }
418 void addThreshold(Threshold *pThreshold);
419 void deleteAllThresholds();
420 void setInstanceDiscoveryMethod(WORD method) { m_instanceDiscoveryMethod = method; }
421 void setInstanceDiscoveryData(const TCHAR *data) { safe_free(m_instanceDiscoveryData); m_instanceDiscoveryData = _tcsdup_ex(data); }
422 void setInstanceFilter(const TCHAR *pszScript);
423
424 static bool testTransformation(DataCollectionTarget *object, const TCHAR *script, const TCHAR *value, TCHAR *buffer, size_t bufSize);
425 };
426
427 /**
428 * Table column definition
429 */
430 class NXCORE_EXPORTABLE DCTableColumn
431 {
432 private:
433 TCHAR m_name[MAX_COLUMN_NAME];
434 TCHAR *m_displayName;
435 SNMP_ObjectId *m_snmpOid;
436 UINT16 m_flags;
437
438 public:
439 DCTableColumn(const DCTableColumn *src);
440 DCTableColumn(NXCPMessage *msg, UINT32 baseId);
441 DCTableColumn(DB_RESULT hResult, int row);
442 DCTableColumn(ConfigEntry *e);
443 ~DCTableColumn();
444
445 const TCHAR *getName() { return m_name; }
446 const TCHAR *getDisplayName() { return (m_displayName != NULL) ? m_displayName : m_name; }
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); }
450 SNMP_ObjectId *getSnmpOid() { return m_snmpOid; }
451 bool isInstanceColumn() { return (m_flags & TCF_INSTANCE_COLUMN) != 0; }
452
453 void createNXMPRecord(String &str, int id);
454 };
455
456 /**
457 * Table column ID hash entry
458 */
459 struct TC_ID_MAP_ENTRY
460 {
461 INT32 id;
462 TCHAR name[MAX_COLUMN_NAME];
463 };
464
465 /**
466 * Condition for table DCI threshold
467 */
468 class NXCORE_EXPORTABLE DCTableCondition
469 {
470 private:
471 TCHAR *m_column;
472 int m_operation;
473 ItemValue m_value;
474
475 public:
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 */
490 class NXCORE_EXPORTABLE DCTableConditionGroup
491 {
492 private:
493 ObjectArray<DCTableCondition> *m_conditions;
494
495 public:
496 DCTableConditionGroup();
497 DCTableConditionGroup(NXCPMessage *msg, UINT32 *baseId);
498 DCTableConditionGroup(DCTableConditionGroup *src);
499 DCTableConditionGroup(ConfigEntry *e);
500 ~DCTableConditionGroup();
501
502 bool check(Table *value, int row);
503
504 UINT32 fillMessage(NXCPMessage *msg, UINT32 baseId);
505
506 ObjectArray<DCTableCondition> *getConditions() { return m_conditions; }
507 };
508
509 /**
510 * Threshold definition for tabe DCI
511 */
512 class NXCORE_EXPORTABLE DCTableThreshold
513 {
514 private:
515 UINT32 m_id;
516 ObjectArray<DCTableConditionGroup> *m_groups;
517 UINT32 m_activationEvent;
518 UINT32 m_deactivationEvent;
519 StringSet *m_activeKeys;
520
521 void loadConditions(DB_HANDLE hdb);
522
523 public:
524 DCTableThreshold();
525 DCTableThreshold(DB_HANDLE hdb, DB_RESULT hResult, int row);
526 DCTableThreshold(NXCPMessage *msg, UINT32 *baseId);
527 DCTableThreshold(DCTableThreshold *src);
528 DCTableThreshold(ConfigEntry *e);
529 ~DCTableThreshold();
530
531 void copyState(DCTableThreshold *src);
532
533 ThresholdCheckResult check(Table *value, int row, const TCHAR *instance);
534
535 bool saveToDatabase(DB_HANDLE hdb, UINT32 tableId, int seq);
536 UINT32 fillMessage(NXCPMessage *msg, UINT32 baseId);
537 void createNXMPRecord(String &str, int id);
538
539 UINT32 getId() { return m_id; }
540 UINT32 getActivationEvent() { return m_activationEvent; }
541 UINT32 getDeactivationEvent() { return m_deactivationEvent; }
542 };
543
544 /**
545 * Tabular data collection object
546 */
547 class NXCORE_EXPORTABLE DCTable : public DCObject
548 {
549 protected:
550 ObjectArray<DCTableColumn> *m_columns;
551 ObjectArray<DCTableThreshold> *m_thresholds;
552 Table *m_lastValue;
553
554 static TC_ID_MAP_ENTRY *m_cache;
555 static int m_cacheSize;
556 static int m_cacheAllocated;
557 static MUTEX m_cacheMutex;
558
559 bool transform(Table *value);
560 void checkThresholds(Table *value);
561
562 bool loadThresholds(DB_HANDLE hdb);
563 bool saveThresholds(DB_HANDLE hdb);
564
565 public:
566 DCTable();
567 DCTable(const DCTable *src);
568 DCTable(UINT32 id, const TCHAR *name, int source, int pollingInterval, int retentionTime,
569 Template *node, const TCHAR *description = NULL, const TCHAR *systemTag = NULL);
570 DCTable(DB_HANDLE hdb, DB_RESULT hResult, int iRow, Template *pNode);
571 DCTable(ConfigEntry *config, Template *owner);
572 virtual ~DCTable();
573
574 virtual int getType() const { return DCO_TYPE_TABLE; }
575
576 virtual void updateFromTemplate(DCObject *dcObject);
577 virtual void updateFromImport(ConfigEntry *config);
578
579 virtual bool saveToDatabase(DB_HANDLE hdb);
580 virtual void deleteFromDatabase();
581
582 virtual bool processNewValue(time_t nTimeStamp, const void *value, bool *updateStatus);
583 virtual void processNewError(bool noInstance, time_t now);
584
585 virtual bool hasValue();
586
587 virtual void createMessage(NXCPMessage *pMsg);
588 virtual void updateFromMessage(NXCPMessage *pMsg);
589
590 virtual void deleteExpiredData();
591 virtual bool deleteAllData();
592
593 virtual void createExportRecord(String &str);
594
595 void fillLastValueMessage(NXCPMessage *msg);
596 void fillLastValueSummaryMessage(NXCPMessage *pMsg, UINT32 dwId);
597
598 int getColumnDataType(const TCHAR *name);
599 ObjectArray<DCTableColumn> *getColumns() { return m_columns; }
600 Table *getLastValue();
601
602 void mergeValues(Table *dest, Table *src, int count);
603
604 void updateResultColumns(Table *t);
605
606 static INT32 columnIdFromName(const TCHAR *name);
607 };
608
609 /**
610 * Data collection object information (for NXSL)
611 */
612 class DCObjectInfo
613 {
614 private:
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
628 public:
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
646 /**
647 * Functions
648 */
649 BOOL InitDataCollector();
650 void DeleteAllItemsForNode(UINT32 dwNodeId);
651 void WriteFullParamListToMessage(NXCPMessage *pMsg, WORD flags);
652 int GetDCObjectType(UINT32 nodeId, UINT32 dciId);
653
654 void CalculateItemValueDiff(ItemValue &result, int nDataType, ItemValue &value1, ItemValue &value2);
655 void CalculateItemValueAverage(ItemValue &result, int nDataType, int nNumValues, ItemValue **ppValueList);
656 void CalculateItemValueMD(ItemValue &result, int nDataType, int nNumValues, ItemValue **ppValueList);
657 void CalculateItemValueTotal(ItemValue &result, int nDataType, int nNumValues, ItemValue **ppValueList);
658 void CalculateItemValueMin(ItemValue &result, int nDataType, int nNumValues, ItemValue **ppValueList);
659 void CalculateItemValueMax(ItemValue &result, int nDataType, int nNumValues, ItemValue **ppValueList);
660
661 /**
662 * Global variables
663 */
664 extern double g_dAvgPollerQueueSize;
665 extern double g_dAvgDBWriterQueueSize;
666 extern double g_dAvgIDataWriterQueueSize;
667 extern double g_dAvgRawDataWriterQueueSize;
668 extern double g_dAvgDBAndIDataWriterQueueSize;
669 extern double g_dAvgSyslogProcessingQueueSize;
670 extern double g_dAvgSyslogWriterQueueSize;
671 extern UINT32 g_dwAvgDCIQueuingTime;
672
673
674 #endif /* _nms_dcoll_h_ */