Fix INADDR_NONE value (for systems which don't define it).
[monitoring-plugins.git] / plugins / urlize.c
blob612dd14daf7ed6ee87b4ce6f8e233756d12253e3
1 /******************************************************************************
3 * Nagios urlize plugin
5 * License: GPL
6 * Copyright (c) 2000-2006 nagios-plugins team
8 * Last Modified: $Date$
10 * Description:
12 * This file contains the urlize plugin
14 * This plugin wraps the text output of another command (plugin)
15 * in HTML <A> tags, thus displaying the plugin output in as a clickable link in
16 * the Nagios status screen. The return status is the same as the invoked plugin.
18 * License Information:
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 $Id$
37 ******************************************************************************/
39 const char *progname = "urlize";
40 const char *revision = "$Revision$";
41 const char *copyright = "2000-2006";
42 const char *email = "nagiosplug-devel@lists.sourceforge.net";
44 #include "common.h"
45 #include "utils.h"
46 #include "popen.h"
48 #define PERF_CHARACTER "|"
49 #define NEWLINE_CHARACTER '\n'
51 void print_help (void);
52 void print_usage (void);
54 int
55 main (int argc, char **argv)
57 int found = 0, result = STATE_UNKNOWN;
58 char *url = NULL;
59 char *cmd;
60 char *buf;
61 char *nstr;
62 char tstr[MAX_INPUT_BUFFER];
64 int c;
65 int option = 0;
66 static struct option longopts[] = {
67 {"help", no_argument, 0, 'h'},
68 {"version", no_argument, 0, 'V'},
69 {"url", required_argument, 0, 'u'},
70 {0, 0, 0, 0}
73 setlocale (LC_ALL, "");
74 bindtextdomain (PACKAGE, LOCALEDIR);
75 textdomain (PACKAGE);
77 /* Need at least 2 args */
78 if (argc < 3) {
79 print_help();
80 exit (STATE_UNKNOWN);
83 while (1) {
84 c = getopt_long (argc, argv, "+hVu:", longopts, &option);
86 if (c == -1 || c == EOF)
87 break;
89 switch (c) {
90 case 'h': /* help */
91 print_help ();
92 exit (EXIT_SUCCESS);
93 break;
94 case 'V': /* version */
95 print_revision (progname, revision);
96 exit (EXIT_SUCCESS);
97 break;
98 case 'u':
99 url = strdup (argv[optind]);
100 break;
101 case '?':
102 default:
103 usage5 ();
107 if (url == NULL)
108 url = strdup (argv[optind++]);
110 cmd = strdup (argv[optind++]);
111 for (c = optind; c < argc; c++) {
112 asprintf (&cmd, "%s %s", cmd, argv[c]);
115 child_process = spopen (cmd);
116 if (child_process == NULL) {
117 printf (_("Could not open pipe: %s\n"), cmd);
118 exit (STATE_UNKNOWN);
121 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
122 if (child_stderr == NULL) {
123 printf (_("Could not open stderr for %s\n"), cmd);
126 bzero(tstr, sizeof(tstr));
127 buf = malloc(MAX_INPUT_BUFFER);
128 printf ("<A href=\"%s\">", argv[1]);
129 while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
130 found++;
131 /* Collect the string in temp str so we can tokenize */
132 strcat(tstr, buf);
135 if (!found)
136 die (STATE_UNKNOWN,
137 _("%s UNKNOWN - No data received from host\nCMD: %s</A>\n"),
138 argv[0], cmd);
141 /* chop the newline character */
142 if ((nstr = strchr(tstr, NEWLINE_CHARACTER)) != NULL)
143 *nstr = '\0';
145 /* tokenize the string for Perfdata if there is some */
146 nstr = strtok(tstr, PERF_CHARACTER);
147 printf ("%s", nstr);
148 printf ("</A>");
149 nstr = strtok(NULL, PERF_CHARACTER);
150 if (nstr != NULL)
151 printf (" | %s", nstr);
153 /* close the pipe */
154 result = spclose (child_process);
156 /* WARNING if output found on stderr */
157 if (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr))
158 result = max_state (result, STATE_WARNING);
160 /* close stderr */
161 (void) fclose (child_stderr);
163 return result;
168 void
169 print_help (void)
171 print_revision (progname, revision);
173 printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
174 printf (COPYRIGHT, copyright, email);
176 printf ("%s\n", _("This plugin wraps the text output of another command (plugin)"));
177 printf ("%s\n", _("in HTML <A> tags, thus displaying the plugin output in as a clickable link in"));
178 printf ("%s\n", _("the Nagios status screen. The return status is the same as the invoked plugin."));
180 printf ("\n\n");
182 print_usage ();
184 printf (_(UT_HELP_VRSN));
186 printf ("\n");
187 printf ("%s\n", _("Examples:"));
188 printf ("%s\n", _("Pay close attention to quoting to ensure that the shell passes the expected"));
189 printf ("%s\n\n", _("data to the plugin. For example, in:"));
190 printf (" %s\n\n", _("urlize http://example.com/ check_http -H example.com -r 'two words'"));
191 printf (" %s\n", _("the shell will remove the single quotes and urlize will see:"));
192 printf (" %s\n\n", _("urlize http://example.com/ check_http -H example.com -r two words"));
193 printf (" %s\n\n", _("You probably want:"));
194 printf (" %s\n", _("urlize http://example.com/ \"check_http -H example.com -r 'two words'\""));
196 printf (_(UT_SUPPORT));
201 void
202 print_usage (void)
204 printf (_("Usage:"));
205 printf ("%s <url> <plugin> <arg1> ... <argN>\n", progname);