Code

use float for time in perf data
[nagiosplug.git] / plugins / check_dig.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 #include "common.h"
20 #include "netutils.h"
21 #include "utils.h"
22 #include "popen.h"
24 int process_arguments (int, char **);
25 int validate_arguments (void);
26 void print_help (void);
27 void print_usage (void);
29 const char *progname = "check_dig";
30 const char *revision = "$Revision$";
31 const char *copyright = "2002-2003";
32 const char *email = "nagiosplug-devel@lists.sourceforge.net";
34 enum {
35         UNDEFINED = 0,
36         DEFAULT_PORT = 53
37 };
39 char *query_address = NULL;
40 char *dns_server = NULL;
41 int verbose = FALSE;
42 int server_port = DEFAULT_PORT;
43 double warning_interval = UNDEFINED;
44 double critical_interval = UNDEFINED;
45 struct timeval tv;
47 int
48 main (int argc, char **argv)
49 {
50         char input_buffer[MAX_INPUT_BUFFER];
51         char *command_line;
52         char *output;
53         long microsec;
54         double elapsed_time;
55         int result = STATE_UNKNOWN;
57         output = strdup ("");
59         setlocale (LC_ALL, "");
60         bindtextdomain (PACKAGE, LOCALEDIR);
61         textdomain (PACKAGE);
63         /* Set signal handling and alarm */
64         if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
65                 usage (_("Cannot catch SIGALRM\n"));
67         if (process_arguments (argc, argv) != OK)
68                 usage (_("Could not parse arguments\n"));
70         /* get the command to run */
71         asprintf (&command_line, "%s @%s -p %d %s",
72                   PATH_TO_DIG, dns_server, server_port, query_address);
74         alarm (timeout_interval);
75         gettimeofday (&tv, NULL);
77         if (verbose)
78                 printf ("%s\n", command_line);
80         /* run the command */
81         child_process = spopen (command_line);
82         if (child_process == NULL) {
83                 printf (_("Could not open pipe: %s\n"), command_line);
84                 return 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"), command_line);
91         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
93                 /* the server is responding, we just got the host name... */
94                 if (strstr (input_buffer, ";; ANSWER SECTION:")) {
96                         /* get the host address */
97                         if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
98                                 break;
100                         if (strpbrk (input_buffer, "\r\n"))
101                                 input_buffer[strcspn (input_buffer, "\r\n")] = '\0';
103                         if (strstr (input_buffer, query_address) == input_buffer) {
104                                 output = strdup(input_buffer);
105                                 result = STATE_OK;
106                         }
107                         else {
108                                 asprintf (&output, _("Server not found in ANSWER SECTION"));
109                                 result = STATE_WARNING;
110                         }
112                         continue;
113                 }
115         }
117         if (result != STATE_OK) {
118                 asprintf (&output, _("No ANSWER SECTION found"));
119         }
121         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
122                 /* If we get anything on STDERR, at least set warning */
123                 result = max_state (result, STATE_WARNING);
124                 printf ("%s", input_buffer);
125                 if (strlen (output) == 0)
126                         output = strdup (1 + index (input_buffer, ':'));
127         }
129         (void) fclose (child_stderr);
131         /* close the pipe */
132         if (spclose (child_process)) {
133                 result = max_state (result, STATE_WARNING);
134                 if (strlen (output) == 0)
135                         asprintf (&output, _("dig returned error status"));
136         }
138         microsec = deltime (tv);
139         elapsed_time = (double)microsec / 1.0e6;
141         if (output == NULL || strlen (output) == 0)
142                 asprintf (&output, _(" Probably a non-existent host/domain"));
144         if (critical_interval > UNDEFINED && elapsed_time > critical_interval)
145                 result = STATE_CRITICAL;
147         else if (warning_interval > UNDEFINED && elapsed_time > warning_interval)
148                 result = STATE_WARNING;
150         asprintf (&output, _("%.3f seconds response time (%s)"), elapsed_time, output);
152         printf ("DNS %s - %s|%s\n",
153                 state_text (result), output,
154                 fperfdata("time", elapsed_time, "s",
155                          (warning_interval>UNDEFINED?TRUE:FALSE),
156                          warning_interval,
157                          (critical_interval>UNDEFINED?TRUE:FALSE),
158                          critical_interval,
159                                                                          TRUE, 0, FALSE, 0));
160         return result;
167 \f
168 /* process command-line arguments */
169 int
170 process_arguments (int argc, char **argv)
172         int c;
174         int option = 0;
175         static struct option longopts[] = {
176                 {"hostname", required_argument, 0, 'H'},
177                 {"query_address", required_argument, 0, 'l'},
178                 {"warning", required_argument, 0, 'w'},
179                 {"critical", required_argument, 0, 'c'},
180                 {"timeout", required_argument, 0, 't'},
181                 {"verbose", no_argument, 0, 'v'},
182                 {"version", no_argument, 0, 'V'},
183                 {"help", no_argument, 0, 'h'},
184                 {0, 0, 0, 0}
185         };
187         if (argc < 2)
188                 return ERROR;
190         while (1) {
191                 c = getopt_long (argc, argv, "hVvt:l:H:w:c:", longopts, &option);
193                 if (c == -1 || c == EOF)
194                         break;
196                 switch (c) {
197                 case '?':                                                                       /* help */
198                         usage3 (_("Unknown argument"), optopt);
199                 case 'h':                                                                       /* help */
200                         print_help ();
201                         exit (STATE_OK);
202                 case 'V':                                                                       /* version */
203                         print_revision (progname, "$Revision$");
204                         exit (STATE_OK);
205                 case 'H':                                                                       /* hostname */
206                         if (is_host (optarg)) {
207                                 dns_server = optarg;
208                         }
209                         else {
210                                 usage2 (_("Invalid host name"), optarg);
211                         }
212                         break;
213                 case 'p':                 /* server port */
214                         if (is_intpos (optarg)) {
215                                 server_port = atoi (optarg);
216                         }
217                         else {
218                                 usage2 (_("Server port must be a nonnegative integer"), optarg);
219                         }
220                         break;
221                 case 'l':                                                                       /* address to lookup */
222                         query_address = optarg;
223                         break;
224                 case 'w':                                                                       /* warning */
225                         if (is_nonnegative (optarg)) {
226                                 warning_interval = strtod (optarg, NULL);
227                         }
228                         else {
229                                 usage2 (_("Warning interval must be a nonnegative integer"), optarg);
230                         }
231                         break;
232                 case 'c':                                                                       /* critical */
233                         if (is_nonnegative (optarg)) {
234                                 critical_interval = strtod (optarg, NULL);
235                         }
236                         else {
237                                 usage2 (_("Critical interval must be a nonnegative integer"), optarg);
238                         }
239                         break;
240                 case 't':                                                                       /* timeout */
241                         if (is_intnonneg (optarg)) {
242                                 timeout_interval = atoi (optarg);
243                         }
244                         else {
245                                 usage2 (_("Time interval must be a nonnegative integer"), optarg);
246                         }
247                         break;
248                 case 'v':                                                                       /* verbose */
249                         verbose = TRUE;
250                         break;
251                 }
252         }
254         c = optind;
255         if (dns_server == NULL) {
256                 if (c < argc) {
257                         if (is_host (argv[c])) {
258                                 dns_server = argv[c];
259                         }
260                         else {
261                                 usage2 (_("Invalid host name"), argv[c]);
262                         }
263                 }
264                 else {
265                         dns_server = strdup ("127.0.0.1");
266                 }
267         }
269         return validate_arguments ();
276 int
277 validate_arguments (void)
279         return OK;
287 \f
288 void
289 print_help (void)
291         char *myport;
293         asprintf (&myport, "%d", DEFAULT_PORT);
295         print_revision (progname, revision);
297         printf (_("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n"));
298         printf (_(COPYRIGHT), copyright, email);
300         printf (_("Test the DNS service on the specified host using dig\n\n"));
302         print_usage ();
304         printf (_(UT_HELP_VRSN));
306         printf (_(UT_HOST_PORT), 'P', myport);
308         printf (_("\
309  -l, --lookup=STRING\n\
310    machine name to lookup\n"));
312         printf (_(UT_WARN_CRIT));
314         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
316         printf (_(UT_VERBOSE));
318         printf (_(UT_SUPPORT));
324 void
325 print_usage (void)
327         printf (_("\
328 Usage: %s -H host -l lookup [-p <server port>] [-w <warning interval>]\n\
329          [-c <critical interval>] [-t <timeout>] [-v]\n"),
330                 progname);
331         printf ("       %s (-h|--help)\n", progname);
332         printf ("       %s (-V|--version)\n", progname);