urlize.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /******************************************************************************
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  13. ******************************************************************************/
  14. const char *progname = "urlize";
  15. const char *revision = "$Revision$";
  16. const char *copyright = "2000-2003";
  17. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  18. #include "common.h"
  19. #include "utils.h"
  20. #include "popen.h"
  21. void print_help (void);
  22. void print_usage (void);
  23. int
  24. main (int argc, char **argv)
  25. {
  26. int found = 0, result = STATE_UNKNOWN;
  27. char *url = NULL;
  28. char *cmd;
  29. char *buf;
  30. int c;
  31. int option = 0;
  32. static struct option longopts[] = {
  33. {"help", no_argument, 0, 'h'},
  34. {"version", no_argument, 0, 'V'},
  35. {"url", required_argument, 0, 'u'},
  36. {0, 0, 0, 0}
  37. };
  38. while (1) {
  39. c = getopt_long (argc, argv, "+hVu:", longopts, &option);
  40. if (c == -1 || c == EOF)
  41. break;
  42. switch (c) {
  43. case 'h': /* help */
  44. print_help ();
  45. exit (EXIT_SUCCESS);
  46. break;
  47. case 'V': /* version */
  48. print_revision (progname, revision);
  49. exit (EXIT_SUCCESS);
  50. break;
  51. case 'u':
  52. url = strdup (argv[optind]);
  53. break;
  54. case '?':
  55. default:
  56. usage3 (_("Unknown argument"), optopt);
  57. break;
  58. }
  59. }
  60. if (url == NULL)
  61. url = strdup (argv[optind++]);
  62. cmd = strdup (argv[optind++]);
  63. for (c = optind; c < argc; c++) {
  64. asprintf (&cmd, "%s %s", cmd, argv[c]);
  65. }
  66. child_process = spopen (cmd);
  67. if (child_process == NULL) {
  68. printf (_("Could not open pipe: %s\n"), cmd);
  69. exit (STATE_UNKNOWN);
  70. }
  71. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  72. if (child_stderr == NULL) {
  73. printf (_("Could not open stderr for %s\n"), cmd);
  74. }
  75. buf = malloc(MAX_INPUT_BUFFER);
  76. printf ("<A href=\"%s\">", argv[1]);
  77. while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
  78. found++;
  79. printf ("%s", buf);
  80. }
  81. if (!found)
  82. die (STATE_UNKNOWN,
  83. _("%s problem - No data recieved from host\nCMD: %s</A>\n"),
  84. argv[0], cmd);
  85. /* close the pipe */
  86. result = spclose (child_process);
  87. /* WARNING if output found on stderr */
  88. if (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr))
  89. result = max_state (result, STATE_WARNING);
  90. /* close stderr */
  91. (void) fclose (child_stderr);
  92. printf ("</A>\n");
  93. return result;
  94. }
  95. void
  96. print_help (void)
  97. {
  98. print_revision (progname, revision);
  99. printf (_("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n"));
  100. printf (_(COPYRIGHT), copyright, email);
  101. printf (_("\n\
  102. This plugin wraps the text output of another command (plugin) in HTML\n\
  103. <A> tags, thus displaying the plugin output in as a clickable link in\n\
  104. the Nagios status screen. The return status is the same as the invoked\n\
  105. plugin.\n\n"));
  106. print_usage ();
  107. printf (_("\n\
  108. Pay close attention to quoting to ensure that the shell passes the expected\n\
  109. data to the plugin. For example, in:\n\
  110. \n\
  111. urlize http://example.com/ check_http -H example.com -r 'two words'\n\
  112. \n\
  113. the shell will remove the single quotes and urlize will see:\n\
  114. \n\
  115. urlize http://example.com/ check_http -H example.com -r two words\n\
  116. \n\
  117. You probably want:\n\
  118. \n\
  119. urlize http://example.com/ \"check_http -H example.com -r 'two words'\"\n"));
  120. printf (_(UT_SUPPORT));
  121. }
  122. void
  123. print_usage (void)
  124. {
  125. printf (_("Usage:\n %s <url> <plugin> <arg1> ... <argN>\n"), progname);
  126. }