check_procs: ignore plugin parent process
[monitoring-plugins.git] / plugins / urlize.c
blob30b2a35333771cc266e01d8bb879a7deed933b61
1 /*****************************************************************************
2 *
3 * Nagios urlize plugin
4 *
5 * License: GPL
6 * Copyright (c) 2000-2007 Nagios Plugins Development Team
7 *
8 * Description:
9 *
10 * This file contains the urlize plugin
12 * This plugin wraps the text output of another command (plugin) in HTML <A>
13 * tags, thus displaying the child plugin's output as a clickable link in the
14 * Nagios status screen. This plugin returns the status of the invoked plugin.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 *****************************************************************************/
33 const char *progname = "urlize";
34 const char *copyright = "2000-2006";
35 const char *email = "nagiosplug-devel@lists.sourceforge.net";
37 #include "common.h"
38 #include "utils.h"
39 #include "popen.h"
41 #define PERF_CHARACTER "|"
42 #define NEWLINE_CHARACTER '\n'
44 void print_help (void);
45 void print_usage (void);
47 int
48 main (int argc, char **argv)
50 int found = 0, result = STATE_UNKNOWN;
51 char *url = NULL;
52 char *cmd;
53 char *buf;
54 char *nstr;
55 char tstr[MAX_INPUT_BUFFER];
57 int c;
58 int option = 0;
59 static struct option longopts[] = {
60 {"help", no_argument, 0, 'h'},
61 {"version", no_argument, 0, 'V'},
62 {"url", required_argument, 0, 'u'},
63 {0, 0, 0, 0}
66 setlocale (LC_ALL, "");
67 bindtextdomain (PACKAGE, LOCALEDIR);
68 textdomain (PACKAGE);
70 /* Need at least 2 args */
71 if (argc < 3) {
72 print_help();
73 exit (STATE_UNKNOWN);
76 while (1) {
77 c = getopt_long (argc, argv, "+hVu:", longopts, &option);
79 if (c == -1 || c == EOF)
80 break;
82 switch (c) {
83 case 'h': /* help */
84 print_help ();
85 exit (EXIT_SUCCESS);
86 break;
87 case 'V': /* version */
88 print_revision (progname, NP_VERSION);
89 exit (EXIT_SUCCESS);
90 break;
91 case 'u':
92 url = strdup (argv[optind]);
93 break;
94 case '?':
95 default:
96 usage5 ();
100 if (url == NULL)
101 url = strdup (argv[optind++]);
103 cmd = strdup (argv[optind++]);
104 for (c = optind; c < argc; c++) {
105 xasprintf (&cmd, "%s %s", cmd, argv[c]);
108 child_process = spopen (cmd);
109 if (child_process == NULL) {
110 printf (_("Could not open pipe: %s\n"), cmd);
111 exit (STATE_UNKNOWN);
114 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
115 if (child_stderr == NULL) {
116 printf (_("Could not open stderr for %s\n"), cmd);
119 bzero(tstr, sizeof(tstr));
120 buf = malloc(MAX_INPUT_BUFFER);
121 printf ("<A href=\"%s\">", argv[1]);
122 while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
123 found++;
124 /* Collect the string in temp str so we can tokenize */
125 strcat(tstr, buf);
128 if (!found)
129 die (STATE_UNKNOWN,
130 _("%s UNKNOWN - No data received from host\nCMD: %s</A>\n"),
131 argv[0], cmd);
134 /* chop the newline character */
135 if ((nstr = strchr(tstr, NEWLINE_CHARACTER)) != NULL)
136 *nstr = '\0';
138 /* tokenize the string for Perfdata if there is some */
139 nstr = strtok(tstr, PERF_CHARACTER);
140 printf ("%s", nstr);
141 printf ("</A>");
142 nstr = strtok(NULL, PERF_CHARACTER);
143 if (nstr != NULL)
144 printf (" | %s", nstr);
146 /* close the pipe */
147 result = spclose (child_process);
149 /* WARNING if output found on stderr */
150 if (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr))
151 result = max_state (result, STATE_WARNING);
153 /* close stderr */
154 (void) fclose (child_stderr);
156 return result;
161 void
162 print_help (void)
164 print_revision (progname, NP_VERSION);
166 printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
167 printf (COPYRIGHT, copyright, email);
169 printf ("%s\n", _("This plugin wraps the text output of another command (plugin)"));
170 printf ("%s\n", _("in HTML <A> tags, thus displaying the child plugin's output as a clickable link in"));
171 printf ("%s\n", _("the Nagios status screen. This plugin returns the status of the invoked plugin."));
173 printf ("\n\n");
175 print_usage ();
177 printf (UT_HELP_VRSN);
179 printf ("\n");
180 printf ("%s\n", _("Examples:"));
181 printf ("%s\n", _("Pay close attention to quoting to ensure that the shell passes the expected"));
182 printf ("%s\n\n", _("data to the plugin. For example, in:"));
183 printf (" %s\n\n", _("urlize http://example.com/ check_http -H example.com -r 'two words'"));
184 printf (" %s\n", _("the shell will remove the single quotes and urlize will see:"));
185 printf (" %s\n\n", _("urlize http://example.com/ check_http -H example.com -r two words"));
186 printf (" %s\n\n", _("You probably want:"));
187 printf (" %s\n", _("urlize http://example.com/ \"check_http -H example.com -r 'two words'\""));
189 printf (UT_SUPPORT);
194 void
195 print_usage (void)
197 printf ("%s\n", _("Usage:"));
198 printf ("%s <url> <plugin> <arg1> ... <argN>\n", progname);