urlize.c 4.0 KB

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