Code

the last round of pedantic compiler warnings
[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         while (1) {
49                 c = getopt_long (argc, argv, "+hVu:", longopts, &option);
51                 if (c == -1 || c == EOF)
52                         break;
54                 switch (c) {
55                 case 'h':     /* help */
56                         print_help ();
57                         exit (EXIT_SUCCESS);
58                         break;
59                 case 'V':     /* version */
60                         print_revision (progname, revision);
61                         exit (EXIT_SUCCESS);
62                         break;
63                 case 'u':
64                         url = strdup (argv[optind]);
65                         break;
66                 case '?':
67                 default:
68                         usage3 (_("Unknown argument"), optopt);
69                         break;
70                 }
71         }
73         if (url == NULL)
74                 url = strdup (argv[optind++]);
76         cmd = strdup (argv[optind++]);
77         for (c = optind; c < argc; c++) {
78                 asprintf (&cmd, "%s %s", cmd, argv[c]);
79         }
81         child_process = spopen (cmd);
82         if (child_process == NULL) {
83                 printf (_("Could not open pipe: %s\n"), cmd);
84                 exit (STATE_UNKNOWN);
85         }
87         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
88         if (child_stderr == NULL) {
89                 printf (_("Could not open stderr for %s\n"), cmd);
90         }
92         buf = malloc(MAX_INPUT_BUFFER);
93         printf ("<A href=\"%s\">", argv[1]);
94         while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
95                 found++;
96                 printf ("%s", buf);
97         }
99         if (!found)
100                 die (STATE_UNKNOWN,
101                      _("%s problem - No data recieved from host\nCMD: %s</A>\n"),
102                      argv[0], cmd);
104         /* close the pipe */
105         result = spclose (child_process);
107         /* WARNING if output found on stderr */
108         if (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr))
109                 result = max_state (result, STATE_WARNING);
111         /* close stderr */
112         (void) fclose (child_stderr);
114         printf ("</A>\n");
115         return result;
122 \f
123 void
124 print_help (void)
126         print_revision (progname, revision);
128         printf (_("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n"));
129         printf (_(COPYRIGHT), copyright, email);
131         printf (_("\n\
132 This plugin wraps the text output of another command (plugin) in HTML\n\
133 <A> tags, thus displaying the plugin output in as a clickable link in\n\
134 the Nagios status screen.  The return status is the same as the invoked\n\
135 plugin.\n\n"));
137         print_usage ();
139         printf (_("\n\
140 Pay close attention to quoting to ensure that the shell passes the expected\n\
141 data to the plugin. For example, in:\n\
142 \n\
143     urlize http://example.com/ check_http -H example.com -r 'two words'\n\
144 \n\
145 the shell will remove the single quotes and urlize will see:\n\
146 \n\
147     urlize http://example.com/ check_http -H example.com -r two words\n\
148 \n\
149 You probably want:\n\
150 \n\
151     urlize http://example.com/ \"check_http -H example.com -r 'two words'\"\n"));
153         printf (_(UT_SUPPORT));
159 void
160 print_usage (void)
162         printf (_("Usage:\n %s <url> <plugin> <arg1> ... <argN>\n"), progname);