urlize.c 4.5 KB

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