check_cluster.c 6.6 KB

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