check_gearman.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*********************************************************************************
  2. *
  3. * Nagios check_gearmand plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2013-2014 Nagios Plugin Development Team
  7. *
  8. * Description:
  9. *
  10. * This file contains the check_gearmand plugin
  11. *
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. *********************************************************************************/
  27. #include "common.h"
  28. #include "utils.h"
  29. #include "utils_tcp.h"
  30. #include "netutils.h"
  31. // progam values
  32. char *progname = "check_gearmand";
  33. char *version = "0.1";
  34. char *email = "devel@nagios-plugins.org";
  35. char *copyright = "2013-2014";
  36. char *developer = "Spenser Reinhardt";
  37. // function initialization
  38. static int process_arguments (int, char **);
  39. void print_help (void);
  40. void print_usage (void);
  41. //base arg values
  42. static char *server_addr = NULL;
  43. static int socket_port = 4730;
  44. static double socket_timeout = 30;
  45. static int crit_worker = 0;
  46. static int warn_worker = 0;
  47. static char *func_str = NULL;
  48. static struct function_string {
  49. char *name = NULL;
  50. int workers = 0;
  51. struct function_string *next = 0;
  52. } func_struct;
  53. // base socket values
  54. static char *server_send = NULL;
  55. static char *server_quit = NULL;
  56. static char **server_expect;
  57. static size_t server_expect_count = 0;
  58. // base program values
  59. static long microsec;
  60. static char buffer[1024];
  61. int main (int argc, char **argv) {
  62. // base values
  63. int result = STATE_UNKNOWN;
  64. char *status = NULL;
  65. struct timeval tv;
  66. struct timeval timeout;
  67. size_t len;
  68. int match = -1;
  69. fd_set rfds;
  70. FD_ZERO ( &rfds );
  71. setlocale ( LC_ALL, "" );
  72. //program and service name
  73. progname = strrchr ( argv[0], '/' );
  74. if ( progname != NULL ) progname++;
  75. else progname = argv[0];
  76. len = strlen ( progname );
  77. if ( len > 6 && !memcmp(progname, "check_", 6) ) {
  78. SERVICE = strdup ( progname + 6 );
  79. for ( i = 0; i < len -6; i++ )
  80. SERVICE[i] = toupper( SERVICE[i] );
  81. }
  82. server_expect = calloc ( sizeof(char *), 2 );
  83. if ( process_arguments (&argc, argv, progname) == ERROR )
  84. usage4 ( _("Cound not parse arguments") );
  85. //timer
  86. signal ( SIGALRM, socket_timeout_alarm_handler );
  87. alarm( socket_timeout );
  88. gettimeofday ( &tv, NULL );
  89. // attempt connection and loop for recv
  90. result = process_tcp_request2( server_addr, server_port, &server_send, &server_expect, &server_expect_count )
  91. } // end main
  92. static int process_arguments (int argc, char **argv) {
  93. int c;
  94. int escape = 0;
  95. char *temp;
  96. int option = 0;
  97. static struct option longopts[] = {
  98. {"hostname", required_argument, 0, 'H'},
  99. {"critical", required_argument, 0, 'c'},
  100. {"warning", required_argument, 0, 'w'},
  101. {"timeout", required_argument, 0, 't'},
  102. {"port", required_argument, 0, 'p'},
  103. {"functions", required_argument, 0, 'f'},
  104. {"verbose", no_argument, 0, 'v'},
  105. {"help", no_argument, 0, 'h'},
  106. {0, 0, 0, 0}
  107. };
  108. if ( argc < 2 ) usage4 ( _("No arguments found.") );
  109. while ( 1 ) {
  110. c = getopt_long ( argc, argv, "+hvH:c:w:t:p:f:", longopts, &option );
  111. if ( c == -1 || c == EOF || c == 1 ) break;
  112. switch ( c ) {
  113. case '?':
  114. usage5 ();
  115. case 'h':
  116. print_help ();
  117. exit ( STATE_OK );
  118. case 'v':
  119. flags |= FLAG_VERBOSE;
  120. match_flags |= NP_MATCH_VERBOSE;
  121. break;
  122. case 'H':
  123. host_specified = TRUE;
  124. server_address = optarg;
  125. break;
  126. case 'p':
  127. if ( !is_intpos (optarg) )
  128. usage4 ( _("Port must be a positive integer.") );
  129. else
  130. server_port = atoi( optarg );
  131. break;
  132. case 't':
  133. if ( !is_intpos (optarg) )
  134. usage4 ( _("Timeout must be a positive integer.") );
  135. else
  136. socket_timeout = atoi ( optarg );
  137. break;
  138. case 'c':
  139. if ( !is_intpos (optarg) )
  140. usage4 ( _("Critical threshold must be a positive integer.") );
  141. else
  142. crit_worker = atoi ( optarg );
  143. break;
  144. case 'w':
  145. if ( !is_intpos (optarg) )
  146. usage ( _("Warning threshold must be a positive integer.") );
  147. else
  148. warn_worker = atoi ( optarg );
  149. break;
  150. case 'f':
  151. if ( optarg == NULL )
  152. usage ( _("Functions must be definied.") );
  153. else
  154. func_str = optarg;
  155. break;
  156. } // end case
  157. } // end while
  158. c = optind;
  159. // verify host has been specified (TRUE is set)
  160. if ( host_specified == FLASE && c < argc )
  161. server_address = strdup ( argv[c++] );
  162. // verify server addr is not null and is a nagios host
  163. if ( server_address == NULL )
  164. usage4 ( _("You must provide a server address.") );
  165. else if ( server_address[0] != '/' && is_host (server_address) == FALSE )
  166. die (STATE_CRITICAL, "%s %s - %s: %s\n", SERVICE state_text(STATE_CRITICAL), _("Invalid hostname, address, or socket"), server_address );
  167. // verify warning is less than crit value
  168. if ( warn_worker >= crit_worker )
  169. die (STATE_CRITICAL, "Warning values must be less than critical values.");
  170. return TRUE;
  171. } // end process_arguments
  172. void print_usage (void) {
  173. printf( "%s\n", _("Usage:") );
  174. printf( "%s", _("check_gearmand -H host -p port -f <func1[:threshold],...,funcN[:thresholdN]>") );
  175. printf( "%s\n", _("[-t <timeout>] [-c <critical workers>] [-w <warning workers>] [-v] [-h") );
  176. } // end usage
  177. void print_help (void) {
  178. print_revision ( progname, NP_VERSION );
  179. printf ( COPYRIGHT, copyright, developer, email );
  180. printf ( "%s\n", _("This plugin tests a gearman job server. It expects all functions in the function list argument to be registered for one or more workers") );
  181. print_usage ();
  182. printf ( UT_HELP_VRSN );
  183. printf ( UT_EXTRA_OPTS );
  184. printf ( UT_HOST_PORT, 'p', "4730" );
  185. printf ( UT_IPv46 );
  186. printf ( "%s\n", _("-f, Comma separated string of functions and optional threshold values, separated by colons(;).") );
  187. printf ( "%s\n", _("-t, Connection timeout, default 10 seconds.") );
  188. printf ( "%s\n", _("-c, Low threshold for critical number of workers per function.") );
  189. printf ( "%s\n", _("-w, Low threshold for warning number of workers per function.") );
  190. printf ( "%s\n", _("-v, Enable verbose output.") );
  191. printf ( "%s\n", _("-h, Print help and usage.") );
  192. printf ( UT_SUPPORT );
  193. } // end print_help