Added new status for collection error. Communication error does not reset status...
[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
215 void lock() { MutexLock(m_hMutex); }
216 void unlock() { MutexUnlock(m_hMutex); }
217
218 bool loadCustomSchedules(DB_HANDLE hdb);
219 bool matchSchedule(struct tm *pCurrTime, const TCHAR *pszSchedule, BOOL *bWithSeconds, time_t currTimestamp);
220
221 void expandMacros(const TCHAR *src, TCHAR *dst, size_t dstLen);
222
223 virtual bool isCacheLoaded();
224
225 // --- constructors ---
226 DCObject();
227 DCObject(const DCObject *src);
228 DCObject(UINT32 dwId, const TCHAR *szName, int iSource, int iPollingInterval, int iRetentionTime, Template *pNode,
229 const TCHAR *pszDescription = NULL, const TCHAR *systemTag = NULL);
230 DCObject(ConfigEntry *config, Template *owner);
231
232 public:
233 virtual ~DCObject();
234
235 virtual int getType() const { return DCO_TYPE_GENERIC; }
236
237 virtual void updateFromTemplate(DCObject *dcObject);
238 virtual void updateFromImport(ConfigEntry *config);
239
240 virtual bool saveToDatabase(DB_HANDLE hdb);
241 virtual void deleteFromDatabase();
242 virtual bool loadThresholdsFromDB(DB_HANDLE hdb);
243
244 virtual bool processNewValue(time_t nTimeStamp, const void *value, bool *updateStatus);
245 void processNewError(bool noInstance);
246 virtual void processNewError(bool noInstance, time_t now);
247
248 virtual bool hasValue();
249
250 UINT32 getId() const { return m_id; }
251 const uuid& getGuid() const { return m_guid; }
252 int getDataSource() const { return m_source; }
253 int getStatus() const { return m_status; }
254 const TCHAR *getName() const { return m_name; }
255 const TCHAR *getDescription() const { return m_description; }
256 const TCHAR *getSystemTag() const { return m_systemTag; }
257 const TCHAR *getPerfTabSettings() const { return m_pszPerfTabSettings; }
258 int getPollingInterval() const { return m_iPollingInterval; }
259 int getEffectivePollingInterval() const { return (m_iPollingInterval > 0) ? m_iPollingInterval : m_defaultPollingInterval; }
260 Template *getOwner() const { return m_owner; }
261 UINT32 getOwnerId() const;
262 const TCHAR *getOwnerName() const;
263 UINT32 getTemplateId() const { return m_dwTemplateId; }
264 UINT32 getTemplateItemId() const { return m_dwTemplateItemId; }
265 UINT32 getResourceId() const { return m_dwResourceId; }
266 UINT32 getSourceNode() const { return m_sourceNode; }
267 time_t getLastPollTime() const { return m_tLastPoll; }
268 UINT32 getErrorCount() const { return m_dwErrorCount; }
269 WORD getSnmpPort() const { return m_snmpPort; }
270 bool isShowOnObjectTooltip() const { return (m_flags & DCF_SHOW_ON_OBJECT_TOOLTIP) ? true : false; }
271 bool isShowInObjectOverview() const { return (m_flags & DCF_SHOW_IN_OBJECT_OVERVIEW) ? true : false; }
272 bool isAggregateOnCluster() const { return (m_flags & DCF_AGGREGATE_ON_CLUSTER) ? true : false; }
273 bool isStatusDCO() const { return (m_flags & DCF_CALCULATE_NODE_STATUS) ? true : false; }
274 bool isAggregateWithErrors() const { return (m_flags & DCF_AGGREGATE_WITH_ERRORS) ? true : false; }
275 int getAggregationFunction() const { return DCF_GET_AGGREGATION_FUNCTION(m_flags); }
276 int getRetentionTime() const { return m_iRetentionTime; }
277 int getEffectiveRetentionTime() const { return (m_iRetentionTime > 0) ? m_iRetentionTime : m_defaultRetentionTime; }
278 const TCHAR *getComments() const { return m_comments; }
279 INT16 getAgentCacheMode();
280
281 bool matchClusterResource();
282 bool isReadyForPolling(time_t currTime);
283 bool isScheduledForDeletion() { return m_scheduledForDeletion ? true : false; }
284 void setLastPollTime(time_t tLastPoll) { m_tLastPoll = tLastPoll; }
285 void setStatus(int status, bool generateEvent);
286 void setBusyFlag(BOOL busy) { m_busy = (BYTE)busy; }
287 void setTemplateId(UINT32 dwTemplateId, UINT32 dwItemId) { m_dwTemplateId = dwTemplateId; m_dwTemplateItemId = dwItemId; }
288
289 virtual void createMessage(NXCPMessage *pMsg);
290 virtual void updateFromMessage(NXCPMessage *pMsg);
291
292 virtual void changeBinding(UINT32 dwNewId, Template *newOwner, BOOL doMacroExpansion);
293
294 virtual void deleteExpiredData();
295 virtual bool deleteAllData();
296
297 virtual void getEventList(IntegerArray<UINT32> *eventList) = 0;
298 virtual void createExportRecord(String &str) = 0;
299
300 NXSL_Value *createNXSLObject();
301
302 void setName(const TCHAR *name) { nx_strncpy(m_name, name, MAX_ITEM_NAME); }
303 void setDescription(const TCHAR *description) { nx_strncpy(m_description, description, MAX_DB_STRING); }
304 void setOrigin(int origin) { m_source = origin; }
305 void setRetentionTime(int nTime) { m_iRetentionTime = nTime; }
306 void setInterval(int nInt) { m_iPollingInterval = nInt; }
307 void setAdvScheduleFlag(BOOL bFlag) { if (bFlag) m_flags |= DCF_ADVANCED_SCHEDULE; else m_flags &= ~DCF_ADVANCED_SCHEDULE; }
308 void addSchedule(const TCHAR *pszSchedule);
309 void setTransformationScript(const TCHAR *source);
310 ClientSession *processForcePoll();
311 void requestForcePoll(ClientSession *session);
312 ClientSession *getPollingSession() { return m_pollingSession; }
313 bool prepareForDeletion();
314
315 static int m_defaultRetentionTime;
316 static int m_defaultPollingInterval;
317 };
318
319 /**
320 * Data collection item class
321 */
322 class NXCORE_EXPORTABLE DCItem : public DCObject
323 {
324 protected:
325 TCHAR m_instance[MAX_DB_STRING];
326 BYTE m_deltaCalculation; // Delta calculation method
327 BYTE m_dataType;
328 int m_sampleCount; // Number of samples required to calculate value
329 ObjectArray<Threshold> *m_thresholds;
330 UINT32 m_cacheSize; // Number of items in cache
331 UINT32 m_requiredCacheSize;
332 ItemValue **m_ppValueCache;
333 ItemValue m_prevRawValue; // Previous raw value (used for delta calculation)
334 time_t m_tPrevValueTimeStamp;
335 bool m_bCacheLoaded;
336 int m_nBaseUnits;
337 int m_nMultiplier;
338 TCHAR *m_customUnitName;
339 WORD m_snmpRawValueType; // Actual SNMP raw value type for input transformation
340 WORD m_instanceDiscoveryMethod;
341 TCHAR *m_instanceDiscoveryData;
342 TCHAR *m_instanceFilterSource;
343 NXSL_Program *m_instanceFilter;
344 TCHAR m_predictionEngine[MAX_NPE_NAME_LEN];
345
346 bool transform(ItemValue &value, time_t nElapsedTime);
347 void checkThresholds(ItemValue &value);
348 void updateCacheSizeInternal(UINT32 conditionId = 0);
349 void clearCache();
350
351 virtual bool isCacheLoaded();
352
353 using DCObject::updateFromMessage;
354
355 public:
356 DCItem();
357 DCItem(const DCItem *pItem);
358 DCItem(DB_HANDLE hdb, DB_RESULT hResult, int iRow, Template *pNode);
359 DCItem(UINT32 dwId, const TCHAR *szName, int iSource, int iDataType,
360 int iPollingInterval, int iRetentionTime, Template *pNode,
361 const TCHAR *pszDescription = NULL, const TCHAR *systemTag = NULL);
362 DCItem(ConfigEntry *config, Template *owner);
363 virtual ~DCItem();
364
365 virtual int getType() const { return DCO_TYPE_ITEM; }
366
367 virtual void updateFromTemplate(DCObject *dcObject);
368 virtual void updateFromImport(ConfigEntry *config);
369
370 virtual bool saveToDatabase(DB_HANDLE hdb);
371 virtual void deleteFromDatabase();
372 virtual bool loadThresholdsFromDB(DB_HANDLE hdb);
373
374 void updateCacheSize(UINT32 conditionId = 0) { lock(); updateCacheSizeInternal(conditionId); unlock(); }
375 void reloadCache();
376
377 int getDataType() const { return m_dataType; }
378 bool isInterpretSnmpRawValue() const { return (m_flags & DCF_RAW_VALUE_OCTET_STRING) ? true : false; }
379 WORD getSnmpRawValueType() const { return m_snmpRawValueType; }
380 bool hasActiveThreshold();
381 int getThresholdSeverity();
382 WORD getInstanceDiscoveryMethod() const { return m_instanceDiscoveryMethod; }
383 const TCHAR *getInstanceDiscoveryData() const { return m_instanceDiscoveryData; }
384 const TCHAR *getInstance() const { return m_instance; }
385 int getSampleCount() const { return m_sampleCount; }
386 const TCHAR *getPredictionEngine() const { return m_predictionEngine; }
387
388 void filterInstanceList(StringMap *instances);
389 void expandInstance();
390
391 virtual bool processNewValue(time_t nTimeStamp, const void *value, bool *updateStatus);
392 virtual void processNewError(bool noInstance, time_t now);
393
394 virtual bool hasValue();
395
396 void fillLastValueMessage(NXCPMessage *pMsg, UINT32 dwId);
397 NXSL_Value *getValueForNXSL(int nFunction, int nPolls);
398 NXSL_Value *getRawValueForNXSL();
399 const TCHAR *getLastValue();
400 ItemValue *getInternalLastValue();
401 TCHAR *getAggregateValue(AggregationFunction func, time_t periodStart, time_t periodEnd);
402
403 virtual void createMessage(NXCPMessage *pMsg);
404 void updateFromMessage(NXCPMessage *pMsg, UINT32 *pdwNumMaps, UINT32 **ppdwMapIndex, UINT32 **ppdwMapId);
405 void fillMessageWithThresholds(NXCPMessage *msg);
406
407 virtual void changeBinding(UINT32 dwNewId, Template *pNode, BOOL doMacroExpansion);
408
409 virtual void deleteExpiredData();
410 virtual bool deleteAllData();
411
412 virtual void getEventList(IntegerArray<UINT32> *eventList);
413 virtual void createExportRecord(String &str);
414
415 int getThresholdCount() const { return (m_thresholds != NULL) ? m_thresholds->size() : 0; }
416 BOOL enumThresholds(BOOL (* pfCallback)(Threshold *, UINT32, void *), void *pArg);
417
418 void setInstance(const TCHAR *instance) { nx_strncpy(m_instance, instance, MAX_DB_STRING); }
419 void setDataType(int dataType) { m_dataType = dataType; }
420 void setDeltaCalcMethod(int method) { m_deltaCalculation = method; }
421 void setAllThresholdsFlag(BOOL bFlag) { if (bFlag) m_flags |= DCF_ALL_THRESHOLDS; else m_flags &= ~DCF_ALL_THRESHOLDS; }
422 void addThreshold(Threshold *pThreshold);
423 void deleteAllThresholds();
424 void setInstanceDiscoveryMethod(WORD method) { m_instanceDiscoveryMethod = method; }
425 void setInstanceDiscoveryData(const TCHAR *data) { safe_free(m_instanceDiscoveryData); m_instanceDiscoveryData = _tcsdup_ex(data); }
426 void setInstanceFilter(const TCHAR *pszScript);
427
428 static bool testTransformation(DataCollectionTarget *object, const TCHAR *script, const TCHAR *value, TCHAR *buffer, size_t bufSize);
429 };
430
431 /**
432 * Table column definition
433 */
434 class NXCORE_EXPORTABLE DCTableColumn
435 {
436 private:
437 TCHAR m_name[MAX_COLUMN_NAME];
438 TCHAR *m_displayName;
439 SNMP_ObjectId *m_snmpOid;
440 UINT16 m_flags;
441
442 public:
443 DCTableColumn(const DCTableColumn *src);
444 DCTableColumn(NXCPMessage *msg, UINT32 baseId);
445 DCTableColumn(DB_RESULT hResult, int row);
446 DCTableColumn(ConfigEntry *e);
447 ~DCTableColumn();
448
449 const TCHAR *getName() { return m_name; }
450 const TCHAR *getDisplayName() { return (m_displayName != NULL) ? m_displayName : m_name; }
451 UINT16 getFlags() { return m_flags; }
452 int getDataType() { return TCF_GET_DATA_TYPE(m_flags); }
453 int getAggregationFunction() { return TCF_GET_AGGREGATION_FUNCTION(m_flags); }
454 SNMP_ObjectId *getSnmpOid() { return m_snmpOid; }
455 bool isInstanceColumn() { return (m_flags & TCF_INSTANCE_COLUMN) != 0; }
456
457 void createNXMPRecord(String &str, int id);
458 };
459
460 /**
461 * Table column ID hash entry
462 */
463 struct TC_ID_MAP_ENTRY
464 {
465 INT32 id;
466 TCHAR name[MAX_COLUMN_NAME];
467 };
468
469 /**
470 * Condition for table DCI threshold
471 */
472 class NXCORE_EXPORTABLE DCTableCondition
473 {
474 private:
475 TCHAR *m_column;
476 int m_operation;
477 ItemValue m_value;
478
479 public:
480 DCTableCondition(const TCHAR *column, int operation, const TCHAR *value);
481 DCTableCondition(DCTableCondition *src);
482 ~DCTableCondition();
483
484 bool check(Table *value, int row);
485
486 const TCHAR *getColumn() { return m_column; }
487 int getOperation() { return m_operation; }
488 const TCHAR *getValue() { return m_value.getString(); }
489 };
490
491 /**
492 * Condition group for table DCI threshold
493 */
494 class NXCORE_EXPORTABLE DCTableConditionGroup
495 {
496 private:
497 ObjectArray<DCTableCondition> *m_conditions;
498
499 public:
500 DCTableConditionGroup();
501 DCTableConditionGroup(NXCPMessage *msg, UINT32 *baseId);
502 DCTableConditionGroup(DCTableConditionGroup *src);
503 DCTableConditionGroup(ConfigEntry *e);
504 ~DCTableConditionGroup();
505
506 bool check(Table *value, int row);
507
508 UINT32 fillMessage(NXCPMessage *msg, UINT32 baseId);
509
510 ObjectArray<DCTableCondition> *getConditions() { return m_conditions; }
511 };
512
513 /**
514 * Threshold definition for tabe DCI
515 */
516 class NXCORE_EXPORTABLE DCTableThreshold
517 {
518 private:
519 UINT32 m_id;
520 ObjectArray<DCTableConditionGroup> *m_groups;
521 UINT32 m_activationEvent;
522 UINT32 m_deactivationEvent;
523 StringSet *m_activeKeys;
524
525 void loadConditions(DB_HANDLE hdb);
526
527 public:
528 DCTableThreshold();
529 DCTableThreshold(DB_HANDLE hdb, DB_RESULT hResult, int row);
530 DCTableThreshold(NXCPMessage *msg, UINT32 *baseId);
531 DCTableThreshold(DCTableThreshold *src);
532 DCTableThreshold(ConfigEntry *e);
533 ~DCTableThreshold();
534
535 void copyState(DCTableThreshold *src);
536
537 ThresholdCheckResult check(Table *value, int row, const TCHAR *instance);
538
539 bool saveToDatabase(DB_HANDLE hdb, UINT32 tableId, int seq);
540 UINT32 fillMessage(NXCPMessage *msg, UINT32 baseId);
541 void createNXMPRecord(String &str, int id);
542
543 UINT32 getId() const { return m_id; }
544 UINT32 getActivationEvent() const { return m_activationEvent; }
545 UINT32 getDeactivationEvent() const { return m_deactivationEvent; }
546 };
547
548 /**
549 * Tabular data collection object
550 */
551 class NXCORE_EXPORTABLE DCTable : public DCObject
552 {
553 protected:
554 ObjectArray<DCTableColumn> *m_columns;
555 ObjectArray<DCTableThreshold> *m_thresholds;
556 Table *m_lastValue;
557
558 static TC_ID_MAP_ENTRY *m_cache;
559 static int m_cacheSize;
560 static int m_cacheAllocated;
561 static MUTEX m_cacheMutex;
562
563 bool transform(Table *value);
564 void checkThresholds(Table *value);
565
566 bool loadThresholds(DB_HANDLE hdb);
567 bool saveThresholds(DB_HANDLE hdb);
568
569 public:
570 DCTable();
571 DCTable(const DCTable *src);
572 DCTable(UINT32 id, const TCHAR *name, int source, int pollingInterval, int retentionTime,
573 Template *node, const TCHAR *description = NULL, const TCHAR *systemTag = NULL);
574 DCTable(DB_HANDLE hdb, DB_RESULT hResult, int iRow, Template *pNode);
575 DCTable(ConfigEntry *config, Template *owner);
576 virtual ~DCTable();
577
578 virtual int getType() const { return DCO_TYPE_TABLE; }
579
580 virtual void updateFromTemplate(DCObject *dcObject);
581 virtual void updateFromImport(ConfigEntry *config);
582
583 virtual bool saveToDatabase(DB_HANDLE hdb);
584 virtual void deleteFromDatabase();
585
586 virtual bool processNewValue(time_t nTimeStamp, const void *value, bool *updateStatus);
587 virtual void processNewError(bool noInstance, time_t now);
588
589 virtual bool hasValue();
590
591 virtual void createMessage(NXCPMessage *pMsg);
592 virtual void updateFromMessage(NXCPMessage *pMsg);
593
594 virtual void deleteExpiredData();
595 virtual bool deleteAllData();
596
597 virtual void getEventList(IntegerArray<UINT32> *eventList);
598 virtual void createExportRecord(String &str);
599
600 void fillLastValueMessage(NXCPMessage *msg);
601 void fillLastValueSummaryMessage(NXCPMessage *pMsg, UINT32 dwId);
602
603 int getColumnDataType(const TCHAR *name);
604 ObjectArray<DCTableColumn> *getColumns() { return m_columns; }
605 Table *getLastValue();
606
607 void mergeValues(Table *dest, Table *src, int count);
608
609 void updateResultColumns(Table *t);
610
611 static INT32 columnIdFromName(const TCHAR *name);
612 };
613
614 /**
615 * Data collection object information (for NXSL)
616 */
617 class DCObjectInfo
618 {
619 private:
620 UINT32 m_id;
621 int m_type;
622 TCHAR m_name[MAX_ITEM_NAME];
623 TCHAR m_description[MAX_DB_STRING];
624 TCHAR m_systemTag[MAX_DB_STRING];
625 TCHAR m_instance[MAX_DB_STRING];
626 TCHAR *m_comments;
627 int m_dataType;
628 int m_origin;
629 int m_status;
630 UINT32 m_errorCount;
631 time_t m_lastPollTime;
632
633 public:
634 DCObjectInfo(DCObject *object);
635 ~DCObjectInfo();
636
637 UINT32 getId() const { return m_id; }
638 int getType() const { return m_type; }
639 const TCHAR *getName() const { return m_name; }
640 const TCHAR *getDescription() const { return m_description; }
641 const TCHAR *getSystemTag() const { return m_systemTag; }
642 const TCHAR *getInstance() const { return m_instance; }
643 const TCHAR *getComments() const { return m_comments; }
644 int getDataType() const { return m_dataType; }
645 int getOrigin() const { return m_origin; }
646 int getStatus() const { return m_status; }
647 UINT32 getErrorCount() const { return m_errorCount; }
648 time_t getLastPollTime() const { return m_lastPollTime; }
649 };
650
651 /**
652 * Functions
653 */
654 BOOL InitDataCollector();
655 void DeleteAllItemsForNode(UINT32 dwNodeId);
656 void WriteFullParamListToMessage(NXCPMessage *pMsg, WORD flags);
657 int GetDCObjectType(UINT32 nodeId, UINT32 dciId);
658
659 void CalculateItemValueDiff(ItemValue &result, int nDataType, const ItemValue &value1, const ItemValue &value2);
660 void CalculateItemValueAverage(ItemValue &result, int nDataType, int nNumValues, ItemValue **ppValueList);
661 void CalculateItemValueMD(ItemValue &result, int nDataType, int nNumValues, ItemValue **ppValueList);
662 void CalculateItemValueTotal(ItemValue &result, int nDataType, int nNumValues, ItemValue **ppValueList);
663 void CalculateItemValueMin(ItemValue &result, int nDataType, int nNumValues, ItemValue **ppValueList);
664 void CalculateItemValueMax(ItemValue &result, int nDataType, int nNumValues, ItemValue **ppValueList);
665
666 /**
667 * Global variables
668 */
669 extern double g_dAvgPollerQueueSize;
670 extern double g_dAvgDBWriterQueueSize;
671 extern double g_dAvgIDataWriterQueueSize;
672 extern double g_dAvgRawDataWriterQueueSize;
673 extern double g_dAvgDBAndIDataWriterQueueSize;
674 extern double g_dAvgSyslogProcessingQueueSize;
675 extern double g_dAvgSyslogWriterQueueSize;
676 extern UINT32 g_dwAvgDCIQueuingTime;
677
678
679 #endif /* _nms_dcoll_h_ */