db_updater: Put parentheses back
[merlin.git] / shared.h
blob93dab4b951076f55566c2683abff43f43cf0ab05
1 /*
2 * shared.h: Header file for code shared between module and daemon
3 */
5 #ifndef INCLUDE_shared_h__
6 #define INCLUDE_shared_h__
8 #define NSCORE
10 #ifndef _GNU_SOURCE
11 # define _GNU_SOURCE 1
12 #endif
14 /** common include files required practically everywhere **/
15 #include <stdio.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <errno.h>
20 #include <netinet/in.h>
21 #include <sys/types.h>
22 #include <time.h>
23 #include <sys/time.h>
24 #include <arpa/inet.h>
25 #include <sys/socket.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <dirent.h>
29 #include <sys/param.h>
30 #include <ctype.h>
32 #include "compat.h"
33 #include "node.h"
34 #include "io.h"
35 #include "ipc.h"
36 #include "mrln_logging.h"
37 #include "cfgfile.h"
38 #include "binlog.h"
39 #include <nagios/nagios.h>
40 #include <nagios/nebstructs.h>
41 #include <nagios/nebcallbacks.h>
42 #include <nagios/broker.h>
45 * debug macros. All of them (including assert), goes away when NDEBUG
46 * is specified. None of these may have side-effects (Heisenbugs)
48 #ifndef NDEBUG
49 # include <assert.h>
50 # define dbug(s) fprintf(stderr, s " @ %s->%s:%d", __FILE__, __func__, __LINE__)
51 #else
52 # define dbug(s)
53 #endif
55 #ifndef offsetof
56 # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
57 #endif
58 #ifndef ARRAY_SIZE
59 # define ARRAY_SIZE(ary) (unsigned int)(sizeof(ary)/sizeof(ary[0]))
60 #endif
62 #define sizeof(x) (uint)sizeof(x)
64 #define is_flag_set(bfield, flag) (!!((bfield) & (flag)))
65 #define safe_str(str) (str == NULL ? "NULL" : str)
66 #define safe_free my_free
68 static inline int max(int a, int b)
70 return a > b ? a : b;
73 static inline int min(int a, int b)
75 return a < b ? a : b;
78 #define safe_strdup(str) str ? strdup(str) : NULL
79 #define prefixcmp(a, b) strncmp(a, b, strlen(b))
81 /** global variables present in both module and daemon **/
82 extern const char *merlin_version;
83 extern int is_module;
84 extern int pulse_interval;
85 extern int debug;
86 extern char *binlog_dir;
87 extern char *merlin_config_file;
90 #define num_masters self->configured_masters
91 #define num_peers self->configured_peers
92 #define num_pollers self->configured_pollers
93 #define num_nodes (num_masters + num_pollers + num_peers)
94 #define online_masters self->active_masters
95 #define online_peers self->active_peers
96 #define online_pollers self->active_pollers
97 #define online_nodes (online_masters + online_pollers + online_peers)
98 extern int use_database;
99 extern merlin_nodeinfo *self;
101 struct strvec {
102 unsigned int entries;
103 char **str;
105 typedef struct strvec strvec;
107 /** event structures where Nagios' doesn't provide good ones */
108 struct monitored_object_state {
109 int initial_state;
110 int flap_detection_enabled;
111 double low_flap_threshold;
112 double high_flap_threshold;
113 int check_freshness;
114 int freshness_threshold;
115 int process_performance_data;
116 int checks_enabled;
117 int accept_passive_checks;
118 int event_handler_enabled;
119 int obsess;
120 int problem_has_been_acknowledged;
121 int acknowledgement_type;
122 int check_type;
123 int current_state;
124 int last_state;
125 int last_hard_state;
126 int state_type;
127 int current_attempt;
128 unsigned long hourly_value;
129 unsigned long current_event_id;
130 unsigned long last_event_id;
131 unsigned long current_problem_id;
132 unsigned long last_problem_id;
133 double latency;
134 double execution_time;
135 int notifications_enabled;
136 time_t last_notification;
137 time_t next_notification;
138 time_t next_check;
139 int should_be_scheduled;
140 time_t last_check;
141 time_t last_state_change;
142 time_t last_hard_state_change;
143 time_t last_time_up;
144 time_t last_time_down;
145 time_t last_time_unreachable;
146 int has_been_checked;
147 int current_notification_number;
148 unsigned long current_notification_id;
149 int check_flapping_recovery_notification;
150 int scheduled_downtime_depth;
151 int pending_flex_downtime;
152 int state_history[MAX_STATE_HISTORY_ENTRIES]; /* flap detection */
153 int state_history_index;
154 int is_flapping;
155 unsigned long flapping_comment_id;
156 double percent_state_change;
157 unsigned long modified_attributes;
158 int notified_on;
159 char *plugin_output;
160 char *long_plugin_output;
161 char *perf_data;
163 typedef struct monitored_object_state monitored_object_state;
165 struct merlin_host_status {
166 int nebattr;
167 monitored_object_state state;
168 char *name;
170 typedef struct merlin_host_status merlin_host_status;
172 struct merlin_service_status {
173 int nebattr;
174 monitored_object_state state;
175 char *host_name;
176 char *service_description;
178 typedef struct merlin_service_status merlin_service_status;
181 #define ISOTIME_PREC_YEAR 0
182 #define ISOTIME_PREC_MONTH 1
183 #define ISOTIME_PREC_DAY 2
184 #define ISOTIME_PREC_HOUR 3
185 #define ISOTIME_PREC_MINUTE 4
186 #define ISOTIME_PREC_SECOND 5
187 #define ISOTIME_PREC_USECOND 6
188 #define ISOTIME_PREC_MAX ISOTIME_PREC_USECOND
190 /** prototypes **/
191 extern strvec *str_explode(char *str, int delim);
192 extern int strtobool(const char *str);
193 extern int grok_seconds(const char *p, long *result);
194 extern const char *isotime(struct timeval *tv, int precision);
195 extern char *tohex(const unsigned char *data, int len);
196 extern void bt_scan(const char *mark, int count);
197 extern const char *human_bytes(unsigned long long n);
198 extern linked_item *add_linked_item(linked_item *list, void *item);
199 extern int merlin_set_socket_options(int sd, int beefup_buffers);
200 extern char *next_word(char *str);
201 extern int grok_confsync_compound(struct cfg_comp *comp, merlin_confsync *csync);
202 extern int grok_common_var(struct cfg_comp *config, struct cfg_var *v);
203 extern const char *callback_name(int id);
204 extern int callback_id(const char *orig_name);
205 extern const char *ctrl_name(uint code);
206 extern const char *node_state_name(int state);
207 extern const char *tv_delta(const struct timeval *start, const struct timeval *stop);
208 extern int dump_nodeinfo(merlin_node *n, int sd, int instance_id);
210 /* data encoding/decoding routines */
211 extern int merlin_encode(void *data, int cb_type, char *buf, int buflen);
212 extern int merlin_decode(void *ds, off_t len, int cb_type);
213 static inline int merlin_encode_event(merlin_event *pkt, void *data)
215 return merlin_encode(data, pkt->hdr.type, pkt->body, sizeof(pkt->body));
217 static inline int merlin_decode_event(merlin_node *node, merlin_event *pkt)
219 int ret = merlin_decode(pkt->body, pkt->hdr.len, pkt->hdr.type);
221 if (ret) {
222 lerr("CODEC: Failed to decode packet from '%s'. type: %u (%s); code: %u; len: %u",
223 node ? node->name : "(unknown)", pkt->hdr.type, callback_name(pkt->hdr.type), pkt->hdr.code, pkt->hdr.len);
225 return ret;
227 #endif