| b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | --- a/main.c |
| 2 | +++ b/main.c |
| 3 | @@ -51,25 +51,40 @@ volatile int stop = 0; |
| 4 | |
| 5 | int quiet = 0; |
| 6 | char machine_readable = 0; |
| 7 | +#ifdef ENABLE_JSON |
| 8 | char json_output = 0; |
| 9 | +#else |
| 10 | +#define json_output 0 |
| 11 | +#endif |
| 12 | char show_ts = 0; |
| 13 | |
| 14 | +#ifdef ENABLE_HELP_FORMAT |
| 15 | int max_x = 80, max_y = 24; |
| 16 | +#endif |
| 17 | |
| 18 | +#ifdef ENABLE_NAGIOS |
| 19 | char nagios_mode = 0; |
| 20 | +#else |
| 21 | +#define nagios_mode 0 |
| 22 | +#endif |
| 23 | +#ifdef NC |
| 24 | char ncurses_mode = 0; |
| 25 | +#else |
| 26 | +#define ncurses_mode 0 |
| 27 | +#endif |
| 28 | |
| 29 | int fd = -1; |
| 30 | |
| 31 | volatile char got_sigquit = 0; |
| 32 | |
| 33 | -void handler_quit(int s) |
| 34 | +void handler_quit() |
| 35 | { |
| 36 | signal(SIGQUIT, handler_quit); |
| 37 | |
| 38 | got_sigquit = 1; |
| 39 | } |
| 40 | |
| 41 | +#ifdef ENABLE_HELP_FORMAT |
| 42 | void determine_terminal_size(int *max_y, int *max_x) |
| 43 | { |
| 44 | struct winsize size; |
| 45 | @@ -104,6 +119,7 @@ void determine_terminal_size(int *max_y, |
| 46 | *max_y = 24; |
| 47 | } |
| 48 | } |
| 49 | +#endif |
| 50 | |
| 51 | void emit_statuslines(double run_time) |
| 52 | { |
| 53 | @@ -159,6 +175,7 @@ void emit_headers(char *in) |
| 54 | #endif |
| 55 | } |
| 56 | |
| 57 | +#ifdef ENABLE_JSON |
| 58 | 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) |
| 59 | { |
| 60 | if (seq > 1) |
| 61 | @@ -203,6 +220,7 @@ void emit_json(char ok, int seq, double |
| 62 | printf("\"tos\" : \"%02x\" ", recv_tos); |
| 63 | printf("}"); |
| 64 | } |
| 65 | +#endif |
| 66 | |
| 67 | char *get_ts_str(int verbose) |
| 68 | { |
| 69 | @@ -240,8 +258,13 @@ void emit_error(int verbose, int seq, do |
| 70 | if (!quiet && !machine_readable && !nagios_mode && !json_output) |
| 71 | printf("%s%s%s%s\n", ts ? ts : "", c_error, get_error(), c_normal); |
| 72 | |
| 73 | +#ifdef ENABLE_JSON |
| 74 | if (json_output) |
| 75 | 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); |
| 76 | +#else |
| 77 | + (void) seq; |
| 78 | + (void) start_ts; |
| 79 | +#endif |
| 80 | |
| 81 | clear_error(); |
| 82 | |
| 83 | @@ -590,6 +613,7 @@ void fetch_proxy_settings(char **proxy_u |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | +#ifdef ENABLE_NAGIOS |
| 88 | void parse_nagios_settings(const char *in, double *nagios_warn, double *nagios_crit) |
| 89 | { |
| 90 | char *dummy = strchr(in, ','); |
| 91 | @@ -600,6 +624,7 @@ void parse_nagios_settings(const char *i |
| 92 | |
| 93 | *nagios_crit = atof(dummy + 1); |
| 94 | } |
| 95 | +#endif |
| 96 | |
| 97 | void parse_bind_to(const char *in, struct sockaddr_in *bind_to_4, struct sockaddr_in6 *bind_to_6, struct sockaddr_in **bind_to) |
| 98 | { |
| 99 | @@ -722,6 +747,7 @@ char check_compressed(const char *reply) |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | +#ifdef ENABLE_NAGIOS |
| 104 | int nagios_result(int ok, int nagios_mode, int nagios_exit_code, double avg_httping_time, double nagios_warn, double nagios_crit) |
| 105 | { |
| 106 | if (nagios_mode == 1) |
| 107 | @@ -762,6 +788,7 @@ int nagios_result(int ok, int nagios_mod |
| 108 | |
| 109 | return -1; |
| 110 | } |
| 111 | +#endif |
| 112 | |
| 113 | void proxy_to_host_and_port(char *in, char **proxy_host, int *proxy_port) |
| 114 | { |
| 115 | @@ -896,8 +923,10 @@ int main(int argc, char *argv[]) |
| 116 | int n_static_cookies = 0, n_dynamic_cookies = 0; |
| 117 | char resolve_once = 0; |
| 118 | char have_resolved = 0; |
| 119 | +#ifdef ENABLE_NAGIOS |
| 120 | double nagios_warn=0.0, nagios_crit=0.0; |
| 121 | int nagios_exit_code = 2; |
| 122 | +#endif |
| 123 | double avg_httping_time = -1.0; |
| 124 | int get_instead_of_head = 0; |
| 125 | char show_Bps = 0, ask_compression = 0; |
| 126 | @@ -928,9 +957,17 @@ int main(int argc, char *argv[]) |
| 127 | stats_t t_connect, t_request, t_total, t_resolve, t_write, t_ssl, t_close, stats_to, tcp_rtt_stats, stats_header_size; |
| 128 | char first_resolve = 1; |
| 129 | double graph_limit = MY_DOUBLE_INF; |
| 130 | +#ifdef NC |
| 131 | +#ifdef FW |
| 132 | char nc_graph = 1; |
| 133 | +#else |
| 134 | + #define nc_graph 1 |
| 135 | +#endif |
| 136 | +#endif |
| 137 | char adaptive_interval = 0; |
| 138 | +#ifdef NC |
| 139 | double show_slow_log = MY_DOUBLE_INF; |
| 140 | +#endif |
| 141 | char use_tcp_nodelay = 1; |
| 142 | int max_mtu = -1; |
| 143 | int write_sleep = 500; /* in us (microseconds), determines resolution of transmit time determination */ |
| 144 | @@ -980,8 +1017,10 @@ int main(int argc, char *argv[]) |
| 145 | {"user-agent", 1, NULL, 'I' }, |
| 146 | {"referer", 1, NULL, 'S' }, |
| 147 | {"resolve-once",0, NULL, 'r' }, |
| 148 | +#ifdef ENABLE_NAGIOS |
| 149 | {"nagios-mode-1", 1, NULL, 'n' }, |
| 150 | {"nagios-mode-2", 1, NULL, 'n' }, |
| 151 | +#endif |
| 152 | {"bind-to", 1, NULL, 'y' }, |
| 153 | {"quiet", 0, NULL, 'q' }, |
| 154 | {"username", 1, NULL, 'U' }, |
| 155 | @@ -1052,7 +1091,9 @@ int main(int argc, char *argv[]) |
| 156 | #endif |
| 157 | init_statst(&stats_header_size); |
| 158 | |
| 159 | +#ifdef ENABLE_HELP_FORMAT |
| 160 | determine_terminal_size(&max_y, &max_x); |
| 161 | +#endif |
| 162 | |
| 163 | signal(SIGPIPE, SIG_IGN); |
| 164 | |
| 165 | @@ -1108,11 +1149,11 @@ int main(int argc, char *argv[]) |
| 166 | case 14: |
| 167 | draw_phase = 1; |
| 168 | break; |
| 169 | -#endif |
| 170 | |
| 171 | case 13: |
| 172 | show_slow_log = atof(optarg); |
| 173 | break; |
| 174 | +#endif |
| 175 | |
| 176 | case 12: |
| 177 | adaptive_interval = 1; |
| 178 | @@ -1144,9 +1185,11 @@ int main(int argc, char *argv[]) |
| 179 | fprintf(stderr, gettext("\n *** -A is no longer required ***\n\n")); |
| 180 | break; |
| 181 | |
| 182 | +#ifdef ENABLE_JSON |
| 183 | case 'M': |
| 184 | json_output = 1; |
| 185 | break; |
| 186 | +#endif |
| 187 | |
| 188 | case 'v': |
| 189 | verbose++; |
| 190 | @@ -1338,6 +1381,7 @@ int main(int argc, char *argv[]) |
| 191 | version(); |
| 192 | return 0; |
| 193 | |
| 194 | +#ifdef ENABLE_NAGIOS |
| 195 | case 'n': |
| 196 | if (nagios_mode) |
| 197 | error_exit(gettext("-n and -N are mutual exclusive\n")); |
| 198 | @@ -1352,6 +1396,7 @@ int main(int argc, char *argv[]) |
| 199 | nagios_mode = 2; |
| 200 | nagios_exit_code = atoi(optarg); |
| 201 | break; |
| 202 | +#endif |
| 203 | |
| 204 | case 'P': |
| 205 | auth_password = optarg; |
| 206 | @@ -1497,8 +1542,10 @@ int main(int argc, char *argv[]) |
| 207 | printf("PING %s%s:%s%d%s (%s):\n", c_green, hostname, c_bright, portnr, c_normal, get); |
| 208 | } |
| 209 | |
| 210 | +#ifdef ENABLE_JSON |
| 211 | if (json_output) |
| 212 | printf("[\n"); |
| 213 | +#endif |
| 214 | |
| 215 | if (adaptive_interval && wait <= 0.0) |
| 216 | error_exit(gettext("Interval must be > 0 when using adaptive interval")); |
| 217 | @@ -1593,7 +1640,10 @@ int main(int argc, char *argv[]) |
| 218 | int age = -1; |
| 219 | char *sc = NULL, *scdummy = NULL; |
| 220 | char *fp = NULL; |
| 221 | - int re_tx = 0, pmtu = 0, recv_tos = 0; |
| 222 | + int recv_tos = 0; |
| 223 | +#if defined(NC) || defined(ENABLE_JSON) |
| 224 | + int re_tx = 0, pmtu = 0; |
| 225 | +#endif |
| 226 | socklen_t recv_tos_len = sizeof recv_tos; |
| 227 | |
| 228 | dstart = get_ts(); |
| 229 | @@ -1607,7 +1657,9 @@ int main(int argc, char *argv[]) |
| 230 | char req_sent = 0; |
| 231 | double dummy_ms = 0.0; |
| 232 | double their_est_ts = -1.0, toff_diff_ts = -1.0; |
| 233 | +#if defined(ENABLE_JSON) || defined(TCP_TFO) |
| 234 | char tfo_success = 0; |
| 235 | +#endif |
| 236 | double ssl_handshake = 0.0; |
| 237 | char cur_have_resolved = 0; |
| 238 | #if defined(linux) || defined(__FreeBSD__) |
| 239 | @@ -2114,7 +2166,7 @@ persistent_loop: |
| 240 | |
| 241 | update_statst(&tcp_rtt_stats, (double)info.tcpi_rtt / 1000.0); |
| 242 | |
| 243 | -#ifdef linux |
| 244 | +#if defined(linux) && (defined(NC) || defined(ENABLE_JSON)) |
| 245 | re_tx = info.tcpi_retransmits; |
| 246 | pmtu = info.tcpi_pmtu; |
| 247 | #endif |
| 248 | @@ -2134,6 +2186,7 @@ persistent_loop: |
| 249 | toff_diff_ts = ((double)their_ts - their_est_ts) * 1000.0; |
| 250 | update_statst(&stats_to, toff_diff_ts); |
| 251 | |
| 252 | +#ifdef ENABLE_JSON |
| 253 | if (json_output) |
| 254 | { |
| 255 | char current_host[4096] = { 0 }; |
| 256 | @@ -2145,7 +2198,9 @@ persistent_loop: |
| 257 | |
| 258 | 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); |
| 259 | } |
| 260 | - else if (machine_readable) |
| 261 | + else |
| 262 | +#endif |
| 263 | + if (machine_readable) |
| 264 | { |
| 265 | if (sc) |
| 266 | { |
| 267 | @@ -2404,14 +2459,18 @@ persistent_loop: |
| 268 | stats_line(1, complete_url, count, curncount, err, ok, started_at, verbose, &t_total, avg_httping_time, show_Bps ? &bps : NULL); |
| 269 | |
| 270 | error_exit: |
| 271 | +#ifdef ENABLE_NAGIOS |
| 272 | if (nagios_mode) |
| 273 | return nagios_result(ok, nagios_mode, nagios_exit_code, avg_httping_time, nagios_warn, nagios_crit); |
| 274 | +#endif |
| 275 | |
| 276 | if (!json_output && !machine_readable) |
| 277 | printf("%s", c_very_normal); |
| 278 | |
| 279 | +#ifdef ENABLE_JSON |
| 280 | if (json_output) |
| 281 | printf("\n]\n"); |
| 282 | +#endif |
| 283 | |
| 284 | free_cookies(static_cookies, n_static_cookies); |
| 285 | free_cookies(dynamic_cookies, n_dynamic_cookies); |
| 286 | --- a/help.c |
| 287 | +++ b/help.c |
| 288 | @@ -93,6 +93,8 @@ void version(void) |
| 289 | fprintf(stderr, gettext("\n")); |
| 290 | } |
| 291 | |
| 292 | +#ifdef ENABLE_HELP |
| 293 | +#ifdef ENABLE_HELP_FORMAT |
| 294 | void format_help(const char *short_str, const char *long_str, const char *descr) |
| 295 | { |
| 296 | int par_width = SWITCHES_COLUMN_WIDTH, max_wrap_width = par_width / 2, cur_par_width = 0; |
| 297 | @@ -167,9 +169,30 @@ void format_help(const char *short_str, |
| 298 | p = n; |
| 299 | } |
| 300 | } |
| 301 | +#else |
| 302 | +void format_help(const char *short_str, const char *long_str, const char *descr) |
| 303 | +{ |
| 304 | + if (short_str) |
| 305 | + { |
| 306 | + fputs(short_str, stderr); |
| 307 | + fputc(' ', stderr); |
| 308 | + } |
| 309 | + |
| 310 | + if (long_str) |
| 311 | + { |
| 312 | + fputs(long_str, stderr); |
| 313 | + fputc(' ', stderr); |
| 314 | + } |
| 315 | + |
| 316 | + fputs(descr, stderr); |
| 317 | + fputc('\n', stderr); |
| 318 | +} |
| 319 | +#endif |
| 320 | +#endif |
| 321 | |
| 322 | void usage(const char *me) |
| 323 | { |
| 324 | +#ifdef ENABLE_HELP |
| 325 | char *dummy = NULL, has_color = 0; |
| 326 | char host[256] = { 0 }; |
| 327 | |
| 328 | @@ -270,11 +293,15 @@ void usage(const char *me) |
| 329 | fprintf(stderr, gettext(" *** output mode settings ***\n")); |
| 330 | format_help("-q", "--quiet", gettext("quiet, only returncode")); |
| 331 | format_help("-m", "--parseable-output", gettext("give machine parseable output (see also -o and -e)")); |
| 332 | +#ifdef ENABLE_JSON |
| 333 | format_help("-M", NULL, gettext("json output, cannot be combined with -m")); |
| 334 | +#endif |
| 335 | 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")); |
| 336 | format_help("-e x", "--result-string", gettext("string to display when http result code doesn't match")); |
| 337 | +#ifdef ENABLE_NAGIOS |
| 338 | 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")); |
| 339 | format_help("-N x", NULL, gettext("Nagios mode 2: return 0 when all fine, 'x' when anything failes")); |
| 340 | +#endif |
| 341 | format_help("-C cookie=value", "--cookie", gettext("add a cookie to the request")); |
| 342 | format_help("-Y", "--colors", gettext("add colors")); |
| 343 | format_help("-a", "--audible-ping", gettext("audible ping")); |
| 344 | @@ -309,5 +336,6 @@ void usage(const char *me) |
| 345 | fprintf(stderr, gettext("Example:\n")); |
| 346 | fprintf(stderr, "\t%s %s%s -s -Z\n\n", me, host, has_color ? " -Y" : ""); |
| 347 | |
| 348 | +#endif |
| 349 | new_version_alert(); |
| 350 | } |
| 351 | --- a/Makefile |
| 352 | +++ b/Makefile |
| 353 | @@ -88,6 +88,22 @@ OBJS+=fft.o |
| 354 | LDFLAGS+=-lfftw3 |
| 355 | endif |
| 356 | |
| 357 | +ifeq ($(JSON),yes) |
| 358 | +CFLAGS+=-DENABLE_JSON |
| 359 | +endif |
| 360 | + |
| 361 | +ifeq ($(NAGIOS),yes) |
| 362 | +CFLAGS+=-DENABLE_NAGIOS |
| 363 | +endif |
| 364 | + |
| 365 | +ifeq ($(HELP),yes) |
| 366 | +CFLAGS+=-DENABLE_HELP |
| 367 | +endif |
| 368 | + |
| 369 | +ifeq ($(HELPFORMAT),yes) |
| 370 | +CFLAGS+=-DENABLE_HELP_FORMAT |
| 371 | +endif |
| 372 | + |
| 373 | ifeq ($(DEBUG),yes) |
| 374 | CFLAGS+=-D_DEBUG -ggdb |
| 375 | LDFLAGS+=-g |