diff options
author | Ethan Galstad <egalstad@users.sourceforge.net> | 2002-02-28 06:42:51 (GMT) |
---|---|---|
committer | Ethan Galstad <egalstad@users.sourceforge.net> | 2002-02-28 06:42:51 (GMT) |
commit | 44a321cb8a42d6c0ea2d96a1086a17f2134c89cc (patch) | |
tree | a1a4d9f7b92412a17ab08f34f04eec45433048b7 /plugins/check_dig.c | |
parent | 54fd5d7022ff2d6a59bc52b8869182f3fc77a058 (diff) | |
download | monitoring-plugins-44a321cb8a42d6c0ea2d96a1086a17f2134c89cc.tar.gz |
Initial revision
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@2 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins/check_dig.c')
-rw-r--r-- | plugins/check_dig.c | 296 |
1 files changed, 296 insertions, 0 deletions
diff --git a/plugins/check_dig.c b/plugins/check_dig.c new file mode 100644 index 0000000..57609ac --- /dev/null +++ b/plugins/check_dig.c | |||
@@ -0,0 +1,296 @@ | |||
1 | /****************************************************************************** | ||
2 | * | ||
3 | * check_dig.c | ||
4 | * | ||
5 | * Program: dig plugin for Nagios | ||
6 | * License: GPL | ||
7 | * Copyright (c) 2000 | ||
8 | * | ||
9 | * $Id$ | ||
10 | * | ||
11 | *****************************************************************************/ | ||
12 | |||
13 | #include "config.h" | ||
14 | #include "common.h" | ||
15 | #include "utils.h" | ||
16 | #include "popen.h" | ||
17 | |||
18 | #define PROGNAME "check_dig" | ||
19 | |||
20 | int process_arguments (int, char **); | ||
21 | int call_getopt (int, char **); | ||
22 | int validate_arguments (void); | ||
23 | int check_disk (int usp, int free_disk); | ||
24 | void print_help (void); | ||
25 | void print_usage (void); | ||
26 | |||
27 | char *query_address = NULL; | ||
28 | char *dns_server = NULL; | ||
29 | int verbose = FALSE; | ||
30 | |||
31 | int | ||
32 | main (int argc, char **argv) | ||
33 | { | ||
34 | char input_buffer[MAX_INPUT_BUFFER]; | ||
35 | char *command_line = NULL; | ||
36 | char *output = NULL; | ||
37 | int result = STATE_UNKNOWN; | ||
38 | |||
39 | /* Set signal handling and alarm */ | ||
40 | if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) | ||
41 | usage ("Cannot catch SIGALRM\n"); | ||
42 | |||
43 | if (process_arguments (argc, argv) != OK) | ||
44 | usage ("Could not parse arguments\n"); | ||
45 | |||
46 | /* get the command to run */ | ||
47 | command_line = | ||
48 | ssprintf (command_line, "%s @%s %s", PATH_TO_DIG, dns_server, | ||
49 | query_address); | ||
50 | |||
51 | alarm (timeout_interval); | ||
52 | time (&start_time); | ||
53 | |||
54 | if (verbose) | ||
55 | printf ("%s\n", command_line); | ||
56 | /* run the command */ | ||
57 | child_process = spopen (command_line); | ||
58 | if (child_process == NULL) { | ||
59 | printf ("Could not open pipe: %s\n", command_line); | ||
60 | return STATE_UNKNOWN; | ||
61 | } | ||
62 | |||
63 | child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | ||
64 | if (child_stderr == NULL) | ||
65 | printf ("Could not open stderr for %s\n", command_line); | ||
66 | |||
67 | output = strscpy (output, ""); | ||
68 | |||
69 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | ||
70 | |||
71 | /* the server is responding, we just got the host name... */ | ||
72 | if (strstr (input_buffer, ";; ANSWER SECTION:")) { | ||
73 | |||
74 | /* get the host address */ | ||
75 | if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) | ||
76 | break; | ||
77 | |||
78 | if (strpbrk (input_buffer, "\r\n")) | ||
79 | input_buffer[strcspn (input_buffer, "\r\n")] = '\0'; | ||
80 | |||
81 | if (strstr (input_buffer, query_address) == input_buffer) { | ||
82 | output = strscpy (output, input_buffer); | ||
83 | result = STATE_OK; | ||
84 | } | ||
85 | else { | ||
86 | strcpy (output, "Server not found in ANSWER SECTION"); | ||
87 | result = STATE_WARNING; | ||
88 | } | ||
89 | |||
90 | continue; | ||
91 | } | ||
92 | |||
93 | } | ||
94 | |||
95 | if (result != STATE_OK) { | ||
96 | strcpy (output, "No ANSWER SECTION found"); | ||
97 | } | ||
98 | |||
99 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { | ||
100 | /* If we get anything on STDERR, at least set warning */ | ||
101 | result = max (result, STATE_WARNING); | ||
102 | printf ("%s", input_buffer); | ||
103 | if (!strcmp (output, "")) | ||
104 | strcpy (output, 1 + index (input_buffer, ':')); | ||
105 | } | ||
106 | |||
107 | (void) fclose (child_stderr); | ||
108 | |||
109 | /* close the pipe */ | ||
110 | if (spclose (child_process)) { | ||
111 | result = max (result, STATE_WARNING); | ||
112 | if (!strcmp (output, "")) | ||
113 | strcpy (output, "nslookup returned error status"); | ||
114 | } | ||
115 | |||
116 | (void) time (&end_time); | ||
117 | |||
118 | if (result == STATE_OK) | ||
119 | printf ("DNS ok - %d seconds response time (%s)\n", | ||
120 | (int) (end_time - start_time), output); | ||
121 | else if (result == STATE_WARNING) | ||
122 | printf ("DNS WARNING - %s\n", | ||
123 | !strcmp (output, | ||
124 | "") ? " Probably a non-existent host/domain" : output); | ||
125 | else if (result == STATE_CRITICAL) | ||
126 | printf ("DNS CRITICAL - %s\n", | ||
127 | !strcmp (output, | ||
128 | "") ? " Probably a non-existent host/domain" : output); | ||
129 | else | ||
130 | printf ("DNS problem - %s\n", | ||
131 | !strcmp (output, | ||
132 | "") ? " Probably a non-existent host/domain" : output); | ||
133 | |||
134 | return result; | ||
135 | } | ||
136 | |||
137 | /* process command-line arguments */ | ||
138 | int | ||
139 | process_arguments (int argc, char **argv) | ||
140 | { | ||
141 | int c; | ||
142 | |||
143 | if (argc < 2) | ||
144 | return ERROR; | ||
145 | |||
146 | |||
147 | c = 0; | ||
148 | while ((c += (call_getopt (argc - c, &argv[c]))) < argc) { | ||
149 | |||
150 | if (is_option (argv[c])) | ||
151 | continue; | ||
152 | |||
153 | if (dns_server == NULL) { | ||
154 | if (is_host (argv[c])) { | ||
155 | dns_server = argv[c]; | ||
156 | } | ||
157 | else { | ||
158 | usage ("Invalid host name"); | ||
159 | } | ||
160 | } | ||
161 | } | ||
162 | |||
163 | if (dns_server == NULL) | ||
164 | dns_server = strscpy (NULL, "127.0.0.1"); | ||
165 | |||
166 | return validate_arguments (); | ||
167 | } | ||
168 | |||
169 | |||
170 | |||
171 | |||
172 | |||
173 | |||
174 | int | ||
175 | call_getopt (int argc, char **argv) | ||
176 | { | ||
177 | int c, i = 0; | ||
178 | |||
179 | #ifdef HAVE_GETOPT_H | ||
180 | int option_index = 0; | ||
181 | static struct option long_options[] = { | ||
182 | {"hostname", required_argument, 0, 'H'}, | ||
183 | {"query_address", required_argument, 0, 'e'}, | ||
184 | {"verbose", no_argument, 0, 'v'}, | ||
185 | {"version", no_argument, 0, 'V'}, | ||
186 | {"help", no_argument, 0, 'h'}, | ||
187 | {0, 0, 0, 0} | ||
188 | }; | ||
189 | #endif | ||
190 | |||
191 | while (1) { | ||
192 | #ifdef HAVE_GETOPT_H | ||
193 | c = getopt_long (argc, argv, "+hVvt:l:H:", long_options, &option_index); | ||
194 | #else | ||
195 | c = getopt (argc, argv, "+?hVvt:l:H:"); | ||
196 | #endif | ||
197 | |||
198 | i++; | ||
199 | |||
200 | if (c == -1 || c == EOF || c == 1) | ||
201 | break; | ||
202 | |||
203 | switch (c) { | ||
204 | case 't': | ||
205 | case 'l': | ||
206 | case 'H': | ||
207 | i++; | ||
208 | } | ||
209 | |||
210 | switch (c) { | ||
211 | case 'H': /* hostname */ | ||
212 | if (is_host (optarg)) { | ||
213 | dns_server = optarg; | ||
214 | } | ||
215 | else { | ||
216 | usage ("Invalid host name\n"); | ||
217 | } | ||
218 | break; | ||
219 | case 'l': /* username */ | ||
220 | query_address = optarg; | ||
221 | break; | ||
222 | case 'v': /* verbose */ | ||
223 | verbose = TRUE; | ||
224 | break; | ||
225 | case 't': /* timeout */ | ||
226 | if (is_intnonneg (optarg)) { | ||
227 | timeout_interval = atoi (optarg); | ||
228 | } | ||
229 | else { | ||
230 | usage ("Time interval must be a nonnegative integer\n"); | ||
231 | } | ||
232 | break; | ||
233 | case 'V': /* version */ | ||
234 | print_revision (PROGNAME, "$Revision$"); | ||
235 | exit (STATE_OK); | ||
236 | case 'h': /* help */ | ||
237 | print_help (); | ||
238 | exit (STATE_OK); | ||
239 | case '?': /* help */ | ||
240 | usage ("Invalid argument\n"); | ||
241 | } | ||
242 | } | ||
243 | return i; | ||
244 | } | ||
245 | |||
246 | |||
247 | |||
248 | |||
249 | |||
250 | int | ||
251 | validate_arguments (void) | ||
252 | { | ||
253 | return OK; | ||
254 | } | ||
255 | |||
256 | |||
257 | |||
258 | |||
259 | |||
260 | void | ||
261 | print_help (void) | ||
262 | { | ||
263 | print_revision (PROGNAME, "$Revision$"); | ||
264 | printf | ||
265 | ("Copyright (c) 2000 Karl DeBisschop\n\n" | ||
266 | "This plugin use dig to test the DNS service on the specified host.\n\n"); | ||
267 | print_usage (); | ||
268 | printf | ||
269 | ("\nOptions:\n" | ||
270 | " -H, --hostname=STRING or IPADDRESS\n" | ||
271 | " Check server on the indicated host\n" | ||
272 | " -l, --lookup=STRING\n" | ||
273 | " machine name to lookup\n" | ||
274 | " -t, --timeout=INTEGER\n" | ||
275 | " Seconds before connection attempt times out (default: %d)\n" | ||
276 | " -v, --verbose\n" | ||
277 | " Print extra information (command-line use only)\n" | ||
278 | " -h, --help\n" | ||
279 | " Print detailed help screen\n" | ||
280 | " -V, --version\n" | ||
281 | " Print version information\n\n", DEFAULT_SOCKET_TIMEOUT); | ||
282 | support (); | ||
283 | } | ||
284 | |||
285 | |||
286 | |||
287 | |||
288 | |||
289 | void | ||
290 | print_usage (void) | ||
291 | { | ||
292 | printf | ||
293 | ("Usage: %s -H host -l lookup [-t timeout] [-v]\n" | ||
294 | " %s --help\n" | ||
295 | " %s --version\n", PROGNAME, PROGNAME, PROGNAME); | ||
296 | } | ||