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