Back to index

nagios-plugins  1.4.16
Functions | Variables
check_mrtgtraf.c File Reference
#include "common.h"
#include "utils.h"

Go to the source code of this file.

Functions

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_mrtgtraf"
const char * copyright = "1999-2007"
const char * email = "nagiosplug-devel@lists.sourceforge.net"
char * log_file = NULL
int expire_minutes = -1
int use_average = TRUE
unsigned long incoming_warning_threshold = 0L
unsigned long incoming_critical_threshold = 0L
unsigned long outgoing_warning_threshold = 0L
unsigned long outgoing_critical_threshold = 0L

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 54 of file check_mrtgtraf.c.

{
       int result = STATE_OK;
       FILE *fp;
       int line;
       char input_buffer[MAX_INPUT_BUFFER];
       char *temp_buffer;
       time_t current_time;
       char *error_message;
       time_t timestamp = 0L;
       unsigned long average_incoming_rate = 0L;
       unsigned long average_outgoing_rate = 0L;
       unsigned long maximum_incoming_rate = 0L;
       unsigned long maximum_outgoing_rate = 0L;
       unsigned long incoming_rate = 0L;
       unsigned long outgoing_rate = 0L;
       double adjusted_incoming_rate = 0.0;
       double adjusted_outgoing_rate = 0.0;
       char incoming_speed_rating[8];
       char outgoing_speed_rating[8];

       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"));

       /* open the MRTG log file for reading */
       fp = fopen (log_file, "r");
       if (fp == NULL)
              usage4 (_("Unable to open MRTG log file"));

       line = 0;
       while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {

              line++;

              /* skip the first line of the log file */
              if (line == 1)
                     continue;

              /* break out of read loop */
              /* if we've passed the number of entries we want to read */
              if (line > 2)
                     break;

              /* grab the timestamp */
              temp_buffer = strtok (input_buffer, " ");
              timestamp = strtoul (temp_buffer, NULL, 10);

              /* grab the average incoming transfer rate */
              temp_buffer = strtok (NULL, " ");
              average_incoming_rate = strtoul (temp_buffer, NULL, 10);

              /* grab the average outgoing transfer rate */
              temp_buffer = strtok (NULL, " ");
              average_outgoing_rate = strtoul (temp_buffer, NULL, 10);

              /* grab the maximum incoming transfer rate */
              temp_buffer = strtok (NULL, " ");
              maximum_incoming_rate = strtoul (temp_buffer, NULL, 10);

              /* grab the maximum outgoing transfer rate */
              temp_buffer = strtok (NULL, " ");
              maximum_outgoing_rate = strtoul (temp_buffer, NULL, 10);
       }

       /* close the log file */
       fclose (fp);

       /* if we couldn't read enough data, return an unknown error */
       if (line <= 2)
              usage4 (_("Unable to process MRTG log file"));

       /* make sure the MRTG data isn't too old */
       time (&current_time);
       if ((expire_minutes > 0) &&
           (current_time - timestamp) > (expire_minutes * 60))
              die (STATE_WARNING,  _("MRTG data has expired (%d minutes old)\n"),
                   (int) ((current_time - timestamp) / 60));

       /* else check the incoming/outgoing rates */
       if (use_average == TRUE) {
              incoming_rate = average_incoming_rate;
              outgoing_rate = average_outgoing_rate;
       }
       else {
              incoming_rate = maximum_incoming_rate;
              outgoing_rate = maximum_outgoing_rate;
       }

       /* report incoming traffic in Bytes/sec */
       if (incoming_rate < 1024) {
              strcpy (incoming_speed_rating, "B/s");
              adjusted_incoming_rate = (double) incoming_rate;
       }

       /* report incoming traffic in KBytes/sec */
       else if (incoming_rate < (1024 * 1024)) {
              strcpy (incoming_speed_rating, "KB/s");
              adjusted_incoming_rate = (double) (incoming_rate / 1024.0);
       }

       /* report incoming traffic in MBytes/sec */
       else {
              strcpy (incoming_speed_rating, "MB/s");
              adjusted_incoming_rate = (double) (incoming_rate / 1024.0 / 1024.0);
       }

       /* report outgoing traffic in Bytes/sec */
       if (outgoing_rate < 1024) {
              strcpy (outgoing_speed_rating, "B/s");
              adjusted_outgoing_rate = (double) outgoing_rate;
       }

       /* report outgoing traffic in KBytes/sec */
       else if (outgoing_rate < (1024 * 1024)) {
              strcpy (outgoing_speed_rating, "KB/s");
              adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0);
       }

       /* report outgoing traffic in MBytes/sec */
       else {
              strcpy (outgoing_speed_rating, "MB/s");
              adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0 / 1024.0);
       }

       if (incoming_rate > incoming_critical_threshold
                     || outgoing_rate > outgoing_critical_threshold) {
              result = STATE_CRITICAL;
       }
       else if (incoming_rate > incoming_warning_threshold
                                    || outgoing_rate > outgoing_warning_threshold) {
              result = STATE_WARNING;
       }

       asprintf (&error_message, _("%s. In = %0.1f %s, %s. Out = %0.1f %s|%s %s\n"),
                 (use_average == TRUE) ? _("Avg") : _("Max"), adjusted_incoming_rate,
                 incoming_speed_rating, (use_average == TRUE) ? _("Avg") : _("Max"),
                 adjusted_outgoing_rate, outgoing_speed_rating,
                 fperfdata("in", adjusted_incoming_rate, incoming_speed_rating,
                          (int)incoming_warning_threshold, incoming_warning_threshold,
                          (int)incoming_critical_threshold, incoming_critical_threshold,
                          TRUE, 0, FALSE, 0),
                 fperfdata("out", adjusted_outgoing_rate, outgoing_speed_rating,
                          (int)outgoing_warning_threshold, outgoing_warning_threshold,
                          (int)outgoing_critical_threshold, outgoing_critical_threshold,
                          TRUE, 0, FALSE, 0));

       printf (_("Traffic %s - %s\n"), state_text(result), error_message);

       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

const char* copyright = "1999-2007"

Definition at line 36 of file check_mrtgtraf.c.

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

Definition at line 37 of file check_mrtgtraf.c.

int expire_minutes = -1

Definition at line 45 of file check_mrtgtraf.c.

unsigned long incoming_critical_threshold = 0L

Definition at line 48 of file check_mrtgtraf.c.

unsigned long incoming_warning_threshold = 0L

Definition at line 47 of file check_mrtgtraf.c.

char* log_file = NULL

Definition at line 44 of file check_mrtgtraf.c.

unsigned long outgoing_critical_threshold = 0L

Definition at line 50 of file check_mrtgtraf.c.

unsigned long outgoing_warning_threshold = 0L

Definition at line 49 of file check_mrtgtraf.c.

const char* progname = "check_mrtgtraf"

Definition at line 35 of file check_mrtgtraf.c.

Definition at line 46 of file check_mrtgtraf.c.