2 * shared.h: Header file for code shared between module and daemon
5 #ifndef INCLUDE_shared_h__
6 #define INCLUDE_shared_h__
11 # define _GNU_SOURCE 1
14 /** common include files required practically everywhere **/
20 #include <netinet/in.h>
21 #include <sys/types.h>
24 #include <arpa/inet.h>
25 #include <sys/socket.h>
29 #include <sys/param.h>
36 #include "mrln_logging.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)
50 # define dbug(s) fprintf(stderr, s " @ %s->%s:%d", __FILE__, __func__, __LINE__)
56 # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
59 # define ARRAY_SIZE(ary) (unsigned int)(sizeof(ary)/sizeof(ary[0]))
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
)
73 static inline int min(int a
, int 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
;
84 extern int pulse_interval
;
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
;
102 unsigned int entries
;
105 typedef struct strvec strvec
;
107 /** event structures where Nagios' doesn't provide good ones */
108 struct monitored_object_state
{
110 int flap_detection_enabled
;
111 double low_flap_threshold
;
112 double high_flap_threshold
;
114 int freshness_threshold
;
115 int process_performance_data
;
117 int accept_passive_checks
;
118 int event_handler_enabled
;
120 int problem_has_been_acknowledged
;
121 int acknowledgement_type
;
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
;
134 double execution_time
;
135 int notifications_enabled
;
136 time_t last_notification
;
137 time_t next_notification
;
139 int should_be_scheduled
;
141 time_t last_state_change
;
142 time_t last_hard_state_change
;
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
;
155 unsigned long flapping_comment_id
;
156 double percent_state_change
;
157 unsigned long modified_attributes
;
160 char *long_plugin_output
;
163 typedef struct monitored_object_state monitored_object_state
;
165 struct merlin_host_status
{
167 monitored_object_state state
;
170 typedef struct merlin_host_status merlin_host_status
;
172 struct merlin_service_status
{
174 monitored_object_state state
;
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
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
);
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
);