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