You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

375 lines
10 KiB

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