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