urlize.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #include "common.h"
  41. #include "utils.h"
  42. #include "popen.h"
  43. void print_usage (const char *);
  44. void print_help (const char *);
  45. int
  46. main (int argc, char **argv)
  47. {
  48. int i = 0, found = 0, result = STATE_UNKNOWN;
  49. char *cmd = NULL;
  50. char input_buffer[MAX_INPUT_BUFFER];
  51. if (argc < 2) {
  52. print_usage (progname);
  53. exit (STATE_UNKNOWN);
  54. }
  55. if (!strcmp (argv[1], "-h") || !strcmp (argv[1], "--help")) {
  56. print_help (argv[0]);
  57. exit (STATE_OK);
  58. }
  59. if (!strcmp (argv[1], "-V") || !strcmp (argv[1], "--version")) {
  60. print_revision (progname, "$Revision$");
  61. exit (STATE_OK);
  62. }
  63. if (argc < 2) {
  64. print_usage (progname);
  65. exit (STATE_UNKNOWN);
  66. }
  67. asprintf (&cmd, "%s", argv[2]);
  68. for (i = 3; i < argc; i++) {
  69. asprintf (&cmd, "%s %s", cmd, argv[i]);
  70. }
  71. child_process = spopen (cmd);
  72. if (child_process == NULL) {
  73. printf ("Could not open pipe: %s\n", cmd);
  74. exit (STATE_UNKNOWN);
  75. }
  76. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  77. if (child_stderr == NULL) {
  78. printf ("Could not open stderr for %s\n", cmd);
  79. }
  80. printf ("<A href=\"%s\">", argv[1]);
  81. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  82. found++;
  83. if (index (input_buffer, '\n')) {
  84. input_buffer[strcspn (input_buffer, "\n")] = 0;
  85. printf ("%s", input_buffer);
  86. }
  87. else {
  88. printf ("%s", input_buffer);
  89. }
  90. }
  91. if (!found) {
  92. printf ("%s problem - No data recieved from host\nCMD: %s</A>\n", argv[0],
  93. cmd);
  94. exit (STATE_UNKNOWN);
  95. }
  96. /* close the pipe */
  97. result = spclose (child_process);
  98. /* WARNING if output found on stderr */
  99. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  100. result = max_state (result, STATE_WARNING);
  101. /* close stderr */
  102. (void) fclose (child_stderr);
  103. printf ("</A>\n");
  104. return result;
  105. }
  106. void
  107. print_usage (const char *cmd)
  108. {
  109. printf ("Usage:\n %s <url> <plugin> <arg1> ... <argN>\n", cmd);
  110. }
  111. void
  112. print_help (const char *cmd)
  113. {
  114. print_revision (progname, "$Revision$");
  115. printf ("\
  116. Copyright (c) 2000 Karl DeBisschop (kdebiss@alum.mit.edu)\n\n\
  117. \nThis plugin wraps the text output of another command (plugin) in HTML\n\
  118. <A> tags, thus displaying the plugin output in as a clickable link in\n\
  119. the Nagios status screen. The return status is the same as the invoked\n\
  120. plugin.\n\n");
  121. print_usage (cmd);
  122. printf ("\n\
  123. Pay close attention to quoting to ensure that the shell passes the expected\n\
  124. data to the plugin. For example, in:\n\
  125. \n\
  126. urlize http://example.com/ check_http -H example.com -r 'two words'\n\
  127. \n\
  128. the shell will remove the single quotes and urlize will see:\n\
  129. \n\
  130. urlize http://example.com/ check_http -H example.com -r two words\n\
  131. \n\
  132. You probably want:\n\
  133. \n\
  134. urlize http://example.com/ \"check_http -H example.com -r 'two words'\"\n");
  135. exit (STATE_OK);
  136. }