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