Back to index

nagios-plugins  1.4.16
Classes | Defines | Functions | Variables
utils.h File Reference
#include "utils_base.h"

Go to the source code of this file.

Classes

struct  timeval

Defines

#define np_extra_opts(acptr, av, pr)   av
#define max(a, b)   (((a)>(b))?(a):(b))
#define min(a, b)   (((a)<(b))?(a):(b))
#define STD_LONG_OPTS
#define COPYRIGHT   "Copyright (c) %s Nagios Plugin Development Team\n\\t<%s>\n\n"
#define UT_HLP_VRS   _("\ %s (-h | --help) for detailed help\n\ %s (-V | --version) for version information\n")
#define UT_HELP_VRSN   _("\\nOptions:\n\ -h, --help\n\ Print detailed help screen\n\ -V, --version\n\ Print version information\n")
#define UT_HOST_PORT   _("\ -H, --hostname=ADDRESS\n\ Host name, IP Address, or unix socket (must be an absolute path)\n\ -%c, --port=INTEGER\n\ Port number (default: %s)\n")
#define UT_IPv46   _("\ -4, --use-ipv4\n\ Use IPv4 connection\n\ -6, --use-ipv6\n\ Use IPv6 connection\n")
#define UT_VERBOSE   _("\ -v, --verbose\n\ Show details for command-line debugging (Nagios may truncate output)\n")
#define UT_WARN_CRIT   _("\ -w, --warning=DOUBLE\n\ Response time to result in warning status (seconds)\n\ -c, --critical=DOUBLE\n\ Response time to result in critical status (seconds)\n")
#define UT_WARN_CRIT_RANGE   _("\ -w, --warning=RANGE\n\ Warning range (format: start:end). Alert if outside this range\n\ -c, --critical=RANGE\n\ Critical range\n")
#define UT_TIMEOUT   _("\ -t, --timeout=INTEGER\n\ Seconds before connection times out (default: %d)\n")
#define UT_EXTRA_OPTS   ""
#define UT_THRESHOLDS_NOTES   _("\ See:\n\ http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT\n\ for THRESHOLD format and examples.\n")
#define UT_SUPPORT   _("\n\Send email to nagios-users@lists.sourceforge.net if you have questions\n\regarding use of this software. To submit patches or suggest improvements,\n\send email to nagiosplug-devel@lists.sourceforge.net\n\n")
#define UT_NOWARRANTY   _("\n\The nagios plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\n\copies of the plugins under the terms of the GNU General Public License.\n\For more information about these matters, see the file named COPYING.\n")

Functions

void support (void)
void print_revision (const char *, const char *)
RETSIGTYPE timeout_alarm_handler (int)
int is_integer (char *)
int is_intpos (char *)
int is_intneg (char *)
int is_intnonneg (char *)
int is_intpercent (char *)
int is_numeric (char *)
int is_positive (char *)
int is_negative (char *)
int is_nonnegative (char *)
int is_percentage (char *)
int is_option (char *)
int gettimeofday (struct timeval *, struct timezone *)
double delta_time (struct timeval tv)
long deltime (struct timeval tv)
void strip (char *)
char * strscpy (char *, const char *)
char * strnl (char *)
char * strpcpy (char *, const char *, const char *)
char * strpcat (char *, const char *, const char *)
int max_state (int a, int b)
int max_state_alt (int a, int b)
void usage (const char *) __attribute__((noreturn))
void usage2 (const char *, const char *) __attribute__((noreturn))
void usage3 (const char *, int) __attribute__((noreturn))
void usage4 (const char *) __attribute__((noreturn))
void usage5 (void) __attribute__((noreturn))
void usage_va (const char *fmt,...) __attribute__((noreturn))
const char * state_text (int)
char * perfdata (const char *, long int, const char *, int, long int, int, long int, int, long int, int, long int)
char * fperfdata (const char *, double, const char *, int, double, int, double, int, double, int, double)

Variables

unsigned int timeout_state = STATE_CRITICAL
unsigned int timeout_interval = DEFAULT_SOCKET_TIMEOUT
time_t start_time
time_t end_time

Class Documentation

struct timeval

Definition at line 64 of file utils.h.

Class Members
long tv_sec
long tv_usec

Define Documentation

#define COPYRIGHT   "Copyright (c) %s Nagios Plugin Development Team\n\\t<%s>\n\n"

Definition at line 137 of file utils.h.

#define max (   a,
 
)    (((a)>(b))?(a):(b))

Definition at line 97 of file utils.h.

#define min (   a,
 
)    (((a)<(b))?(a):(b))

Definition at line 98 of file utils.h.

#define np_extra_opts (   acptr,
  av,
  pr 
)    av

Definition at line 24 of file utils.h.

#define STD_LONG_OPTS
Value:
{"version",no_argument,0,'V'},\
{"verbose",no_argument,0,'v'},\
{"help",no_argument,0,'h'},\
{"timeout",required_argument,0,'t'},\
{"critical",required_argument,0,'c'},\
{"warning",required_argument,0,'w'},\
{"hostname",required_argument,0,'H'}

Definition at line 128 of file utils.h.

#define UT_EXTRA_OPTS   ""

Definition at line 189 of file utils.h.

#define UT_HELP_VRSN   _("\\nOptions:\n\ -h, --help\n\ Print detailed help screen\n\ -V, --version\n\ Print version information\n")

Definition at line 144 of file utils.h.

#define UT_HLP_VRS   _("\ %s (-h | --help) for detailed help\n\ %s (-V | --version) for version information\n")

Definition at line 140 of file utils.h.

#define UT_HOST_PORT   _("\ -H, --hostname=ADDRESS\n\ Host name, IP Address, or unix socket (must be an absolute path)\n\ -%c, --port=INTEGER\n\ Port number (default: %s)\n")

Definition at line 151 of file utils.h.

#define UT_IPv46   _("\ -4, --use-ipv4\n\ Use IPv4 connection\n\ -6, --use-ipv6\n\ Use IPv6 connection\n")

Definition at line 157 of file utils.h.

#define UT_NOWARRANTY   _("\n\The nagios plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\n\copies of the plugins under the terms of the GNU General Public License.\n\For more information about these matters, see the file named COPYING.\n")

Definition at line 202 of file utils.h.

#define UT_SUPPORT   _("\n\Send email to nagios-users@lists.sourceforge.net if you have questions\n\regarding use of this software. To submit patches or suggest improvements,\n\send email to nagiosplug-devel@lists.sourceforge.net\n\n")

Definition at line 197 of file utils.h.

#define UT_THRESHOLDS_NOTES   _("\ See:\n\ http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT\n\ for THRESHOLD format and examples.\n")

Definition at line 192 of file utils.h.

#define UT_TIMEOUT   _("\ -t, --timeout=INTEGER\n\ Seconds before connection times out (default: %d)\n")

Definition at line 179 of file utils.h.

#define UT_VERBOSE   _("\ -v, --verbose\n\ Show details for command-line debugging (Nagios may truncate output)\n")

Definition at line 163 of file utils.h.

#define UT_WARN_CRIT   _("\ -w, --warning=DOUBLE\n\ Response time to result in warning status (seconds)\n\ -c, --critical=DOUBLE\n\ Response time to result in critical status (seconds)\n")

Definition at line 167 of file utils.h.

#define UT_WARN_CRIT_RANGE   _("\ -w, --warning=RANGE\n\ Warning range (format: start:end). Alert if outside this range\n\ -c, --critical=RANGE\n\ Critical range\n")

Definition at line 173 of file utils.h.


Function Documentation

double delta_time ( struct timeval  tv)

Definition at line 304 of file utils.c.

{
       struct timeval now;

       gettimeofday (&now, NULL);
       return ((double)(now.tv_sec - tv.tv_sec) + (double)(now.tv_usec - tv.tv_usec) / (double)1000000);
}

Here is the call graph for this function:

Here is the caller graph for this function:

long deltime ( struct timeval  tv)

Definition at line 315 of file utils.c.

{
       struct timeval now;
       gettimeofday (&now, NULL);
       return (now.tv_sec - tv.tv_sec)*1000000 + now.tv_usec - tv.tv_usec;
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* fperfdata ( const char *  ,
double  ,
const char *  ,
int  ,
double  ,
int  ,
double  ,
int  ,
double  ,
int  ,
double   
)

Definition at line 561 of file utils.c.

{
       char *data = NULL;

       if (strpbrk (label, "'= "))
              asprintf (&data, "'%s'=", label);
       else
              asprintf (&data, "%s=", label);

       asprintf (&data, "%s%f", data, val);
       asprintf (&data, "%s%s;", data, uom);

       if (warnp)
              asprintf (&data, "%s%f", data, warn);

       asprintf (&data, "%s;", data);

       if (critp)
              asprintf (&data, "%s%f", data, crit);

       asprintf (&data, "%s;", data);

       if (minp)
              asprintf (&data, "%s%f", data, minv);

       if (maxp) {
              asprintf (&data, "%s;", data);
              asprintf (&data, "%s%f", data, maxv);
       }

       return data;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int gettimeofday ( struct timeval ,
struct timezone  
)

Here is the caller graph for this function:

int is_integer ( char *  )

Definition at line 229 of file utils.c.

{
       long int n;

       if (!number || (strspn (number, "-0123456789 ") != strlen (number)))
              return FALSE;

       n = strtol (number, NULL, 10);

       if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX)
              return TRUE;
       else
              return FALSE;
}

Here is the caller graph for this function:

int is_intneg ( char *  )

Definition at line 254 of file utils.c.

{
       if (is_integer (number) && atoi (number) < 0)
              return TRUE;
       else
              return FALSE;
}

Here is the call graph for this function:

int is_intnonneg ( char *  )

Definition at line 263 of file utils.c.

{
       if (is_integer (number) && atoi (number) >= 0)
              return TRUE;
       else
              return FALSE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int is_intpercent ( char *  )

Definition at line 272 of file utils.c.

{
       int i;
       if (is_integer (number) && (i = atoi (number)) >= 0 && i <= 100)
              return TRUE;
       else
              return FALSE;
}

Here is the call graph for this function:

int is_intpos ( char *  )

Definition at line 245 of file utils.c.

{
       if (is_integer (number) && atoi (number) > 0)
              return TRUE;
       else
              return FALSE;
}

Here is the call graph for this function:

int is_negative ( char *  )

Definition at line 201 of file utils.c.

{
       if (is_numeric (number) && atof (number) < 0.0)
              return TRUE;
       else
              return FALSE;
}

Here is the call graph for this function:

int is_nonnegative ( char *  )

Definition at line 210 of file utils.c.

{
       if (is_numeric (number) && atof (number) >= 0.0)
              return TRUE;
       else
              return FALSE;
}

Here is the call graph for this function:

int is_numeric ( char *  )

Definition at line 178 of file utils.c.

{
       char tmp[1];
       float x;

       if (!number)
              return FALSE;
       else if (sscanf (number, "%f%c", &x, tmp) == 1)
              return TRUE;
       else
              return FALSE;
}

Here is the caller graph for this function:

int is_option ( char *  )

Definition at line 282 of file utils.c.

{
       if (!str)
              return FALSE;
       else if (strspn (str, "-") == 1 || strspn (str, "-") == 2)
              return TRUE;
       else
              return FALSE;
}
int is_percentage ( char *  )

Definition at line 219 of file utils.c.

{
       int x;
       if (is_numeric (number) && (x = atof (number)) >= 0 && x <= 100)
              return TRUE;
       else
              return FALSE;
}

Here is the call graph for this function:

int is_positive ( char *  )

Definition at line 192 of file utils.c.

{
       if (is_numeric (number) && atof (number) > 0.0)
              return TRUE;
       else
              return FALSE;
}

Here is the call graph for this function:

int max_state ( int  a,
int  b 
)

Definition at line 50 of file utils.c.

{
       if (a == STATE_CRITICAL || b == STATE_CRITICAL)
              return STATE_CRITICAL;
       else if (a == STATE_WARNING || b == STATE_WARNING)
              return STATE_WARNING;
       else if (a == STATE_OK || b == STATE_OK)
              return STATE_OK;
       else if (a == STATE_UNKNOWN || b == STATE_UNKNOWN)
              return STATE_UNKNOWN;
       else if (a == STATE_DEPENDENT || b == STATE_DEPENDENT)
              return STATE_DEPENDENT;
       else
              return max (a, b);
}

Here is the caller graph for this function:

int max_state_alt ( int  a,
int  b 
)

Definition at line 77 of file utils.c.

{
       if (a == STATE_CRITICAL || b == STATE_CRITICAL)
              return STATE_CRITICAL;
       else if (a == STATE_WARNING || b == STATE_WARNING)
              return STATE_WARNING;
       else if (a == STATE_UNKNOWN || b == STATE_UNKNOWN)
              return STATE_UNKNOWN;
       else if (a == STATE_DEPENDENT || b == STATE_DEPENDENT)
              return STATE_DEPENDENT;
       else if (a == STATE_OK || b == STATE_OK)
              return STATE_OK;
       else
              return max (a, b);
}

Here is the caller graph for this function:

char* perfdata ( const char *  ,
long  int,
const char *  ,
int  ,
long  int,
int  ,
long  int,
int  ,
long  int,
int  ,
long  int 
)

Definition at line 522 of file utils.c.

{
       char *data = NULL;

       if (strpbrk (label, "'= "))
              asprintf (&data, "'%s'=%ld%s;", label, val, uom);
       else
              asprintf (&data, "%s=%ld%s;", label, val, uom);

       if (warnp)
              asprintf (&data, "%s%ld;", data, warn);
       else
              asprintf (&data, "%s;", data);

       if (critp)
              asprintf (&data, "%s%ld;", data, crit);
       else
              asprintf (&data, "%s;", data);

       if (minp)
              asprintf (&data, "%s%ld", data, minv);

       if (maxp)
              asprintf (&data, "%s;%ld", data, maxv);

       return data;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void print_revision ( const char *  ,
const char *   
)

Definition at line 142 of file utils.c.

{
       char plugin_revision[STRLEN];

       printf ("%s v%s (%s %s)\n",
                command_name, revision, PACKAGE, VERSION);
}

Here is the caller graph for this function:

const char* state_text ( int  )

Definition at line 151 of file utils.c.

{
       switch (result) {
       case STATE_OK:
              return "OK";
       case STATE_WARNING:
              return "WARNING";
       case STATE_CRITICAL:
              return "CRITICAL";
       case STATE_DEPENDENT:
              return "DEPENDENT";
       default:
              return "UNKNOWN";
       }
}

Here is the caller graph for this function:

void strip ( char *  )

Definition at line 326 of file utils.c.

{
       size_t x;
       int i;

       for (x = strlen (buffer); x >= 1; x--) {
              i = x - 1;
              if (buffer[i] == ' ' ||
                            buffer[i] == '\r' || buffer[i] == '\n' || buffer[i] == '\t')
                     buffer[i] = '\0';
              else
                     break;
       }
       return;
}

Here is the caller graph for this function:

char* strnl ( char *  )

Definition at line 419 of file utils.c.

{
       size_t len;
       if (str == NULL)
              return NULL;
       str = strpbrk (str, "\r\n");
       if (str == NULL)
              return NULL;
       len = strspn (str, "\r\n");
       if (str[len] == '\0')
              return NULL;
       str += len;
       if (strlen (str) == 0)
              return NULL;
       return str;
}
char* strpcat ( char *  ,
const char *  ,
const char *   
)

Definition at line 490 of file utils.c.

{
       size_t len, l2;

       if (dest)
              len = strlen (dest);
       else
              len = 0;

       if (src) {
              l2 = strcspn (src, str);
       }
       else {
              return dest;
       }

       dest = realloc (dest, len + l2 + 1);
       if (dest == NULL)
              die (STATE_UNKNOWN, _("failed malloc in strscat\n"));

       strncpy (dest + len, src, l2);
       dest[len + l2] = '\0';

       return dest;
}

Here is the call graph for this function:

char* strpcpy ( char *  ,
const char *  ,
const char *   
)

Definition at line 454 of file utils.c.

{
       size_t len;

       if (src)
              len = strcspn (src, str);
       else
              return NULL;

       if (dest == NULL || strlen (dest) < len)
              dest = realloc (dest, len + 1);
       if (dest == NULL)
              die (STATE_UNKNOWN, _("failed realloc in strpcpy\n"));

       strncpy (dest, src, len);
       dest[len] = '\0';

       return dest;
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* strscpy ( char *  ,
const char *   
)

Definition at line 356 of file utils.c.

{
       if (src == NULL)
              return NULL;

       asprintf (&dest, "%s", src);

       return dest;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void support ( void  )
RETSIGTYPE timeout_alarm_handler ( int  )

Definition at line 168 of file utils.c.

{
       if (signo == SIGALRM) {
              printf (_("%s - Plugin timed out after %d seconds\n"),
                                          state_text(timeout_state), timeout_interval);
              exit (timeout_state);
       }
}

Here is the call graph for this function:

void usage ( const char *  )

Definition at line 93 of file utils.c.

{
       printf ("%s\n", msg);
       print_usage ();
       exit (STATE_UNKNOWN);
}

Here is the call graph for this function:

void usage2 ( const char *  ,
const char *   
)

Definition at line 111 of file utils.c.

{
       printf ("%s: %s - %s\n", progname, msg, arg?arg:"(null)" );
       print_usage ();
       exit (STATE_UNKNOWN);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void usage3 ( const char *  ,
int   
)

Definition at line 119 of file utils.c.

{
       printf ("%s: %s - %c\n", progname, msg, arg);
       print_usage();
       exit (STATE_UNKNOWN);
}

Here is the call graph for this function:

void usage4 ( const char *  )

Definition at line 127 of file utils.c.

{
       printf ("%s: %s\n", progname, msg);
       print_usage();
       exit (STATE_UNKNOWN);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void usage5 ( void  )

Definition at line 135 of file utils.c.

{
       print_usage();
       exit (STATE_UNKNOWN);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void usage_va ( const char *  fmt,
  ... 
)

Definition at line 100 of file utils.c.

{
       va_list ap;
       printf("%s: ", progname);
       va_start(ap, fmt);
       vprintf(fmt, ap);
       va_end(ap);
       printf("\n");
       exit (STATE_UNKNOWN);
}

Here is the caller graph for this function:


Variable Documentation

time_t end_time

Definition at line 44 of file utils.h.

time_t start_time

Definition at line 44 of file utils.h.

Definition at line 40 of file utils.h.

unsigned int timeout_state = STATE_CRITICAL

Definition at line 39 of file utils.h.