1 /******************************************************************************
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 $Id$
19 ******************************************************************************/
21 const char *progname = "urlize";
22 const char *revision = "$Revision$";
23 const char *copyright = "2000-2004";
24 const char *email = "nagiosplug-devel@lists.sourceforge.net";
26 #include "common.h"
27 #include "utils.h"
28 #include "popen.h"
30 void print_help (void);
31 void print_usage (void);
33 int
34 main (int argc, char **argv)
35 {
36 int found = 0, result = STATE_UNKNOWN;
37 char *url = NULL;
38 char *cmd;
39 char *buf;
41 int c;
42 int option = 0;
43 static struct option longopts[] = {
44 {"help", no_argument, 0, 'h'},
45 {"version", no_argument, 0, 'V'},
46 {"url", required_argument, 0, 'u'},
47 {0, 0, 0, 0}
48 };
50 setlocale (LC_ALL, "");
51 bindtextdomain (PACKAGE, LOCALEDIR);
52 textdomain (PACKAGE);
54 while (1) {
55 c = getopt_long (argc, argv, "+hVu:", longopts, &option);
57 if (c == -1 || c == EOF)
58 break;
60 switch (c) {
61 case 'h': /* help */
62 print_help ();
63 exit (EXIT_SUCCESS);
64 break;
65 case 'V': /* version */
66 print_revision (progname, revision);
67 exit (EXIT_SUCCESS);
68 break;
69 case 'u':
70 url = strdup (argv[optind]);
71 break;
72 case '?':
73 default:
74 printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
75 print_usage ();
76 exit (STATE_UNKNOWN);
77 }
78 }
80 if (url == NULL)
81 url = strdup (argv[optind++]);
83 cmd = strdup (argv[optind++]);
84 for (c = optind; c < argc; c++) {
85 asprintf (&cmd, "%s %s", cmd, argv[c]);
86 }
88 child_process = spopen (cmd);
89 if (child_process == NULL) {
90 printf (_("Could not open pipe: %s\n"), cmd);
91 exit (STATE_UNKNOWN);
92 }
94 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
95 if (child_stderr == NULL) {
96 printf (_("Could not open stderr for %s\n"), cmd);
97 }
99 buf = malloc(MAX_INPUT_BUFFER);
100 printf ("<A href=\"%s\">", argv[1]);
101 while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
102 found++;
103 printf ("%s", buf);
104 }
106 if (!found)
107 die (STATE_UNKNOWN,
108 _("%s UNKNOWN - No data received from host\nCMD: %s</A>\n"),
109 argv[0], cmd);
111 /* close the pipe */
112 result = spclose (child_process);
114 /* WARNING if output found on stderr */
115 if (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr))
116 result = max_state (result, STATE_WARNING);
118 /* close stderr */
119 (void) fclose (child_stderr);
121 printf ("</A>\n");
122 return result;
123 }
127 void
128 print_help (void)
129 {
130 print_revision (progname, revision);
132 printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
133 printf (COPYRIGHT, copyright, email);
135 printf (_("\n\
136 This 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"));
141 print_usage ();
143 printf (_("\n\
144 Pay close attention to quoting to ensure that the shell passes the expected\n\
145 data to the plugin. For example, in:\n\
146 \n\
147 urlize http://example.com/ check_http -H example.com -r 'two words'\n\
148 \n\
149 the shell will remove the single quotes and urlize will see:\n\
150 \n\
151 urlize http://example.com/ check_http -H example.com -r two words\n\
152 \n\
153 You probably want:\n\
154 \n\
155 urlize http://example.com/ \"check_http -H example.com -r 'two words'\"\n"));
157 printf (_(UT_SUPPORT));
158 }
162 void
163 print_usage (void)
164 {
165 printf ("Usage:\n %s <url> <plugin> <arg1> ... <argN>\n", progname);
166 }