diff options
Diffstat (limited to 'plugins/urlize.c')
-rw-r--r-- | plugins/urlize.c | 225 |
1 files changed, 114 insertions, 111 deletions
diff --git a/plugins/urlize.c b/plugins/urlize.c index 12fb3ec9..f4bc67b3 100644 --- a/plugins/urlize.c +++ b/plugins/urlize.c | |||
@@ -1,41 +1,20 @@ | |||
1 | /****************************************************************************** | 1 | /****************************************************************************** |
2 | * | 2 | |
3 | * urlize.c | 3 | This program is free software; you can redistribute it and/or modify |
4 | * | 4 | it under the terms of the GNU General Public License as published by |
5 | * Program: plugin wrapper for Nagios | 5 | the Free Software Foundation; either version 2 of the License, or |
6 | * License: GPL | 6 | (at your option) any later version. |
7 | * Copyright (c) 2000 Karl DeBisschop (kdebiss@alum.mit.edu) | 7 | |
8 | * | 8 | This program is distributed in the hope that it will be useful, |
9 | * Last Modified: $Date$ | 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * 2000-06-01 Karl DeBisschop <karl@debisschop.net> | 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * Written based of concept in urlize.pl | 11 | GNU General Public License for more details. |
12 | * | 12 | |
13 | * Usage: urlize <url> <plugin> <arg1> ... <argN> | 13 | You should have received a copy of the GNU General Public License |
14 | * | 14 | along with this program; if not, write to the Free Software |
15 | * Description: | 15 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
16 | * | 16 | |
17 | * This plugin wraps the text output of another command (plugin) in HTML | 17 | ******************************************************************************/ |
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 | *****************************************************************************/ | ||
39 | 18 | ||
40 | const char *progname = "urlize"; | 19 | const char *progname = "urlize"; |
41 | const char *revision = "$Revision$"; | 20 | const char *revision = "$Revision$"; |
@@ -46,12 +25,101 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net"; | |||
46 | #include "utils.h" | 25 | #include "utils.h" |
47 | #include "popen.h" | 26 | #include "popen.h" |
48 | 27 | ||
49 | void | 28 | void print_help (void); |
50 | print_usage (void) | 29 | void print_usage (void); |
30 | |||
31 | int | ||
32 | main (int argc, char **argv) | ||
51 | { | 33 | { |
52 | printf (_("Usage:\n %s <url> <plugin> <arg1> ... <argN>\n"), progname); | 34 | int found = 0, result = STATE_UNKNOWN; |
35 | char *url = NULL; | ||
36 | char *cmd; | ||
37 | char *buf; | ||
38 | |||
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 | }; | ||
47 | |||
48 | while (1) { | ||
49 | c = getopt_long (argc, argv, "+hVu:", longopts, &option); | ||
50 | |||
51 | if (c == -1 || c == EOF) | ||
52 | break; | ||
53 | |||
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 | } | ||
72 | |||
73 | if (url == NULL) | ||
74 | url = strdup (argv[optind++]); | ||
75 | |||
76 | cmd = strdup (argv[optind++]); | ||
77 | for (c = optind; c < argc; c++) { | ||
78 | asprintf (&cmd, "%s %s", cmd, argv[c]); | ||
79 | } | ||
80 | |||
81 | child_process = spopen (cmd); | ||
82 | if (child_process == NULL) { | ||
83 | printf (_("Could not open pipe: %s\n"), cmd); | ||
84 | exit (STATE_UNKNOWN); | ||
85 | } | ||
86 | |||
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 | } | ||
91 | |||
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 | } | ||
98 | |||
99 | if (!found) | ||
100 | die (STATE_UNKNOWN, | ||
101 | _("%s problem - No data recieved from host\nCMD: %s</A>\n"), | ||
102 | argv[0], cmd); | ||
103 | |||
104 | /* close the pipe */ | ||
105 | result = spclose (child_process); | ||
106 | |||
107 | /* WARNING if output found on stderr */ | ||
108 | if (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr)) | ||
109 | result = max_state (result, STATE_WARNING); | ||
110 | |||
111 | /* close stderr */ | ||
112 | (void) fclose (child_stderr); | ||
113 | |||
114 | printf ("</A>\n"); | ||
115 | return result; | ||
53 | } | 116 | } |
54 | 117 | ||
118 | |||
119 | |||
120 | |||
121 | |||
122 | |||
55 | void | 123 | void |
56 | print_help (void) | 124 | print_help (void) |
57 | { | 125 | { |
@@ -81,80 +149,15 @@ the shell will remove the single quotes and urlize will see:\n\ | |||
81 | You probably want:\n\ | 149 | You probably want:\n\ |
82 | \n\ | 150 | \n\ |
83 | urlize http://example.com/ \"check_http -H example.com -r 'two words'\"\n")); | 151 | urlize http://example.com/ \"check_http -H example.com -r 'two words'\"\n")); |
84 | exit (STATE_OK); | ||
85 | } | ||
86 | |||
87 | int | ||
88 | main (int argc, char **argv) | ||
89 | { | ||
90 | int i = 0, found = 0, result = STATE_UNKNOWN; | ||
91 | char *cmd = NULL; | ||
92 | char input_buffer[MAX_INPUT_BUFFER]; | ||
93 | |||
94 | if (argc < 2) { | ||
95 | print_usage (); | ||
96 | exit (STATE_UNKNOWN); | ||
97 | } | ||
98 | |||
99 | if (!strcmp (argv[1], "-h") || !strcmp (argv[1], "--help")) { | ||
100 | print_help (); | ||
101 | exit (STATE_OK); | ||
102 | } | ||
103 | |||
104 | if (!strcmp (argv[1], "-V") || !strcmp (argv[1], "--version")) { | ||
105 | print_revision (progname, revision); | ||
106 | exit (STATE_OK); | ||
107 | } | ||
108 | |||
109 | if (argc < 2) { | ||
110 | print_usage (); | ||
111 | exit (STATE_UNKNOWN); | ||
112 | } | ||
113 | |||
114 | asprintf (&cmd, "%s", argv[2]); | ||
115 | for (i = 3; i < argc; i++) { | ||
116 | asprintf (&cmd, "%s %s", cmd, argv[i]); | ||
117 | } | ||
118 | |||
119 | child_process = spopen (cmd); | ||
120 | if (child_process == NULL) { | ||
121 | printf (_("Could not open pipe: %s\n"), cmd); | ||
122 | exit (STATE_UNKNOWN); | ||
123 | } | ||
124 | |||
125 | child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | ||
126 | if (child_stderr == NULL) { | ||
127 | printf (_("Could not open stderr for %s\n"), cmd); | ||
128 | } | ||
129 | |||
130 | printf ("<A href=\"%s\">", argv[1]); | ||
131 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | ||
132 | found++; | ||
133 | if (index (input_buffer, '\n')) { | ||
134 | input_buffer[strcspn (input_buffer, "\n")] = 0; | ||
135 | printf ("%s", input_buffer); | ||
136 | } | ||
137 | else { | ||
138 | printf ("%s", input_buffer); | ||
139 | } | ||
140 | } | ||
141 | 152 | ||
142 | if (!found) { | 153 | printf (_(UT_SUPPORT)); |
143 | printf (_("%s problem - No data recieved from host\nCMD: %s</A>\n"), argv[0], | 154 | } |
144 | cmd); | ||
145 | exit (STATE_UNKNOWN); | ||
146 | } | ||
147 | 155 | ||
148 | /* close the pipe */ | ||
149 | result = spclose (child_process); | ||
150 | 156 | ||
151 | /* WARNING if output found on stderr */ | ||
152 | if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) | ||
153 | result = max_state (result, STATE_WARNING); | ||
154 | 157 | ||
155 | /* close stderr */ | ||
156 | (void) fclose (child_stderr); | ||
157 | 158 | ||
158 | printf ("</A>\n"); | 159 | void |
159 | return result; | 160 | print_usage (void) |
161 | { | ||
162 | printf (_("Usage:\n %s <url> <plugin> <arg1> ... <argN>\n"), progname); | ||
160 | } | 163 | } |