aboutsummaryrefslogtreecommitdiff
path: root/contrib/check_uptime.c
blob: 46a1b8261a71cab60c2e483211120ea977592e37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/******************************************************************************
 *
 * CHECK_UPTIME.C
 *
 * Program: Uptime plugin for Nagios
 * License: GPL
 * Copyright (c) 2000 Teresa Ramanan (teresa@redowl.org)
 *
 * Based on CHECK_LOAD.C
 * Copyright (c) 1999 Felipe Gustavo de Almeida <galmeida@linux.ime.usp.br>
 *
 * Last Modified: $Date$
 *
 * Command line: CHECK_UPTIME <host_address>
 * 
 * Description:
 *
 * This plugin parses the output from "uptime", tokenizing it with ',' as the
 * delimiter. Returning only the number of days and/or the hours and minutes
 * a machine has been up and running.
 *
 *****************************************************************************/

#include "common/config.h"
#include "common/common.h"
#include "common/utils.h"
#include "common/popen.h"

int main(int argc, char **argv)
{

  int result;
  char input_buffer[MAX_INPUT_BUFFER];
  int ct;
  int i;
  char *tok1 = NULL;
  char *daytok = NULL;
  char *hrmintok = NULL;
  char *runstr = NULL;
  char tempp;
  char ch;
  char delim[] = ",";

  if(argc != 2){
    printf("Incorrect number of arguments supplied\n");
    printf("\n");
    print_revision(argv[0],"$Revision$");
    printf("Copyright (c) 2000 Teresa Ramanan (tlr@redowl.org)\n");
    printf("\n");
    printf("Usage: %s <host_address>\n",argv[0]);
    printf("\n");
    return STATE_UNKNOWN;
  }

  child_process = spopen(PATH_TO_UPTIME);
  if(child_process==NULL){
      printf("Error opening %s\n",PATH_TO_UPTIME);
      return STATE_UNKNOWN;
  }
  child_stderr=fdopen(child_stderr_array[fileno(child_process)],"r");
  if(child_stderr==NULL){
    printf("Could not open stderr for %s\n",PATH_TO_UPTIME);
  }
  fgets(input_buffer,MAX_INPUT_BUFFER-1,child_process);
  i = 0;
  ct = 0;

  /* Let's mark the end of this string for parsing purposes */
  input_buffer[strlen(input_buffer)-1]='\0';

  tempp = input_buffer[0];
  while(ch != '\0'){
    ch = (&tempp)[i];
    if (ch == ',') { ct++; }
    i++;
  }
  runstr = input_buffer;
  tok1 = strsep(&runstr, delim);
  if (ct > 4) {
    hrmintok = strsep(&runstr, delim);
    hrmintok++;
    daytok = strstr(tok1,"up");
  }
  else {
    hrmintok = strstr(tok1, "up");
  }

  result = spclose(child_process);
  if(result){
    printf("Error code %d returned in %s\n",result,PATH_TO_UPTIME);
    return STATE_UNKNOWN;
  }
  if (hrmintok == NULL) {
    printf("Problem - unexpected data returned\n");
    return STATE_UNKNOWN;
  }
  printf("%s%s%s\n",(daytok == NULL)?"":daytok,(daytok == NULL)?"":",",hrmintok);
  return STATE_OK;
}