check_cluster.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. * Copyright (c) 2007 nagios-plugins team
  7. * License: GPL
  8. * Last Modified: $Date$
  9. *
  10. * License Information:
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. * $Id$
  27. *
  28. ******************************************************************************/
  29. const char *progname = "check_cluster";
  30. const char *revision = "$Revision$";
  31. const char *copyright = "2000-2007";
  32. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  33. #include "common.h"
  34. #include "utils.h"
  35. #define CHECK_SERVICES 1
  36. #define CHECK_HOSTS 2
  37. int total_services_ok=0;
  38. int total_services_warning=0;
  39. int total_services_unknown=0;
  40. int total_services_critical=0;
  41. int total_hosts_up=0;
  42. int total_hosts_down=0;
  43. int total_hosts_unreachable=0;
  44. char *warn_threshold;
  45. char *crit_threshold;
  46. int check_type=CHECK_SERVICES;
  47. char *data_vals=NULL;
  48. char *label=NULL;
  49. int verbose=0;
  50. int process_arguments(int,char **);
  51. int main(int argc, char **argv){
  52. char input_buffer[MAX_INPUT_BUFFER];
  53. char *ptr;
  54. int data_val;
  55. int return_code=STATE_OK;
  56. int error=FALSE;
  57. thresholds *thresholds;
  58. if(process_arguments(argc,argv)==ERROR)
  59. usage(_("Could not parse arguments"));
  60. /* Initialize the thresholds */
  61. set_thresholds(&thresholds, warn_threshold, crit_threshold);
  62. if(verbose)
  63. print_thresholds("check_cluster", thresholds);
  64. /* check the data values */
  65. for(ptr=strtok(data_vals,",");ptr!=NULL;ptr=strtok(NULL,",")){
  66. data_val=atoi(ptr);
  67. if(check_type==CHECK_SERVICES){
  68. switch(data_val){
  69. case 0:
  70. total_services_ok++;
  71. break;
  72. case 1:
  73. total_services_warning++;
  74. break;
  75. case 2:
  76. total_services_critical++;
  77. break;
  78. case 3:
  79. total_services_unknown++;
  80. break;
  81. default:
  82. break;
  83. }
  84. }
  85. else{
  86. switch(data_val){
  87. case 0:
  88. total_hosts_up++;
  89. break;
  90. case 1:
  91. total_hosts_down++;
  92. break;
  93. case 2:
  94. total_hosts_unreachable++;
  95. break;
  96. default:
  97. break;
  98. }
  99. }
  100. }
  101. /* return the status of the cluster */
  102. if(check_type==CHECK_SERVICES){
  103. return_code=get_status(total_services_warning+total_services_unknown+total_services_critical, thresholds);
  104. printf("CLUSTER %s: %s: %d ok, %d warning, %d unknown, %d critical\n",
  105. state_text(return_code), (label==NULL)?"Service cluster":label,
  106. total_services_ok,total_services_warning,
  107. total_services_unknown,total_services_critical);
  108. }
  109. else{
  110. return_code=get_status(total_hosts_down+total_hosts_unreachable, thresholds);
  111. printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n",
  112. state_text(return_code), (label==NULL)?"Host cluster":label,
  113. total_hosts_up,total_hosts_down,total_hosts_unreachable);
  114. }
  115. return return_code;
  116. }
  117. int process_arguments(int argc, char **argv){
  118. int c;
  119. int option=0;
  120. static struct option longopts[]={
  121. {"data", required_argument,0,'d'},
  122. {"warning", required_argument,0,'w'},
  123. {"critical", required_argument,0,'c'},
  124. {"label", required_argument,0,'l'},
  125. {"host", no_argument, 0,'h'},
  126. {"service", no_argument, 0,'s'},
  127. {"verbose", no_argument, 0,'v'},
  128. {"help", no_argument, 0,'H'},
  129. {0,0,0,0}
  130. };
  131. /* no options were supplied */
  132. if(argc<2)
  133. return ERROR;
  134. while(1){
  135. c=getopt_long(argc,argv,"hHsvw:c:d:l:",longopts,&option);
  136. if(c==-1 || c==EOF || c==1)
  137. break;
  138. switch(c){
  139. case 'h': /* host cluster */
  140. check_type=CHECK_HOSTS;
  141. break;
  142. case 's': /* service cluster */
  143. check_type=CHECK_SERVICES;
  144. break;
  145. case 'w': /* warning threshold */
  146. if (strspn (optarg, "0123456789:,") < strlen (optarg))
  147. usage2 (_("Invalid warning threshold: %s\n"), optarg);
  148. warn_threshold = strdup(optarg);
  149. break;
  150. case 'c': /* warning threshold */
  151. if (strspn (optarg, "0123456789:,") < strlen (optarg))
  152. usage2 (_("Invalid critical threshold: %s\n"), optarg);
  153. crit_threshold = strdup(optarg);
  154. break;
  155. case 'd': /* data values */
  156. data_vals=(char *)strdup(optarg);
  157. break;
  158. case 'l': /* text label */
  159. label=(char *)strdup(optarg);
  160. break;
  161. case 'v': /* verbose */
  162. verbose++;
  163. break;
  164. case 'H': /* help */
  165. print_help();
  166. exit(STATE_UNKNOWN);
  167. break;
  168. default:
  169. return ERROR;
  170. break;
  171. }
  172. }
  173. if(data_vals==NULL)
  174. return ERROR;
  175. return OK;
  176. }
  177. void
  178. print_help(void)
  179. {
  180. print_revision(progname, revision);
  181. printf(COPYRIGHT, copyright, email);
  182. printf("%s\n", _("Host/Service Cluster Plugin for Nagios 2"));
  183. print_usage();
  184. printf("%s\n", _("Options:"));
  185. printf (" %s\n", "-s, --service");
  186. printf (" %s\n", _("Check service cluster status"));
  187. printf (" %s\n", "-h, --host");
  188. printf (" %s\n", _("Check host cluster status"));
  189. printf (" %s\n", "-l, --label=STRING");
  190. printf (" %s\n", _("Optional prepended text output (i.e. \"Host cluster\")"));
  191. printf (" %s\n", "-w, --warning=THRESHOLD");
  192. printf (" %s\n", _("Specifies the range of hosts or services in cluster that must be in a"));
  193. printf (" %s\n", _("non-OK state in order to return a WARNING status level"));
  194. printf (" %s\n", "-c, --critical=THRESHOLD");
  195. printf (" %s\n", _("Specifies the range of hosts or services in cluster that must be in a"));
  196. printf (" %s\n", _(" non-OK state in order to return a CRITICAL status level"));
  197. printf (" %s\n", "-d, --data=LIST");
  198. printf (" %s\n", _("The status codes of the hosts or services in the cluster, separated by"));
  199. printf (" %s\n", _("commas"));
  200. printf(_(UT_VERBOSE));
  201. printf("\n");
  202. printf("%s\n", _("Notes:"));
  203. printf(" %s\n", _("See:"));
  204. printf(" %s\n", _("http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT"));
  205. printf(" %s\n", _("for THRESHOLD format and examples."));
  206. printf(_(UT_SUPPORT));
  207. printf("\n");
  208. }
  209. void
  210. print_usage(void)
  211. {
  212. printf("\n");
  213. printf(_("Usage:"));
  214. printf(" %s (-s | -h) -d val1[,val2,...,valn] [-l label]\n", progname);
  215. printf("[-w threshold] [-c threshold] [-v] [--help]\n");
  216. printf("\n");
  217. }
  218. #if 0
  219. #endif