check_uptime.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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$
  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. *****************************************************************************/
  23. #include "config.h"
  24. #include "common.h"
  25. #include "utils.h"
  26. #include "popen.h"
  27. int main(int argc, char **argv)
  28. {
  29. int result;
  30. char input_buffer[MAX_INPUT_BUFFER];
  31. int ct;
  32. int i;
  33. char *tok1 = NULL;
  34. char *daytok = NULL;
  35. char *hrmintok = NULL;
  36. char *runstr = NULL;
  37. char tempp;
  38. char ch;
  39. char delim[] = ",";
  40. if(argc != 2){
  41. printf("Incorrect number of arguments supplied\n");
  42. printf("\n");
  43. print_revision(argv[0],"$Revision$");
  44. printf("Copyright (c) 2000 Teresa Ramanan (tlr@redowl.org)\n");
  45. printf("\n");
  46. printf("Usage: %s <host_address>\n",argv[0]);
  47. printf("\n");
  48. return STATE_UNKNOWN;
  49. }
  50. child_process = spopen(PATH_TO_UPTIME);
  51. if(child_process==NULL){
  52. printf("Error opening %s\n",PATH_TO_UPTIME);
  53. return STATE_UNKNOWN;
  54. }
  55. child_stderr=fdopen(child_stderr_array[fileno(child_process)],"r");
  56. if(child_stderr==NULL){
  57. printf("Could not open stderr for %s\n",PATH_TO_UPTIME);
  58. }
  59. fgets(input_buffer,MAX_INPUT_BUFFER-1,child_process);
  60. i = 0;
  61. ct = 0;
  62. /* Let's mark the end of this string for parsing purposes */
  63. input_buffer[strlen(input_buffer)-1]='\0';
  64. tempp = input_buffer[0];
  65. while(ch != '\0'){
  66. ch = (&tempp)[i];
  67. if (ch == ',') { ct++; }
  68. i++;
  69. }
  70. runstr = input_buffer;
  71. tok1 = strsep(&runstr, delim);
  72. if (ct > 4) {
  73. hrmintok = strsep(&runstr, delim);
  74. hrmintok++;
  75. daytok = strstr(tok1,"up");
  76. }
  77. else {
  78. hrmintok = strstr(tok1, "up");
  79. }
  80. result = spclose(child_process);
  81. if(result){
  82. printf("Error code %d returned in %s\n",result,PATH_TO_UPTIME);
  83. return STATE_UNKNOWN;
  84. }
  85. if (hrmintok == NULL) {
  86. printf("Problem - unexpected data returned\n");
  87. return STATE_UNKNOWN;
  88. }
  89. printf("%s%s%s\n",(daytok == NULL)?"":daytok,(daytok == NULL)?"":",",hrmintok);
  90. return STATE_OK;
  91. }