urlize.c 4.8 KB

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