urlize.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*****************************************************************************
  2. *
  3. * Monitoring urlize plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2000-2007 Monitoring Plugins Development Team
  7. *
  8. * Description:
  9. *
  10. * This file contains the urlize plugin
  11. *
  12. * This plugin wraps the text output of another command (plugin) in HTML <A>
  13. * tags. This plugin returns the status of the invoked plugin.
  14. *
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation, either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. *
  29. *
  30. *****************************************************************************/
  31. const char *progname = "urlize";
  32. const char *copyright = "2000-2006";
  33. const char *email = "devel@monitoring-plugins.org";
  34. #include "common.h"
  35. #include "utils.h"
  36. #include "popen.h"
  37. #define PERF_CHARACTER "|"
  38. #define NEWLINE_CHARACTER '\n'
  39. void print_help (void);
  40. void print_usage (void);
  41. int
  42. main (int argc, char **argv)
  43. {
  44. int found = 0, result = STATE_UNKNOWN;
  45. char *url = NULL;
  46. char *cmd;
  47. char *buf;
  48. char *nstr;
  49. char tstr[MAX_INPUT_BUFFER];
  50. int c;
  51. int option = 0;
  52. static struct option longopts[] = {
  53. {"help", no_argument, 0, 'h'},
  54. {"version", no_argument, 0, 'V'},
  55. {"url", required_argument, 0, 'u'},
  56. {0, 0, 0, 0}
  57. };
  58. setlocale (LC_ALL, "");
  59. bindtextdomain (PACKAGE, LOCALEDIR);
  60. textdomain (PACKAGE);
  61. /* Need at least 2 args */
  62. if (argc < 3) {
  63. print_help();
  64. exit (STATE_UNKNOWN);
  65. }
  66. while (1) {
  67. c = getopt_long (argc, argv, "+hVu:", longopts, &option);
  68. if (c == -1 || c == EOF)
  69. break;
  70. switch (c) {
  71. case 'h': /* help */
  72. print_help ();
  73. exit (EXIT_SUCCESS);
  74. break;
  75. case 'V': /* version */
  76. print_revision (progname, NP_VERSION);
  77. exit (EXIT_SUCCESS);
  78. break;
  79. case 'u':
  80. url = strdup (argv[optind]);
  81. break;
  82. case '?':
  83. default:
  84. usage5 ();
  85. }
  86. }
  87. if (url == NULL)
  88. url = strdup (argv[optind++]);
  89. cmd = strdup (argv[optind++]);
  90. for (c = optind; c < argc; c++) {
  91. xasprintf (&cmd, "%s %s", cmd, argv[c]);
  92. }
  93. child_process = spopen (cmd);
  94. if (child_process == NULL) {
  95. printf (_("Could not open pipe: %s\n"), cmd);
  96. exit (STATE_UNKNOWN);
  97. }
  98. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  99. if (child_stderr == NULL) {
  100. printf (_("Could not open stderr for %s\n"), cmd);
  101. }
  102. bzero(tstr, sizeof(tstr));
  103. buf = malloc(MAX_INPUT_BUFFER);
  104. printf ("<A href=\"%s\">", argv[1]);
  105. while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
  106. found++;
  107. /* Collect the string in temp str so we can tokenize */
  108. strcat(tstr, buf);
  109. }
  110. if (!found)
  111. die (STATE_UNKNOWN,
  112. _("%s UNKNOWN - No data received from host\nCMD: %s</A>\n"),
  113. argv[0], cmd);
  114. /* chop the newline character */
  115. if ((nstr = strchr(tstr, NEWLINE_CHARACTER)) != NULL)
  116. *nstr = '\0';
  117. /* tokenize the string for Perfdata if there is some */
  118. nstr = strtok(tstr, PERF_CHARACTER);
  119. printf ("%s", nstr);
  120. printf ("</A>");
  121. nstr = strtok(NULL, PERF_CHARACTER);
  122. if (nstr != NULL)
  123. printf (" | %s", nstr);
  124. /* close the pipe */
  125. result = spclose (child_process);
  126. /* WARNING if output found on stderr */
  127. if (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr))
  128. result = max_state (result, STATE_WARNING);
  129. /* close stderr */
  130. (void) fclose (child_stderr);
  131. return result;
  132. }
  133. void
  134. print_help (void)
  135. {
  136. print_revision (progname, NP_VERSION);
  137. printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
  138. printf (COPYRIGHT, copyright, email);
  139. printf ("%s\n", _("This plugin wraps the text output of another command (plugin) in HTML <A>"));
  140. printf ("%s\n", _("tags, thus displaying the child plugin's output as a clickable link in compatible"));
  141. printf ("%s\n", _("monitoring status screen. This plugin returns the status of the invoked plugin."));
  142. printf ("\n\n");
  143. print_usage ();
  144. printf (UT_HELP_VRSN);
  145. printf ("\n");
  146. printf ("%s\n", _("Examples:"));
  147. printf ("%s\n", _("Pay close attention to quoting to ensure that the shell passes the expected"));
  148. printf ("%s\n\n", _("data to the plugin. For example, in:"));
  149. printf (" %s\n\n", _("urlize http://example.com/ check_http -H example.com -r 'two words'"));
  150. printf (" %s\n", _("the shell will remove the single quotes and urlize will see:"));
  151. printf (" %s\n\n", _("urlize http://example.com/ check_http -H example.com -r two words"));
  152. printf (" %s\n\n", _("You probably want:"));
  153. printf (" %s\n", _("urlize http://example.com/ \"check_http -H example.com -r 'two words'\""));
  154. printf (UT_SUPPORT);
  155. }
  156. void
  157. print_usage (void)
  158. {
  159. printf ("%s\n", _("Usage:"));
  160. printf ("%s <url> <plugin> <arg1> ... <argN>\n", progname);
  161. }