Code

Initial revision
[nagiosplug.git] / plugins / urlize.c
1 /******************************************************************************
2  *
3  * urlize.c
4  *
5  * Program: plugin wrapper for Nagios
6  * License: GPL
7  * Copyright (c) 2000 Karl DeBisschop (kdebiss@alum.mit.edu)
8  *
9  * Last Modified: $Date$
10  * 2000-06-01 Karl DeBisschop <karl@debisschop.net>
11  *  Written based of concept in urlize.pl
12  *
13  * Usage: urlize <url> <plugin> <arg1> ... <argN>
14  *
15  * Description:
16  *
17  * This plugin wraps the text output of another command (plugin) in HTML
18  * <A> tags, thus displaying the plugin output in as a clickable link in
19  * the Nagios status screen.  The return status is the same as the plugin
20  * invoked by urlize
21  *
22  * License Information:
23  *
24  * This program is free software; you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation; either version 2 of the License, or
27  * (at your option) any later version.
28  *
29  * This program is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  * GNU General Public License for more details.
33  *
34  * You should have received a copy of the GNU General Public License
35  * along with this program; if not, write to the Free Software
36  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37  *
38  *****************************************************************************/
40 #include "common.h"
41 #include "utils.h"
42 #include "popen.h"
44 void print_usage (char *);
45 void print_help (char *);
47 int
48 main (int argc, char **argv)
49 {
50         int i = 0, found = 0, result = STATE_UNKNOWN;
51         char command_line[MAX_INPUT_BUFFER] = "";
52         char input_buffer[MAX_INPUT_BUFFER];
54         if (argc < 2) {
55                 print_usage (my_basename (argv[0]));
56                 exit (STATE_UNKNOWN);
57         }
59         if (!strcmp (argv[1], "-h") || !strcmp (argv[1], "--help")) {
60                 print_help (argv[0]);
61                 exit (STATE_OK);
62         }
64         if (!strcmp (argv[1], "-V") || !strcmp (argv[1], "--version")) {
65                 print_revision (my_basename (argv[0]), "$Revision$");
66                 exit (STATE_OK);
67         }
69         if (argc < 2) {
70                 print_usage (my_basename (argv[0]));
71                 exit (STATE_UNKNOWN);
72         }
74         sprintf (command_line, "%s", argv[2]);
75         for (i = 3; i < argc; i++) {
76                 sprintf (command_line, "%s %s", command_line, argv[i]);
77         }
79         child_process = spopen (command_line);
80         if (child_process == NULL) {
81                 printf ("Could not open pipe: %s\n", command_line);
82                 exit (STATE_UNKNOWN);
83         }
85         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
86         if (child_stderr == NULL) {
87                 printf ("Could not open stderr for %s\n", command_line);
88         }
90         printf ("<A href=\"%s\">", argv[1]);
91         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
92                 found++;
93                 if (index (input_buffer, '\n')) {
94                         input_buffer[strcspn (input_buffer, "\n")] = 0;
95                         printf ("%s", input_buffer);
96                 }
97                 else {
98                         printf ("%s", input_buffer);
99                 }
100         }
102         if (!found) {
103                 printf ("%s problem - No data recieved from host\nCMD: %s\n", argv[0],
104                                                 command_line);
105                 exit (STATE_UNKNOWN);
106         }
108         /* close the pipe */
109         result = spclose (child_process);
111         /* WARNING if output found on stderr */
112         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
113                 result = max (result, STATE_WARNING);
115         /* close stderr */
116         (void) fclose (child_stderr);
118         printf ("</A>\n");
119         return result;
122 void
123 print_usage (char *cmd)
125         printf ("Usage:\n %s <url> <plugin> <arg1> ... <argN>\n",
126                                         my_basename (cmd));
129 void
130 print_help (char *cmd)
132         print_revision ("urlize", "$Revision$");
133         printf
134                 ("Copyright (c) 2000 Karl DeBisschop (kdebiss@alum.mit.edu)\n\n"
135                  "\nThis plugin wraps the text output of another command (plugin) in HTML\n"
136                  "<A> tags, thus displaying the plugin output in as a clickable link in\n"
137                  "the Nagios status screen.  The return status is the same as the invoked\n"
138                  "plugin.\n\n");
139         print_usage (cmd);
140         exit (STATE_OK);