Code

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