lib: clang-format
[monitoring-plugins.git] / lib / utils_base.h
bloba209cb6d85d4f0de538217270467e5cf60d8c490
1 #ifndef _UTILS_BASE_
2 #define _UTILS_BASE_
3 /* Header file for Monitoring Plugins utils_base.c */
5 #ifndef USE_OPENSSL
6 # include "sha256.h"
7 #endif
9 /* This file holds header information for thresholds - use this in preference to
10 individual plugin logic */
12 /* This has not been merged with utils.h because of problems with
13 timeout_interval when other utils_*.h files use utils.h */
15 /* Long term, add new functions to utils_base.h for common routines
16 and utils_*.h for specific to plugin routines. If routines are
17 placed in utils_*.h, then these can be tested with libtap */
19 #define OUTSIDE 0
20 #define INSIDE 1
22 typedef struct range_struct {
23 double start;
24 bool start_infinity;
25 double end;
26 int end_infinity;
27 int alert_on; /* OUTSIDE (default) or INSIDE */
28 char *text; /* original unparsed text input */
29 } range;
31 typedef struct thresholds_struct {
32 range *warning;
33 range *critical;
34 } thresholds;
36 #define NP_STATE_FORMAT_VERSION 1
38 typedef struct state_data_struct {
39 time_t time;
40 void *data;
41 int length; /* Of binary data */
42 } state_data;
44 typedef struct state_key_struct {
45 char *name;
46 char *plugin_name;
47 int data_version;
48 char *_filename;
49 state_data *state_data;
50 } state_key;
52 typedef struct np_struct {
53 char *plugin_name;
54 state_key *state;
55 int argc;
56 char **argv;
57 } monitoring_plugin;
59 range *parse_range_string(char *);
60 int _set_thresholds(thresholds **, char *, char *);
61 void set_thresholds(thresholds **, char *, char *);
62 void print_thresholds(const char *, thresholds *);
63 bool check_range(double, range *);
64 int get_status(double, thresholds *);
66 /* Handle timeouts */
67 extern int timeout_state;
68 extern unsigned int timeout_interval;
70 /* All possible characters in a threshold range */
71 #define NP_THRESHOLDS_CHARS "-0123456789.:@~"
73 char *np_escaped_string(const char *);
75 void die(int, const char *, ...) __attribute__((noreturn, format(printf, 2, 3)));
77 /* Return codes for _set_thresholds */
78 #define NP_RANGE_UNPARSEABLE 1
79 #define NP_WARN_WITHIN_CRIT 2
81 /* a simple check to see if we're running as root.
82 * returns zero on failure, nonzero on success */
83 int np_check_if_root(void);
85 /* mp_suid() returns true if the real and effective uids differs, such as when
86 * running a suid plugin */
87 #define mp_suid() (getuid() != geteuid())
90 * Extract the value from key/value pairs, or return NULL. The value returned
91 * can be free()ed.
92 * This function can be used to parse NTP control packet data and performance
93 * data strings.
95 char *np_extract_value(const char *, const char *, char);
98 * Same as np_extract_value with separator suitable for NTP control packet
99 * payloads (comma)
101 #define np_extract_ntpvar(l, n) np_extract_value(l, n, ',')
104 * Read a string representing a state (ok, warning... or numeric: 0, 1) and
105 * return the corresponding NP_STATE or ERROR)
107 int mp_translate_state(char *);
109 void np_enable_state(char *, int);
110 state_data *np_state_read();
111 void np_state_write_string(time_t, char *);
113 void np_init(char *, int argc, char **argv);
114 void np_set_args(int argc, char **argv);
115 void np_cleanup();
116 const char *state_text(int);
118 #endif /* _UTILS_BASE_ */