urlize.c 5.2 KB

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