Revert "check_disk - show all disks if state is ok and option error only is used"
[monitoring-plugins.git] / plugins / urlize.c
blob6fda72d15cfd6fa800d7c836ea19b49256d89c99
1 /*****************************************************************************
2 *
3 * Monitoring urlize plugin
4 *
5 * License: GPL
6 * Copyright (c) 2000-2007 Monitoring 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. This plugin returns the status of the invoked plugin.
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 *****************************************************************************/
32 const char *progname = "urlize";
33 const char *copyright = "2000-2006";
34 const char *email = "devel@monitoring-plugins.org";
36 #include "common.h"
37 #include "utils.h"
38 #include "popen.h"
40 #define PERF_CHARACTER "|"
41 #define NEWLINE_CHARACTER '\n'
43 void print_help (void);
44 void print_usage (void);
46 int
47 main (int argc, char **argv)
49 int found = 0, result = STATE_UNKNOWN;
50 char *url = NULL;
51 char *cmd;
52 char *buf;
53 char *nstr;
54 char tstr[MAX_INPUT_BUFFER];
56 int c;
57 int option = 0;
58 static struct option longopts[] = {
59 {"help", no_argument, 0, 'h'},
60 {"version", no_argument, 0, 'V'},
61 {"url", required_argument, 0, 'u'},
62 {0, 0, 0, 0}
65 setlocale (LC_ALL, "");
66 bindtextdomain (PACKAGE, LOCALEDIR);
67 textdomain (PACKAGE);
69 /* Need at least 2 args */
70 if (argc < 3) {
71 print_help();
72 exit (STATE_UNKNOWN);
75 while (1) {
76 c = getopt_long (argc, argv, "+hVu:", longopts, &option);
78 if (c == -1 || c == EOF)
79 break;
81 switch (c) {
82 case 'h': /* help */
83 print_help ();
84 exit (EXIT_SUCCESS);
85 break;
86 case 'V': /* version */
87 print_revision (progname, NP_VERSION);
88 exit (EXIT_SUCCESS);
89 break;
90 case 'u':
91 url = strdup (argv[optind]);
92 break;
93 case '?':
94 default:
95 usage5 ();
99 if (url == NULL)
100 url = strdup (argv[optind++]);
102 cmd = strdup (argv[optind++]);
103 for (c = optind; c < argc; c++) {
104 xasprintf (&cmd, "%s %s", cmd, argv[c]);
107 child_process = spopen (cmd);
108 if (child_process == NULL) {
109 printf (_("Could not open pipe: %s\n"), cmd);
110 exit (STATE_UNKNOWN);
113 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
114 if (child_stderr == NULL) {
115 printf (_("Could not open stderr for %s\n"), cmd);
118 bzero(tstr, sizeof(tstr));
119 buf = malloc(MAX_INPUT_BUFFER);
120 printf ("<A href=\"%s\">", argv[1]);
121 while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
122 found++;
123 /* Collect the string in temp str so we can tokenize */
124 strcat(tstr, buf);
127 if (!found)
128 die (STATE_UNKNOWN,
129 _("%s UNKNOWN - No data received from host\nCMD: %s</A>\n"),
130 argv[0], cmd);
133 /* chop the newline character */
134 if ((nstr = strchr(tstr, NEWLINE_CHARACTER)) != NULL)
135 *nstr = '\0';
137 /* tokenize the string for Perfdata if there is some */
138 nstr = strtok(tstr, PERF_CHARACTER);
139 printf ("%s", nstr);
140 printf ("</A>");
141 nstr = strtok(NULL, PERF_CHARACTER);
142 if (nstr != NULL)
143 printf (" | %s", nstr);
145 /* close the pipe */
146 result = spclose (child_process);
148 /* WARNING if output found on stderr */
149 if (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr))
150 result = max_state (result, STATE_WARNING);
152 /* close stderr */
153 (void) fclose (child_stderr);
155 return result;
160 void
161 print_help (void)
163 print_revision (progname, NP_VERSION);
165 printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
166 printf (COPYRIGHT, copyright, email);
168 printf ("%s\n", _("This plugin wraps the text output of another command (plugin) in HTML <A>"));
169 printf ("%s\n", _("tags, thus displaying the child plugin's output as a clickable link in compatible"));
170 printf ("%s\n", _("monitoring status screen. This plugin returns the status of the invoked plugin."));
172 printf ("\n\n");
174 print_usage ();
176 printf (UT_HELP_VRSN);
178 printf ("\n");
179 printf ("%s\n", _("Examples:"));
180 printf ("%s\n", _("Pay close attention to quoting to ensure that the shell passes the expected"));
181 printf ("%s\n\n", _("data to the plugin. For example, in:"));
182 printf (" %s\n\n", _("urlize http://example.com/ check_http -H example.com -r 'two words'"));
183 printf (" %s\n", _("the shell will remove the single quotes and urlize will see:"));
184 printf (" %s\n\n", _("urlize http://example.com/ check_http -H example.com -r two words"));
185 printf (" %s\n\n", _("You probably want:"));
186 printf (" %s\n", _("urlize http://example.com/ \"check_http -H example.com -r 'two words'\""));
188 printf (UT_SUPPORT);
193 void
194 print_usage (void)
196 printf ("%s\n", _("Usage:"));
197 printf ("%s <url> <plugin> <arg1> ... <argN>\n", progname);