| 1 | package Service::Engine::Health::Backlog; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Data::Dumper; | ||
| 7 | use JSON; | ||
| 8 | |||
| 9 | use Service::Engine; | ||
| 10 | |||
| 11 | our $Config; | ||
| 12 | our $Log; | ||
| 13 | our $Threads; | ||
| 14 | our $EngineName; | ||
| 15 | our $EngineInstance; | ||
| 16 | |||
| 17 | # what's our backlog? | ||
| 18 | |||
| 19 | sub new { | ||
| 20 | |||
| 21 | my ($class,$options) = @_; | ||
| 22 | |||
| 23 | # set some defaults | ||
| 24 | my $attributes = {'method'=>''}; | ||
| 25 | |||
| 26 | # load options | ||
| 27 | if (ref($options) eq 'HASH') { | ||
| 28 | foreach my $option (keys %{$options}) { | ||
| 29 | $attributes->{$option} = $options->{$option}; | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | # pull in some Service::Engine globals | ||
| 34 | $Config = $Service::Engine::Config; | ||
| 35 | $Log = $Service::Engine::Log; | ||
| 36 | $Threads = $Service::Engine::Threads; | ||
| 37 | $EngineName = $Service::Engine::EngineName; | ||
| 38 | $EngineInstance = $Service::Engine::EngineInstance; | ||
| 39 | |||
| 40 | # set some defaults | ||
| 41 | |||
| 42 | my $self = bless $attributes, $class; | ||
| 43 | |||
| 44 | return $self; | ||
| 45 | |||
| 46 | } | ||
| 47 | |||
| 48 | # a check returns a status object | ||
| 49 | sub check { | ||
| 50 | my ($self) = @_; | ||
| 51 | |||
| 52 | # do our test and set state | ||
| 53 | my $options = $Config->get_config('health')->{'modules'}->{'Backlog'}->{'options'}; | ||
| 54 | if (ref($options) ne 'HASH') { | ||
| 55 | $Log->log({'msg'=>"Backlog options not found",'level'=>2}); | ||
| 56 | return {}; | ||
| 57 | } | ||
| 58 | |||
| 59 | my $critical = $options->{'critical'}; | ||
| 60 | my $warning = $options->{'warning'}; | ||
| 61 | my $total_backlog = $Threads->get_queue_count(); | ||
| 62 | my $thread_count = $Threads->get_thread_count(); | ||
| 63 | $thread_count ||= 1; | ||
| 64 | my $backlog = $total_backlog / $thread_count; | ||
| 65 | |||
| 66 | my $condition = ($backlog >= $critical) ? 'critical' : ($backlog > $warning) ? 'warning' : 'info'; | ||
| 67 | my $state = ($condition eq 'info') ? 'ok' : 'error'; | ||
| 68 | my $msg = uc($condition) . ": $EngineName:$EngineInstance average thread backlog is $backlog, queue backlog is $total_backlog"; | ||
| 69 | my $data = {'total_backlog'=>$total_backlog, 'average_backlog'=>$backlog, 'condition'=>$condition, 'state'=>$state, 'msg'=>$msg, 'time'=>time()}; | ||
| 70 | |||
| 71 | my $status = { | ||
| 72 | 'state'=>$state, # ok | error | ||
| 73 | 'condition'=>$condition, # info|warning|critical | ||
| 74 | 'msg'=> $msg, | ||
| 75 | 'data'=>$data, # any data we want to return | ||
| 76 | }; | ||
| 77 | |||
| 78 | return $status; | ||
| 79 | } | ||
| 80 | |||
| 81 | 1; |