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-2006";
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 /* Need at least 2 args */
60 if (argc < 3) {
61 print_help();
62 exit (STATE_UNKNOWN);
63 }
65 while (1) {
66 c = getopt_long (argc, argv, "+hVu:", longopts, &option);
68 if (c == -1 || c == EOF)
69 break;
71 switch (c) {
72 case 'h': /* help */
73 print_help ();
74 exit (EXIT_SUCCESS);
75 break;
76 case 'V': /* version */
77 print_revision (progname, revision);
78 exit (EXIT_SUCCESS);
79 break;
80 case 'u':
81 url = strdup (argv[optind]);
82 break;
83 case '?':
84 default:
85 usage2 (_("Unknown argument"), optarg);
86 }
87 }
89 if (url == NULL)
90 url = strdup (argv[optind++]);
92 cmd = strdup (argv[optind++]);
93 for (c = optind; c < argc; c++) {
94 asprintf (&cmd, "%s %s", cmd, argv[c]);
95 }
97 child_process = spopen (cmd);
98 if (child_process == NULL) {
99 printf (_("Could not open pipe: %s\n"), cmd);
100 exit (STATE_UNKNOWN);
101 }
103 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
104 if (child_stderr == NULL) {
105 printf (_("Could not open stderr for %s\n"), cmd);
106 }
108 bzero(tstr, sizeof(tstr));
109 buf = malloc(MAX_INPUT_BUFFER);
110 printf ("<A href=\"%s\">", argv[1]);
111 while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
112 found++;
113 /* Collect the string in temp str so we can tokenize */
114 strcat(tstr, buf);
115 }
117 if (!found)
118 die (STATE_UNKNOWN,
119 _("%s UNKNOWN - No data received from host\nCMD: %s</A>\n"),
120 argv[0], cmd);
123 /* chop the newline character */
124 if ((nstr = strchr(tstr, NEWLINE_CHARACTER)) != NULL)
125 *nstr = '\0';
127 /* tokenize the string for Perfdata if there is some */
128 nstr = strtok(tstr, PERF_CHARACTER);
129 printf ("%s", nstr);
130 printf ("</A>");
131 nstr = strtok(NULL, PERF_CHARACTER);
132 if (nstr != NULL)
133 printf (" | %s", nstr);
135 /* close the pipe */
136 result = spclose (child_process);
138 /* WARNING if output found on stderr */
139 if (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr))
140 result = max_state (result, STATE_WARNING);
142 /* close stderr */
143 (void) fclose (child_stderr);
145 return result;
146 }
150 void
151 print_help (void)
152 {
153 print_revision (progname, revision);
155 printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
156 printf (COPYRIGHT, copyright, email);
158 printf ("%s\n", _("This plugin wraps the text output of another command (plugin)"));
159 printf ("%s\n", _("in HTML <A> tags, thus displaying the plugin output in as a clickable link in"));
160 printf ("%s\n", _("the Nagios status screen. The return status is the same as the invoked plugin."));
162 printf ("\n\n");
164 print_usage ();
166 printf (_(UT_HELP_VRSN));
168 printf ("\n");
169 printf ("%s\n", _("Examples:"));
170 printf ("%s\n", _("Pay close attention to quoting to ensure that the shell passes the expected"));
171 printf ("%s\n\n", _("data to the plugin. For example, in:"));
172 printf (" %s\n\n", _("urlize http://example.com/ check_http -H example.com -r 'two words'"));
173 printf (" %s\n", _("the shell will remove the single quotes and urlize will see:"));
174 printf (" %s\n\n", _("urlize http://example.com/ check_http -H example.com -r two words"));
175 printf (" %s\n\n", _("You probably want:"));
176 printf (" %s\n", _("urlize http://example.com/ \"check_http -H example.com -r 'two words'\""));
178 printf (_(UT_SUPPORT));
179 }
183 void
184 print_usage (void)
185 {
186 printf (_("Usage:"));
187 printf ("%s <url> <plugin> <arg1> ... <argN>\n", progname);
188 }