Code

Updated link to sfsnapshotgit
[nagiosplug.git] / contrib / check_uptime.c
1 /******************************************************************************
2  *
3  * CHECK_UPTIME.C
4  *
5  * Program: Uptime plugin for Nagios
6  * License: GPL
7  * Copyright (c) 2000 Teresa Ramanan (teresa@redowl.org)
8  *
9  * Based on CHECK_LOAD.C
10  * Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>
11  *
12  * Last Modified: $Date: 2002-02-28 21:42:56 -0500 (Thu, 28 Feb 2002) $
13  *
14  * Command line: CHECK_UPTIME <host_address>
15  * 
16  * Description:
17  *
18  * This plugin parses the output from "uptime", tokenizing it with ',' as the
19  * delimiter. Returning only the number of days and/or the hours and minutes
20  * a machine has been up and running.
21  *
22  *****************************************************************************/
24 #include "config.h"
25 #include "common.h"
26 #include "utils.h"
27 #include "popen.h"
29 int main(int argc, char **argv)
30 {
32   int result;
33   char input_buffer[MAX_INPUT_BUFFER];
34   int ct;
35   int i;
36   char *tok1 = NULL;
37   char *daytok = NULL;
38   char *hrmintok = NULL;
39   char *runstr = NULL;
40   char tempp;
41   char ch;
42   char delim[] = ",";
44   if(argc != 2){
45     printf("Incorrect number of arguments supplied\n");
46     printf("\n");
47     print_revision(argv[0],"$Revision: 6 $");
48     printf("Copyright (c) 2000 Teresa Ramanan (tlr@redowl.org)\n");
49     printf("\n");
50     printf("Usage: %s <host_address>\n",argv[0]);
51     printf("\n");
52     return STATE_UNKNOWN;
53   }
55   child_process = spopen(PATH_TO_UPTIME);
56   if(child_process==NULL){
57       printf("Error opening %s\n",PATH_TO_UPTIME);
58       return STATE_UNKNOWN;
59   }
60   child_stderr=fdopen(child_stderr_array[fileno(child_process)],"r");
61   if(child_stderr==NULL){
62     printf("Could not open stderr for %s\n",PATH_TO_UPTIME);
63   }
64   fgets(input_buffer,MAX_INPUT_BUFFER-1,child_process);
65   i = 0;
66   ct = 0;
68   /* Let's mark the end of this string for parsing purposes */
69   input_buffer[strlen(input_buffer)-1]='\0';
71   tempp = input_buffer[0];
72   while(ch != '\0'){
73     ch = (&tempp)[i];
74     if (ch == ',') { ct++; }
75     i++;
76   }
77   runstr = input_buffer;
78   tok1 = strsep(&runstr, delim);
79   if (ct > 4) {
80     hrmintok = strsep(&runstr, delim);
81     hrmintok++;
82     daytok = strstr(tok1,"up");
83   }
84   else {
85     hrmintok = strstr(tok1, "up");
86   }
88   result = spclose(child_process);
89   if(result){
90     printf("Error code %d returned in %s\n",result,PATH_TO_UPTIME);
91     return STATE_UNKNOWN;
92   }
93   if (hrmintok == NULL) {
94     printf("Problem - unexpected data returned\n");
95     return STATE_UNKNOWN;
96   }
97   printf("%s%s%s\n",(daytok == NULL)?"":daytok,(daytok == NULL)?"":",",hrmintok);
98   return STATE_OK;
99 }