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