Code

add function for elapsed tim ein microseconds
[nagiosplug.git] / plugins / utils.c
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 #define LOCAL_TIMEOUT_ALARM_HANDLER
16 #include "config.h"
17 #include "common.h"
18 #include "utils.h"
19 #include <stdarg.h>
20 #include <limits.h>
22 #include <arpa/inet.h>
24 extern void print_usage (void);
25 extern const char *progname;
27 #define STRLEN 64
28 #define TXTBLK 128
30 /* **************************************************************************
31  * max_state(STATE_x, STATE_y)
32  * compares STATE_x to  STATE_y and returns result based on the following
33  * STATE_UNKNOWN < STATE_OK < STATE_WARNING < STATE_CRITICAL
34  *
35  * Note that numerically the above does not hold
36  ****************************************************************************/
38 int
39 max_state (int a, int b)
40 {
41         if (a == STATE_CRITICAL || b == STATE_CRITICAL)
42                 return STATE_CRITICAL;
43         else if (a == STATE_WARNING || b == STATE_WARNING)
44                 return STATE_WARNING;
45         else if (a == STATE_OK || b == STATE_OK)
46                 return STATE_OK;
47         else if (a == STATE_UNKNOWN || b == STATE_UNKNOWN)
48                 return STATE_UNKNOWN;
49         else if (a == STATE_DEPENDENT || b == STATE_DEPENDENT)
50                 return STATE_DEPENDENT;
51         else
52                 return max (a, b);
53 }
55 void usage (char *msg)
56 {
57         printf ("%s", msg);
58         print_usage ();
59         exit (STATE_UNKNOWN);
60 }
62 void usage2(char *msg, char *arg)
63 {
64         printf ("%s: %s - %s\n",progname,msg,arg);
65         print_usage ();
66         exit (STATE_UNKNOWN);
67 }
69 void
70 usage3 (char *msg, int arg)
71 {
72         printf ("%s: %s - %c\n", progname, msg, arg);
73         print_usage();
74         exit (STATE_UNKNOWN);
75 }
78 void
79 support (void)
80 {
81         printf (_("\n\
82 Send email to nagios-users@lists.sourceforge.net if you have questions\n\
83 regarding use of this software. To submit patches or suggest improvements,\n\
84 send email to nagiosplug-devel@lists.sourceforge.net\n"));
85 }
88 char *
89 clean_revstring (const char *revstring)
90 {
91         char plugin_revision[STRLEN];
92         if (sscanf (revstring,"$Revision: %[0-9.]",plugin_revision) == 1)
93                 return strscpy (NULL, plugin_revision);
94         else
95           return strscpy (NULL, "N/A");
96 }
98 void
99 print_revision (const char *command_name, const char *revision_string)
101         char plugin_revision[STRLEN];
103         if (sscanf (revision_string, "$Revision: %[0-9.]", plugin_revision) != 1)
104                 strncpy (plugin_revision, "N/A", STRLEN);
105         printf ("%s (%s %s) %s\n",
106                                         command_name, PACKAGE, VERSION, plugin_revision);
107         printf (_("\
108 The nagios plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\n\
109 copies of the plugins under the terms of the GNU General Public License.\n\
110 For more information about these matters, see the file named COPYING.\n"));
114 const char *
115 state_text (int result)
117         switch (result) {
118         case STATE_OK:
119                 return "OK";
120         case STATE_WARNING:
121                 return "WARNING";
122         case STATE_CRITICAL:
123                 return "CRITICAL";
124         case STATE_DEPENDENT:
125                 return "DEPENDENT";
126         default:
127                 return "UNKNOWN";
128         }
131 void
132 die (int result, const char *fmt, ...)
134         va_list ap;
135         va_start (ap, fmt);
136         vprintf (fmt, ap);
137         va_end (ap);
138         exit (result);
141 void
142 timeout_alarm_handler (int signo)
144         if (signo == SIGALRM) {
145                 printf ("CRITICAL - Plugin timed out after %d seconds\n",
146                                                 timeout_interval);
147                 exit (STATE_CRITICAL);
148         }
151 int
152 is_numeric (char *number)
154         char tmp[1];
155         float x;
157         if (!number)
158                 return FALSE;
159         else if (sscanf (number, "%f%c", &x, tmp) == 1)
160                 return TRUE;
161         else
162                 return FALSE;
165 int
166 is_positive (char *number)
168         if (is_numeric (number) && atof (number) > 0.0)
169                 return TRUE;
170         else
171                 return FALSE;
174 int
175 is_negative (char *number)
177         if (is_numeric (number) && atof (number) < 0.0)
178                 return TRUE;
179         else
180                 return FALSE;
183 int
184 is_nonnegative (char *number)
186         if (is_numeric (number) && atof (number) >= 0.0)
187                 return TRUE;
188         else
189                 return FALSE;
192 int
193 is_percentage (char *number)
195         int x;
196         if (is_numeric (number) && (x = atof (number)) >= 0 && x <= 100)
197                 return TRUE;
198         else
199                 return FALSE;
202 int
203 is_integer (char *number)
205         long int n;
207         if (!number || (strspn (number, "-0123456789 ") != strlen (number)))
208                 return FALSE;
210         n = strtol (number, NULL, 10);
212         if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX)
213                 return TRUE;
214         else
215                 return FALSE;
218 int
219 is_intpos (char *number)
221         if (is_integer (number) && atoi (number) > 0)
222                 return TRUE;
223         else
224                 return FALSE;
227 int
228 is_intneg (char *number)
230         if (is_integer (number) && atoi (number) < 0)
231                 return TRUE;
232         else
233                 return FALSE;
236 int
237 is_intnonneg (char *number)
239         if (is_integer (number) && atoi (number) >= 0)
240                 return TRUE;
241         else
242                 return FALSE;
245 int
246 is_intpercent (char *number)
248         int i;
249         if (is_integer (number) && (i = atoi (number)) >= 0 && i <= 100)
250                 return TRUE;
251         else
252                 return FALSE;
255 int
256 is_option (char *str)
258         if (!str)
259                 return FALSE;
260         else if (strspn (str, "-") == 1 || strspn (str, "-") == 2)
261                 return TRUE;
262         else
263                 return FALSE;
268 #ifdef NEED_GETTIMEOFDAY
269 int
270 gettimeofday (struct timeval *tv, struct timezone *tz)
272         tv->tv_usec = 0;
273         tv->tv_sec = (long) time ((time_t) 0);
275 #endif
279 double
280 delta_time (struct timeval tv)
282         struct timeval now;
284         gettimeofday (&now, NULL);
285         return ((double)(now.tv_sec - tv.tv_sec) + (double)(now.tv_usec - tv.tv_usec) / (double)1000000);
290 long
291 deltime (struct timeval tv)
293         struct timeval now;
294         gettimeofday (&now, NULL);
295         return (now.tv_sec - tv.tv_sec)*1000000 + now.tv_usec - tv.tv_usec;
301 void
302 strip (char *buffer)
304         size_t x;
305         int i;
307         for (x = strlen (buffer); x >= 1; x--) {
308                 i = x - 1;
309                 if (buffer[i] == ' ' ||
310                                 buffer[i] == '\r' || buffer[i] == '\n' || buffer[i] == '\t')
311                         buffer[i] = '\0';
312                 else
313                         break;
314         }
315         return;
322 /******************************************************************************
323  *
324  * Copies one string to another. Any previously existing data in
325  * the destination string is lost.
326  *
327  * Example:
328  *
329  * char *str=NULL;
330  * str = strscpy("This is a line of text with no trailing newline");
331  *
332  *****************************************************************************/
334 char *
335 strscpy (char *dest, const char *src)
337         if (src == NULL)
338                 return NULL;
340         asprintf (&dest, "%s", src);
342         return dest;
349 /******************************************************************************
350  *
351  * Returns a pointer to the next line of a multiline string buffer
352  *
353  * Given a pointer string, find the text following the next sequence
354  * of \r and \n characters. This has the effect of skipping blank
355  * lines as well
356  *
357  * Example:
358  *
359  * Given text as follows:
360  *
361  * ==============================
362  * This
363  * is
364  * a
365  * 
366  * multiline string buffer
367  * ==============================
368  *
369  * int i=0;
370  * char *str=NULL;
371  * char *ptr=NULL;
372  * str = strscpy(str,"This\nis\r\na\n\nmultiline string buffer\n");
373  * ptr = str;
374  * while (ptr) {
375  *   printf("%d %s",i++,firstword(ptr));
376  *   ptr = strnl(ptr);
377  * }
378  * 
379  * Produces the following:
380  *
381  * 1 This
382  * 2 is
383  * 3 a
384  * 4 multiline
385  *
386  * NOTE: The 'firstword()' function is conceptual only and does not
387  *       exist in this package.
388  *
389  * NOTE: Although the second 'ptr' variable is not strictly needed in
390  *       this example, it is good practice with these utilities. Once
391  *       the * pointer is advance in this manner, it may no longer be
392  *       handled with * realloc(). So at the end of the code fragment
393  *       above, * strscpy(str,"foo") work perfectly fine, but
394  *       strscpy(ptr,"foo") will * cause the the program to crash with
395  *       a segmentation fault.
396  *
397  *****************************************************************************/
399 char *
400 strnl (char *str)
402         size_t len;
403         if (str == NULL)
404                 return NULL;
405         str = strpbrk (str, "\r\n");
406         if (str == NULL)
407                 return NULL;
408         len = strspn (str, "\r\n");
409         if (str[len] == '\0')
410                 return NULL;
411         str += len;
412         if (strlen (str) == 0)
413                 return NULL;
414         return str;
421 /******************************************************************************
422  *
423  * Like strscpy, except only the portion of the source string up to
424  * the provided delimiter is copied.
425  *
426  * Example:
427  *
428  * str = strpcpy(str,"This is a line of text with no trailing newline","x");
429  * printf("%s\n",str);
430  *
431  * Produces:
432  *
433  *This is a line of te
434  *
435  *****************************************************************************/
437 char *
438 strpcpy (char *dest, const char *src, const char *str)
440         size_t len;
442         if (src)
443                 len = strcspn (src, str);
444         else
445                 return NULL;
447         if (dest == NULL || strlen (dest) < len)
448                 dest = realloc (dest, len + 1);
449         if (dest == NULL)
450                 die (STATE_UNKNOWN, "failed realloc in strpcpy\n");
452         strncpy (dest, src, len);
453         dest[len] = '\0';
455         return dest;
462 /******************************************************************************
463  *
464  * Like strscat, except only the portion of the source string up to
465  * the provided delimiter is copied.
466  *
467  * str = strpcpy(str,"This is a line of text with no trailing newline","x");
468  * str = strpcat(str,"This is a line of text with no trailing newline","x");
469  * printf("%s\n",str);
470  * 
471  *This is a line of texThis is a line of tex
472  *
473  *****************************************************************************/
475 char *
476 strpcat (char *dest, const char *src, const char *str)
478         size_t len, l2;
480         if (dest)
481                 len = strlen (dest);
482         else
483                 len = 0;
485         if (src) {
486                 l2 = strcspn (src, str);
487         }
488         else {
489                 return dest;
490         }
492         dest = realloc (dest, len + l2 + 1);
493         if (dest == NULL)
494                 die (STATE_UNKNOWN, "failed malloc in strscat\n");
496         strncpy (dest + len, src, l2);
497         dest[len + l2] = '\0';
499         return dest;