| 1 | package | ||
| 2 | Beekeeper::Service::Dashboard::Worker; # hide from PAUSE | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | our $VERSION = '0.10'; | ||
| 8 | |||
| 9 | use Beekeeper::Worker ':log'; | ||
| 10 | use base 'Beekeeper::Worker'; | ||
| 11 | |||
| 12 | use Beekeeper::Worker::Extension::RemoteSession; | ||
| 13 | use Beekeeper::Service::Supervisor; | ||
| 14 | use Beekeeper::Service::LogTail; | ||
| 15 | use Beekeeper::Config; | ||
| 16 | |||
| 17 | use AnyEvent; | ||
| 18 | use JSON::XS; | ||
| 19 | use Scalar::Util 'weaken'; | ||
| 20 | use Digest::SHA 'sha256_hex'; | ||
| 21 | use Fcntl qw(:DEFAULT :flock); | ||
| 22 | use Time::HiRes; | ||
| 23 | |||
| 24 | use constant AVERAGE => 0; | ||
| 25 | use constant MAXIMUM => 1; | ||
| 26 | use constant TOTAL => 2; | ||
| 27 | use constant COUNT => 3; | ||
| 28 | use constant TSTAMP => 0; | ||
| 29 | use constant STATS => 1; | ||
| 30 | |||
| 31 | |||
| 32 | sub authorize_request { | ||
| 33 | my ($self, $req) = @_; | ||
| 34 | |||
| 35 | # Explicitly authorize the login method | ||
| 36 | return BKPR_REQUEST_AUTHORIZED if $req->{method} eq 'bkpr.dashboard.login'; | ||
| 37 | |||
| 38 | # Require an user logged in for any other request | ||
| 39 | return unless $self->get_authentication_data; | ||
| 40 | |||
| 41 | return BKPR_REQUEST_AUTHORIZED; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub on_startup { | ||
| 45 | my ($self) = @_; | ||
| 46 | |||
| 47 | my $config = Beekeeper::Config->read_config_file( 'dashboard.config.json' ); | ||
| 48 | $self->{config} = $config || {}; | ||
| 49 | |||
| 50 | unless ($config) { | ||
| 51 | log_warn "Couldn't read config file 'dashboard.config.json'"; | ||
| 52 | } | ||
| 53 | |||
| 54 | unless ($config && $config->{users} && %{$config->{users}}) { | ||
| 55 | log_warn "No users defined into config file 'dashboard.config.json'"; | ||
| 56 | } | ||
| 57 | |||
| 58 | $self->accept_remote_calls( | ||
| 59 | 'bkpr.dashboard.login' => 'login', | ||
| 60 | 'bkpr.dashboard.services' => 'service_stats', | ||
| 61 | 'bkpr.dashboard.logs' => 'log_tail', | ||
| 62 | ); | ||
| 63 | |||
| 64 | $self->_init_collector; | ||
| 65 | |||
| 66 | log_info "Ready"; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub on_shutdown { | ||
| 70 | my ($self) = @_; | ||
| 71 | |||
| 72 | $self->_save_state; | ||
| 73 | |||
| 74 | log_info "Stopped"; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub stop_working { | ||
| 78 | my ($self) = @_; | ||
| 79 | |||
| 80 | delete $self->{collect_tmr}; | ||
| 81 | delete $self->{consolidate_5s_tmr}; | ||
| 82 | delete $self->{consolidate_1m_tmr}; | ||
| 83 | |||
| 84 | $self->SUPER::stop_working; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub login { | ||
| 88 | my ($self, $params) = @_; | ||
| 89 | |||
| 90 | my $username = $params->{username}; | ||
| 91 | my $password = $params->{password}; | ||
| 92 | |||
| 93 | AUTH: { | ||
| 94 | |||
| 95 | last unless defined $username; | ||
| 96 | last unless defined $password; | ||
| 97 | |||
| 98 | my $users_cfg = $self->{config}->{users}; | ||
| 99 | my $pwd_hash = sha256_hex('Dashboard'.$username.$password); | ||
| 100 | |||
| 101 | last unless $users_cfg; | ||
| 102 | last unless $users_cfg->{$username}; | ||
| 103 | last unless $users_cfg->{$username}->{'password'}; | ||
| 104 | last unless $users_cfg->{$username}->{'password'} eq $pwd_hash; | ||
| 105 | |||
| 106 | # Set authentication data and assign an address to the user | ||
| 107 | $self->set_authentication_data( $username ); | ||
| 108 | $self->bind_remote_session( address => "frontend.dashboard-$username" ); | ||
| 109 | |||
| 110 | return 1; | ||
| 111 | } | ||
| 112 | |||
| 113 | Beekeeper::JSONRPC::Error->request_not_authenticated; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub service_stats { | ||
| 117 | my ($self, $params, $req) = @_; | ||
| 118 | |||
| 119 | my $resol = $params->{'resolution'} || '1s'; | ||
| 120 | my $count = $params->{'count'} || 1; | ||
| 121 | my $class = $params->{'class'}; | ||
| 122 | my $after = $params->{'after'}; | ||
| 123 | |||
| 124 | $req->deflate_response; | ||
| 125 | |||
| 126 | my $stats = $self->{"services_$resol"} or die "Invalid resolution"; | ||
| 127 | |||
| 128 | my $idx = @$stats - $count; | ||
| 129 | $idx = 0 if $idx < 0; | ||
| 130 | |||
| 131 | if ($after) { | ||
| 132 | my $min = $idx; | ||
| 133 | my $new_data; | ||
| 134 | for (my $i = @$stats - 1; $i >= 0; $i--) { | ||
| 135 | last if $stats->[$i]->[TSTAMP] <= $after; | ||
| 136 | last if $i < $min; | ||
| 137 | $new_data = $i; | ||
| 138 | } | ||
| 139 | return [] unless $new_data; | ||
| 140 | $idx = $new_data; | ||
| 141 | } | ||
| 142 | |||
| 143 | unless ($class) { | ||
| 144 | return [ @$stats[$idx..(@$stats - 1)] ]; | ||
| 145 | } | ||
| 146 | |||
| 147 | my @svc_stats; | ||
| 148 | |||
| 149 | foreach my $st (@$stats[$idx..(@$stats - 1)]) { | ||
| 150 | next unless exists $st->[STATS]->{$class}; | ||
| 151 | push @svc_stats, [ $st->[TSTAMP], $st->[STATS]->{$class} ]; | ||
| 152 | } | ||
| 153 | |||
| 154 | return \@svc_stats; | ||
| 155 | } | ||
| 156 | |||
| 157 | sub log_tail { | ||
| 158 | my ($self, $params, $req) = @_; | ||
| 159 | |||
| 160 | my %filters; | ||
| 161 | |||
| 162 | foreach my $filter (qw'service count level after host pool message') { | ||
| 163 | next unless $params->{$filter}; | ||
| 164 | $filters{$filter} = $params->{$filter}; | ||
| 165 | } | ||
| 166 | |||
| 167 | $req->async_response; | ||
| 168 | $req->deflate_response; | ||
| 169 | |||
| 170 | Beekeeper::Service::LogTail->tail_async( | ||
| 171 | %filters, | ||
| 172 | on_success => sub { | ||
| 173 | my ($resp) = @_; | ||
| 174 | $req->send_response( $resp->result ); | ||
| 175 | }, | ||
| 176 | on_error => sub { | ||
| 177 | my ($resp) = @_; | ||
| 178 | $req->send_response( $resp ); | ||
| 179 | }, | ||
| 180 | ); | ||
| 181 | } | ||
| 182 | |||
| 183 | |||
| 184 | sub _init_collector { | ||
| 185 | my ($self) = @_; | ||
| 186 | weaken($self); | ||
| 187 | |||
| 188 | $self->_load_state; | ||
| 189 | |||
| 190 | $self->{services_1s} ||= []; | ||
| 191 | $self->{services_5s} ||= []; | ||
| 192 | $self->{services_2m} ||= []; | ||
| 193 | $self->{services_15m} ||= []; | ||
| 194 | $self->{services_1h} ||= []; | ||
| 195 | |||
| 196 | my $now = Time::HiRes::time; | ||
| 197 | my $msec = $now - int($now); | ||
| 198 | |||
| 199 | my $offs_1s = $msec; | ||
| 200 | my $offs_5s = $now % 5 + $msec; | ||
| 201 | my $offs_1m = $now % 60 + $msec; | ||
| 202 | |||
| 203 | $self->{collect_tmr} = AnyEvent->timer( | ||
| 204 | after => 1 - $offs_1s, | ||
| 205 | interval => 1, | ||
| 206 | cb => sub { | ||
| 207 | Beekeeper::Service::Supervisor->get_services_status_async( | ||
| 208 | on_success => sub { | ||
| 209 | my ($resp) = @_; | ||
| 210 | $self->_collect_stats( $resp->result ); | ||
| 211 | }, | ||
| 212 | on_error => sub { | ||
| 213 | my ($error) = @_; | ||
| 214 | log_warn $error->message; | ||
| 215 | }, | ||
| 216 | ); | ||
| 217 | }, | ||
| 218 | ); | ||
| 219 | |||
| 220 | $self->{consolidate_5s_tmr} = AnyEvent->timer( | ||
| 221 | after => 5 - $offs_5s, | ||
| 222 | interval => 5, | ||
| 223 | cb => sub { | ||
| 224 | |||
| 225 | # 1 hour in 5 sec resolution | ||
| 226 | $self->_consolidate( | ||
| 227 | from => $self->{services_1s}, | ||
| 228 | into => $self->{services_5s}, | ||
| 229 | period => 5, | ||
| 230 | keep => 60 * 60/5, # 720 | ||
| 231 | ); | ||
| 232 | }, | ||
| 233 | ); | ||
| 234 | |||
| 235 | $self->{consolidate_1m_tmr} = AnyEvent->timer( | ||
| 236 | after => 60 - $offs_1m, | ||
| 237 | interval => 60, | ||
| 238 | cb => sub { | ||
| 239 | |||
| 240 | # 1 day in 2 min resolution | ||
| 241 | $self->_consolidate( | ||
| 242 | from => $self->{services_5s}, | ||
| 243 | into => $self->{services_2m}, | ||
| 244 | period => 2 * 60, | ||
| 245 | keep => 24 * 60/2, # 720 | ||
| 246 | ); | ||
| 247 | |||
| 248 | # 1 week in 15 min resolution | ||
| 249 | $self->_consolidate( | ||
| 250 | from => $self->{services_2m}, | ||
| 251 | into => $self->{services_15m}, | ||
| 252 | period => 15 * 60, | ||
| 253 | keep => 7 * 24 * 60/15, # 672 | ||
| 254 | ); | ||
| 255 | |||
| 256 | # 1 month in 1 hour resolution | ||
| 257 | $self->_consolidate( | ||
| 258 | from => $self->{services_15m}, | ||
| 259 | into => $self->{services_1h}, | ||
| 260 | period => 60 * 60, | ||
| 261 | keep => 31 * 24, # 744 | ||
| 262 | ); | ||
| 263 | }, | ||
| 264 | ); | ||
| 265 | } | ||
| 266 | |||
| 267 | sub _collect_stats { | ||
| 268 | my ($self, $stats) = @_; | ||
| 269 | |||
| 270 | my $now = int( time ); | ||
| 271 | my $global = { load => 0 }; | ||
| 272 | |||
| 273 | foreach my $class (keys %$stats) { | ||
| 274 | foreach my $metric (keys %{$stats->{$class}}) { | ||
| 275 | |||
| 276 | my $val = $stats->{$class}->{$metric}; | ||
| 277 | |||
| 278 | $stats->{$class}->{$metric} = [ $val, $val ]; | ||
| 279 | |||
| 280 | if ($metric eq 'load') { | ||
| 281 | # Global load is the load of the most stressed service | ||
| 282 | next unless $global->{load} < $val; | ||
| 283 | $global->{load} = $val; | ||
| 284 | } | ||
| 285 | else { | ||
| 286 | $global->{$metric} += $val; | ||
| 287 | } | ||
| 288 | } | ||
| 289 | } | ||
| 290 | |||
| 291 | foreach my $metric (keys %$global) { | ||
| 292 | my $val = sprintf("%.2f", $global->{$metric}); | ||
| 293 | $global->{$metric} = [ $val, $val ]; | ||
| 294 | } | ||
| 295 | |||
| 296 | $stats->{'_global'} = $global; | ||
| 297 | |||
| 298 | my $services_1s = $self->{services_1s}; | ||
| 299 | push @$services_1s, [ $now, $stats ]; | ||
| 300 | |||
| 301 | # 10 min in 1 sec resolution | ||
| 302 | shift @$services_1s if (@$services_1s >= 10 * 60); | ||
| 303 | } | ||
| 304 | |||
| 305 | sub _consolidate { | ||
| 306 | my ($self, %args) = @_; | ||
| 307 | |||
| 308 | my $stats_src = $args{'from'}; | ||
| 309 | my $stats_dest = $args{'into'}; | ||
| 310 | my $period = $args{'period'}; | ||
| 311 | my $keep = $args{'keep'}; | ||
| 312 | my $since; | ||
| 313 | |||
| 314 | if (@$stats_dest) { | ||
| 315 | # Update last consolidated period | ||
| 316 | $since = $stats_dest->[-1]->[TSTAMP]; | ||
| 317 | return if $stats_src->[-1]->[TSTAMP] < $since; | ||
| 318 | pop @$stats_dest; | ||
| 319 | } | ||
| 320 | elsif (@$stats_src) { | ||
| 321 | # Consolidate all data available | ||
| 322 | $since = $stats_src->[0]->[TSTAMP]; | ||
| 323 | $since -= $since % $period; | ||
| 324 | } | ||
| 325 | else { | ||
| 326 | return; | ||
| 327 | } | ||
| 328 | |||
| 329 | my $since_idx; | ||
| 330 | my $now = int( time ); | ||
| 331 | |||
| 332 | for (my $i = @$stats_src - 1; $i >= 0; $i--) { | ||
| 333 | last if $stats_src->[$i]->[TSTAMP] < $since; | ||
| 334 | $since_idx = $i; | ||
| 335 | } | ||
| 336 | |||
| 337 | for (my $start = $since; $start <= $now; $start += $period) { | ||
| 338 | |||
| 339 | my $end = $start + $period - 1; | ||
| 340 | $end = $now if ($end > $now); | ||
| 341 | |||
| 342 | my $cons = {}; | ||
| 343 | |||
| 344 | for (my $i = $since_idx; $i < @$stats_src; $i++) { | ||
| 345 | |||
| 346 | my $data_point = $stats_src->[$i]; | ||
| 347 | |||
| 348 | if ($data_point->[TSTAMP] < $start) { | ||
| 349 | next; | ||
| 350 | } | ||
| 351 | if ($data_point->[TSTAMP] > $end) { | ||
| 352 | last; | ||
| 353 | } | ||
| 354 | |||
| 355 | my $stats = $data_point->[STATS]; | ||
| 356 | |||
| 357 | foreach my $class (keys %$stats) { | ||
| 358 | foreach my $metric (keys %{$stats->{$class}}) { | ||
| 359 | |||
| 360 | my $src = $stats->{$class}->{$metric}; | ||
| 361 | my $dest = $cons->{$class}->{$metric} ||= []; | ||
| 362 | |||
| 363 | $dest->[TOTAL] += $src->[AVERAGE]; | ||
| 364 | $dest->[COUNT]++; | ||
| 365 | |||
| 366 | $dest->[MAXIMUM] = 0 unless defined $dest->[MAXIMUM]; | ||
| 367 | if ($src->[MAXIMUM] > $dest->[MAXIMUM]) { | ||
| 368 | $dest->[MAXIMUM] = $src->[MAXIMUM]; | ||
| 369 | } | ||
| 370 | } | ||
| 371 | } | ||
| 372 | } | ||
| 373 | |||
| 374 | foreach my $class (keys %$cons) { | ||
| 375 | foreach my $metric (keys %{$cons->{$class}}) { | ||
| 376 | |||
| 377 | my $dest = $cons->{$class}->{$metric}; | ||
| 378 | |||
| 379 | my ($total, $count) = splice(@$dest, TOTAL, COUNT); | ||
| 380 | |||
| 381 | $dest->[MAXIMUM] = sprintf("%.2f", $dest->[MAXIMUM]); | ||
| 382 | $dest->[AVERAGE] = sprintf("%.2f", $total / $count); | ||
| 383 | } | ||
| 384 | } | ||
| 385 | |||
| 386 | $cons->{global}->{load}->[MAXIMUM] = 0; | ||
| 387 | $cons->{global}->{load}->[AVERAGE] = 0; | ||
| 388 | |||
| 389 | foreach my $class (keys %$cons) { | ||
| 390 | |||
| 391 | next if ($class eq '_global'); | ||
| 392 | |||
| 393 | if ($cons->{global}->{load}->[MAXIMUM] < $cons->{$class}->{load}->[MAXIMUM]) { | ||
| 394 | $cons->{global}->{load}->[MAXIMUM] = $cons->{$class}->{load}->[MAXIMUM]; | ||
| 395 | } | ||
| 396 | if ($cons->{global}->{load}->[AVERAGE] < $cons->{$class}->{load}->[AVERAGE]) { | ||
| 397 | $cons->{global}->{load}->[AVERAGE] = $cons->{$class}->{load}->[AVERAGE]; | ||
| 398 | } | ||
| 399 | } | ||
| 400 | |||
| 401 | push @$stats_dest, [ $start, $cons ]; | ||
| 402 | } | ||
| 403 | |||
| 404 | if (@$stats_dest > $keep) { | ||
| 405 | # Discard old data | ||
| 406 | shift @$stats_dest; | ||
| 407 | } | ||
| 408 | } | ||
| 409 | |||
| 410 | sub _save_state { | ||
| 411 | my ($self) = @_; | ||
| 412 | |||
| 413 | my $pool_id = $self->{_WORKER}->{pool_id}; | ||
| 414 | ($pool_id) = ($pool_id =~ m/^([\w-]+)$/); # untaint | ||
| 415 | my $tmp_file = "/tmp/beekeeper-dashboard-$pool_id-stats.dump"; | ||
| 416 | |||
| 417 | # Avoid stampede when several workers are exiting simultaneously | ||
| 418 | return if (-e $tmp_file && (stat($tmp_file))[9] == time()); | ||
| 419 | |||
| 420 | # Lock file because several workers may try to write simultaneously to it | ||
| 421 | sysopen(my $fh, $tmp_file, O_RDWR|O_CREAT) or return; | ||
| 422 | flock($fh, LOCK_EX | LOCK_NB) or return; | ||
| 423 | truncate($fh, 0) or return; | ||
| 424 | |||
| 425 | print $fh encode_json([ | ||
| 426 | $self->{services_1s}, | ||
| 427 | $self->{services_5s}, | ||
| 428 | $self->{services_2m}, | ||
| 429 | $self->{services_15m}, | ||
| 430 | $self->{services_1h}, | ||
| 431 | ]); | ||
| 432 | |||
| 433 | close($fh); | ||
| 434 | } | ||
| 435 | |||
| 436 | sub _load_state { | ||
| 437 | my ($self) = @_; | ||
| 438 | |||
| 439 | my $pool_id = $self->{_WORKER}->{pool_id}; | ||
| 440 | ($pool_id) = ($pool_id =~ m/^([\w-]+)$/); # untaint | ||
| 441 | my $tmp_file = "/tmp/beekeeper-dashboard-$pool_id-stats.dump"; | ||
| 442 | |||
| 443 | return unless (-e $tmp_file); | ||
| 444 | |||
| 445 | local($/); | ||
| 446 | open(my $fh, '<', $tmp_file) or die "Couldn't read $tmp_file: $!"; | ||
| 447 | my $data = <$fh>; | ||
| 448 | close($fh); | ||
| 449 | |||
| 450 | local $@; | ||
| 451 | my $dump = eval { decode_json($data) }; | ||
| 452 | return if $@; | ||
| 453 | |||
| 454 | $self->{services_1s} = $dump->[0]; | ||
| 455 | $self->{services_5s} = $dump->[1]; | ||
| 456 | $self->{services_2m} = $dump->[2]; | ||
| 457 | $self->{services_15m} = $dump->[3]; | ||
| 458 | $self->{services_1h} = $dump->[4]; | ||
| 459 | } | ||
| 460 | |||
| 461 | 1; | ||
| 462 | |||
| 463 | __END__ |