Back to index

nagios-plugins  1.4.16
check_uptime.c
Go to the documentation of this file.
00001 /******************************************************************************
00002  *
00003  * CHECK_UPTIME.C
00004  *
00005  * Program: Uptime plugin for Nagios
00006  * License: GPL
00007  * Copyright (c) 2000 Teresa Ramanan (teresa@redowl.org)
00008  *
00009  * Based on CHECK_LOAD.C
00010  * Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>
00011  *
00012  * Last Modified: $Date: 2002-02-28 21:42:56 -0500 (Thu, 28 Feb 2002) $
00013  *
00014  * Command line: CHECK_UPTIME <host_address>
00015  * 
00016  * Description:
00017  *
00018  * This plugin parses the output from "uptime", tokenizing it with ',' as the
00019  * delimiter. Returning only the number of days and/or the hours and minutes
00020  * a machine has been up and running.
00021  *
00022  *****************************************************************************/
00023 
00024 #include "config.h"
00025 #include "common.h"
00026 #include "utils.h"
00027 #include "popen.h"
00028 
00029 int main(int argc, char **argv)
00030 {
00031 
00032   int result;
00033   char input_buffer[MAX_INPUT_BUFFER];
00034   int ct;
00035   int i;
00036   char *tok1 = NULL;
00037   char *daytok = NULL;
00038   char *hrmintok = NULL;
00039   char *runstr = NULL;
00040   char tempp;
00041   char ch;
00042   char delim[] = ",";
00043 
00044   if(argc != 2){
00045     printf("Incorrect number of arguments supplied\n");
00046     printf("\n");
00047     print_revision(argv[0],"$Revision: 6 $");
00048     printf("Copyright (c) 2000 Teresa Ramanan (tlr@redowl.org)\n");
00049     printf("\n");
00050     printf("Usage: %s <host_address>\n",argv[0]);
00051     printf("\n");
00052     return STATE_UNKNOWN;
00053   }
00054 
00055   child_process = spopen(PATH_TO_UPTIME);
00056   if(child_process==NULL){
00057       printf("Error opening %s\n",PATH_TO_UPTIME);
00058       return STATE_UNKNOWN;
00059   }
00060   child_stderr=fdopen(child_stderr_array[fileno(child_process)],"r");
00061   if(child_stderr==NULL){
00062     printf("Could not open stderr for %s\n",PATH_TO_UPTIME);
00063   }
00064   fgets(input_buffer,MAX_INPUT_BUFFER-1,child_process);
00065   i = 0;
00066   ct = 0;
00067 
00068   /* Let's mark the end of this string for parsing purposes */
00069   input_buffer[strlen(input_buffer)-1]='\0';
00070 
00071   tempp = input_buffer[0];
00072   while(ch != '\0'){
00073     ch = (&tempp)[i];
00074     if (ch == ',') { ct++; }
00075     i++;
00076   }
00077   runstr = input_buffer;
00078   tok1 = strsep(&runstr, delim);
00079   if (ct > 4) {
00080     hrmintok = strsep(&runstr, delim);
00081     hrmintok++;
00082     daytok = strstr(tok1,"up");
00083   }
00084   else {
00085     hrmintok = strstr(tok1, "up");
00086   }
00087 
00088   result = spclose(child_process);
00089   if(result){
00090     printf("Error code %d returned in %s\n",result,PATH_TO_UPTIME);
00091     return STATE_UNKNOWN;
00092   }
00093   if (hrmintok == NULL) {
00094     printf("Problem - unexpected data returned\n");
00095     return STATE_UNKNOWN;
00096   }
00097   printf("%s%s%s\n",(daytok == NULL)?"":daytok,(daytok == NULL)?"":",",hrmintok);
00098   return STATE_OK;
00099 }