4
0

check_cluster.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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-2014 Nagios Plugins Development Team
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. *
  23. *****************************************************************************/
  24. const char *progname = "check_cluster";
  25. const char *copyright = "2000-2014";
  26. const char *email = "devel@nagios-plugins.org";
  27. #include "common.h"
  28. #include "utils.h"
  29. #include "utils_base.h"
  30. #define CHECK_SERVICES 1
  31. #define CHECK_HOSTS 2
  32. void print_help (void);
  33. void print_usage (void);
  34. int total_services_ok=0;
  35. int total_services_warning=0;
  36. int total_services_unknown=0;
  37. int total_services_critical=0;
  38. int total_hosts_up=0;
  39. int total_hosts_down=0;
  40. int total_hosts_unreachable=0;
  41. char *warn_threshold;
  42. char *crit_threshold;
  43. int check_type=CHECK_SERVICES;
  44. char *data_vals=NULL;
  45. char *label=NULL;
  46. int verbose=0;
  47. int process_arguments(int,char **);
  48. int main(int argc, char **argv){
  49. char *ptr;
  50. int data_val;
  51. int return_code=STATE_OK;
  52. thresholds *thresholds = NULL;
  53. setlocale (LC_ALL, "");
  54. bindtextdomain (PACKAGE, LOCALEDIR);
  55. textdomain (PACKAGE);
  56. /* Parse extra opts if any */
  57. argv=np_extra_opts(&argc, argv, progname);
  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. {"version", 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,"hHsvVw: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. warn_threshold = strdup(optarg);
  148. break;
  149. case 'c': /* warning threshold */
  150. crit_threshold = strdup(optarg);
  151. break;
  152. case 'd': /* data values */
  153. data_vals=(char *)strdup(optarg);
  154. break;
  155. case 'l': /* text label */
  156. label=(char *)strdup(optarg);
  157. break;
  158. case 'v': /* verbose */
  159. verbose++;
  160. break;
  161. case 'V': /* version */
  162. print_revision (progname, NP_VERSION);
  163. exit (STATE_OK);
  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, NP_VERSION);
  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(UT_EXTRA_OPTS);
  190. printf (" %s\n", "-s, --service");
  191. printf (" %s\n", _("Check service cluster status"));
  192. printf (" %s\n", "-h, --host");
  193. printf (" %s\n", _("Check host cluster status"));
  194. printf (" %s\n", "-l, --label=STRING");
  195. printf (" %s\n", _("Optional prepended text output (i.e. \"Host cluster\")"));
  196. printf (" %s\n", "-w, --warning=THRESHOLD");
  197. printf (" %s\n", _("Specifies the range of hosts or services in cluster that must be in a"));
  198. printf (" %s\n", _("non-OK state in order to return a WARNING status level"));
  199. printf (" %s\n", "-c, --critical=THRESHOLD");
  200. printf (" %s\n", _("Specifies the range of hosts or services in cluster that must be in a"));
  201. printf (" %s\n", _("non-OK state in order to return a CRITICAL status level"));
  202. printf (" %s\n", "-d, --data=LIST");
  203. printf (" %s\n", _("The status codes of the hosts or services in the cluster, separated by"));
  204. printf (" %s\n", _("commas"));
  205. printf(UT_VERBOSE);
  206. printf("\n");
  207. printf("%s\n", _("Notes:"));
  208. printf(UT_THRESHOLDS_NOTES);
  209. printf ("\n");
  210. printf ("%s\n", _("Examples:"));
  211. printf (" %s\n", "check_cluster -s -d 2,0,2,0 -c @3:");
  212. printf (" %s\n", _("Will alert critical if there are 3 or more service data points in a non-OK") );
  213. printf (" %s\n", _("state.") );
  214. printf(UT_SUPPORT);
  215. }
  216. void
  217. print_usage(void)
  218. {
  219. printf("%s\n", _("Usage:"));
  220. printf(" %s (-s | -h) -d val1[,val2,...,valn] [-l label]\n", progname);
  221. printf("[-w threshold] [-c threshold] [-v] [--help]\n");
  222. }