| 1 |
/* |
| 2 |
This file is the for the Flex code-generator: http://flex.sourceforge.net/ |
| 3 |
It also requires libmemcached: http://libmemcached.org/libMemcached.html |
| 4 |
|
| 5 |
For yum based distributions, eg Red Hat/Fedora/SuSE/Mageia |
| 6 |
yum install gcc flex glibc-devel libmemcached-devel |
| 7 |
|
| 8 |
For apt based distributions, eg Debian/Ubuntu |
| 9 |
apt-get install gcc make flex libc6-dev libmemcached-dev |
| 10 |
|
| 11 |
Build it using: |
| 12 |
make LDFLAGS=-lmemcached check_memcached |
| 13 |
or |
| 14 |
make LDFLAGS="-lmemcached -lpthread" check_memcached |
| 15 |
(depending on your Linux distro) |
| 16 |
|
| 17 |
This program was initially developed by Lonely Planet for internal use |
| 18 |
and has kindly been made available to the Open Source community for |
| 19 |
redistribution and further development under the terms of the |
| 20 |
GNU General Public License v3: http://www.gnu.org/licenses/gpl.html |
| 21 |
|
| 22 |
This program is supplied 'as-is', in the hope that it will be useful, but |
| 23 |
neither Lonely Planet nor the authors make any warranties or guarantees |
| 24 |
as to its correct operation, including its intended function. |
| 25 |
|
| 26 |
Or in other words: |
| 27 |
Test it yourself, and make sure it works for YOU. |
| 28 |
|
| 29 |
Author: George Hansper e-mail: [email protected] |
| 30 |
|
| 31 |
*/ |
| 32 |
%{ |
| 33 |
|
| 34 |
/* --------------------------- #include ------------------------------ */ |
| 35 |
|
| 36 |
#include <stdio.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#include <string.h> |
| 39 |
/* For getopt() */ |
| 40 |
#include <unistd.h> |
| 41 |
extern int optind, opterr, optopt; |
| 42 |
|
| 43 |
#include <errno.h> |
| 44 |
#include <libmemcached/memcached.h> |
| 45 |
|
| 46 |
#define print_v(args...) if ( verbose ) fprintf(stderr,args ); |
| 47 |
|
| 48 |
#define DEBUG 0 |
| 49 |
#if DEBUG == 1 |
| 50 |
#define print_debug(args...) fprintf(stderr,args ); |
| 51 |
#else |
| 52 |
#define print_debug(args...) |
| 53 |
#endif |
| 54 |
|
| 55 |
YY_BUFFER_STATE yy_memcache_buffer; |
| 56 |
|
| 57 |
#define YY_USER_ACTION buffer_position+=yyleng; |
| 58 |
|
| 59 |
/* --------------------------- static/global variables ------------------------------ */ |
| 60 |
|
| 61 |
static int default_port = 11211; |
| 62 |
static int max_n_stats = 30; |
| 63 |
static int min_stats_interval = 30; /* Compare stats at least 30 minutes old, at least */ |
| 64 |
static int verbose=0; |
| 65 |
static int perfstats_for_rrdtool=0; |
| 66 |
static double min_hit_miss = 2.0; |
| 67 |
static uint64_t max_evictions = 10; |
| 68 |
static double timeout=1.0; |
| 69 |
|
| 70 |
static char *hostname; |
| 71 |
static int port; |
| 72 |
static char *argv0; |
| 73 |
|
| 74 |
static char *memcached_key = "check_memcached"; /* The key we will store our data under */ |
| 75 |
|
| 76 |
/* The expiry time on the stats object, in seconds. Two hours between checks is pretty generous */ |
| 77 |
static int memcache_stats_object_expiry_time = 60*60*2; |
| 78 |
|
| 79 |
static char *memory_error_message = "CRITICAL: Could not allocate memory"; |
| 80 |
|
| 81 |
static int buffer_position; |
| 82 |
static memcached_stat_st stats; |
| 83 |
static uint64_t obj_time_last = 0; |
| 84 |
static uint64_t obj_time_oldest = 0; |
| 85 |
static uint64_t obj_time = 0; |
| 86 |
static int obj_n_prune = 0; |
| 87 |
static int obj_n_stats; |
| 88 |
static uint64_t obj_get_hits; |
| 89 |
static uint64_t obj_get_misses; |
| 90 |
static uint64_t obj_evictions; |
| 91 |
static uint64_t obj_cmd_get; |
| 92 |
static uint64_t obj_cmd_set; |
| 93 |
static uint64_t *save_to; |
| 94 |
|
| 95 |
/* --------------------------- functions() ------------------------------ */ |
| 96 |
void usage(); |
| 97 |
int check_memcached(); |
| 98 |
char * get_current_stats(memcached_st*); |
| 99 |
char * update_stats_obj(char *, size_t , char *); |
| 100 |
|
| 101 |
/* ===================================================================================== */ |
| 102 |
%} |
| 103 |
|
| 104 |
%option yylineno |
| 105 |
|
| 106 |
%s FIND_OFFSET FIND_STATS DUMMY |
| 107 |
|
| 108 |
%% |
| 109 |
<DUMMY>.|\n ECHO; |
| 110 |
time= { save_to = &obj_time_last; obj_n_stats++; } |
| 111 |
get_hits= { save_to = &obj_get_hits; } |
| 112 |
get_misses= { save_to = &obj_get_misses; } |
| 113 |
evictions= { save_to = &obj_evictions; } |
| 114 |
cmd_get= { save_to = &obj_cmd_get; } |
| 115 |
cmd_set= { save_to = &obj_cmd_set; } |
| 116 |
<FIND_STATS>[0-9]+ { |
| 117 |
if ( save_to != NULL ) { |
| 118 |
if ( save_to == &obj_time_last ) { |
| 119 |
/* New time-stamp */ |
| 120 |
(*save_to) = (uint64_t) strtoll(yytext, NULL, 10); |
| 121 |
save_to = NULL; |
| 122 |
if ( obj_time_last <= stats.time - 60*min_stats_interval && |
| 123 |
obj_time_last > obj_time ) { |
| 124 |
obj_time = obj_time_last; |
| 125 |
/* Remove any stale data */ |
| 126 |
obj_get_hits = 0; |
| 127 |
obj_get_misses = 0; |
| 128 |
obj_evictions = 0; |
| 129 |
obj_cmd_get = 0; |
| 130 |
obj_cmd_set = 0; |
| 131 |
} |
| 132 |
if( obj_time_oldest == 0 || obj_time_last < obj_time_oldest ) { |
| 133 |
obj_time_oldest = obj_time_last; |
| 134 |
} |
| 135 |
} else if( obj_time == obj_time_last ) { |
| 136 |
/* New cadidate data - save it */ |
| 137 |
(*save_to) = (uint64_t) strtoll(yytext, NULL, 10); |
| 138 |
save_to = NULL; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
<FIND_OFFSET>[0-9]+ ; /* Don't convert or save integers, just find the offset of the 1st item to keep */ |
| 143 |
<FIND_OFFSET>\n { |
| 144 |
if ( obj_n_stats >= obj_n_prune ) { |
| 145 |
return(buffer_position); |
| 146 |
} |
| 147 |
} |
| 148 |
<FIND_OFFSET><<EOF>> { return(buffer_position); } |
| 149 |
"\t"|" " ; |
| 150 |
. { save_to = NULL; } |
| 151 |
\n { save_to = NULL; } |
| 152 |
%% |
| 153 |
/* ==================================================================================================== */ |
| 154 |
|
| 155 |
yywrap() { |
| 156 |
return 1; |
| 157 |
} |
| 158 |
|
| 159 |
/* ==================================================================================================== */ |
| 160 |
int main(int argc, char **argv) { |
| 161 |
char opt_c; |
| 162 |
int arg_int; |
| 163 |
char *trail_c; |
| 164 |
int n; |
| 165 |
int i; |
| 166 |
int result; |
| 167 |
int arg_error = 0; |
| 168 |
argv0 = argv[0]; |
| 169 |
while ( ( opt_c = getopt(argc, argv, "hvrH:p:w:c:t:T:n:E:k:K:") ) != -1 ) { |
| 170 |
switch( opt_c ) { |
| 171 |
case 'H': |
| 172 |
/* Hostname or IP address - support for comma-separated list */ |
| 173 |
hostname = malloc(sizeof(char) * (strlen(optarg)+1)); |
| 174 |
result = sscanf(optarg, "%[-.0-9a-zA-Z_]%n:%d%n",hostname,&n,&port,&n); |
| 175 |
print_debug("%d+%d # >>> %s <<< === %d === %s\n",result,n,hostname, port, optarg); |
| 176 |
switch(result) { |
| 177 |
case 1: |
| 178 |
/* Port not specified - use default_port */ |
| 179 |
port = 0; |
| 180 |
break; |
| 181 |
case 2: |
| 182 |
/* OK hostname:port specified */ |
| 183 |
break; |
| 184 |
default: |
| 185 |
arg_error = 1; |
| 186 |
break; |
| 187 |
} |
| 188 |
break; |
| 189 |
case 'p': |
| 190 |
arg_int = strtol(optarg,&trail_c,10); |
| 191 |
if ( *trail_c != '\0' ) { |
| 192 |
fprintf(stderr,"option \"-p port\": port must be a number, not \"%s\"\n",optarg); |
| 193 |
arg_error = 1; |
| 194 |
} else { |
| 195 |
default_port = arg_int; |
| 196 |
} |
| 197 |
break; |
| 198 |
case 'n': |
| 199 |
arg_int = strtol(optarg,&trail_c,10); |
| 200 |
if ( *trail_c != '\0' ) { |
| 201 |
fprintf(stderr,"option \"-n max_stats\": max_stats must be a number, not \"%s\"\n",optarg); |
| 202 |
arg_error = 1; |
| 203 |
} else { |
| 204 |
max_n_stats = arg_int; |
| 205 |
} |
| 206 |
break; |
| 207 |
case 'T': |
| 208 |
min_stats_interval = strtol(optarg,NULL,10); |
| 209 |
break; |
| 210 |
case 'v': |
| 211 |
verbose=1; |
| 212 |
break; |
| 213 |
case 'w': |
| 214 |
min_hit_miss = strtod(optarg,NULL); |
| 215 |
break; |
| 216 |
case 'E': |
| 217 |
max_evictions = strtoll(optarg,NULL,10); |
| 218 |
break; |
| 219 |
case 't': |
| 220 |
timeout = strtod(optarg,NULL); |
| 221 |
break; |
| 222 |
case 'r': |
| 223 |
perfstats_for_rrdtool=1; |
| 224 |
break; |
| 225 |
case 'c': |
| 226 |
/* Ignored - for now */ |
| 227 |
break; |
| 228 |
case 'k': |
| 229 |
if ( optarg != NULL && strlen(optarg) > 1 ) { |
| 230 |
memcached_key = optarg; |
| 231 |
} |
| 232 |
break; |
| 233 |
case 'K': |
| 234 |
memcache_stats_object_expiry_time = strtol(optarg,NULL,10); |
| 235 |
break; |
| 236 |
case 'h': |
| 237 |
default: |
| 238 |
arg_error = 1; |
| 239 |
break; |
| 240 |
} |
| 241 |
} |
| 242 |
if ( port == 0 ) { |
| 243 |
port = default_port; |
| 244 |
} |
| 245 |
if ( arg_error || hostname == NULL ) { |
| 246 |
usage(); |
| 247 |
exit(1); |
| 248 |
} |
| 249 |
|
| 250 |
return (check_memcached()); |
| 251 |
} |
| 252 |
|
| 253 |
/* ==================================================================================================== */ |
| 254 |
int check_memcached() { |
| 255 |
memcached_st *my_memcached; |
| 256 |
memcached_return error; |
| 257 |
memcached_result_st *result; |
| 258 |
uint32_t flags; |
| 259 |
char *object; |
| 260 |
char *new_object; |
| 261 |
size_t object_size; |
| 262 |
char new_stats; |
| 263 |
int n; |
| 264 |
char *current_stats_str; |
| 265 |
double hit_miss; |
| 266 |
char *nagios_service_tmp; |
| 267 |
char *nagios_service_output; /* error/warning message */ |
| 268 |
char *nagios_perfdata; /* Performance data */ |
| 269 |
size_t str_bytes = 0; |
| 270 |
int nagios_result; |
| 271 |
|
| 272 |
/* ----------------------------------------------------------------------------------------- */ |
| 273 |
/* Connect to the memcached server */ |
| 274 |
|
| 275 |
my_memcached = memcached_create(NULL); |
| 276 |
if ( my_memcached == NULL ) { |
| 277 |
puts("CRITICAL: Could not create memcached_st using memcached_create()\n"); |
| 278 |
exit(2); |
| 279 |
} |
| 280 |
|
| 281 |
error = memcached_server_add(my_memcached, hostname, port); |
| 282 |
if ( error ) { |
| 283 |
printf("CRITICAL: %s\n",memcached_strerror(my_memcached, error)); |
| 284 |
exit(2); |
| 285 |
} |
| 286 |
memcached_behavior_set(my_memcached, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, (uint64_t) (timeout * 1000)); |
| 287 |
memcached_behavior_set(my_memcached, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, (uint64_t) (timeout * 1000)); |
| 288 |
memcached_behavior_set(my_memcached, MEMCACHED_BEHAVIOR_NO_BLOCK, 0); |
| 289 |
|
| 290 |
print_debug("default timeout = %lu %lu %lu %lu... %1.1f\n",my_memcached->connect_timeout, |
| 291 |
my_memcached->poll_timeout, |
| 292 |
my_memcached->snd_timeout, |
| 293 |
my_memcached->rcv_timeout, |
| 294 |
timeout); |
| 295 |
/* ----------------------------------------------------------------------------------------- */ |
| 296 |
/* Get the stats from this server */ |
| 297 |
current_stats_str = get_current_stats(my_memcached); |
| 298 |
|
| 299 |
/* ----------------------------------------------------------------------------------------- */ |
| 300 |
/* Get our data (if it's there) */ |
| 301 |
/* memcached_behavior_set(my_memcached, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 0); */ |
| 302 |
flags = 0; |
| 303 |
object = memcached_get(my_memcached, memcached_key, strlen(memcached_key), &object_size, &flags, &error); |
| 304 |
print_debug("Object = %p, Length = %lu, Error = %d (%s)\n",object,object_size,error,memcached_strerror(my_memcached, error)); |
| 305 |
if ( error ) { |
| 306 |
print_v("No stats history object found (key=%s)\n",memcached_key); |
| 307 |
} |
| 308 |
|
| 309 |
print_debug("uint64_t = %d, long = %d, long long = %d\n",sizeof(uint64_t), sizeof(long int), sizeof(long long int)); |
| 310 |
|
| 311 |
print_debug("Here it is...%d bytes\n%s\n\n",object_size,object); |
| 312 |
|
| 313 |
obj_n_stats=0; |
| 314 |
BEGIN(FIND_STATS); |
| 315 |
yy_memcache_buffer = yy_scan_bytes ( object, object_size ); |
| 316 |
yylex(); |
| 317 |
|
| 318 |
print_debug("Found %d items in stats history\n",obj_n_stats); |
| 319 |
print_debug("Best stats:\ntime=%llu cmd_get=%llu cmd_set=%llu get_hits=%llu get_misses=%llu evictions=%llu\n", |
| 320 |
obj_time,obj_cmd_get,obj_cmd_set,obj_get_hits,obj_get_misses,obj_evictions); |
| 321 |
print_debug("New stats\n%s\n",current_stats_str); |
| 322 |
|
| 323 |
new_object = update_stats_obj(object, object_size, current_stats_str); |
| 324 |
|
| 325 |
/* ----------------------------------------------------------------------------------------- */ |
| 326 |
/* Store new data if it's at least 1 minute newer */ |
| 327 |
nagios_result=0; |
| 328 |
nagios_service_output = NULL; |
| 329 |
if( new_object != NULL ) { |
| 330 |
error = memcached_set(my_memcached, |
| 331 |
memcached_key, |
| 332 |
strlen(memcached_key), |
| 333 |
new_object, |
| 334 |
strlen(new_object), /* trailing \0 is not stored with the object(!) */ |
| 335 |
(time_t) memcache_stats_object_expiry_time, /* Expire after (default) 2 hours */ |
| 336 |
0 |
| 337 |
); |
| 338 |
if ( error ) { |
| 339 |
str_bytes = asprintf(&nagios_service_output,"Could not store updated stats object - %s; ", |
| 340 |
memcached_strerror(my_memcached, error)); |
| 341 |
nagios_result|=2; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
if ( nagios_service_output == NULL ) { |
| 346 |
str_bytes = asprintf(&nagios_service_output,""); |
| 347 |
} |
| 348 |
/* ----------------------------------------------------------------------------------------- */ |
| 349 |
/* ---- Analyze the stats, return 0,1,2 as required ---------------------------------------- */ |
| 350 |
|
| 351 |
if ( obj_time == 0 ) { |
| 352 |
nagios_service_tmp = nagios_service_output; |
| 353 |
str_bytes = asprintf(&nagios_service_output, |
| 354 |
"%sno stats available yet. Come back in %d minutes; ", |
| 355 |
nagios_service_tmp, |
| 356 |
min_stats_interval - (stats.time - obj_time_oldest ) / 60 |
| 357 |
); |
| 358 |
free ( nagios_service_tmp ); |
| 359 |
} else { |
| 360 |
/* ---- Evictions ---- */ |
| 361 |
if ( max_evictions > 0 && ( stats.evictions - obj_evictions) >= max_evictions ) { |
| 362 |
nagios_result|=1; |
| 363 |
nagios_service_tmp = nagios_service_output; |
| 364 |
str_bytes = asprintf(&nagios_service_output, |
| 365 |
"%sToo many evictions: %d; ", |
| 366 |
nagios_service_tmp, |
| 367 |
stats.evictions - obj_evictions |
| 368 |
); |
| 369 |
free ( nagios_service_tmp ); |
| 370 |
} |
| 371 |
|
| 372 |
/* ---- Hit/Miss ratio ---- */ |
| 373 |
nagios_service_tmp = nagios_service_output; |
| 374 |
if ( ( stats.get_misses - obj_get_misses ) > 0 ) { |
| 375 |
hit_miss = ( stats.get_hits - obj_get_hits ) * 1.0 / ( stats.get_misses - obj_get_misses ); |
| 376 |
if ( hit_miss < min_hit_miss ) { |
| 377 |
nagios_result|=1; |
| 378 |
str_bytes = asprintf(&nagios_service_output, |
| 379 |
"%sLow hit/miss rate: %1.1f; ", |
| 380 |
nagios_service_tmp, |
| 381 |
hit_miss |
| 382 |
); |
| 383 |
free ( nagios_service_tmp ); |
| 384 |
} else { |
| 385 |
str_bytes = asprintf(&nagios_service_output, |
| 386 |
"%shit/miss=%1.1f; ", |
| 387 |
nagios_service_tmp, |
| 388 |
hit_miss |
| 389 |
); |
| 390 |
} |
| 391 |
} else { |
| 392 |
hit_miss = 0; |
| 393 |
str_bytes = asprintf(&nagios_service_output,"%shit/miss=%llu/0; ", |
| 394 |
nagios_service_tmp, |
| 395 |
stats.get_hits - obj_get_hits |
| 396 |
); |
| 397 |
free ( nagios_service_tmp ); |
| 398 |
} |
| 399 |
|
| 400 |
nagios_service_tmp = nagios_service_output; |
| 401 |
str_bytes = asprintf(&nagios_service_output, |
| 402 |
"%shits=%llu misses=%llu evictions=%llu interval=%lu mins", |
| 403 |
nagios_service_tmp, |
| 404 |
stats.get_hits - obj_get_hits, |
| 405 |
stats.get_misses - obj_get_misses, |
| 406 |
stats.evictions - obj_evictions, |
| 407 |
(uint32_t) (stats.time - obj_time ) / 60 |
| 408 |
); |
| 409 |
free ( nagios_service_tmp ); |
| 410 |
} |
| 411 |
/* Add the performance data */ |
| 412 |
/* Remove trailing newline from current_stats_str */ |
| 413 |
n = strlen(current_stats_str); |
| 414 |
if ( n > 1 && *(current_stats_str+n-1) == '\n' ) { |
| 415 |
*(current_stats_str+n-1) = '\0'; |
| 416 |
} |
| 417 |
if ( str_bytes == -1 ) { |
| 418 |
puts(memory_error_message); |
| 419 |
exit(2); |
| 420 |
} |
| 421 |
|
| 422 |
/* Delta times may not apply if no suitable object found. Also, if NO object exists yet, there is nothing to calculate */ |
| 423 |
if ( perfstats_for_rrdtool == 1 ) { |
| 424 |
if ( obj_time == 0 ) { |
| 425 |
/* No performance stats available (yet) */ |
| 426 |
nagios_perfdata = ""; |
| 427 |
} else { |
| 428 |
/* |
| 429 |
str_bytes = asprintf(&nagios_perfdata, |
| 430 |
"%s delta_time_s=%lu gets_per_min=%llu sets_per_min=%llu hits_per_min=%llu misses_per_min=%llu evictions_per_min=%llu", |
| 431 |
current_stats_str, |
| 432 |
(uint32_t) ( stats.time - obj_time ), |
| 433 |
60 * ( stats.cmd_get - obj_cmd_get) / ( stats.time - obj_time ), |
| 434 |
60 * ( stats.cmd_set - obj_cmd_set )/ ( stats.time - obj_time ), |
| 435 |
60 * ( stats.get_hits - obj_get_hits ) / ( stats.time - obj_time ), |
| 436 |
60 * ( stats.get_misses - obj_get_misses ) / ( stats.time - obj_time ), |
| 437 |
60 * ( stats.evictions - obj_evictions ) / ( stats.time - obj_time ) |
| 438 |
); |
| 439 |
*/ |
| 440 |
str_bytes = asprintf(&nagios_perfdata, |
| 441 |
"gets_per_min=%.2f sets_per_min=%.2f hits_per_min=%.2f misses_per_min=%.2f evictions_per_min=%.2f hit_miss_ratio=%.2f", |
| 442 |
60.0 * ( stats.cmd_get - obj_cmd_get) / ( stats.time - obj_time ), |
| 443 |
60.0 * ( stats.cmd_set - obj_cmd_set )/ ( stats.time - obj_time ), |
| 444 |
60.0 * ( stats.get_hits - obj_get_hits ) / ( stats.time - obj_time ), |
| 445 |
60.0 * ( stats.get_misses - obj_get_misses ) / ( stats.time - obj_time ), |
| 446 |
60.0 * ( stats.evictions - obj_evictions ) / ( stats.time - obj_time ), |
| 447 |
/* Hit/Miss ratio */ |
| 448 |
0. + hit_miss |
| 449 |
); |
| 450 |
} |
| 451 |
|
| 452 |
} else if ( obj_time == 0 ) { |
| 453 |
nagios_perfdata = ""; |
| 454 |
} else { |
| 455 |
str_bytes = asprintf(&nagios_perfdata, |
| 456 |
"%s delta_time=%lu delta_cmd_get=%llu delta_cmd_set=%llu delta_get_hits=%llu delta_get_misses=%llu delta_evictions=%llu", |
| 457 |
current_stats_str, |
| 458 |
(uint32_t) ( stats.time - obj_time ), |
| 459 |
stats.cmd_get - obj_cmd_get, |
| 460 |
stats.cmd_set - obj_cmd_set, |
| 461 |
stats.get_hits - obj_get_hits, |
| 462 |
stats.get_misses - obj_get_misses, |
| 463 |
stats.evictions - obj_evictions |
| 464 |
); |
| 465 |
} |
| 466 |
|
| 467 |
switch(nagios_result) { |
| 468 |
case 0: |
| 469 |
printf("OK: %s|%s\n",nagios_service_output,nagios_perfdata); |
| 470 |
break; |
| 471 |
case 1: |
| 472 |
printf("WARNING: %s|%s\n",nagios_service_output,nagios_perfdata); |
| 473 |
break; |
| 474 |
default: |
| 475 |
printf("CRITICAL: %s|%s\n",nagios_service_output,nagios_perfdata); |
| 476 |
nagios_result = 2; |
| 477 |
break; |
| 478 |
} |
| 479 |
|
| 480 |
if ( ( stats.get_misses - obj_get_misses ) > 0 ) { |
| 481 |
print_v("\nHit/Miss = %llu / %llu = %1.1f\n", |
| 482 |
stats.get_hits - obj_get_hits, |
| 483 |
stats.get_misses - obj_get_misses, |
| 484 |
hit_miss); |
| 485 |
} else { |
| 486 |
print_v("\nNo misses - very good\n"); |
| 487 |
} |
| 488 |
|
| 489 |
print_v("\nHistory object '%s':\n%s\nNew stats:\n%s (%sstored)\n",memcached_key,object,current_stats_str, |
| 490 |
new_object== NULL? "not ":""); |
| 491 |
|
| 492 |
memcached_free(my_memcached); |
| 493 |
|
| 494 |
return(nagios_result); |
| 495 |
} |
| 496 |
|
| 497 |
/* ==================================================================================================== */ |
| 498 |
char *get_current_stats(memcached_st *my_memcached) { |
| 499 |
memcached_return error; |
| 500 |
int str_bytes; |
| 501 |
char * current_stats_str_format; |
| 502 |
char * current_stats_str; |
| 503 |
memcached_stat_st *stats_array; |
| 504 |
|
| 505 |
/* error = memcached_stat_servername(&stats, NULL, hostname, port); - cannot set timeout, but gives better error responses :-( */ |
| 506 |
stats_array = memcached_stat(my_memcached, NULL, &error); |
| 507 |
|
| 508 |
if ( error ) { |
| 509 |
printf("CRITICAL: Could not connect to server %s:%d - %s (%d)\n",hostname, port, |
| 510 |
memcached_strerror(my_memcached, error), error |
| 511 |
); |
| 512 |
exit(2); |
| 513 |
} |
| 514 |
stats = stats_array[0]; |
| 515 |
free(stats_array); |
| 516 |
|
| 517 |
current_stats_str_format = "time=%lu cmd_get=%llu cmd_set=%llu get_hits=%llu get_misses=%llu evictions=%llu\n"; |
| 518 |
|
| 519 |
str_bytes = asprintf(¤t_stats_str,current_stats_str_format, |
| 520 |
stats.time,stats.cmd_get,stats.cmd_set,stats.get_hits,stats.get_misses,stats.evictions); |
| 521 |
|
| 522 |
obj_time_oldest = stats.time; |
| 523 |
|
| 524 |
if ( str_bytes == -1 ) { |
| 525 |
puts(memory_error_message); |
| 526 |
exit(2); |
| 527 |
} |
| 528 |
|
| 529 |
return(current_stats_str); |
| 530 |
} |
| 531 |
|
| 532 |
/* ==================================================================================================== */ |
| 533 |
char * update_stats_obj(char *object, size_t object_size, char *current_stats_str) { |
| 534 |
int obj_offset; |
| 535 |
char *new_object; |
| 536 |
size_t new_object_size; |
| 537 |
size_t graft_offset; |
| 538 |
int graft_size; |
| 539 |
char *s; |
| 540 |
if ( stats.time < obj_time_last + 30 ) { |
| 541 |
/* Only store new stats if at least 1 minute has passed since the last stats were stored */ |
| 542 |
print_v("New stats are less than 30s newer than last entry - not stored\n"); |
| 543 |
return(NULL); |
| 544 |
} |
| 545 |
/* Prune stats from the start of the object */ |
| 546 |
obj_n_prune = 1 + obj_n_stats - max_n_stats; |
| 547 |
if ( obj_n_prune > 0 ) { |
| 548 |
buffer_position=0; |
| 549 |
obj_n_stats = 0; |
| 550 |
BEGIN(FIND_OFFSET); |
| 551 |
yy_memcache_buffer = yy_scan_bytes ( object, object_size ); |
| 552 |
obj_offset = yylex(); |
| 553 |
} else { |
| 554 |
obj_offset = 0; |
| 555 |
} |
| 556 |
/* |
| 557 |
while( yylex() ) { |
| 558 |
} |
| 559 |
print_debug("Prune %d items / %d chars from start of object\nNew:\n%s\n",obj_n_prune,obj_offset,object+obj_offset); |
| 560 |
*/ |
| 561 |
/* Add extra 2 chars for trailing \n\0 */ |
| 562 |
new_object_size = object_size - obj_offset + (size_t) ( (strlen(current_stats_str)+ 2) * sizeof(char)); |
| 563 |
new_object = malloc(new_object_size * sizeof(char) ); |
| 564 |
if (new_object == NULL ) { |
| 565 |
puts(memory_error_message); |
| 566 |
exit(2); |
| 567 |
} |
| 568 |
graft_size = object_size - obj_offset; |
| 569 |
if ( graft_size > 0 ) { |
| 570 |
memcpy(new_object,object+obj_offset,graft_size); |
| 571 |
} else { |
| 572 |
graft_size = 0; |
| 573 |
} |
| 574 |
/* Ensure there is a \0 on the end of the object */ |
| 575 |
*(new_object+graft_size)='\0'; |
| 576 |
/* Make sure there is a newline between each item in the list */ |
| 577 |
s = new_object + graft_size; |
| 578 |
if ( graft_size > 0 ) { |
| 579 |
while ( ( *s == '\0' || *s == '\n' ) && s > new_object ) { |
| 580 |
/* Trailing \0 and trailing \n - rewind a char or more */ |
| 581 |
s--; |
| 582 |
} |
| 583 |
/* Add \n\0 to end of buffer */ |
| 584 |
strcpy(++s,"\n"); |
| 585 |
} |
| 586 |
strcat(s,current_stats_str); |
| 587 |
return(new_object); |
| 588 |
print_debug("New Object is:\n%s\nNew size: %d\nReal size: %d\n",new_object, new_object_size, (strlen(new_object)+1)); |
| 589 |
return(new_object); |
| 590 |
} |
| 591 |
|
| 592 |
/* ==================================================================================================== */ |
| 593 |
|
| 594 |
void usage() { |
| 595 |
puts("Usage:"); |
| 596 |
printf("\t%s -H hostname[:port] [-v] [-p port] [-t time_out] [-w min_hit_miss] [-n max_stats] [-T min_stats_interval] [-E max_evictions] [-k key] [-K key_expiry_time] [-r]\n", argv0); |
| 597 |
printf("\t-H ... Hostname or IP address (required)\n\t optional \":port\" overrides -p\n"); |
| 598 |
printf("\t-p ... Port number (default: %u)\n",default_port); |
| 599 |
printf("\t-v ... verbose messages\n"); |
| 600 |
printf("\t-n ... Keep up to this many items in the history object in memcached (default: %u)\n",max_n_stats); |
| 601 |
printf("\t-T ... Minimum time interval (in minutes) to use to analyse stats. (default: %u)\n",min_stats_interval); |
| 602 |
printf("\t-w ... Generate warning if quotient of hits/misses falls below this value (default: %1.1f)\n",min_hit_miss); |
| 603 |
printf("\t-E ... Generate warning if number of evictions exceeds this threshold. 0=disable. (default: %llu)\n",max_evictions); |
| 604 |
printf("\t-t ... timeout in seconds (default: %1.1f)\n",timeout); |
| 605 |
printf("\t-k ... key name for history object (default: %s)\n",memcached_key); |
| 606 |
printf("\t-K ... expiry time in seconds for history object (default: %u)\n",memcache_stats_object_expiry_time); |
| 607 |
printf("\t-r ... output performance statistics as rate-per-minute figures (better suited to pnp4nagios)\n"); |
| 608 |
printf("\nExample:\n"); |
| 609 |
printf("\t%s -H 192.168.1.1 -p 11212 -w 10 -E 5 -t 0.3 -T 10 -n 10\n",argv0); |
| 610 |
printf("\nNote: the history object \"%s\" will not be updated if the new stats are less than 30 seconds old\n",memcached_key); |
| 611 |
printf( " compared to the most recent stats in the \"%s\" object\n",memcached_key); |
| 612 |
|
| 613 |
} |