urlize.c 5.2 KB

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