| 1 | package Service::Engine::Health::Memory; | ||
| 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 memory usage looking like? | ||
| 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 | # set some defaults | ||
| 40 | |||
| 41 | my $self = bless $attributes, $class; | ||
| 42 | |||
| 43 | return $self; | ||
| 44 | |||
| 45 | } | ||
| 46 | |||
| 47 | # a check returns a status object | ||
| 48 | sub check { | ||
| 49 | my ($self) = @_; | ||
| 50 | |||
| 51 | # do our test and set state | ||
| 52 | my $options = $Config->get_config('health')->{'modules'}->{'Memory'}->{'options'}; | ||
| 53 | if (ref($options) ne 'HASH') { | ||
| 54 | $Log->log({'msg'=>"Memory options not found",'level'=>2}); | ||
| 55 | return {}; | ||
| 56 | } | ||
| 57 | |||
| 58 | my $critical = $options->{'critical'}; | ||
| 59 | my $warning = $options->{'warning'}; | ||
| 60 | |||
| 61 | my $os = $^O; | ||
| 62 | my $mem_usage = '--'; | ||
| 63 | |||
| 64 | if ($os ne 'darwin') { # this chokes on a mac - no /proc table | ||
| 65 | use Memory::Usage; | ||
| 66 | my $mu = Memory::Usage->new(); | ||
| 67 | |||
| 68 | # Record amount of memory used by current process | ||
| 69 | $mu->record('starting work'); | ||
| 70 | |||
| 71 | # Spit out a report | ||
| 72 | $mem_usage = $mu->report(); | ||
| 73 | } | ||
| 74 | |||
| 75 | ###>>>TO DO: need to parse out memory usage and alert accordingly | ||
| 76 | |||
| 77 | # my $condition = ($backlog >= $critical) ? 'critical' : ($backlog > $warning) ? 'warning' : 'info'; | ||
| 78 | # my $state = ($condition eq 'info') ? 'ok' : 'error'; | ||
| 79 | # my $msg = uc($condition) . ": $EngineName queue backlog is $backlog"; | ||
| 80 | # my $data = {'backlog'=>$backlog, 'condition'=>$condition, 'state'=>$state, 'msg'=>$msg, 'time'=>time()}; | ||
| 81 | |||
| 82 | my $condition = 'info'; | ||
| 83 | my $state = 'ok'; | ||
| 84 | my $msg = uc($condition) . ": $EngineName::$EngineInstance memory usage report: $mem_usage"; | ||
| 85 | my $data = {'condition'=>$condition, 'state'=>$state, 'msg'=>$msg, 'time'=>time()}; | ||
| 86 | |||
| 87 | my $status = { | ||
| 88 | 'state'=>$state, # ok | error | ||
| 89 | 'condition'=>$condition, # info|warning|critical | ||
| 90 | 'msg'=> $msg, | ||
| 91 | 'data'=>$data, # any data we want to return | ||
| 92 | }; | ||
| 93 | |||
| 94 | return $status; | ||
| 95 | } | ||
| 96 | |||
| 97 | 1; |