urlize.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. $Id$
  14. ******************************************************************************/
  15. const char *progname = "urlize";
  16. const char *revision = "$Revision$";
  17. const char *copyright = "2000-2004";
  18. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  19. #include "common.h"
  20. #include "utils.h"
  21. #include "popen.h"
  22. void print_help (void);
  23. void print_usage (void);
  24. int
  25. main (int argc, char **argv)
  26. {
  27. int found = 0, result = STATE_UNKNOWN;
  28. char *url = NULL;
  29. char *cmd;
  30. char *buf;
  31. int c;
  32. int option = 0;
  33. static struct option longopts[] = {
  34. {"help", no_argument, 0, 'h'},
  35. {"version", no_argument, 0, 'V'},
  36. {"url", required_argument, 0, 'u'},
  37. {0, 0, 0, 0}
  38. };
  39. setlocale (LC_ALL, "");
  40. bindtextdomain (PACKAGE, LOCALEDIR);
  41. textdomain (PACKAGE);
  42. while (1) {
  43. c = getopt_long (argc, argv, "+hVu:", longopts, &option);
  44. if (c == -1 || c == EOF)
  45. break;
  46. switch (c) {
  47. case 'h': /* help */
  48. print_help ();
  49. exit (EXIT_SUCCESS);
  50. break;
  51. case 'V': /* version */
  52. print_revision (progname, revision);
  53. exit (EXIT_SUCCESS);
  54. break;
  55. case 'u':
  56. url = strdup (argv[optind]);
  57. break;
  58. case '?':
  59. default:
  60. printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
  61. print_usage ();
  62. exit (STATE_UNKNOWN);
  63. }
  64. }
  65. if (url == NULL)
  66. url = strdup (argv[optind++]);
  67. cmd = strdup (argv[optind++]);
  68. for (c = optind; c < argc; c++) {
  69. asprintf (&cmd, "%s %s", cmd, argv[c]);
  70. }
  71. child_process = spopen (cmd);
  72. if (child_process == NULL) {
  73. printf (_("Could not open pipe: %s\n"), cmd);
  74. exit (STATE_UNKNOWN);
  75. }
  76. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  77. if (child_stderr == NULL) {
  78. printf (_("Could not open stderr for %s\n"), cmd);
  79. }
  80. buf = malloc(MAX_INPUT_BUFFER);
  81. printf ("<A href=\"%s\">", argv[1]);
  82. while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
  83. found++;
  84. printf ("%s", buf);
  85. }
  86. if (!found)
  87. die (STATE_UNKNOWN,
  88. _("%s UNKNOWN - No data received from host\nCMD: %s</A>\n"),
  89. argv[0], cmd);
  90. /* close the pipe */
  91. result = spclose (child_process);
  92. /* WARNING if output found on stderr */
  93. if (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr))
  94. result = max_state (result, STATE_WARNING);
  95. /* close stderr */
  96. (void) fclose (child_stderr);
  97. printf ("</A>\n");
  98. return result;
  99. }
  100. void
  101. print_help (void)
  102. {
  103. print_revision (progname, revision);
  104. printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
  105. printf (COPYRIGHT, copyright, email);
  106. printf (_("\n\
  107. This plugin wraps the text output of another command (plugin) in HTML\n\
  108. <A> tags, thus displaying the plugin output in as a clickable link in\n\
  109. the Nagios status screen. The return status is the same as the invoked\n\
  110. plugin.\n\n"));
  111. print_usage ();
  112. printf (_("\n\
  113. Pay close attention to quoting to ensure that the shell passes the expected\n\
  114. data to the plugin. For example, in:\n\
  115. \n\
  116. urlize http://example.com/ check_http -H example.com -r 'two words'\n\
  117. \n\
  118. the shell will remove the single quotes and urlize will see:\n\
  119. \n\
  120. urlize http://example.com/ check_http -H example.com -r two words\n\
  121. \n\
  122. You probably want:\n\
  123. \n\
  124. urlize http://example.com/ \"check_http -H example.com -r 'two words'\"\n"));
  125. printf (_(UT_SUPPORT));
  126. }
  127. void
  128. print_usage (void)
  129. {
  130. printf ("Usage:\n %s <url> <plugin> <arg1> ... <argN>\n", progname);
  131. }