Back to index

nagios-plugins  1.4.16
Defines | Functions | Variables
utils.c File Reference
#include "common.h"
#include "utils.h"
#include "utils_base.h"
#include <stdarg.h>
#include <limits.h>
#include <arpa/inet.h>

Go to the source code of this file.

Defines

#define LOCAL_TIMEOUT_ALARM_HANDLER
#define STRLEN   64
#define TXTBLK   128

Functions

void print_usage (void)
int max_state (int a, int b)
int max_state_alt (int a, int b)
void usage (const char *msg)
void usage_va (const char *fmt,...)
void usage2 (const char *msg, const char *arg)
void usage3 (const char *msg, int arg)
void usage4 (const char *msg)
void usage5 (void)
void print_revision (const char *command_name, const char *revision)
const char * state_text (int result)
void timeout_alarm_handler (int signo)
int is_numeric (char *number)
int is_positive (char *number)
int is_negative (char *number)
int is_nonnegative (char *number)
int is_percentage (char *number)
int is_integer (char *number)
int is_intpos (char *number)
int is_intneg (char *number)
int is_intnonneg (char *number)
int is_intpercent (char *number)
int is_option (char *str)
double delta_time (struct timeval tv)
long deltime (struct timeval tv)
void strip (char *buffer)
char * strscpy (char *dest, const char *src)
char * strnl (char *str)
char * strpcpy (char *dest, const char *src, const char *str)
char * strpcat (char *dest, const char *src, const char *str)
char * perfdata (const char *label, long int val, const char *uom, int warnp, long int warn, int critp, long int crit, int minp, long int minv, int maxp, long int maxv)
char * fperfdata (const char *label, double val, const char *uom, int warnp, double warn, int critp, double crit, int minp, double minv, int maxp, double maxv)

Variables

const char * progname

Define Documentation

Definition at line 25 of file utils.c.

#define STRLEN   64

Definition at line 38 of file utils.c.

#define TXTBLK   128

Definition at line 39 of file utils.c.


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 *  label,
double  val,
const char *  uom,
int  warnp,
double  warn,
int  critp,
double  crit,
int  minp,
double  minv,
int  maxp,
double  maxv 
)

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 is_integer ( char *  number)

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 *  number)

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 *  number)

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 *  number)

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 *  number)

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 *  number)

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 *  number)

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 *  number)

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 *  str)

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 *  number)

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 *  number)

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 *  label,
long int  val,
const char *  uom,
int  warnp,
long int  warn,
int  critp,
long int  crit,
int  minp,
long int  minv,
int  maxp,
long int  maxv 
)

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 *  command_name,
const char *  revision 
)

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:

void print_usage ( void  )

Definition at line 389 of file check_cpqarray.c.

{
  printf("cpqarrayd [options]\n");
  printf("   -h         prints this text\n");
  printf("   -d         enables debugging\n");
}

Here is the caller graph for this function:

const char* state_text ( int  result)

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 *  buffer)

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 *  str)

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 *  dest,
const char *  src,
const char *  str 
)

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 *  dest,
const char *  src,
const char *  str 
)

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 *  dest,
const char *  src 
)

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 timeout_alarm_handler ( int  signo)

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 *  msg)

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 *  msg,
const char *  arg 
)

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 *  msg,
int  arg 
)

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 *  msg)

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

const char* progname

Definition at line 39 of file check_http-with-client-certificate.c.