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 usage2 (_("Unknown argument"), optarg);
75 }
76 }
78 if (url == NULL)
79 url = strdup (argv[optind++]);
81 cmd = strdup (argv[optind++]);
82 for (c = optind; c < argc; c++) {
83 asprintf (&cmd, "%s %s", cmd, argv[c]);
84 }
86 child_process = spopen (cmd);
87 if (child_process == NULL) {
88 printf (_("Could not open pipe: %s\n"), cmd);
89 exit (STATE_UNKNOWN);
90 }
92 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
93 if (child_stderr == NULL) {
94 printf (_("Could not open stderr for %s\n"), cmd);
95 }
97 buf = malloc(MAX_INPUT_BUFFER);
98 printf ("<A href=\"%s\">", argv[1]);
99 while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
100 found++;
101 printf ("%s", buf);
102 }
104 if (!found)
105 die (STATE_UNKNOWN,
106 _("%s UNKNOWN - No data received from host\nCMD: %s</A>\n"),
107 argv[0], cmd);
109 /* close the pipe */
110 result = spclose (child_process);
112 /* WARNING if output found on stderr */
113 if (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr))
114 result = max_state (result, STATE_WARNING);
116 /* close stderr */
117 (void) fclose (child_stderr);
119 printf ("</A>\n");
120 return result;
121 }
125 void
126 print_help (void)
127 {
128 print_revision (progname, revision);
130 printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
131 printf (COPYRIGHT, copyright, email);
133 printf (_("\n\
134 This plugin wraps the text output of another command (plugin) in HTML\n\
135 <A> tags, thus displaying the plugin output in as a clickable link in\n\
136 the Nagios status screen. The return status is the same as the invoked\n\
137 plugin.\n\n"));
139 print_usage ();
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"));
155 printf (_(UT_SUPPORT));
156 }
160 void
161 print_usage (void)
162 {
163 printf ("Usage:\n %s <url> <plugin> <arg1> ... <argN>\n", progname);
164 }