Code

Updated output to UNKNOWN
[nagiosplug.git] / plugins / urlize.c
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 ******************************************************************************/
19 const char *progname = "urlize";
20 const char *revision = "$Revision$";
21 const char *copyright = "2000-2003";
22 const char *email = "nagiosplug-devel@lists.sourceforge.net";
24 #include "common.h"
25 #include "utils.h"
26 #include "popen.h"
28 void print_help (void);
29 void print_usage (void);
31 int
32 main (int argc, char **argv)
33 {
34         int found = 0, result = STATE_UNKNOWN;
35         char *url = NULL;
36         char *cmd;
37         char *buf;
39         int c;
40         int option = 0;
41         static struct option longopts[] = {
42                 {"help", no_argument, 0, 'h'},
43                 {"version", no_argument, 0, 'V'},
44                 {"url", required_argument, 0, 'u'},
45                 {0, 0, 0, 0}
46         };
48         setlocale (LC_ALL, "");
49         bindtextdomain (PACKAGE, LOCALEDIR);
50         textdomain (PACKAGE);
52         while (1) {
53                 c = getopt_long (argc, argv, "+hVu:", longopts, &option);
55                 if (c == -1 || c == EOF)
56                         break;
58                 switch (c) {
59                 case 'h':     /* help */
60                         print_help ();
61                         exit (EXIT_SUCCESS);
62                         break;
63                 case 'V':     /* version */
64                         print_revision (progname, revision);
65                         exit (EXIT_SUCCESS);
66                         break;
67                 case 'u':
68                         url = strdup (argv[optind]);
69                         break;
70                 case '?':
71                 default:
72                         usage3 (_("Unknown argument"), optopt);
73                         break;
74                 }
75         }
77         if (url == NULL)
78                 url = strdup (argv[optind++]);
80         cmd = strdup (argv[optind++]);
81         for (c = optind; c < argc; c++) {
82                 asprintf (&cmd, "%s %s", cmd, argv[c]);
83         }
85         child_process = spopen (cmd);
86         if (child_process == NULL) {
87                 printf (_("Could not open pipe: %s\n"), cmd);
88                 exit (STATE_UNKNOWN);
89         }
91         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
92         if (child_stderr == NULL) {
93                 printf (_("Could not open stderr for %s\n"), cmd);
94         }
96         buf = malloc(MAX_INPUT_BUFFER);
97         printf ("<A href=\"%s\">", argv[1]);
98         while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
99                 found++;
100                 printf ("%s", buf);
101         }
103         if (!found)
104                 die (STATE_UNKNOWN,
105                      _("%s UNKNOWN - No data received from host\nCMD: %s</A>\n"),
106                      argv[0], cmd);
108         /* close the pipe */
109         result = spclose (child_process);
111         /* WARNING if output found on stderr */
112         if (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr))
113                 result = max_state (result, STATE_WARNING);
115         /* close stderr */
116         (void) fclose (child_stderr);
118         printf ("</A>\n");
119         return result;
126 \f
127 void
128 print_help (void)
130         print_revision (progname, revision);
132         printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
133         printf (COPYRIGHT, copyright, email);
135         printf (_("\n\
136 This plugin wraps the text output of another command (plugin) in HTML\n\
137 <A> tags, thus displaying the plugin output in as a clickable link in\n\
138 the Nagios status screen.  The return status is the same as the invoked\n\
139 plugin.\n\n"));
141         print_usage ();
143         printf (_("\n\
144 Pay close attention to quoting to ensure that the shell passes the expected\n\
145 data to the plugin. For example, in:\n\
146 \n\
147     urlize http://example.com/ check_http -H example.com -r 'two words'\n\
148 \n\
149 the shell will remove the single quotes and urlize will see:\n\
150 \n\
151     urlize http://example.com/ check_http -H example.com -r two words\n\
152 \n\
153 You probably want:\n\
154 \n\
155     urlize http://example.com/ \"check_http -H example.com -r 'two words'\"\n"));
157         printf (_(UT_SUPPORT));
163 void
164 print_usage (void)
166         printf (_("Usage:\n %s <url> <plugin> <arg1> ... <argN>\n"), progname);