| 1 | package Service::Engine::Health::Threads; | ||
| 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 | # are enough threads running? | ||
| 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'}->{'Threads'}->{'options'}; | ||
| 54 | if (ref($options) ne 'HASH') { | ||
| 55 | $Log->log({'msg'=>"Threads options not found",'level'=>2}); | ||
| 56 | return {}; | ||
| 57 | } | ||
| 58 | |||
| 59 | my $critical = $options->{'critical'}; | ||
| 60 | my $warning = $options->{'warning'}; | ||
| 61 | my $thread_count = $Threads->get_thread_count(); | ||
| 62 | |||
| 63 | my $condition = ($thread_count <= $critical) ? 'critical' : ($thread_count < $warning) ? 'warning' : 'info'; | ||
| 64 | my $state = ($condition eq 'info') ? 'ok' : 'error'; | ||
| 65 | my $msg = uc($condition) . ": $EngineName:$EngineInstance thread count is $thread_count"; | ||
| 66 | my $data = {'thread_count'=>$thread_count, 'condition'=>$condition, 'state'=>$state, 'msg'=>$msg, 'time'=>time()}; | ||
| 67 | |||
| 68 | my $status = { | ||
| 69 | 'state'=>$state, # ok | error | ||
| 70 | 'condition'=>$condition, # info|warning|critical | ||
| 71 | 'msg'=> $msg, | ||
| 72 | 'data'=>$data, # any data we want to return | ||
| 73 | }; | ||
| 74 | |||
| 75 | return $status; | ||
| 76 | } | ||
| 77 | |||
| 78 | 1; |