check_cluster2.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*****************************************************************************
  2. *
  3. * CHECK_CLUSTER2.C - Host and Service Cluster Plugin for Nagios 2.x
  4. *
  5. * Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)
  6. * License: GPL
  7. * Last Modified: 03-11-2004
  8. *
  9. * License:
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. *****************************************************************************/
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <getopt.h>
  31. #define OK 0
  32. #define ERROR -1
  33. #define TRUE 1
  34. #define FALSE 0
  35. #define CHECK_SERVICES 1
  36. #define CHECK_HOSTS 2
  37. #define MAX_INPUT_BUFFER 1024
  38. #define STATE_OK 0
  39. #define STATE_WARNING 1
  40. #define STATE_CRITICAL 2
  41. #define STATE_UNKNOWN 3
  42. int total_services_ok=0;
  43. int total_services_warning=0;
  44. int total_services_unknown=0;
  45. int total_services_critical=0;
  46. int total_hosts_up=0;
  47. int total_hosts_down=0;
  48. int total_hosts_unreachable=0;
  49. int warning_threshold=1;
  50. int critical_threshold=1;
  51. int check_type=CHECK_SERVICES;
  52. char *data_vals=NULL;
  53. char *label=NULL;
  54. int process_arguments(int,char **);
  55. int main(int argc, char **argv){
  56. char input_buffer[MAX_INPUT_BUFFER];
  57. char *ptr;
  58. int data_val;
  59. int return_code=STATE_OK;
  60. int error=FALSE;
  61. if(process_arguments(argc,argv)==ERROR){
  62. printf("Invalid arguments supplied\n");
  63. printf("\n");
  64. printf("Host/Service Cluster Plugin for Nagios 2\n");
  65. printf("Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)\n");
  66. printf("Last Modified: 03-11-2004\n");
  67. printf("License: GPL\n");
  68. printf("\n");
  69. printf("Usage: %s (-s | -h) [-l label] [-w threshold] [-c threshold] [-d val1,val2,...,valn]\n",argv[0]);
  70. printf("\n");
  71. printf("Options:\n");
  72. printf(" -s, --service = Check service cluster status\n");
  73. printf(" -h, --host = Check host cluster status\n");
  74. printf(" -l, --label = Optional prepended text output (i.e. \"Host cluster\")\n");
  75. printf(" -w, --warning = Specifies the number of hosts or services in cluster that must be in\n");
  76. printf(" a non-OK state in order to return a WARNING status level\n");
  77. printf(" -c, --critical = Specifies the number of hosts or services in cluster that must be in\n");
  78. printf(" a non-OK state in order to return a CRITICAL status level\n");
  79. printf(" -d, --data = The status codes of the hosts or services in the cluster, separated\n");
  80. printf(" by commas\n");
  81. printf("\n");
  82. return STATE_UNKNOWN;
  83. }
  84. /* check the data values */
  85. for(ptr=strtok(data_vals,",");ptr!=NULL;ptr=strtok(NULL,",")){
  86. data_val=atoi(ptr);
  87. if(check_type==CHECK_SERVICES){
  88. switch(data_val){
  89. case 0:
  90. total_services_ok++;
  91. break;
  92. case 1:
  93. total_services_warning++;
  94. break;
  95. case 2:
  96. total_services_critical++;
  97. break;
  98. case 3:
  99. total_services_unknown++;
  100. break;
  101. default:
  102. break;
  103. }
  104. }
  105. else{
  106. switch(data_val){
  107. case 0:
  108. total_hosts_up++;
  109. break;
  110. case 1:
  111. total_hosts_down++;
  112. break;
  113. case 2:
  114. total_hosts_unreachable++;
  115. break;
  116. default:
  117. break;
  118. }
  119. }
  120. }
  121. /* return the status of the cluster */
  122. if(check_type==CHECK_SERVICES){
  123. if((total_services_warning+total_services_unknown+total_services_critical) >= critical_threshold)
  124. return_code=STATE_CRITICAL;
  125. else if((total_services_warning+total_services_unknown+total_services_critical) >= warning_threshold)
  126. return_code=STATE_WARNING;
  127. else
  128. return_code=STATE_OK;
  129. printf("%s %s: %d ok, %d warning, %d unknown, %d critical\n",(label==NULL)?"Service cluster":label,(return_code==STATE_OK)?"ok":"problem",total_services_ok,total_services_warning,total_services_unknown,total_services_critical);
  130. }
  131. else{
  132. if((total_hosts_down+total_hosts_unreachable) >= critical_threshold)
  133. return_code=STATE_CRITICAL;
  134. else if((total_hosts_down+total_hosts_unreachable) >= warning_threshold)
  135. return_code=STATE_WARNING;
  136. else
  137. return_code=STATE_OK;
  138. printf("%s %s: %d up, %d down, %d unreachable\n",(label==NULL)?"Host cluster":label,(return_code==STATE_OK)?"ok":"problem",total_hosts_up,total_hosts_down,total_hosts_unreachable);
  139. }
  140. return return_code;
  141. }
  142. int process_arguments(int argc, char **argv){
  143. int c;
  144. int option=0;
  145. static struct option longopts[]={
  146. {"data", required_argument,0,'d'},
  147. {"warning", required_argument,0,'w'},
  148. {"critical", required_argument,0,'c'},
  149. {"label", required_argument,0,'l'},
  150. {"host", no_argument, 0,'h'},
  151. {"service", no_argument, 0,'s'},
  152. {0,0,0,0}
  153. };
  154. /* no options were supplied */
  155. if(argc<2)
  156. return ERROR;
  157. while(1){
  158. c=getopt_long(argc,argv,"hsw:c:d:l:",longopts,&option);
  159. if(c==-1 || c==EOF || c==1)
  160. break;
  161. switch(c){
  162. case 'h': /* host cluster */
  163. check_type=CHECK_HOSTS;
  164. break;
  165. case 's': /* service cluster */
  166. check_type=CHECK_SERVICES;
  167. break;
  168. case 'w': /* warning threshold */
  169. warning_threshold=atoi(optarg);
  170. break;
  171. case 'c': /* warning threshold */
  172. critical_threshold=atoi(optarg);
  173. break;
  174. case 'd': /* data values */
  175. data_vals=(char *)strdup(optarg);
  176. break;
  177. case 'l': /* text label */
  178. label=(char *)strdup(optarg);
  179. break;
  180. default:
  181. return ERROR;
  182. break;
  183. }
  184. }
  185. if(data_vals==NULL)
  186. return ERROR;
  187. return OK;
  188. }