]> arthur.barton.de Git - netdata.git/blob - src/health.h
f009e4ab190aebf85e9ed4454263ccfadb40ae0f
[netdata.git] / src / health.h
1 #ifndef NETDATA_HEALTH_H
2 #define NETDATA_HEALTH_H
3
4 extern int health_enabled;
5
6 extern int rrdvar_compare(void *a, void *b);
7
8 #define RRDVAR_TYPE_CALCULATED              1
9 #define RRDVAR_TYPE_TIME_T                  2
10 #define RRDVAR_TYPE_COLLECTED               3
11 #define RRDVAR_TYPE_TOTAL                   4
12 #define RRDVAR_TYPE_INT                     5
13 #define RRDVAR_TYPE_CALCULATED_ALLOCATED    6
14
15
16 // the variables as stored in the variables indexes
17 // there are 3 indexes:
18 // 1. at each chart   (RRDSET.variables_root_index)
19 // 2. at each context (RRDFAMILY.variables_root_index)
20 // 3. at each host    (RRDHOST.variables_root_index)
21 typedef struct rrdvar {
22     avl avl;
23
24     char *name;
25     uint32_t hash;
26
27     int type;
28     void *value;
29
30     time_t last_updated;
31 } RRDVAR;
32
33 // variables linked to charts
34 // We link variables to point to the values that are already
35 // calculated / processed by the normal data collection process
36 // This means, there will be no speed penalty for using
37 // these variables
38 typedef struct rrdsetvar {
39     char *key_fullid;               // chart type.chart id.variable
40     char *key_fullname;             // chart type.chart name.variable
41     char *variable;             // variable
42
43     int type;
44     void *value;
45
46     uint32_t options;
47
48     RRDVAR *var_local;
49     RRDVAR *var_family;
50     RRDVAR *var_host;
51     RRDVAR *var_family_name;
52     RRDVAR *var_host_name;
53
54     struct rrdset *rrdset;
55
56     struct rrdsetvar *next;
57 } RRDSETVAR;
58
59
60 // variables linked to individual dimensions
61 // We link variables to point the values that are already
62 // calculated / processed by the normal data collection process
63 // This means, there will be no speed penalty for using
64 // these variables
65 typedef struct rrddimvar {
66     char *prefix;
67     char *suffix;
68
69     char *key_id;                   // dimension id
70     char *key_name;                 // dimension name
71     char *key_contextid;            // context + dimension id
72     char *key_contextname;          // context + dimension name
73     char *key_fullidid;             // chart type.chart id + dimension id
74     char *key_fullidname;           // chart type.chart id + dimension name
75     char *key_fullnameid;           // chart type.chart name + dimension id
76     char *key_fullnamename;         // chart type.chart name + dimension name
77
78     int type;
79     void *value;
80
81     uint32_t options;
82
83     RRDVAR *var_local_id;
84     RRDVAR *var_local_name;
85
86     RRDVAR *var_family_id;
87     RRDVAR *var_family_name;
88     RRDVAR *var_family_contextid;
89     RRDVAR *var_family_contextname;
90
91     RRDVAR *var_host_chartidid;
92     RRDVAR *var_host_chartidname;
93     RRDVAR *var_host_chartnameid;
94     RRDVAR *var_host_chartnamename;
95
96     struct rrddim *rrddim;
97
98     struct rrddimvar *next;
99 } RRDDIMVAR;
100
101 // calculated variables (defined in health configuration)
102 // These aggregate time-series data at fixed intervals
103 // (defined in their update_every member below)
104 // These increase the overhead of netdata.
105 //
106 // These calculations are allocated and linked (->next)
107 // under RRDHOST.
108 // Then are also linked to RRDSET (of course only when the
109 // chart is found, via ->rrdset_next and ->rrdset_prev).
110 // This double-linked list is maintained sorted at all times
111 // having as RRDSET.calculations the RRDCALC to be processed
112 // next.
113
114 #define RRDCALC_STATUS_REMOVED       -2
115 #define RRDCALC_STATUS_UNDEFINED     -1
116 #define RRDCALC_STATUS_UNINITIALIZED  0
117 #define RRDCALC_STATUS_CLEAR          1
118 #define RRDCALC_STATUS_RAISED         2
119 #define RRDCALC_STATUS_WARNING        3
120 #define RRDCALC_STATUS_CRITICAL       4
121
122 #define RRDCALC_FLAG_DB_ERROR      0x00000001
123 #define RRDCALC_FLAG_DB_NAN        0x00000002
124 /* #define RRDCALC_FLAG_DB_STALE      0x00000004 */
125 #define RRDCALC_FLAG_CALC_ERROR    0x00000008
126 #define RRDCALC_FLAG_WARN_ERROR    0x00000010
127 #define RRDCALC_FLAG_CRIT_ERROR    0x00000020
128 #define RRDCALC_FLAG_RUNNABLE      0x00000040
129
130 typedef struct rrdcalc {
131     uint32_t id;                    // the unique id of this alarm
132     uint32_t next_event_id;         // the next event id that will be used for this alarm
133
134     char *name;                     // the name of this alarm
135     uint32_t hash;      
136
137     char *exec;                     // the command to execute when this alarm switches state
138     char *recipient;                // the recipient of the alarm (the first parameter to exec)
139
140     char *chart;                    // the chart id this should be linked to
141     uint32_t hash_chart;
142
143     char *source;                   // the source of this alarm
144     char *units;                    // the units of the alarm
145     char *info;                     // a short description of the alarm
146
147     int update_every;               // update frequency for the alarm
148
149     // the red and green threshold of this alarm (to be set to the chart)
150     calculated_number green;
151     calculated_number red;
152
153     // ------------------------------------------------------------------------
154     // database lookup settings
155
156     char *dimensions;               // the chart dimensions
157     int group;                      // grouping method: average, max, etc.
158     int before;                     // ending point in time-series
159     int after;                      // starting point in time-series
160     uint32_t options;               // calculation options
161
162     // ------------------------------------------------------------------------
163     // expressions related to the alarm
164
165     EVAL_EXPRESSION *calculation;   // expression to calculate the value of the alarm
166     EVAL_EXPRESSION *warning;       // expression to check the warning condition
167     EVAL_EXPRESSION *critical;      // expression to check the critical condition
168
169     // ------------------------------------------------------------------------
170     // notification delay settings
171
172     int delay_up_duration;         // duration to delay notifications when alarm raises
173     int delay_down_duration;       // duration to delay notifications when alarm lowers
174     int delay_max_duration;        // the absolute max delay to apply to this alarm
175     float delay_multiplier;        // multiplier for all delays when alarms switch status
176                                    // while now < delay_up_to
177
178     // ------------------------------------------------------------------------
179     // runtime information
180
181     int status;                     // the current status of the alarm
182
183     calculated_number value;        // the current value of the alarm
184     calculated_number old_value;    // the previous value of the alarm
185
186     uint32_t rrdcalc_flags;         // check RRDCALC_FLAG_*
187
188     time_t last_updated;            // the last update timestamp of the alarm
189     time_t next_update;             // the next update timestamp of the alarm
190     time_t last_status_change;      // the timestamp of the last time this alarm changed status
191
192     time_t db_after;                // the first timestamp evaluated by the db lookup
193     time_t db_before;               // the last timestamp evaluated by the db lookup
194
195     time_t delay_up_to_timestamp;   // the timestamp up to which we should delay notifications
196     int delay_up_current;           // the current up notification delay duration
197     int delay_down_current;         // the current down notification delay duration
198     int delay_last;                 // the last delay we used
199
200     // ------------------------------------------------------------------------
201     // variables this alarm exposes to the rest of the alarms
202
203     RRDVAR *local;
204     RRDVAR *family;
205     RRDVAR *hostid;
206     RRDVAR *hostname;
207
208     // ------------------------------------------------------------------------
209     // the chart this alarm it is linked to
210
211     struct rrdset *rrdset;
212
213     // linking of this alarm on its chart
214     struct rrdcalc *rrdset_next;
215     struct rrdcalc *rrdset_prev;
216
217     struct rrdcalc *next;
218 } RRDCALC;
219
220 #define RRDCALC_HAS_DB_LOOKUP(rc) ((rc)->after)
221
222 // RRDCALCTEMPLATE
223 // these are to be applied to charts found dynamically
224 // based on their context.
225 typedef struct rrdcalctemplate {
226     char *name;
227     uint32_t hash_name;
228
229     char *exec;
230     char *recipient;
231
232     char *context;
233     uint32_t hash_context;
234
235     char *family_match;
236     SIMPLE_PATTERN *family_pattern;
237
238     char *source;                   // the source of this alarm
239     char *units;                    // the units of the alarm
240     char *info;                     // a short description of the alarm
241
242     int update_every;               // update frequency for the alarm
243
244     // the red and green threshold of this alarm (to be set to the chart)
245     calculated_number green;
246     calculated_number red;
247
248     // ------------------------------------------------------------------------
249     // database lookup settings
250
251     char *dimensions;               // the chart dimensions
252     int group;                      // grouping method: average, max, etc.
253     int before;                     // ending point in time-series
254     int after;                      // starting point in time-series
255     uint32_t options;               // calculation options
256
257     // ------------------------------------------------------------------------
258     // notification delay settings
259
260     int delay_up_duration;         // duration to delay notifications when alarm raises
261     int delay_down_duration;       // duration to delay notifications when alarm lowers
262     int delay_max_duration;        // the absolute max delay to apply to this alarm
263     float delay_multiplier;        // multiplier for all delays when alarms switch status
264
265     // ------------------------------------------------------------------------
266     // expressions related to the alarm
267
268     EVAL_EXPRESSION *calculation;
269     EVAL_EXPRESSION *warning;
270     EVAL_EXPRESSION *critical;
271
272     struct rrdcalctemplate *next;
273 } RRDCALCTEMPLATE;
274
275 #define RRDCALCTEMPLATE_HAS_CALCULATION(rt) ((rt)->after)
276
277 #define HEALTH_ENTRY_FLAG_PROCESSED    0x00000001
278 #define HEALTH_ENTRY_FLAG_UPDATED      0x00000002
279 #define HEALTH_ENTRY_FLAG_EXEC_RUN     0x00000004
280 #define HEALTH_ENTRY_FLAG_EXEC_FAILED  0x00000008
281 #define HEALTH_ENTRY_FLAG_SAVED        0x10000000
282
283 typedef struct alarm_entry {
284     uint32_t unique_id;
285     uint32_t alarm_id;
286     uint32_t alarm_event_id;
287
288     time_t when;
289     time_t duration;
290     time_t non_clear_duration;
291
292     char *name;
293     uint32_t hash_name;
294
295     char *chart;
296     uint32_t hash_chart;
297
298     char *family;
299
300     char *exec;
301     char *recipient;
302     time_t exec_run_timestamp;
303     int exec_code;
304
305     char *source;
306     char *units;
307     char *info;
308
309     calculated_number old_value;
310     calculated_number new_value;
311
312     char *old_value_string;
313     char *new_value_string;
314
315     int old_status;
316     int new_status;
317
318     uint32_t flags;
319
320     int delay;
321     time_t delay_up_to_timestamp;
322
323     uint32_t updated_by_id;
324     uint32_t updates_id;
325     
326     struct alarm_entry *next;
327 } ALARM_ENTRY;
328
329 typedef struct alarm_log {
330     uint32_t next_log_id;
331     uint32_t next_alarm_id;
332     unsigned int count;
333     unsigned int max;
334     ALARM_ENTRY *alarms;
335     pthread_rwlock_t alarm_log_rwlock;
336 } ALARM_LOG;
337
338 #include "rrd.h"
339
340 extern void rrdsetvar_rename_all(RRDSET *st);
341 extern RRDSETVAR *rrdsetvar_create(RRDSET *st, const char *variable, int type, void *value, uint32_t options);
342 extern void rrdsetvar_free(RRDSETVAR *rs);
343
344 extern void rrddimvar_rename_all(RRDDIM *rd);
345 extern RRDDIMVAR *rrddimvar_create(RRDDIM *rd, int type, const char *prefix, const char *suffix, void *value, uint32_t options);
346 extern void rrddimvar_free(RRDDIMVAR *rs);
347
348 extern void rrdsetcalc_link_matching(RRDSET *st);
349 extern void rrdsetcalc_unlink(RRDCALC *rc);
350 extern void rrdcalctemplate_link_matching(RRDSET *st);
351 extern RRDCALC *rrdcalc_find(RRDSET *st, const char *name);
352
353 extern void health_init(void);
354 extern void *health_main(void *ptr);
355
356 extern void health_reload(void);
357
358 extern int health_variable_lookup(const char *variable, uint32_t hash, RRDCALC *rc, calculated_number *result);
359 extern void health_alarms2json(RRDHOST *host, BUFFER *wb, int all);
360 extern void health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after);
361
362 void health_api_v1_chart_variables2json(RRDSET *st, BUFFER *buf);
363
364 extern RRDVAR *rrdvar_custom_host_variable_create(RRDHOST *host, const char *name);
365 extern void rrdvar_custom_host_variable_destroy(RRDHOST *host, const char *name);
366 extern void rrdvar_custom_host_variable_set(RRDVAR *rv, calculated_number value);
367
368 extern const char *rrdcalc_status2string(int status);
369
370 #endif //NETDATA_HEALTH_H