urlize.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /******************************************************************************
  2. *
  3. * urlize.c
  4. *
  5. * Program: plugin wrapper for Nagios
  6. * License: GPL
  7. * Copyright (c) 2000 Karl DeBisschop (kdebiss@alum.mit.edu)
  8. *
  9. * Last Modified: $Date$
  10. * 2000-06-01 Karl DeBisschop <karl@debisschop.net>
  11. * Written based of concept in urlize.pl
  12. *
  13. * Usage: urlize <url> <plugin> <arg1> ... <argN>
  14. *
  15. * Description:
  16. *
  17. * This plugin wraps the text output of another command (plugin) in HTML
  18. * <A> tags, thus displaying the plugin output in as a clickable link in
  19. * the Nagios status screen. The return status is the same as the plugin
  20. * invoked by urlize
  21. *
  22. * License Information:
  23. *
  24. * This program is free software; you can redistribute it and/or modify
  25. * it under the terms of the GNU General Public License as published by
  26. * the Free Software Foundation; either version 2 of the License, or
  27. * (at your option) any later version.
  28. *
  29. * This program is distributed in the hope that it will be useful,
  30. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. * GNU General Public License for more details.
  33. *
  34. * You should have received a copy of the GNU General Public License
  35. * along with this program; if not, write to the Free Software
  36. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  37. *
  38. *****************************************************************************/
  39. const char *progname = "urlize";
  40. const char *revision = "$Revision$";
  41. const char *copyright = "2000-2003";
  42. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  43. #include "common.h"
  44. #include "utils.h"
  45. #include "popen.h"
  46. void
  47. print_usage (void)
  48. {
  49. printf (_("Usage:\n %s <url> <plugin> <arg1> ... <argN>\n"), progname);
  50. }
  51. void
  52. print_help (void)
  53. {
  54. print_revision (progname, revision);
  55. printf (_("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n"));
  56. printf (_(COPYRIGHT), copyright, email);
  57. printf (_("\n\
  58. This plugin wraps the text output of another command (plugin) in HTML\n\
  59. <A> tags, thus displaying the plugin output in as a clickable link in\n\
  60. the Nagios status screen. The return status is the same as the invoked\n\
  61. plugin.\n\n"));
  62. print_usage ();
  63. printf (_("\n\
  64. Pay close attention to quoting to ensure that the shell passes the expected\n\
  65. data to the plugin. For example, in:\n\
  66. \n\
  67. urlize http://example.com/ check_http -H example.com -r 'two words'\n\
  68. \n\
  69. the shell will remove the single quotes and urlize will see:\n\
  70. \n\
  71. urlize http://example.com/ check_http -H example.com -r two words\n\
  72. \n\
  73. You probably want:\n\
  74. \n\
  75. urlize http://example.com/ \"check_http -H example.com -r 'two words'\"\n"));
  76. exit (STATE_OK);
  77. }
  78. int
  79. main (int argc, char **argv)
  80. {
  81. int i = 0, found = 0, result = STATE_UNKNOWN;
  82. char *cmd = NULL;
  83. char input_buffer[MAX_INPUT_BUFFER];
  84. if (argc < 2) {
  85. print_usage ();
  86. exit (STATE_UNKNOWN);
  87. }
  88. if (!strcmp (argv[1], "-h") || !strcmp (argv[1], "--help")) {
  89. print_help ();
  90. exit (STATE_OK);
  91. }
  92. if (!strcmp (argv[1], "-V") || !strcmp (argv[1], "--version")) {
  93. print_revision (progname, revision);
  94. exit (STATE_OK);
  95. }
  96. if (argc < 2) {
  97. print_usage ();
  98. exit (STATE_UNKNOWN);
  99. }
  100. asprintf (&cmd, "%s", argv[2]);
  101. for (i = 3; i < argc; i++) {
  102. asprintf (&cmd, "%s %s", cmd, argv[i]);
  103. }
  104. child_process = spopen (cmd);
  105. if (child_process == NULL) {
  106. printf (_("Could not open pipe: %s\n"), cmd);
  107. exit (STATE_UNKNOWN);
  108. }
  109. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  110. if (child_stderr == NULL) {
  111. printf (_("Could not open stderr for %s\n"), cmd);
  112. }
  113. printf ("<A href=\"%s\">", argv[1]);
  114. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  115. found++;
  116. if (index (input_buffer, '\n')) {
  117. input_buffer[strcspn (input_buffer, "\n")] = 0;
  118. printf ("%s", input_buffer);
  119. }
  120. else {
  121. printf ("%s", input_buffer);
  122. }
  123. }
  124. if (!found) {
  125. printf (_("%s problem - No data recieved from host\nCMD: %s</A>\n"), argv[0],
  126. cmd);
  127. exit (STATE_UNKNOWN);
  128. }
  129. /* close the pipe */
  130. result = spclose (child_process);
  131. /* WARNING if output found on stderr */
  132. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  133. result = max_state (result, STATE_WARNING);
  134. /* close stderr */
  135. (void) fclose (child_stderr);
  136. printf ("</A>\n");
  137. return result;
  138. }