cassandra.pl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Getopt::Long;
  5. use POSIX ':sys_wait_h';
  6. my %messages = (
  7. bad_args => ' bad arguments;',
  8. incorrect_args => ' "warning" should be less than "critical";',
  9. node_tool_error => ' cannot run %s;',
  10. node_down => ' Cassandra node is DOWN',
  11. heap_usage => ' %d%% heap used;'
  12. );
  13. my $help;
  14. #Default threshold values:
  15. my $heap_w = 85;
  16. my $heap_c = 95;
  17. my $GetOptions_result = GetOptions(
  18. 'help' => \$help,
  19. 'heap_w=i' => \$heap_w,
  20. 'heap_c=i' => \$heap_c,
  21. );
  22. if ( $GetOptions_result != 1 ) {
  23. mydie( $messages{'bad_args'} );
  24. }
  25. if ( $heap_w >= $heap_c ) {
  26. mydie( $messages{'incorrect_args'} );
  27. }
  28. if ($help) {
  29. print <<"HELP";
  30. Usage: $0 [--heap_w=percent] [--heap_c=percent]
  31. --heap_w warning value for Java heap usage in percents (default=85)
  32. --heap_c critical value for Java heap usage in percents (default=95)
  33. HELP
  34. exit;
  35. }
  36. my $result = 'OK';
  37. my $result_description = '';
  38. my $result_perf = '';
  39. my %result_rank = (
  40. 'OK' => 0,
  41. 'WARNING' => 1,
  42. 'CRITICAL' => 2,
  43. );
  44. my $host = '127.0.0.1';
  45. my $nodetool_path = '/usr/bin/nodetool';
  46. my $nodetool_opts = '2>&1';
  47. check_cassandra_status();
  48. print "CASSANDRA $result - $result_description | $result_perf\n";
  49. exit $result_rank{$result};
  50. sub check_cassandra_status {
  51. my $heap_usage = 0;
  52. if ( !-x $nodetool_path ) {
  53. mydie( sprintf $messages{'node_tool_error'}, $nodetool_path );
  54. }
  55. my @return = `$nodetool_path -h $host info $nodetool_opts`;
  56. my $current_exit_status = WEXITSTATUS($?);
  57. if ( $current_exit_status != 0 ){
  58. set_result('CRITICAL');
  59. $result_description .= $messages{'node_down'};
  60. return;
  61. }
  62. foreach my $line (@return) {
  63. if ( $line =~ m{^Heap Memory \(MB\)\s*:\s*([\d\,\.]+)\s/\s([\d\,\.]+)} ) {
  64. my $heap_used = $1;
  65. my $heap_available = $2;
  66. $heap_used =~ s/\,/./;
  67. $heap_available =~ s/\,/./;
  68. $heap_usage = 100 * $heap_used / $heap_available;
  69. }
  70. }
  71. if ( $heap_usage >= $heap_c ) {
  72. set_result('CRITICAL');
  73. $result_description .= sprintf $messages{'heap_usage'}, $heap_usage;
  74. }
  75. elsif ( $heap_usage >= $heap_w ) {
  76. set_result('WARNING');
  77. $result_description .= sprintf $messages{'heap_usage'}, $heap_usage;
  78. }
  79. $result_perf .= sprintf ("heap_mem=%.2f ", $heap_usage);
  80. return;
  81. }
  82. sub set_result {
  83. my ($new_result) = @_;
  84. if ( $result_rank{$result} < $result_rank{$new_result} ) {
  85. $result = $new_result;
  86. if ( $result_description eq '' ) {
  87. $result_description .= 'WARNING:';
  88. }
  89. }
  90. return;
  91. }
  92. sub mydie {
  93. my ($message) = @_;
  94. print "CASSANDRA CRITICAL - $message\n";
  95. exit 2;
  96. }