urlize.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #include "common.h"
  40. #include "utils.h"
  41. #include "popen.h"
  42. void print_usage (char *);
  43. void print_help (char *);
  44. int
  45. main (int argc, char **argv)
  46. {
  47. int i = 0, found = 0, result = STATE_UNKNOWN;
  48. char *cmd = NULL;
  49. char input_buffer[MAX_INPUT_BUFFER];
  50. if (argc < 2) {
  51. print_usage (my_basename (argv[0]));
  52. exit (STATE_UNKNOWN);
  53. }
  54. if (!strcmp (argv[1], "-h") || !strcmp (argv[1], "--help")) {
  55. print_help (argv[0]);
  56. exit (STATE_OK);
  57. }
  58. if (!strcmp (argv[1], "-V") || !strcmp (argv[1], "--version")) {
  59. print_revision (my_basename (argv[0]), "$Revision$");
  60. exit (STATE_OK);
  61. }
  62. if (argc < 2) {
  63. print_usage (my_basename (argv[0]));
  64. exit (STATE_UNKNOWN);
  65. }
  66. cmd = ssprintf (cmd, "%s", argv[2]);
  67. for (i = 3; i < argc; i++) {
  68. cmd = ssprintf (cmd, "%s %s", cmd, argv[i]);
  69. }
  70. child_process = spopen (cmd);
  71. if (child_process == NULL) {
  72. printf ("Could not open pipe: %s\n", cmd);
  73. exit (STATE_UNKNOWN);
  74. }
  75. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  76. if (child_stderr == NULL) {
  77. printf ("Could not open stderr for %s\n", cmd);
  78. }
  79. printf ("<A href=\"%s\">", argv[1]);
  80. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  81. found++;
  82. if (index (input_buffer, '\n')) {
  83. input_buffer[strcspn (input_buffer, "\n")] = 0;
  84. printf ("%s", input_buffer);
  85. }
  86. else {
  87. printf ("%s", input_buffer);
  88. }
  89. }
  90. if (!found) {
  91. printf ("%s problem - No data recieved from host\nCMD: %s\n", argv[0],
  92. cmd);
  93. exit (STATE_UNKNOWN);
  94. }
  95. /* close the pipe */
  96. result = spclose (child_process);
  97. /* WARNING if output found on stderr */
  98. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  99. result = max_state (result, STATE_WARNING);
  100. /* close stderr */
  101. (void) fclose (child_stderr);
  102. printf ("</A>\n");
  103. return result;
  104. }
  105. void
  106. print_usage (char *cmd)
  107. {
  108. printf ("Usage:\n %s <url> <plugin> <arg1> ... <argN>\n",
  109. my_basename (cmd));
  110. }
  111. void
  112. print_help (char *cmd)
  113. {
  114. print_revision ("urlize", "$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. exit (STATE_OK);
  123. }