Code

Remove getopt_long checks
[nagiosplug.git] / plugins / check_dig.c
1 /******************************************************************************
2  *
3  * Program: SNMP plugin for Nagios
4  * License: GPL
5  *
6  * License Information:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *****************************************************************************/
23 #include "config.h"
24 #include "common.h"
25 #include "utils.h"
26 #include "popen.h"
28 const char *progname = "check_dig";
29 #define REVISION "$Revision$"
30 #define COPYRIGHT "2000-2002"
31 #define AUTHOR "Karl DeBisschop"
32 #define EMAIL "karl@debisschop.net"
33 #define SUMMARY "Test the DNS service on the specified host using dig\n"
35 int process_arguments (int, char **);
36 int validate_arguments (void);
37 void print_help (void);
38 void print_usage (void);
40 char *query_address = NULL;
41 char *dns_server = NULL;
42 int verbose = FALSE;
44 int
45 main (int argc, char **argv)
46 {
47         char input_buffer[MAX_INPUT_BUFFER];
48         char *command_line = NULL;
49         char *output = "";
50         int result = STATE_UNKNOWN;
52         /* Set signal handling and alarm */
53         if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
54                 usage ("Cannot catch SIGALRM\n");
56         if (process_arguments (argc, argv) != OK)
57                 usage ("Could not parse arguments\n");
59         /* get the command to run */
60         asprintf (&command_line, "%s @%s %s", PATH_TO_DIG, dns_server, query_address);
62         alarm (timeout_interval);
63         time (&start_time);
65         if (verbose)
66                 printf ("%s\n", command_line);
67         /* run the command */
68         child_process = spopen (command_line);
69         if (child_process == NULL) {
70                 printf ("Could not open pipe: %s\n", command_line);
71                 return STATE_UNKNOWN;
72         }
74         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
75         if (child_stderr == NULL)
76                 printf ("Could not open stderr for %s\n", command_line);
78         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
80                 /* the server is responding, we just got the host name... */
81                 if (strstr (input_buffer, ";; ANSWER SECTION:")) {
83                         /* get the host address */
84                         if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
85                                 break;
87                         if (strpbrk (input_buffer, "\r\n"))
88                                 input_buffer[strcspn (input_buffer, "\r\n")] = '\0';
90                         if (strstr (input_buffer, query_address) == input_buffer) {
91                                 asprintf (&output, input_buffer);
92                                 result = STATE_OK;
93                         }
94                         else {
95                                 asprintf (&output, "Server not found in ANSWER SECTION");
96                                 result = STATE_WARNING;
97                         }
99                         continue;
100                 }
102         }
104         if (result != STATE_OK) {
105                 asprintf (&output, "No ANSWER SECTION found");
106         }
108         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
109                 /* If we get anything on STDERR, at least set warning */
110                 result = max_state (result, STATE_WARNING);
111                 printf ("%s", input_buffer);
112                 if (strlen (output) == 0)
113                         asprintf (&output, 1 + index (input_buffer, ':'));
114         }
116         (void) fclose (child_stderr);
118         /* close the pipe */
119         if (spclose (child_process)) {
120                 result = max_state (result, STATE_WARNING);
121                 if (strlen (output) == 0)
122                         asprintf (&output, "dig returned error status");
123         }
125         (void) time (&end_time);
127         if (output == NULL || strlen (output) == 0)
128                 asprintf (&output, " Probably a non-existent host/domain");
130         if (result == STATE_OK)
131                 printf ("DNS ok - %d seconds response time (%s)\n",
132                                                 (int) (end_time - start_time), output);
133         else if (result == STATE_WARNING)
134                 printf ("DNS WARNING - %s\n", output);
135         else if (result == STATE_CRITICAL)
136                 printf ("DNS CRITICAL - %s\n", output);
137         else
138                 printf ("DNS problem - %s\n", output);
140         return result;
143 /* process command-line arguments */
144 int
145 process_arguments (int argc, char **argv)
147         int c;
149         int option_index = 0;
150         static struct option long_options[] = {
151                 {"hostname", required_argument, 0, 'H'},
152                 {"query_address", required_argument, 0, 'e'},
153                 {"verbose", no_argument, 0, 'v'},
154                 {"version", no_argument, 0, 'V'},
155                 {"help", no_argument, 0, 'h'},
156                 {0, 0, 0, 0}
157         };
159         if (argc < 2)
160                 return ERROR;
162         while (1) {
163                 c = getopt_long (argc, argv, "hVvt:l:H:", long_options, &option_index);
165                 if (c == -1 || c == EOF)
166                         break;
168                 switch (c) {
169                 case '?':                                                                       /* help */
170                         usage3 ("Unknown argument", optopt);
171                 case 'H':                                                                       /* hostname */
172                         if (is_host (optarg)) {
173                                 dns_server = optarg;
174                         }
175                         else {
176                                 usage ("Invalid host name\n");
177                         }
178                         break;
179                 case 'l':                                                                       /* username */
180                         query_address = optarg;
181                         break;
182                 case 'v':                                                                       /* verbose */
183                         verbose = TRUE;
184                         break;
185                 case 't':                                                                       /* timeout */
186                         if (is_intnonneg (optarg)) {
187                                 timeout_interval = atoi (optarg);
188                         }
189                         else {
190                                 usage ("Time interval must be a nonnegative integer\n");
191                         }
192                         break;
193                 case 'V':                                                                       /* version */
194                         print_revision (progname, "$Revision$");
195                         exit (STATE_OK);
196                 case 'h':                                                                       /* help */
197                         print_help ();
198                         exit (STATE_OK);
199                 }
200         }
202         c = optind;
203         if (dns_server == NULL) {
204                 if (c < argc) {
205                         if (is_host (argv[c])) {
206                                 dns_server = argv[c];
207                         }
208                         else {
209                                 usage ("Invalid host name");
210                         }
211                 }
212                 else {
213                         asprintf (&dns_server, "127.0.0.1");
214                 }
215         }
217         return validate_arguments ();
224 int
225 validate_arguments (void)
227         return OK;
234 void
235 print_help (void)
237         print_revision (progname, "$Revision$");
238         printf
239                 ("Copyright (c) %s %s <%s>\n\n%s\n",
240                  COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
241         print_usage ();
242         printf
243                 ("\nOptions:\n"
244                  " -H, --hostname=STRING or IPADDRESS\n"
245                  "   Check server on the indicated host\n"
246                  " -l, --lookup=STRING\n"
247                  "   machine name to lookup\n"
248                  " -t, --timeout=INTEGER\n"
249                  "   Seconds before connection attempt times out (default: %d)\n"
250                  " -v, --verbose\n"
251                  "   Print extra information (command-line use only)\n"
252                  " -h, --help\n"
253                  "   Print detailed help screen\n"
254                  " -V, --version\n"
255                  "   Print version information\n\n", DEFAULT_SOCKET_TIMEOUT);
256         support ();
263 void
264 print_usage (void)
266         printf
267                 ("Usage: %s -H host -l lookup [-t timeout] [-v]\n"
268                  "       %s --help\n"
269                  "       %s --version\n", progname, progname, progname);