1 /*****************************************************************************
2 *
3 * utils.c
4 *
5 * Library of useful functions for plugins
6 *
7 * Copyright (c) 2000 Karl DeBisschop (karl@debisschop.net)
8 * License: GPL
9 *
10 * $Revision$
11 * $Date$
12 ****************************************************************************/
14 #include "config.h"
15 #include "common.h"
16 #include "version.h"
17 #include <stdarg.h>
18 #include <limits.h>
20 extern int timeout_interval;
21 extern const char *progname;
23 void support (void);
24 char *clean_revstring (const char *);
25 void print_revision (const char *, const char *);
26 void terminate (int, const char *fmt, ...);
27 RETSIGTYPE timeout_alarm_handler (int);
29 int is_host (char *);
30 int is_dotted_quad (char *);
31 int is_hostname (char *);
33 int is_integer (char *);
34 int is_intpos (char *);
35 int is_intneg (char *);
36 int is_intnonneg (char *);
37 int is_intpercent (char *);
39 int is_numeric (char *);
40 int is_positive (char *);
41 int is_negative (char *);
42 int is_nonnegative (char *);
43 int is_percentage (char *);
45 int is_option (char *str);
47 double delta_time (struct timeval tv);
49 void strip (char *);
50 char *strscpy (char *dest, const char *src);
51 char *strscat (char *dest, char *src);
52 char *strnl (char *str);
53 char *strpcpy (char *dest, const char *src, const char *str);
54 char *strpcat (char *dest, const char *src, const char *str);
56 #define LABELLEN 63
57 #define STRLEN 64
58 #define TXTBLK 128
60 /* **************************************************************************
61 * max_state(STATE_x, STATE_y)
62 * compares STATE_x to STATE_y and returns result based on the following
63 * STATE_UNKNOWN < STATE_OK < STATE_WARNING < STATE_CRITICAL
64 *
65 * Note that numerically the above does not hold
66 ****************************************************************************/
68 #define max(a,b) (((a)>(b))?(a):(b))
70 int
71 max_state (int a, int b)
72 {
73 if (a == STATE_CRITICAL || b == STATE_CRITICAL)
74 return STATE_CRITICAL;
75 else if (a == STATE_WARNING || b == STATE_WARNING)
76 return STATE_WARNING;
77 else if (a == STATE_OK || b == STATE_OK)
78 return STATE_OK;
79 else if (a == STATE_UNKNOWN || b == STATE_UNKNOWN)
80 return STATE_UNKNOWN;
81 else if (a == STATE_DEPENDENT || b == STATE_DEPENDENT)
82 return STATE_DEPENDENT;
83 else
84 return max (a, b);
85 }
87 void usage (char *msg)
88 {
89 printf (msg);
90 print_usage ();
91 exit (STATE_UNKNOWN);
92 }
94 void usage2(char *msg, char *arg)
95 {
96 printf ("%s: %s - %s\n",progname,msg,arg);
97 print_usage ();
98 exit (STATE_UNKNOWN);
99 }
101 void
102 usage3 (char *msg, char arg)
103 {
104 printf ("%s: %s - %c\n", progname, msg, arg);
105 print_usage();
106 exit (STATE_UNKNOWN);
107 }
110 void
111 support (void)
112 {
113 printf
114 ("Send email to nagios-users@lists.sourceforge.net if you have questions\n"
115 "regarding use of this software. To submit patches or suggest improvements,\n"
116 "send email to nagiosplug-devel@lists.sourceforge.net\n");
117 }
120 char *
121 clean_revstring (const char *revstring)
122 {
123 char plugin_revision[STRLEN];
124 if (sscanf (revstring,"$Revision: %[0-9.]",plugin_revision) == 1)
125 return strscpy (NULL, plugin_revision);
126 else
127 return strscpy (NULL, "N/A");
128 }
130 void
131 print_revision (const char *command_name, const char *revision_string)
132 {
133 char plugin_revision[STRLEN];
135 if (sscanf (revision_string, "$Revision: %[0-9.]", plugin_revision) != 1)
136 strncpy (plugin_revision, "N/A", STRLEN);
137 printf ("%s (nagios-plugins %s) %s\n",
138 progname, VERSION, plugin_revision);
139 printf
140 ("The nagios plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\n"
141 "copies of the plugins under the terms of the GNU General Public License.\n"
142 "For more information about these matters, see the file named COPYING.\n");
144 }
147 void
148 terminate (int result, const char *fmt, ...)
149 {
150 va_list ap;
151 va_start (ap, fmt);
152 vprintf (fmt, ap);
153 va_end (ap);
154 exit (result);
155 }
157 void
158 timeout_alarm_handler (int signo)
159 {
160 if (signo == SIGALRM) {
161 printf ("CRITICAL - Plugin timed out after %d seconds\n",
162 timeout_interval);
163 exit (STATE_CRITICAL);
164 }
165 }
167 int
168 is_host (char *address)
169 {
170 if (is_dotted_quad (address) || is_hostname (address))
171 return (TRUE);
172 return (FALSE);
173 }
175 int
176 is_dotted_quad (char *address)
177 {
178 int o1, o2, o3, o4;
179 char c[1];
181 if (!address)
182 return FALSE;
184 if (sscanf (address, "%d.%d.%d.%d%c", &o1, &o2, &o3, &o4, c) != 4)
185 return FALSE;
186 else if (o1 > 255 || o2 > 255 || o3 > 255 || o4 > 255)
187 return FALSE;
188 else if (o1 < 0 || o2 < 0 || o3 < 0 || o4 < 0)
189 return FALSE;
190 else
191 return TRUE;
192 }
194 /* from RFC-1035
195 *
196 * The labels must follow the rules for ARPANET host names. They must
197 * start with a letter, end with a letter or digit, and have as interior
198 * characters only letters, digits, and hyphen. There are also some
199 * restrictions on the length. Labels must be 63 characters or less. */
201 int
202 is_hostname (char *s1)
203 {
204 if (!s1 || strlen (s1) > 63) {
205 return FALSE;
206 }
207 if (strcspn (s1, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUWVXYZ0123456789-.") != 0) {
208 return FALSE;
209 }
210 if (strspn (s1, "0123456789-.") == 1) {
211 return FALSE;
212 }
213 while ((s1 = index (s1, '.'))) {
214 s1++;
215 if (strspn (s1, "0123456789-.") == 1) {
216 return FALSE;
217 }
218 }
219 return TRUE;
220 }
222 int
223 is_numeric (char *number)
224 {
225 char tmp[1];
226 float x;
228 if (!number)
229 return FALSE;
230 else if (sscanf (number, "%f%c", &x, tmp) == 1)
231 return TRUE;
232 else
233 return FALSE;
234 }
236 int
237 is_positive (char *number)
238 {
239 if (is_numeric (number) && atof (number) > 0.0)
240 return TRUE;
241 else
242 return FALSE;
243 }
245 int
246 is_negative (char *number)
247 {
248 if (is_numeric (number) && atof (number) < 0.0)
249 return TRUE;
250 else
251 return FALSE;
252 }
254 int
255 is_nonnegative (char *number)
256 {
257 if (is_numeric (number) && atof (number) >= 0.0)
258 return TRUE;
259 else
260 return FALSE;
261 }
263 int
264 is_percentage (char *number)
265 {
266 int x;
267 if (is_numeric (number) && (x = atof (number)) >= 0 && x <= 100)
268 return TRUE;
269 else
270 return FALSE;
271 }
273 int
274 is_integer (char *number)
275 {
276 long int n;
278 if (!number || (strspn (number, "-0123456789 ") != strlen (number)))
279 return FALSE;
281 n = strtol (number, NULL, 10);
283 if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX)
284 return TRUE;
285 else
286 return FALSE;
287 }
289 int
290 is_intpos (char *number)
291 {
292 if (is_integer (number) && atoi (number) > 0)
293 return TRUE;
294 else
295 return FALSE;
296 }
298 int
299 is_intneg (char *number)
300 {
301 if (is_integer (number) && atoi (number) < 0)
302 return TRUE;
303 else
304 return FALSE;
305 }
307 int
308 is_intnonneg (char *number)
309 {
310 if (is_integer (number) && atoi (number) >= 0)
311 return TRUE;
312 else
313 return FALSE;
314 }
316 int
317 is_intpercent (char *number)
318 {
319 int i;
320 if (is_integer (number) && (i = atoi (number)) >= 0 && i <= 100)
321 return TRUE;
322 else
323 return FALSE;
324 }
326 int
327 is_option (char *str)
328 {
329 if (!str)
330 return FALSE;
331 else if (strspn (str, "-") == 1 || strspn (str, "-") == 2)
332 return TRUE;
333 else
334 return FALSE;
335 }
339 #ifdef NEED_GETTIMEOFDAY
340 int
341 gettimeofday (struct timeval *tv, struct timezone *tz)
342 {
343 tv->tv_usec = 0;
344 tv->tv_sec = (long) time ((time_t) 0);
345 }
346 #endif
350 double
351 delta_time (struct timeval tv)
352 {
353 struct timeval now;
355 gettimeofday (&now, NULL);
356 return ((double)(now.tv_sec - tv.tv_sec) + (double)(now.tv_usec - tv.tv_usec) / (double)1000000);
357 }
362 void
363 strip (char *buffer)
364 {
365 size_t x;
366 int i;
368 for (x = strlen (buffer); x >= 1; x--) {
369 i = x - 1;
370 if (buffer[i] == ' ' ||
371 buffer[i] == '\r' || buffer[i] == '\n' || buffer[i] == '\t')
372 buffer[i] = '\0';
373 else
374 break;
375 }
376 return;
377 }
383 /******************************************************************************
384 *
385 * Copies one string to another. Any previously existing data in
386 * the destination string is lost.
387 *
388 * Example:
389 *
390 * char *str=NULL;
391 * str = strscpy("This is a line of text with no trailing newline");
392 *
393 *****************************************************************************/
395 char *
396 strscpy (char *dest, const char *src)
397 {
398 if (src == NULL)
399 return NULL;
401 asprintf (&dest, "%s", src);
403 return dest;
404 }
410 /******************************************************************************
411 *
412 * Concatenates one string to the end of another
413 *
414 * Given a pointer destination string, which may or may not already
415 * hold some text, and a source string with additional text (possibly
416 * NULL or empty), returns a pointer to a string that is the first
417 * string with the second concatenated to it. Uses realloc to free
418 * memory held by the dest argument if new storage space is required.
419 *
420 * Example:
421 *
422 * char *str=NULL;
423 * str = strscpy("This is a line of text with no trailing newline");
424 * str = strscat(str,"\n");
425 *
426 *****************************************************************************/
428 char *
429 strscat (char *dest, char *src)
430 {
432 if (dest == NULL)
433 return src;
434 if (src != NULL)
435 asprintf (&dest, "%s%s", dest, src);
437 return dest;
438 }
444 /******************************************************************************
445 *
446 * Returns a pointer to the next line of a multiline string buffer
447 *
448 * Given a pointer string, find the text following the next sequence
449 * of \r and \n characters. This has the effect of skipping blank
450 * lines as well
451 *
452 * Example:
453 *
454 * Given text as follows:
455 *
456 * ==============================
457 * This
458 * is
459 * a
460 *
461 * multiline string buffer
462 * ==============================
463 *
464 * int i=0;
465 * char *str=NULL;
466 * char *ptr=NULL;
467 * str = strscpy(str,"This\nis\r\na\n\nmultiline string buffer\n");
468 * ptr = str;
469 * while (ptr) {
470 * printf("%d %s",i++,firstword(ptr));
471 * ptr = strnl(ptr);
472 * }
473 *
474 * Produces the following:
475 *
476 * 1 This
477 * 2 is
478 * 3 a
479 * 4 multiline
480 *
481 * NOTE: The 'firstword()' function is conceptual only and does not
482 * exist in this package.
483 *
484 * NOTE: Although the second 'ptr' variable is not strictly needed in
485 * this example, it is good practice with these utilities. Once
486 * the * pointer is advance in this manner, it may no longer be
487 * handled with * realloc(). So at the end of the code fragment
488 * above, * strscpy(str,"foo") work perfectly fine, but
489 * strscpy(ptr,"foo") will * cause the the program to crash with
490 * a segmentation fault.
491 *
492 *****************************************************************************/
494 char *
495 strnl (char *str)
496 {
497 size_t len;
498 if (str == NULL)
499 return NULL;
500 str = strpbrk (str, "\r\n");
501 if (str == NULL)
502 return NULL;
503 len = strspn (str, "\r\n");
504 if (str[len] == '\0')
505 return NULL;
506 str += len;
507 if (strlen (str) == 0)
508 return NULL;
509 return str;
510 }
516 /******************************************************************************
517 *
518 * Like strscpy, except only the portion of the source string up to
519 * the provided delimiter is copied.
520 *
521 * Example:
522 *
523 * str = strpcpy(str,"This is a line of text with no trailing newline","x");
524 * printf("%s\n",str);
525 *
526 * Produces:
527 *
528 *This is a line of te
529 *
530 *****************************************************************************/
532 char *
533 strpcpy (char *dest, const char *src, const char *str)
534 {
535 size_t len;
537 if (src)
538 len = strcspn (src, str);
539 else
540 return NULL;
542 if (dest == NULL || strlen (dest) < len)
543 dest = realloc (dest, len + 1);
544 if (dest == NULL)
545 terminate (STATE_UNKNOWN, "failed realloc in strpcpy\n");
547 strncpy (dest, src, len);
548 dest[len] = '\0';
550 return dest;
551 }
557 /******************************************************************************
558 *
559 * Like strscat, except only the portion of the source string up to
560 * the provided delimiter is copied.
561 *
562 * str = strpcpy(str,"This is a line of text with no trailing newline","x");
563 * str = strpcat(str,"This is a line of text with no trailing newline","x");
564 * printf("%s\n",str);
565 *
566 *This is a line of texThis is a line of tex
567 *
568 *****************************************************************************/
570 char *
571 strpcat (char *dest, const char *src, const char *str)
572 {
573 size_t len, l2;
575 if (dest)
576 len = strlen (dest);
577 else
578 len = 0;
580 if (src) {
581 l2 = strcspn (src, str);
582 }
583 else {
584 return dest;
585 }
587 dest = realloc (dest, len + l2 + 1);
588 if (dest == NULL)
589 terminate (STATE_UNKNOWN, "failed malloc in strscat\n");
591 strncpy (dest + len, src, l2);
592 dest[len + l2] = '\0';
594 return dest;
595 }