Back to index

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

Go to the source code of this file.

Defines

#define CHECK_NONE   0
#define UPS_NONE   0 /* no supported options */
#define UPS_UTILITY   1 /* supports utility line voltage */
#define UPS_BATTPCT   2 /* supports percent battery remaining */
#define UPS_STATUS   4 /* supports UPS status */
#define UPS_TEMP   8 /* supports UPS temperature */
#define UPS_LOADPCT   16 /* supports load percent */
#define UPSSTATUS_NONE   0
#define UPSSTATUS_OFF   1
#define UPSSTATUS_OL   2
#define UPSSTATUS_OB   4
#define UPSSTATUS_LB   8
#define UPSSTATUS_CAL   16
#define UPSSTATUS_RB   32 /*Replace Battery */
#define UPSSTATUS_BYPASS   64
#define UPSSTATUS_OVER   128
#define UPSSTATUS_TRIM   256
#define UPSSTATUS_BOOST   512
#define UPSSTATUS_CHRG   1024
#define UPSSTATUS_DISCHRG   2048
#define UPSSTATUS_UNKOWN   4096

Enumerations

enum  { PORT = 3493 }
enum  { NOSUCHVAR = ERROR-1 }

Functions

int determine_status (void)
int get_ups_variable (const char *, char *, size_t)
int process_arguments (int, char **)
int validate_arguments (void)
void print_help (void)
void print_usage (void)
int main (int argc, char **argv)

Variables

const char * progname = "check_ups"
const char * copyright = "2000-2007"
const char * email = "nagiosplug-devel@lists.sourceforge.net"
int server_port = PORT
char * server_address
char * ups_name = NULL
double warning_value = 0.0
double critical_value = 0.0
int check_warn = FALSE
int check_crit = FALSE
int check_variable = UPS_NONE
int supported_options = UPS_NONE
int status = UPSSTATUS_NONE
double ups_utility_voltage = 0.0
double ups_battery_percent = 0.0
double ups_load_percent = 0.0
double ups_temperature = 0.0
char * ups_status
int temp_output_c = 0

Define Documentation

#define CHECK_NONE   0

Definition at line 47 of file check_ups.c.

#define UPS_BATTPCT   2 /* supports percent battery remaining */

Definition at line 51 of file check_ups.c.

#define UPS_LOADPCT   16 /* supports load percent */

Definition at line 54 of file check_ups.c.

#define UPS_NONE   0 /* no supported options */

Definition at line 49 of file check_ups.c.

#define UPS_STATUS   4 /* supports UPS status */

Definition at line 52 of file check_ups.c.

#define UPS_TEMP   8 /* supports UPS temperature */

Definition at line 53 of file check_ups.c.

#define UPS_UTILITY   1 /* supports utility line voltage */

Definition at line 50 of file check_ups.c.

#define UPSSTATUS_BOOST   512

Definition at line 66 of file check_ups.c.

#define UPSSTATUS_BYPASS   64

Definition at line 63 of file check_ups.c.

#define UPSSTATUS_CAL   16

Definition at line 61 of file check_ups.c.

#define UPSSTATUS_CHRG   1024

Definition at line 67 of file check_ups.c.

#define UPSSTATUS_DISCHRG   2048

Definition at line 68 of file check_ups.c.

#define UPSSTATUS_LB   8

Definition at line 60 of file check_ups.c.

#define UPSSTATUS_NONE   0

Definition at line 56 of file check_ups.c.

#define UPSSTATUS_OB   4

Definition at line 59 of file check_ups.c.

#define UPSSTATUS_OFF   1

Definition at line 57 of file check_ups.c.

#define UPSSTATUS_OL   2

Definition at line 58 of file check_ups.c.

#define UPSSTATUS_OVER   128

Definition at line 64 of file check_ups.c.

#define UPSSTATUS_RB   32 /*Replace Battery */

Definition at line 62 of file check_ups.c.

#define UPSSTATUS_TRIM   256

Definition at line 65 of file check_ups.c.

#define UPSSTATUS_UNKOWN   4096

Definition at line 69 of file check_ups.c.


Enumeration Type Documentation

anonymous enum
Enumerator:
PORT 

Definition at line 43 of file check_ups.c.

     {
       PORT = 3493
};
anonymous enum
Enumerator:
NOSUCHVAR 

Definition at line 71 of file check_ups.c.

{ NOSUCHVAR = ERROR-1 };

Function Documentation

int determine_status ( void  )

Definition at line 338 of file check_ups.c.

{
       char recv_buffer[MAX_INPUT_BUFFER];
       char temp_buffer[MAX_INPUT_BUFFER];
       char *ptr;
       int res;

       res=get_ups_variable ("ups.status", recv_buffer, sizeof (recv_buffer));
       if (res == NOSUCHVAR) return OK;
       if (res != STATE_OK) {
              printf ("%s\n", _("Invalid response received from host"));
              return ERROR;
       }

       supported_options |= UPS_STATUS;

       strcpy (temp_buffer, recv_buffer);
       for (ptr = (char *) strtok (temp_buffer, " "); ptr != NULL;
                      ptr = (char *) strtok (NULL, " ")) {
              if (!strcmp (ptr, "OFF"))
                     status |= UPSSTATUS_OFF;
              else if (!strcmp (ptr, "OL"))
                     status |= UPSSTATUS_OL;
              else if (!strcmp (ptr, "OB"))
                     status |= UPSSTATUS_OB;
              else if (!strcmp (ptr, "LB"))
                     status |= UPSSTATUS_LB;
              else if (!strcmp (ptr, "CAL"))
                     status |= UPSSTATUS_CAL;
              else if (!strcmp (ptr, "RB"))
                     status |= UPSSTATUS_RB;
              else if (!strcmp (ptr, "BYPASS"))
                     status |= UPSSTATUS_BYPASS;
              else if (!strcmp (ptr, "OVER"))
                     status |= UPSSTATUS_OVER;
              else if (!strcmp (ptr, "TRIM"))
                     status |= UPSSTATUS_TRIM;
              else if (!strcmp (ptr, "BOOST"))
                     status |= UPSSTATUS_BOOST;
              else if (!strcmp (ptr, "CHRG"))
                     status |= UPSSTATUS_CHRG;
              else if (!strcmp (ptr, "DISCHRG"))
                     status |= UPSSTATUS_DISCHRG;
              else
                     status |= UPSSTATUS_UNKOWN;
       }

       return OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int get_ups_variable ( const char *  varname,
char *  buf,
size_t  buflen 
)

Definition at line 391 of file check_ups.c.

{
       /*  char command[MAX_INPUT_BUFFER]; */
       char temp_buffer[MAX_INPUT_BUFFER];
       char send_buffer[MAX_INPUT_BUFFER];
       char *ptr;
       char *logout = "OK Goodbye\n";
       int logout_len = strlen(logout);
       int len;

       *buf=0;

       /* create the command string to send to the UPS daemon */
       /* Add LOGOUT to avoid read failure logs */
       sprintf (send_buffer, "GET VAR %s %s\nLOGOUT\n", ups_name, varname);

       /* send the command to the daemon and get a response back */
       if (process_tcp_request
                     (server_address, server_port, send_buffer, temp_buffer,
                      sizeof (temp_buffer)) != STATE_OK) {
              printf ("%s\n", _("Invalid response received from host"));
              return ERROR;
       }

       ptr = temp_buffer;
       len = strlen(ptr);
       if (len > logout_len && strcmp (ptr + len - logout_len, logout) == 0) len -= logout_len;
       if (len > 0 && ptr[len-1] == '\n') ptr[len-1]=0;
       if (strcmp (ptr, "ERR UNKNOWN-UPS") == 0) {
              printf (_("CRITICAL - no such UPS '%s' on that host\n"), ups_name);
              return ERROR;
       }

       if (strcmp (ptr, "ERR VAR-NOT-SUPPORTED") == 0) {
              /*printf ("Error: Variable '%s' is not supported\n", varname);*/
              return NOSUCHVAR;
       }

       if (strcmp (ptr, "ERR DATA-STALE") == 0) {
              printf ("%s\n", _("CRITICAL - UPS data is stale"));
              return ERROR;
       }

       if (strncmp (ptr, "ERR", 3) == 0) {
              printf (_("Unknown error: %s\n"), ptr);
              return ERROR;
       }

       ptr = temp_buffer + strlen (varname) + strlen (ups_name) + 6;
       len = strlen(ptr);
       if (len < 2 || ptr[0] != '"' || ptr[len-1] != '"') {
              printf ("%s\n", _("Error: unable to parse variable"));
              return ERROR;
       }
       strncpy (buf, ptr+1, len - 2);
       buf[len - 2] = 0;

       return OK;
}

Here is the caller graph for this function:

int main ( int  argc,
char **  argv 
)

Definition at line 100 of file check_ups.c.

{
       int result = STATE_UNKNOWN;
       char *message;
       char *data;
       char *tunits;
       char temp_buffer[MAX_INPUT_BUFFER];
       double ups_utility_deviation = 0.0;
       int res;

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

       ups_status = strdup ("N/A");
       data = strdup ("");
       message = strdup ("");

       /* 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);

       /* get the ups status if possible */
       if (determine_status () != OK)
              return STATE_CRITICAL;
       if (supported_options & UPS_STATUS) {

              ups_status = strdup ("");
              result = STATE_OK;

              if (status & UPSSTATUS_OFF) {
                     asprintf (&ups_status, "Off");
                     result = STATE_CRITICAL;
              }
              else if ((status & (UPSSTATUS_OB | UPSSTATUS_LB)) ==
                                           (UPSSTATUS_OB | UPSSTATUS_LB)) {
                     asprintf (&ups_status, _("On Battery, Low Battery"));
                     result = STATE_CRITICAL;
              }
              else {
                     if (status & UPSSTATUS_OL) {
                            asprintf (&ups_status, "%s%s", ups_status, _("Online"));
                     }
                     if (status & UPSSTATUS_OB) {
                            asprintf (&ups_status, "%s%s", ups_status, _("On Battery"));
                            result = STATE_WARNING;
                     }
                     if (status & UPSSTATUS_LB) {
                            asprintf (&ups_status, "%s%s", ups_status, _(", Low Battery"));
                            result = STATE_WARNING;
                     }
                     if (status & UPSSTATUS_CAL) {
                            asprintf (&ups_status, "%s%s", ups_status, _(", Calibrating"));
                     }
                     if (status & UPSSTATUS_RB) {
                            asprintf (&ups_status, "%s%s", ups_status, _(", Replace Battery"));
                            result = STATE_WARNING;
                     }
                     if (status & UPSSTATUS_BYPASS) {
                            asprintf (&ups_status, "%s%s", ups_status, _(", On Bypass"));
                     }
                     if (status & UPSSTATUS_OVER) {
                            asprintf (&ups_status, "%s%s", ups_status, _(", Overload"));
                     }
                     if (status & UPSSTATUS_TRIM) {
                            asprintf (&ups_status, "%s%s", ups_status, _(", Trimming"));
                     }
                     if (status & UPSSTATUS_BOOST) {
                            asprintf (&ups_status, "%s%s", ups_status, _(", Boosting"));
                     }
                     if (status & UPSSTATUS_CHRG) {
                            asprintf (&ups_status, "%s%s", ups_status, _(", Charging"));
                     }
                     if (status & UPSSTATUS_DISCHRG) {
                            asprintf (&ups_status, "%s%s", ups_status, _(", Discharging"));
                     }
                     if (status & UPSSTATUS_UNKOWN) {
                            asprintf (&ups_status, "%s%s", ups_status, _(", Unknown"));
                     }
              }
              asprintf (&message, "%sStatus=%s ", message, ups_status);
       }

       /* get the ups utility voltage if possible */
       res=get_ups_variable ("input.voltage", temp_buffer, sizeof (temp_buffer));
       if (res == NOSUCHVAR) supported_options &= ~UPS_UTILITY;
       else if (res != OK)
              return STATE_CRITICAL;
       else {
              supported_options |= UPS_UTILITY;

              ups_utility_voltage = atof (temp_buffer);
              asprintf (&message, "%sUtility=%3.1fV ", message, ups_utility_voltage);

              if (ups_utility_voltage > 120.0)
                     ups_utility_deviation = 120.0 - ups_utility_voltage;
              else
                     ups_utility_deviation = ups_utility_voltage - 120.0;

              if (check_variable == UPS_UTILITY) {
                     if (check_crit==TRUE && ups_utility_deviation>=critical_value) {
                            result = STATE_CRITICAL;
                     }
                     else if (check_warn==TRUE && ups_utility_deviation>=warning_value) {
                            result = max_state (result, STATE_WARNING);
                     }
                     asprintf (&data, "%s",
                               perfdata ("voltage", (long)(1000*ups_utility_voltage), "mV",
                                         check_warn, (long)(1000*warning_value),
                                         check_crit, (long)(1000*critical_value),
                                         TRUE, 0, FALSE, 0));
              } else {
                     asprintf (&data, "%s",
                               perfdata ("voltage", (long)(1000*ups_utility_voltage), "mV",
                                         FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
              }
       }

       /* get the ups battery percent if possible */
       res=get_ups_variable ("battery.charge", temp_buffer, sizeof (temp_buffer));
       if (res == NOSUCHVAR) supported_options &= ~UPS_BATTPCT;
       else if ( res != OK)
              return STATE_CRITICAL;
       else {
              supported_options |= UPS_BATTPCT;
              ups_battery_percent = atof (temp_buffer);
              asprintf (&message, "%sBatt=%3.1f%% ", message, ups_battery_percent);

              if (check_variable == UPS_BATTPCT) {
                     if (check_crit==TRUE && ups_battery_percent <= critical_value) {
                            result = STATE_CRITICAL;
                     }
                     else if (check_warn==TRUE && ups_battery_percent<=warning_value) {
                            result = max_state (result, STATE_WARNING);
                     }
                     asprintf (&data, "%s %s", data,
                               perfdata ("battery", (long)ups_battery_percent, "%",
                                         check_warn, (long)(1000*warning_value),
                                         check_crit, (long)(1000*critical_value),
                                         TRUE, 0, TRUE, 100));
              } else {
                     asprintf (&data, "%s %s", data,
                               perfdata ("battery", (long)ups_battery_percent, "%",
                                         FALSE, 0, FALSE, 0, TRUE, 0, TRUE, 100));
              }
       }

       /* get the ups load percent if possible */
       res=get_ups_variable ("ups.load", temp_buffer, sizeof (temp_buffer));
       if ( res == NOSUCHVAR ) supported_options &= ~UPS_LOADPCT;
       else if ( res != OK)
              return STATE_CRITICAL;
       else {
              supported_options |= UPS_LOADPCT;
              ups_load_percent = atof (temp_buffer);
              asprintf (&message, "%sLoad=%3.1f%% ", message, ups_load_percent);

              if (check_variable == UPS_LOADPCT) {
                     if (check_crit==TRUE && ups_load_percent>=critical_value) {
                            result = STATE_CRITICAL;
                     }
                     else if (check_warn==TRUE && ups_load_percent>=warning_value) {
                            result = max_state (result, STATE_WARNING);
                     }
                     asprintf (&data, "%s %s", data,
                               perfdata ("load", (long)ups_load_percent, "%",
                                         check_warn, (long)(1000*warning_value),
                                         check_crit, (long)(1000*critical_value),
                                         TRUE, 0, TRUE, 100));
              } else {
                     asprintf (&data, "%s %s", data,
                               perfdata ("load", (long)ups_load_percent, "%",
                                         FALSE, 0, FALSE, 0, TRUE, 0, TRUE, 100));
              }
       }

       /* get the ups temperature if possible */
       res=get_ups_variable ("ups.temperature", temp_buffer, sizeof (temp_buffer));
       if ( res == NOSUCHVAR ) supported_options &= ~UPS_TEMP;
       else if ( res != OK)
              return STATE_CRITICAL;
       else {
              supported_options |= UPS_TEMP;
              if (temp_output_c) {
                tunits="degC";
                ups_temperature = atof (temp_buffer);
                asprintf (&message, "%sTemp=%3.1fC", message, ups_temperature);
              }
              else {
                tunits="degF";
                ups_temperature = (atof (temp_buffer) * 1.8) + 32;
                asprintf (&message, "%sTemp=%3.1fF", message, ups_temperature);
              }

              if (check_variable == UPS_TEMP) {
                     if (check_crit==TRUE && ups_temperature>=critical_value) {
                            result = STATE_CRITICAL;
                     }
                     else if (check_warn == TRUE && ups_temperature>=warning_value) {
                            result = max_state (result, STATE_WARNING);
                     }
                     asprintf (&data, "%s %s", data,
                               perfdata ("temp", (long)ups_temperature, tunits,
                                         check_warn, (long)(1000*warning_value),
                                         check_crit, (long)(1000*critical_value),
                                         TRUE, 0, FALSE, 0));
              } else {
                     asprintf (&data, "%s %s", data,
                               perfdata ("temp", (long)ups_temperature, tunits,
                                         FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
              }
       }

       /* if the UPS does not support any options we are looking for, report an error */
       if (supported_options == UPS_NONE) {
              result = STATE_CRITICAL;
              asprintf (&message, _("UPS does not support any available options\n"));
       }

       /* reset timeout */
       alarm (0);

       printf ("UPS %s - %s|%s\n", state_text(result), message, data);
       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 
)
int validate_arguments ( void  )

Variable Documentation

Definition at line 79 of file check_ups.c.

Definition at line 80 of file check_ups.c.

Definition at line 78 of file check_ups.c.

const char* copyright = "2000-2007"

Definition at line 36 of file check_ups.c.

double critical_value = 0.0

Definition at line 77 of file check_ups.c.

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

Definition at line 37 of file check_ups.c.

const char* progname = "check_ups"

Definition at line 35 of file check_ups.c.

Definition at line 74 of file check_ups.c.

Definition at line 73 of file check_ups.c.

Definition at line 82 of file check_ups.c.

Definition at line 81 of file check_ups.c.

int temp_output_c = 0

Definition at line 89 of file check_ups.c.

double ups_battery_percent = 0.0

Definition at line 85 of file check_ups.c.

double ups_load_percent = 0.0

Definition at line 86 of file check_ups.c.

char* ups_name = NULL

Definition at line 75 of file check_ups.c.

char* ups_status

Definition at line 88 of file check_ups.c.

double ups_temperature = 0.0

Definition at line 87 of file check_ups.c.

double ups_utility_voltage = 0.0

Definition at line 84 of file check_ups.c.

double warning_value = 0.0

Definition at line 76 of file check_ups.c.