Back to index

nagios-plugins  1.4.16
Defines | Enumerations | Functions | Variables
check_time.c File Reference
#include "common.h"
#include "netutils.h"
#include "utils.h"

Go to the source code of this file.

Defines

#define UNIX_EPOCH   2208988800UL

Enumerations

enum  { TIME_PORT = 37 }

Functions

int process_arguments (int, char **)
void print_help (void)
void print_usage (void)
int main (int argc, char **argv)

Variables

const char * progname = "check_time"
const char * copyright = "1999-2007"
const char * email = "nagiosplug-devel@lists.sourceforge.net"
uint32_t raw_server_time
unsigned long server_time
unsigned long diff_time
int warning_time = 0
int check_warning_time = FALSE
int critical_time = 0
int check_critical_time = FALSE
unsigned long warning_diff = 0
int check_warning_diff = FALSE
unsigned long critical_diff = 0
int check_critical_diff = FALSE
int server_port = TIME_PORT
char * server_address = NULL
int use_udp = FALSE

Define Documentation

#define UNIX_EPOCH   2208988800UL

Definition at line 43 of file check_time.c.


Enumeration Type Documentation

anonymous enum
Enumerator:
TIME_PORT 

Definition at line 39 of file check_time.c.

     {
       TIME_PORT = 37
};

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 64 of file check_time.c.

{
       int sd;
       int result = STATE_UNKNOWN;
       time_t conntime;

       setlocale (LC_ALL, "");
       bindtextdomain (PACKAGE, LOCALEDIR);
       textdomain (PACKAGE);

       /* Parse extra opts if any */
       argv=np_extra_opts (&argc, argv, progname);

       if (process_arguments (argc, argv) == ERROR)
              usage4 (_("Could not parse arguments"));

       /* initialize alarm signal handling */
       signal (SIGALRM, socket_timeout_alarm_handler);

       /* set socket timeout */
       alarm (socket_timeout);
       time (&start_time);

       /* try to connect to the host at the given port number */
       if (use_udp) {
              result = my_udp_connect (server_address, server_port, &sd);
       } else {
              result = my_tcp_connect (server_address, server_port, &sd);
       }

       if (result != STATE_OK) {
              if (check_critical_time == TRUE)
                     result = STATE_CRITICAL;
              else if (check_warning_time == TRUE)
                     result = STATE_WARNING;
              else
                     result = STATE_UNKNOWN;
              die (result,
                         _("TIME UNKNOWN - could not connect to server %s, port %d\n"),
                         server_address, server_port);
       }

       if (use_udp) {
              if (send (sd, "", 0, 0) < 0) {
                     if (check_critical_time == TRUE)
                            result = STATE_CRITICAL;
                     else if (check_warning_time == TRUE)
                            result = STATE_WARNING;
                     else
                            result = STATE_UNKNOWN;
                     die (result,
                       _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"),
                       server_address, server_port);
              }
       }

       /* watch for the connection string */
       result = recv (sd, (void *)&raw_server_time, sizeof (raw_server_time), 0);

       /* close the connection */
       close (sd);

       /* reset the alarm */
       time (&end_time);
       alarm (0);

       /* return a WARNING status if we couldn't read any data */
       if (result <= 0) {
              if (check_critical_time == TRUE)
                     result = STATE_CRITICAL;
              else if (check_warning_time == TRUE)
                     result = STATE_WARNING;
              else
                     result = STATE_UNKNOWN;
              die (result,
                                                  _("TIME UNKNOWN - no data received from server %s, port %d\n"),
                                                  server_address, server_port);
       }

       result = STATE_OK;

       conntime = (end_time - start_time);
       if (check_critical_time == TRUE && conntime > critical_time)
              result = STATE_CRITICAL;
       else if (check_warning_time == TRUE && conntime > warning_time)
              result = STATE_WARNING;

       if (result != STATE_OK)
              die (result, _("TIME %s - %d second response time|%s\n"),
                   state_text (result), (int)conntime,
                   perfdata ("time", (long)conntime, "s",
                             check_warning_time, (long)warning_time,
                             check_critical_time, (long)critical_time,
                             TRUE, 0, FALSE, 0));

       server_time = ntohl (raw_server_time) - UNIX_EPOCH;
       if (server_time > (unsigned long)end_time)
              diff_time = server_time - (unsigned long)end_time;
       else
              diff_time = (unsigned long)end_time - server_time;

       if (check_critical_diff == TRUE && diff_time > critical_diff)
              result = STATE_CRITICAL;
       else if (check_warning_diff == TRUE && diff_time > warning_diff)
              result = STATE_WARNING;

       printf (_("TIME %s - %lu second time difference|%s %s\n"),
               state_text (result), diff_time,
               perfdata ("time", (long)conntime, "s",
                         check_warning_time, (long)warning_time,
                         check_critical_time, (long)critical_time,
                         TRUE, 0, FALSE, 0),
               perfdata ("offset", diff_time, "s",
                         check_warning_diff, warning_diff,
                         check_critical_diff, critical_diff,
                         TRUE, 0, FALSE, 0));
       return result;
}

Here is the call graph for this function:

void print_help ( void  )
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");
}
int process_arguments ( int  argc,
char **  argv 
)

Variable Documentation

Definition at line 54 of file check_time.c.

Definition at line 50 of file check_time.c.

Definition at line 52 of file check_time.c.

Definition at line 48 of file check_time.c.

const char* copyright = "1999-2007"

Definition at line 32 of file check_time.c.

unsigned long critical_diff = 0

Definition at line 53 of file check_time.c.

int critical_time = 0

Definition at line 49 of file check_time.c.

unsigned long diff_time

Definition at line 46 of file check_time.c.

const char* email = "nagiosplug-devel@lists.sourceforge.net"

Definition at line 33 of file check_time.c.

const char* progname = "check_time"

Definition at line 31 of file check_time.c.

Definition at line 45 of file check_time.c.

Definition at line 56 of file check_time.c.

Definition at line 55 of file check_time.c.

unsigned long server_time

Definition at line 46 of file check_time.c.

int use_udp = FALSE

Definition at line 57 of file check_time.c.

unsigned long warning_diff = 0

Definition at line 51 of file check_time.c.

int warning_time = 0

Definition at line 47 of file check_time.c.