| 1 | package Service::Engine::Health; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Data::Dumper; | ||
| 7 | use Module::Runtime qw(require_module); | ||
| 8 | |||
| 9 | use Service::Engine; | ||
| 10 | use threads; | ||
| 11 | use threads::shared; | ||
| 12 | use JSON; | ||
| 13 | |||
| 14 | our $Config; | ||
| 15 | our $Log; | ||
| 16 | our $Data; | ||
| 17 | our $EngineName; | ||
| 18 | our $EngineInstance; | ||
| 19 | our $Threads; | ||
| 20 | our $Checks = {}; | ||
| 21 | our $CheckList = []; | ||
| 22 | our $CheckCounts = {}; | ||
| 23 | our $Admin; | ||
| 24 | our $Alert; | ||
| 25 | our $check_prefix; | ||
| 26 | our $Thresholds; | ||
| 27 | our $Memcached; | ||
| 28 | |||
| 29 | share($Threads); | ||
| 30 | share($Checks); | ||
| 31 | share($CheckList); | ||
| 32 | share($CheckCounts); | ||
| 33 | share($Admin); | ||
| 34 | share($Log); | ||
| 35 | |||
| 36 | sub new { | ||
| 37 | |||
| 38 | my ($class,$options) = @_; | ||
| 39 | |||
| 40 | # set some defaults | ||
| 41 | my $attributes = {}; | ||
| 42 | |||
| 43 | # load options | ||
| 44 | if (ref($options) eq 'HASH') { | ||
| 45 | foreach my $option (keys %{$options}) { | ||
| 46 | $attributes->{$option} = $options->{$option}; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | # pull in some Service::Engine globals | ||
| 51 | $Config = $Service::Engine::Config; | ||
| 52 | $EngineName = $Service::Engine::EngineName; | ||
| 53 | $EngineInstance = $Service::Engine::EngineInstance; | ||
| 54 | $Threads = $Service::Engine::Threads; | ||
| 55 | $Log = $Service::Engine::Log; | ||
| 56 | $Admin = $Service::Engine::Admin; | ||
| 57 | $Alert = $Service::Engine::Alert; | ||
| 58 | |||
| 59 | $check_prefix = $EngineName . ':' . $EngineInstance . ':healthcheckID:'; | ||
| 60 | |||
| 61 | my $self = bless $attributes, $class; | ||
| 62 | |||
| 63 | # create our health checks | ||
| 64 | |||
| 65 | if (ref($Config->get_config('health')) eq 'HASH') { | ||
| 66 | |||
| 67 | my @options = (); | ||
| 68 | foreach my $service (keys $Config->get_config('health')->{'modules'}) { | ||
| 69 | |||
| 70 | my $service_conf = $Config->get_config('health')->{'modules'}->{$service}; | ||
| 71 | |||
| 72 | $Log->log({'msg'=>Dumper($service_conf),'level'=>3}); | ||
| 73 | |||
| 74 | if ($service_conf->{'enabled'}) { | ||
| 75 | $Log->log({'msg'=>"loading $service",'level'=>2}); | ||
| 76 | my $package = 'Service::Engine::Health::' . ucfirst lc $service; | ||
| 77 | |||
| 78 | if ($service_conf->{'module_name'}) { | ||
| 79 | $package = $EngineName . '::Modules::' . $service_conf->{'module_name'}; | ||
| 80 | } | ||
| 81 | my $res = eval { require_module($package) }; | ||
| 82 | if ($@) { | ||
| 83 | $Log->log({'msg'=>"error loading $service: $@",'level'=>1}); | ||
| 84 | } else { | ||
| 85 | my $obj = $package->new($Admin); | ||
| 86 | $Checks->{$service} = shared_clone($obj); | ||
| 87 | push @options, $service; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | } | ||
| 92 | |||
| 93 | $CheckList = \@options; | ||
| 94 | |||
| 95 | } | ||
| 96 | |||
| 97 | if ($Admin) { | ||
| 98 | $Admin->add_command({'module'=>$self, 'method'=>'overview', 'label'=>'Overview'}); | ||
| 99 | $Admin->add_command({'module'=>$self, 'method'=>'api_overview', 'label'=>'API Overview'}); | ||
| 100 | } | ||
| 101 | |||
| 102 | return $self; | ||
| 103 | |||
| 104 | } | ||
| 105 | |||
| 106 | sub start { # intended to run in its own thread in a loop | ||
| 107 | |||
| 108 | my ($self) = @_; | ||
| 109 | |||
| 110 | $Log->log({'msg'=>"starting Health Monitor",'level'=>1}); | ||
| 111 | |||
| 112 | my $timeout = $Config->get_config('health')->{'frequency'}; | ||
| 113 | $Thresholds = $Config->get_config('health')->{'alerting'}; | ||
| 114 | my $health_alerts_enabled = $Config->get_config('health')->{'health_alerts_enabled'}; | ||
| 115 | |||
| 116 | my $memcached_config = $Config->get_config('health')->{'memcached'}; | ||
| 117 | if (ref($memcached_config) eq 'HASH') { | ||
| 118 | if (ref($memcached_config->{'options'}) eq 'HASH') { | ||
| 119 | if ($memcached_config->{'options'}->{'handle'} && $memcached_config->{'options'}->{'enabled'}) { | ||
| 120 | $Memcached = $Data->$memcached_config->{'options'}->{'handle'}; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | while (1) { | ||
| 126 | |||
| 127 | $Log->log({msg=>"checking health",level=>3}); | ||
| 128 | |||
| 129 | warn(Dumper($Checks)); | ||
| 130 | |||
| 131 | foreach my $check (keys %{$Checks}) { | ||
| 132 | $Log->log({msg=>"checking $check",level=>3}); | ||
| 133 | my $status = $Checks->{$check}->check(); | ||
| 134 | if ($health_alerts_enabled) { | ||
| 135 | $self->_process_status($check,$status); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | sleep($timeout); | ||
| 140 | } | ||
| 141 | |||
| 142 | } | ||
| 143 | |||
| 144 | sub _process_status { | ||
| 145 | |||
| 146 | my ($self, $check, $status) = @_; | ||
| 147 | |||
| 148 | $Log->log({'msg'=>"$check status:" . Dumper($status),'level'=>3}); | ||
| 149 | |||
| 150 | if (ref($status) ne 'HASH') { | ||
| 151 | $Log->log({'msg'=>"$check check status was not an object" . Dumper($status),'level'=>2}); | ||
| 152 | return; | ||
| 153 | } | ||
| 154 | |||
| 155 | # check the healthcheckID counter | ||
| 156 | my $check_count = $self->get_check_count($check); | ||
| 157 | |||
| 158 | $Log->log({'msg'=>"$check check is in error" . Dumper($status),'level'=>2}) unless ($status->{'state'} eq 'ok'); | ||
| 159 | |||
| 160 | # we go through this regardless of state to process info objects for logging | ||
| 161 | |||
| 162 | my $condition_config = $Thresholds->{$status->{'condition'}}; | ||
| 163 | |||
| 164 | if (ref($condition_config) ne 'HASH') { | ||
| 165 | $Log->log({'msg'=>"$check condition " . $status->{'condition'} . " is undefined",'level'=>2}); | ||
| 166 | return; | ||
| 167 | } | ||
| 168 | |||
| 169 | foreach my $alerting_module (keys %{$condition_config->{'modules'}}) { | ||
| 170 | my $alerting_module_config = $condition_config->{'modules'}->{$alerting_module}; | ||
| 171 | warn("$alerting_module " . Dumper($alerting_module_config)); | ||
| 172 | if (ref($alerting_module_config) ne 'HASH') { | ||
| 173 | $Log->log({'msg'=>"$check alerting module $alerting_module is undefined",'level'=>2}); | ||
| 174 | next; | ||
| 175 | } | ||
| 176 | foreach my $handle (keys %{$alerting_module_config}) { | ||
| 177 | my $handle_config = $alerting_module_config->{$handle}; | ||
| 178 | if (ref($handle_config) ne 'HASH') { | ||
| 179 | $Log->log({'msg'=>"$check alerting method $alerting_module : $handle is undefined",'level'=>2}); | ||
| 180 | next; | ||
| 181 | } | ||
| 182 | if (!$handle_config->{'enabled'}) { | ||
| 183 | next; | ||
| 184 | } | ||
| 185 | |||
| 186 | my $threshold = $handle_config->{'every'}; | ||
| 187 | $threshold ||= 0; | ||
| 188 | # we finally have a valid alerting method | ||
| 189 | # lets check our threshold | ||
| 190 | my $modulus = 0; | ||
| 191 | if ($threshold) { | ||
| 192 | $modulus = $check_count % $threshold; | ||
| 193 | warn("$alerting_module:$handle modulus: $modulus threshold: $threshold count: $check_count"); | ||
| 194 | } | ||
| 195 | if (!$modulus || !$threshold || !$check_count) { | ||
| 196 | $Alert->send({'msg'=>$status->{'msg'},'module'=>$alerting_module, 'handle'=>$handle, 'options'=>$handle_config}); | ||
| 197 | } | ||
| 198 | |||
| 199 | } | ||
| 200 | |||
| 201 | } | ||
| 202 | |||
| 203 | # increment the healthcheckID counter | ||
| 204 | $check_count++; | ||
| 205 | $self->set_check_count($check, $check_count); | ||
| 206 | |||
| 207 | if ($status->{'state'} eq 'ok') { | ||
| 208 | # reset the healthcheckID counter | ||
| 209 | $self->set_check_count($check, 0) unless !$check_count; | ||
| 210 | } | ||
| 211 | |||
| 212 | return ''; | ||
| 213 | } | ||
| 214 | |||
| 215 | sub get_check_count { | ||
| 216 | |||
| 217 | my ($self,$check) = @_; | ||
| 218 | |||
| 219 | my $count = 0; | ||
| 220 | |||
| 221 | my $key = $check_prefix . $check; | ||
| 222 | |||
| 223 | if ($Memcached) { | ||
| 224 | $count = $Memcached->get($key); | ||
| 225 | } else { | ||
| 226 | $count = $CheckCounts->{$key} ||= 0; | ||
| 227 | } | ||
| 228 | |||
| 229 | warn("COUNT[$key]: $count\n"); | ||
| 230 | |||
| 231 | return $count; | ||
| 232 | |||
| 233 | } | ||
| 234 | |||
| 235 | sub set_check_count { | ||
| 236 | |||
| 237 | my ($self,$check,$count) = @_; | ||
| 238 | |||
| 239 | my $key = $check_prefix . $check; | ||
| 240 | |||
| 241 | if ($Memcached) { | ||
| 242 | $Memcached->set($key,$count); | ||
| 243 | } else { | ||
| 244 | $CheckCounts->{$key} = $count; | ||
| 245 | } | ||
| 246 | |||
| 247 | return ''; | ||
| 248 | |||
| 249 | } | ||
| 250 | |||
| 251 | sub overview { # this method is invoked from the admin prompt | ||
| 252 | |||
| 253 | my ($self, $fh, $for_api) = @_; | ||
| 254 | |||
| 255 | my $overview = {}; | ||
| 256 | my @overviews = (); | ||
| 257 | |||
| 258 | foreach my $check (keys %{$Checks}) { | ||
| 259 | $Log->log({msg=>"checking $check",level=>3}); | ||
| 260 | my $status = $Checks->{$check}->check(); | ||
| 261 | $overview->{$check} = $status->{'data'}; | ||
| 262 | push @overviews, $status->{'msg'}; | ||
| 263 | } | ||
| 264 | |||
| 265 | my $overview_txt = join "\n", @overviews; | ||
| 266 | |||
| 267 | my $backlog = $Threads->get_queue_count(); | ||
| 268 | |||
| 269 | if ($fh) { | ||
| 270 | $fh->write($overview_txt); | ||
| 271 | } | ||
| 272 | |||
| 273 | if ($for_api) { | ||
| 274 | return $overview; | ||
| 275 | } | ||
| 276 | |||
| 277 | |||
| 278 | } | ||
| 279 | |||
| 280 | sub api_overview { | ||
| 281 | |||
| 282 | my ($self, $fh) = @_; | ||
| 283 | |||
| 284 | my $data = $self->overview(undef,1); | ||
| 285 | my $json = JSON->new->allow_nonref; | ||
| 286 | my $json_string = eval{$json->encode( $data )}; | ||
| 287 | $Log->log({'msg'=>"$@",'level'=>2}) if $@; | ||
| 288 | if ($fh) { | ||
| 289 | $fh->write($json_string); | ||
| 290 | } else { | ||
| 291 | return $json_string; | ||
| 292 | } | ||
| 293 | |||
| 294 | } | ||
| 295 | |||
| 296 | |||
| 297 | |||
| 298 | 1; |