check_cluster.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 = "2007";
  32. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  33. #include "common.h"
  34. #include "utils.h"
  35. #include "utils_base.h"
  36. #define CHECK_SERVICES 1
  37. #define CHECK_HOSTS 2
  38. void print_help (void);
  39. void print_usage (void);
  40. int total_services_ok=0;
  41. int total_services_warning=0;
  42. int total_services_unknown=0;
  43. int total_services_critical=0;
  44. int total_hosts_up=0;
  45. int total_hosts_down=0;
  46. int total_hosts_unreachable=0;
  47. char *warn_threshold;
  48. char *crit_threshold;
  49. int check_type=CHECK_SERVICES;
  50. char *data_vals=NULL;
  51. char *label=NULL;
  52. int verbose=0;
  53. int process_arguments(int,char **);
  54. int main(int argc, char **argv){
  55. char *ptr;
  56. int data_val;
  57. int return_code=STATE_OK;
  58. thresholds *thresholds;
  59. if(process_arguments(argc,argv)==ERROR)
  60. usage(_("Could not parse arguments"));
  61. /* Initialize the thresholds */
  62. set_thresholds(&thresholds, warn_threshold, crit_threshold);
  63. if(verbose)
  64. print_thresholds("check_cluster", thresholds);
  65. /* check the data values */
  66. for(ptr=strtok(data_vals,",");ptr!=NULL;ptr=strtok(NULL,",")){
  67. data_val=atoi(ptr);
  68. if(check_type==CHECK_SERVICES){
  69. switch(data_val){
  70. case 0:
  71. total_services_ok++;
  72. break;
  73. case 1:
  74. total_services_warning++;
  75. break;
  76. case 2:
  77. total_services_critical++;
  78. break;
  79. case 3:
  80. total_services_unknown++;
  81. break;
  82. default:
  83. break;
  84. }
  85. }
  86. else{
  87. switch(data_val){
  88. case 0:
  89. total_hosts_up++;
  90. break;
  91. case 1:
  92. total_hosts_down++;
  93. break;
  94. case 2:
  95. total_hosts_unreachable++;
  96. break;
  97. default:
  98. break;
  99. }
  100. }
  101. }
  102. /* return the status of the cluster */
  103. if(check_type==CHECK_SERVICES){
  104. return_code=get_status(total_services_warning+total_services_unknown+total_services_critical, thresholds);
  105. printf("CLUSTER %s: %s: %d ok, %d warning, %d unknown, %d critical\n",
  106. state_text(return_code), (label==NULL)?"Service cluster":label,
  107. total_services_ok,total_services_warning,
  108. total_services_unknown,total_services_critical);
  109. }
  110. else{
  111. return_code=get_status(total_hosts_down+total_hosts_unreachable, thresholds);
  112. printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n",
  113. state_text(return_code), (label==NULL)?"Host cluster":label,
  114. total_hosts_up,total_hosts_down,total_hosts_unreachable);
  115. }
  116. return return_code;
  117. }
  118. int process_arguments(int argc, char **argv){
  119. int c;
  120. int option=0;
  121. static struct option longopts[]={
  122. {"data", required_argument,0,'d'},
  123. {"warning", required_argument,0,'w'},
  124. {"critical", required_argument,0,'c'},
  125. {"label", required_argument,0,'l'},
  126. {"host", no_argument, 0,'h'},
  127. {"service", no_argument, 0,'s'},
  128. {"verbose", no_argument, 0,'v'},
  129. {"help", no_argument, 0,'H'},
  130. {0,0,0,0}
  131. };
  132. /* no options were supplied */
  133. if(argc<2)
  134. return ERROR;
  135. while(1){
  136. c=getopt_long(argc,argv,"hHsvw:c:d:l:",longopts,&option);
  137. if(c==-1 || c==EOF || c==1)
  138. break;
  139. switch(c){
  140. case 'h': /* host cluster */
  141. check_type=CHECK_HOSTS;
  142. break;
  143. case 's': /* service cluster */
  144. check_type=CHECK_SERVICES;
  145. break;
  146. case 'w': /* warning threshold */
  147. if (strspn (optarg, "0123456789:,") < strlen (optarg))
  148. usage2 (_("Invalid warning threshold: %s\n"), optarg);
  149. warn_threshold = strdup(optarg);
  150. break;
  151. case 'c': /* warning threshold */
  152. if (strspn (optarg, "0123456789:,") < strlen (optarg))
  153. usage2 (_("Invalid critical threshold: %s\n"), optarg);
  154. crit_threshold = strdup(optarg);
  155. break;
  156. case 'd': /* data values */
  157. data_vals=(char *)strdup(optarg);
  158. break;
  159. case 'l': /* text label */
  160. label=(char *)strdup(optarg);
  161. break;
  162. case 'v': /* verbose */
  163. verbose++;
  164. break;
  165. case 'H': /* help */
  166. print_help();
  167. exit(STATE_UNKNOWN);
  168. break;
  169. default:
  170. return ERROR;
  171. break;
  172. }
  173. }
  174. if(data_vals==NULL)
  175. return ERROR;
  176. return OK;
  177. }
  178. void
  179. print_help(void)
  180. {
  181. print_revision(progname, revision);
  182. printf ("Copyright (c) 2000-2004 Ethan Galstad (nagios@nagios.org)\n");
  183. printf(COPYRIGHT, copyright, email);
  184. printf(_("Host/Service Cluster Plugin for Nagios 2"));
  185. printf("\n\n");
  186. print_usage();
  187. printf("\n");
  188. printf("%s\n", _("Options:"));
  189. printf (" %s\n", "-s, --service");
  190. printf (" %s\n", _("Check service cluster status"));
  191. printf (" %s\n", "-h, --host");
  192. printf (" %s\n", _("Check host cluster status"));
  193. printf (" %s\n", "-l, --label=STRING");
  194. printf (" %s\n", _("Optional prepended text output (i.e. \"Host cluster\")"));
  195. printf (" %s\n", "-w, --warning=THRESHOLD");
  196. printf (" %s\n", _("Specifies the range of hosts or services in cluster that must be in a"));
  197. printf (" %s\n", _("non-OK state in order to return a WARNING status level"));
  198. printf (" %s\n", "-c, --critical=THRESHOLD");
  199. printf (" %s\n", _("Specifies the range of hosts or services in cluster that must be in a"));
  200. printf (" %s\n", _(" non-OK state in order to return a CRITICAL status level"));
  201. printf (" %s\n", "-d, --data=LIST");
  202. printf (" %s\n", _("The status codes of the hosts or services in the cluster, separated by"));
  203. printf (" %s\n", _("commas"));
  204. printf(_(UT_VERBOSE));
  205. printf("\n");
  206. printf("%s\n", _("Notes:"));
  207. printf(" %s\n", _("See:"));
  208. printf(" %s\n", ("http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT"));
  209. printf(" %s\n", _("for THRESHOLD format and examples."));
  210. printf(_(UT_SUPPORT));
  211. printf("\n");
  212. }
  213. void
  214. print_usage(void)
  215. {
  216. printf(_("Usage:"));
  217. printf(" %s (-s | -h) -d val1[,val2,...,valn] [-l label]\n", progname);
  218. printf("[-w threshold] [-c threshold] [-v] [--help]\n");
  219. }