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 #define PERF_CHARACTER "|"
31 #define NEWLINE_CHARACTER '\n'
33 void print_help (void);
34 void print_usage (void);
36 int
37 main (int argc, char **argv)
38 {
39 int found = 0, result = STATE_UNKNOWN;
40 char *url = NULL;
41 char *cmd;
42 char *buf;
43 char *nstr;
44 char tstr[MAX_INPUT_BUFFER];
46 int c;
47 int option = 0;
48 static struct option longopts[] = {
49 {"help", no_argument, 0, 'h'},
50 {"version", no_argument, 0, 'V'},
51 {"url", required_argument, 0, 'u'},
52 {0, 0, 0, 0}
53 };
55 setlocale (LC_ALL, "");
56 bindtextdomain (PACKAGE, LOCALEDIR);
57 textdomain (PACKAGE);
59 while (1) {
60 c = getopt_long (argc, argv, "+hVu:", longopts, &option);
62 if (c == -1 || c == EOF)
63 break;
65 switch (c) {
66 case 'h': /* help */
67 print_help ();
68 exit (EXIT_SUCCESS);
69 break;
70 case 'V': /* version */
71 print_revision (progname, revision);
72 exit (EXIT_SUCCESS);
73 break;
74 case 'u':
75 url = strdup (argv[optind]);
76 break;
77 case '?':
78 default:
79 usage2 (_("Unknown argument"), optarg);
80 }
81 }
83 if (url == NULL)
84 url = strdup (argv[optind++]);
86 cmd = strdup (argv[optind++]);
87 for (c = optind; c < argc; c++) {
88 asprintf (&cmd, "%s %s", cmd, argv[c]);
89 }
91 child_process = spopen (cmd);
92 if (child_process == NULL) {
93 printf (_("Could not open pipe: %s\n"), cmd);
94 exit (STATE_UNKNOWN);
95 }
97 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
98 if (child_stderr == NULL) {
99 printf (_("Could not open stderr for %s\n"), cmd);
100 }
102 bzero(tstr, sizeof(tstr));
103 buf = malloc(MAX_INPUT_BUFFER);
104 printf ("<A href=\"%s\">", argv[1]);
105 while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
106 found++;
107 /* Collect the string in temp str so we can tokenize */
108 strcat(tstr, buf);
109 }
111 if (!found)
112 die (STATE_UNKNOWN,
113 _("%s UNKNOWN - No data received from host\nCMD: %s</A>\n"),
114 argv[0], cmd);
117 /* chop the newline character */
118 if ((nstr = strchr(tstr, NEWLINE_CHARACTER)) != NULL)
119 *nstr = '\0';
121 /* tokenize the string for Perfdata if there is some */
122 nstr = strtok(tstr, PERF_CHARACTER);
123 printf ("%s", nstr);
124 printf ("</A>");
125 nstr = strtok(NULL, PERF_CHARACTER);
126 if (nstr != NULL)
127 printf (" | %s", nstr);
129 /* close the pipe */
130 result = spclose (child_process);
132 /* WARNING if output found on stderr */
133 if (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr))
134 result = max_state (result, STATE_WARNING);
136 /* close stderr */
137 (void) fclose (child_stderr);
139 return result;
140 }
144 void
145 print_help (void)
146 {
147 print_revision (progname, revision);
149 printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
150 printf (COPYRIGHT, copyright, email);
152 printf (_("\n\
153 This plugin wraps the text output of another command (plugin) in HTML\n\
154 <A> tags, thus displaying the plugin output in as a clickable link in\n\
155 the Nagios status screen. The return status is the same as the invoked\n\
156 plugin.\n\n"));
158 print_usage ();
160 printf (_("\n\
161 Pay close attention to quoting to ensure that the shell passes the expected\n\
162 data to the plugin. For example, in:\n\
163 \n\
164 urlize http://example.com/ check_http -H example.com -r 'two words'\n\
165 \n\
166 the shell will remove the single quotes and urlize will see:\n\
167 \n\
168 urlize http://example.com/ check_http -H example.com -r two words\n\
169 \n\
170 You probably want:\n\
171 \n\
172 urlize http://example.com/ \"check_http -H example.com -r 'two words'\"\n"));
174 printf (_(UT_SUPPORT));
175 }
179 void
180 print_usage (void)
181 {
182 printf ("Usage:\n %s <url> <plugin> <arg1> ... <argN>\n", progname);
183 }