urlize.c 5.1 KB

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