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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
--- a/main.c
+++ b/main.c
@@ -51,25 +51,40 @@ volatile int stop = 0;
int quiet = 0;
char machine_readable = 0;
+#ifdef ENABLE_JSON
char json_output = 0;
+#else
+#define json_output 0
+#endif
char show_ts = 0;
+#ifdef ENABLE_HELP_FORMAT
int max_x = 80, max_y = 24;
+#endif
+#ifdef ENABLE_NAGIOS
char nagios_mode = 0;
+#else
+#define nagios_mode 0
+#endif
+#ifdef NC
char ncurses_mode = 0;
+#else
+#define ncurses_mode 0
+#endif
int fd = -1;
volatile char got_sigquit = 0;
-void handler_quit(int s)
+void handler_quit()
{
signal(SIGQUIT, handler_quit);
got_sigquit = 1;
}
+#ifdef ENABLE_HELP_FORMAT
void determine_terminal_size(int *max_y, int *max_x)
{
struct winsize size;
@@ -104,6 +119,7 @@ void determine_terminal_size(int *max_y,
*max_y = 24;
}
}
+#endif
void emit_statuslines(double run_time)
{
@@ -159,6 +175,7 @@ void emit_headers(char *in)
#endif
}
+#ifdef ENABLE_JSON
void emit_json(char ok, int seq, double start_ts, stats_t *t_resolve, stats_t *t_connect, stats_t *t_request, int http_code, const char *msg, int header_size, int data_size, double Bps, const char *host, const char *ssl_fp, double toff_diff_ts, char tfo_success, stats_t *t_ssl, stats_t *t_write, stats_t *t_close, int n_cookies, stats_t *stats_to, stats_t *tcp_rtt_stats, int re_tx, int pmtu, int recv_tos, stats_t *t_total)
{
if (seq > 1)
@@ -203,6 +220,7 @@ void emit_json(char ok, int seq, double
printf("\"tos\" : \"%02x\" ", recv_tos);
printf("}");
}
+#endif
char *get_ts_str(int verbose)
{
@@ -240,8 +258,13 @@ void emit_error(int verbose, int seq, do
if (!quiet && !machine_readable && !nagios_mode && !json_output)
printf("%s%s%s%s\n", ts ? ts : "", c_error, get_error(), c_normal);
+#ifdef ENABLE_JSON
if (json_output)
emit_json(0, seq, start_ts, NULL, NULL, NULL, -1, get_error(), -1, -1, -1, "", "", -1, 0, NULL, NULL, NULL, 0, NULL, NULL, 0, 0, 0, NULL);
+#else
+ (void) seq;
+ (void) start_ts;
+#endif
clear_error();
@@ -590,6 +613,7 @@ void fetch_proxy_settings(char **proxy_u
}
}
+#ifdef ENABLE_NAGIOS
void parse_nagios_settings(const char *in, double *nagios_warn, double *nagios_crit)
{
char *dummy = strchr(in, ',');
@@ -600,6 +624,7 @@ void parse_nagios_settings(const char *i
*nagios_crit = atof(dummy + 1);
}
+#endif
void parse_bind_to(const char *in, struct sockaddr_in *bind_to_4, struct sockaddr_in6 *bind_to_6, struct sockaddr_in **bind_to)
{
@@ -722,6 +747,7 @@ char check_compressed(const char *reply)
return 0;
}
+#ifdef ENABLE_NAGIOS
int nagios_result(int ok, int nagios_mode, int nagios_exit_code, double avg_httping_time, double nagios_warn, double nagios_crit)
{
if (nagios_mode == 1)
@@ -762,6 +788,7 @@ int nagios_result(int ok, int nagios_mod
return -1;
}
+#endif
void proxy_to_host_and_port(char *in, char **proxy_host, int *proxy_port)
{
@@ -896,8 +923,10 @@ int main(int argc, char *argv[])
int n_static_cookies = 0, n_dynamic_cookies = 0;
char resolve_once = 0;
char have_resolved = 0;
+#ifdef ENABLE_NAGIOS
double nagios_warn=0.0, nagios_crit=0.0;
int nagios_exit_code = 2;
+#endif
double avg_httping_time = -1.0;
int get_instead_of_head = 0;
char show_Bps = 0, ask_compression = 0;
@@ -928,9 +957,17 @@ int main(int argc, char *argv[])
stats_t t_connect, t_request, t_total, t_resolve, t_write, t_ssl, t_close, stats_to, tcp_rtt_stats, stats_header_size;
char first_resolve = 1;
double graph_limit = MY_DOUBLE_INF;
+#ifdef NC
+#ifdef FW
char nc_graph = 1;
+#else
+ #define nc_graph 1
+#endif
+#endif
char adaptive_interval = 0;
+#ifdef NC
double show_slow_log = MY_DOUBLE_INF;
+#endif
char use_tcp_nodelay = 1;
int max_mtu = -1;
int write_sleep = 500; /* in us (microseconds), determines resolution of transmit time determination */
@@ -980,8 +1017,10 @@ int main(int argc, char *argv[])
{"user-agent", 1, NULL, 'I' },
{"referer", 1, NULL, 'S' },
{"resolve-once",0, NULL, 'r' },
+#ifdef ENABLE_NAGIOS
{"nagios-mode-1", 1, NULL, 'n' },
{"nagios-mode-2", 1, NULL, 'n' },
+#endif
{"bind-to", 1, NULL, 'y' },
{"quiet", 0, NULL, 'q' },
{"username", 1, NULL, 'U' },
@@ -1052,7 +1091,9 @@ int main(int argc, char *argv[])
#endif
init_statst(&stats_header_size);
+#ifdef ENABLE_HELP_FORMAT
determine_terminal_size(&max_y, &max_x);
+#endif
signal(SIGPIPE, SIG_IGN);
@@ -1108,11 +1149,11 @@ int main(int argc, char *argv[])
case 14:
draw_phase = 1;
break;
-#endif
case 13:
show_slow_log = atof(optarg);
break;
+#endif
case 12:
adaptive_interval = 1;
@@ -1144,9 +1185,11 @@ int main(int argc, char *argv[])
fprintf(stderr, gettext("\n *** -A is no longer required ***\n\n"));
break;
+#ifdef ENABLE_JSON
case 'M':
json_output = 1;
break;
+#endif
case 'v':
verbose++;
@@ -1338,6 +1381,7 @@ int main(int argc, char *argv[])
version();
return 0;
+#ifdef ENABLE_NAGIOS
case 'n':
if (nagios_mode)
error_exit(gettext("-n and -N are mutual exclusive\n"));
@@ -1352,6 +1396,7 @@ int main(int argc, char *argv[])
nagios_mode = 2;
nagios_exit_code = atoi(optarg);
break;
+#endif
case 'P':
auth_password = optarg;
@@ -1497,8 +1542,10 @@ int main(int argc, char *argv[])
printf("PING %s%s:%s%d%s (%s):\n", c_green, hostname, c_bright, portnr, c_normal, get);
}
+#ifdef ENABLE_JSON
if (json_output)
printf("[\n");
+#endif
if (adaptive_interval && wait <= 0.0)
error_exit(gettext("Interval must be > 0 when using adaptive interval"));
@@ -1593,7 +1640,10 @@ int main(int argc, char *argv[])
int age = -1;
char *sc = NULL, *scdummy = NULL;
char *fp = NULL;
- int re_tx = 0, pmtu = 0, recv_tos = 0;
+ int recv_tos = 0;
+#if defined(NC) || defined(ENABLE_JSON)
+ int re_tx = 0, pmtu = 0;
+#endif
socklen_t recv_tos_len = sizeof recv_tos;
dstart = get_ts();
@@ -1607,7 +1657,9 @@ int main(int argc, char *argv[])
char req_sent = 0;
double dummy_ms = 0.0;
double their_est_ts = -1.0, toff_diff_ts = -1.0;
+#if defined(ENABLE_JSON) || defined(TCP_TFO)
char tfo_success = 0;
+#endif
double ssl_handshake = 0.0;
char cur_have_resolved = 0;
#if defined(linux) || defined(__FreeBSD__)
@@ -2114,7 +2166,7 @@ persistent_loop:
update_statst(&tcp_rtt_stats, (double)info.tcpi_rtt / 1000.0);
-#ifdef linux
+#if defined(linux) && (defined(NC) || defined(ENABLE_JSON))
re_tx = info.tcpi_retransmits;
pmtu = info.tcpi_pmtu;
#endif
@@ -2134,6 +2186,7 @@ persistent_loop:
toff_diff_ts = ((double)their_ts - their_est_ts) * 1000.0;
update_statst(&stats_to, toff_diff_ts);
+#ifdef ENABLE_JSON
if (json_output)
{
char current_host[4096] = { 0 };
@@ -2145,7 +2198,9 @@ persistent_loop:
emit_json(1, curncount, dstart, &t_resolve, &t_connect, &t_request, atoi(sc ? sc : "-1"), sc ? sc : "?", headers_len, len, Bps, current_host, fp, toff_diff_ts, tfo_success, &t_ssl, &t_write, &t_close, n_dynamic_cookies, &stats_to, &tcp_rtt_stats, re_tx, pmtu, recv_tos, &t_total);
}
- else if (machine_readable)
+ else
+#endif
+ if (machine_readable)
{
if (sc)
{
@@ -2404,14 +2459,18 @@ persistent_loop:
stats_line(1, complete_url, count, curncount, err, ok, started_at, verbose, &t_total, avg_httping_time, show_Bps ? &bps : NULL);
error_exit:
+#ifdef ENABLE_NAGIOS
if (nagios_mode)
return nagios_result(ok, nagios_mode, nagios_exit_code, avg_httping_time, nagios_warn, nagios_crit);
+#endif
if (!json_output && !machine_readable)
printf("%s", c_very_normal);
+#ifdef ENABLE_JSON
if (json_output)
printf("\n]\n");
+#endif
free_cookies(static_cookies, n_static_cookies);
free_cookies(dynamic_cookies, n_dynamic_cookies);
--- a/help.c
+++ b/help.c
@@ -93,6 +93,8 @@ void version(void)
fprintf(stderr, gettext("\n"));
}
+#ifdef ENABLE_HELP
+#ifdef ENABLE_HELP_FORMAT
void format_help(const char *short_str, const char *long_str, const char *descr)
{
int par_width = SWITCHES_COLUMN_WIDTH, max_wrap_width = par_width / 2, cur_par_width = 0;
@@ -167,9 +169,30 @@ void format_help(const char *short_str,
p = n;
}
}
+#else
+void format_help(const char *short_str, const char *long_str, const char *descr)
+{
+ if (short_str)
+ {
+ fputs(short_str, stderr);
+ fputc(' ', stderr);
+ }
+
+ if (long_str)
+ {
+ fputs(long_str, stderr);
+ fputc(' ', stderr);
+ }
+
+ fputs(descr, stderr);
+ fputc('\n', stderr);
+}
+#endif
+#endif
void usage(const char *me)
{
+#ifdef ENABLE_HELP
char *dummy = NULL, has_color = 0;
char host[256] = { 0 };
@@ -270,11 +293,15 @@ void usage(const char *me)
fprintf(stderr, gettext(" *** output mode settings ***\n"));
format_help("-q", "--quiet", gettext("quiet, only returncode"));
format_help("-m", "--parseable-output", gettext("give machine parseable output (see also -o and -e)"));
+#ifdef ENABLE_JSON
format_help("-M", NULL, gettext("json output, cannot be combined with -m"));
+#endif
format_help("-o rc,rc,...", "--ok-result-codes", gettext("what http results codes indicate 'ok' comma separated WITHOUT spaces inbetween default is 200, use with -e"));
format_help("-e x", "--result-string", gettext("string to display when http result code doesn't match"));
+#ifdef ENABLE_NAGIOS
format_help("-n warn,crit", "--nagios-mode-1 / --nagios-mode-2", gettext("Nagios-mode: return 1 when avg. response time >= warn, 2 if >= crit, otherwhise return 0"));
format_help("-N x", NULL, gettext("Nagios mode 2: return 0 when all fine, 'x' when anything failes"));
+#endif
format_help("-C cookie=value", "--cookie", gettext("add a cookie to the request"));
format_help("-Y", "--colors", gettext("add colors"));
format_help("-a", "--audible-ping", gettext("audible ping"));
@@ -309,5 +336,6 @@ void usage(const char *me)
fprintf(stderr, gettext("Example:\n"));
fprintf(stderr, "\t%s %s%s -s -Z\n\n", me, host, has_color ? " -Y" : "");
+#endif
new_version_alert();
}
--- a/Makefile
+++ b/Makefile
@@ -88,6 +88,22 @@ OBJS+=fft.o
LDFLAGS+=-lfftw3
endif
+ifeq ($(JSON),yes)
+CFLAGS+=-DENABLE_JSON
+endif
+
+ifeq ($(NAGIOS),yes)
+CFLAGS+=-DENABLE_NAGIOS
+endif
+
+ifeq ($(HELP),yes)
+CFLAGS+=-DENABLE_HELP
+endif
+
+ifeq ($(HELPFORMAT),yes)
+CFLAGS+=-DENABLE_HELP_FORMAT
+endif
+
ifeq ($(DEBUG),yes)
CFLAGS+=-D_DEBUG -ggdb
LDFLAGS+=-g
|