check_cluster.c 6.6 KB

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