| 1 | package Helios::Panoptes::CollectiveAdmin; | ||
| 2 | |||
| 3 | use 5.008; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use base 'Helios::Panoptes::Base'; | ||
| 7 | |||
| 8 | use CGI::Application::Plugin::DBH qw(dbh_config dbh); | ||
| 9 | |||
| 10 | our $VERSION = '1.51_2830'; | ||
| 11 | |||
| 12 | sub setup { | ||
| 13 | my $self = shift; | ||
| 14 | $self->start_mode('collective'); | ||
| 15 | $self->mode_param('rm'); | ||
| 16 | $self->run_modes( | ||
| 17 | collective => 'rm_collective', | ||
| 18 | conf_mod => 'rm_conf_mod' | ||
| 19 | ); | ||
| 20 | my $config = $self->parseConfig(); | ||
| 21 | |||
| 22 | # connect to db | ||
| 23 | $self->dbh_config($config->{dsn},$config->{user},$config->{password}); | ||
| 24 | |||
| 25 | } | ||
| 26 | |||
| 27 | =head2 rm_collective() | ||
| 28 | |||
| 29 | The rm_collective() run mode provides the Collective Admin display, a list of | ||
| 30 | what Helios service daemons are running on the various worker hosts. | ||
| 31 | Collective Admin also provides some basic controls for adjusting service | ||
| 32 | daemon and worker process parameters. | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | sub rm_collective { | ||
| 37 | my $self = shift; | ||
| 38 | my $q = $self->query(); | ||
| 39 | if ( defined($q->param('groupby')) && $q->param('groupby') eq 'service') { | ||
| 40 | return $self->collective_by_service(); | ||
| 41 | } else { | ||
| 42 | return $self->collective_by_host(); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | =head2 collective_by_host() | ||
| 48 | |||
| 49 | The collective_by_host() method provides the Collective Admin display | ||
| 50 | grouped by host. | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | sub collective_by_host { | ||
| 55 | my $self = shift; | ||
| 56 | my $dbh = $self->dbh(); | ||
| 57 | my $q = $self->query(); | ||
| 58 | my $config = $self->parseAllConfigParams('host'); | ||
| 59 | my $register_interval = $config->{register_interval} ? $config->{register_interval} : 360; | ||
| 60 | |||
| 61 | my $register_threshold = time() - $register_interval; | ||
| 62 | |||
| 63 | my $sql = <<STATUSSQL; | ||
| 64 | SELECT host, | ||
| 65 | worker_class, | ||
| 66 | worker_version, | ||
| 67 | process_id, | ||
| 68 | register_time, | ||
| 69 | start_time | ||
| 70 | FROM helios_worker_registry_tb | ||
| 71 | WHERE register_time > ? | ||
| 72 | ORDER BY host, worker_class | ||
| 73 | STATUSSQL | ||
| 74 | |||
| 75 | my $sth = $dbh->prepare_cached($sql); | ||
| 76 | $sth->execute($register_threshold); | ||
| 77 | my $rs = $sth->fetchall_arrayref(); | ||
| 78 | $sth->finish(); | ||
| 79 | |||
| 80 | my @collective; | ||
| 81 | my @dbresult; | ||
| 82 | my $current_host; | ||
| 83 | my $first_result = 1; | ||
| 84 | my $last_host = undef; | ||
| 85 | foreach my $result (@$rs) { | ||
| 86 | if ($first_result) { | ||
| 87 | $last_host = $result->[0]; | ||
| 88 | $first_result = 0; | ||
| 89 | } | ||
| 90 | if ($result->[0] ne $last_host) { | ||
| 91 | push(@collective, $current_host); | ||
| 92 | undef $current_host; | ||
| 93 | $last_host = $result->[0]; | ||
| 94 | } | ||
| 95 | |||
| 96 | my $dhash = $self->parseEpochDate($result->[4]); | ||
| 97 | $current_host->{HOST} = $result->[0]; | ||
| 98 | |||
| 99 | # calc uptime | ||
| 100 | my $uptime_string = ''; | ||
| 101 | { | ||
| 102 | use integer; | ||
| 103 | my $uptime = time() - $result->[5]; | ||
| 104 | my $uptime_days = $uptime/86400; | ||
| 105 | my $uptime_hours = ($uptime % 86400)/3600; | ||
| 106 | my $uptime_mins = (($uptime % 86400) % 3600)/60; | ||
| 107 | if ($uptime_days != 0) { $uptime_string .= $uptime_days.'d '; } | ||
| 108 | if ($uptime_hours != 0) { $uptime_string .= $uptime_hours.'h '; } | ||
| 109 | if ($uptime_mins != 0) { $uptime_string .= $uptime_mins.'m '; } | ||
| 110 | } | ||
| 111 | |||
| 112 | # max_workers | ||
| 113 | my $max_workers = 1; | ||
| 114 | if ( defined($config->{ $result->[0] }->{ $result->[1] }->{MAX_WORKERS}) ) { | ||
| 115 | $max_workers = $config->{ $result->[0] }->{ $result->[1] }->{MAX_WORKERS}; | ||
| 116 | } | ||
| 117 | |||
| 118 | # figure out status (normal/overdrive/holding/halting) | ||
| 119 | my $status; | ||
| 120 | my $halt_status = 0; | ||
| 121 | my $hold_status = 0; | ||
| 122 | my $overdrive_status = 0; | ||
| 123 | if ( (defined( $config->{$result->[0] }->{ $result->[1] }->{OVERDRIVE}) && ($config->{$result->[0] }->{ $result->[1] }->{OVERDRIVE} == 1) ) || | ||
| 124 | (defined( $config->{'*'}->{ $result->[1] }->{OVERDRIVE}) && ($config->{'*'}->{ $result->[1] }->{OVERDRIVE} == 1) ) ) { | ||
| 125 | $overdrive_status = 1; | ||
| 126 | $status = "Overdrive"; | ||
| 127 | } | ||
| 128 | if ( (defined( $config->{$result->[0] }->{ $result->[1] }->{HOLD}) && ($config->{$result->[0] }->{ $result->[1] }->{HOLD} == 1) ) || | ||
| 129 | (defined( $config->{'*'}->{ $result->[1] }->{HOLD}) && ($config->{'*'}->{ $result->[1] }->{HOLD} == 1) ) ) { | ||
| 130 | $hold_status = 1; | ||
| 131 | $status = "HOLDING"; | ||
| 132 | } | ||
| 133 | if ( defined( $config->{$result->[0] }->{ $result->[1] }->{HALT}) || | ||
| 134 | defined( $config->{'*'}->{ $result->[1] }->{HALT}) ) { | ||
| 135 | $halt_status = 1; | ||
| 136 | $status = "HALTING"; | ||
| 137 | } | ||
| 138 | push(@{ $current_host->{SERVICES} }, | ||
| 139 | { HOST => $result->[0], | ||
| 140 | SERVICE_CLASS => $result->[1], | ||
| 141 | SERVICE_VERSION => $result->[2], | ||
| 142 | PROCESS_ID => $result->[3], | ||
| 143 | REGISTER_TIME => $dhash->{yyyy}.'-'.$dhash->{mm}.'-'.$dhash->{dd}.' '.$dhash->{hh}.':'.$dhash->{mi}.':'.$dhash->{ss}, | ||
| 144 | UPTIME => $uptime_string, | ||
| 145 | STATUS => $status, | ||
| 146 | MAX_WORKERS => $max_workers, | ||
| 147 | OVERDRIVE => $overdrive_status, | ||
| 148 | HOLDING => $hold_status, | ||
| 149 | HALTING => $halt_status, | ||
| 150 | }); | ||
| 151 | } | ||
| 152 | push(@collective, $current_host); | ||
| 153 | |||
| 154 | my $tmpl = $self->load_tmpl('collective_host.html', die_on_bad_params => 0); | ||
| 155 | $tmpl->param(TITLE => 'Helios - Collective Admin'); | ||
| 156 | $tmpl->param(COLLECTIVE => \@collective); | ||
| 157 | return $tmpl->output(); | ||
| 158 | } | ||
| 159 | |||
| 160 | |||
| 161 | =head2 collective_by_service() | ||
| 162 | |||
| 163 | The collective_by_service() method provides the Collective Admin display | ||
| 164 | grouped by service. | ||
| 165 | |||
| 166 | =cut | ||
| 167 | |||
| 168 | sub collective_by_service { | ||
| 169 | my $self = shift; | ||
| 170 | my $dbh = $self->dbh(); | ||
| 171 | my $q = $self->query(); | ||
| 172 | my $config = $self->parseAllConfigParams('service'); | ||
| 173 | my $register_interval = $config->{register_interval} ? $config->{register_interval} : 300; | ||
| 174 | |||
| 175 | my $register_threshold = time() - $register_interval; | ||
| 176 | |||
| 177 | my $sql = <<STATUSSQL; | ||
| 178 | SELECT worker_class AS service, | ||
| 179 | host, | ||
| 180 | worker_version AS version, | ||
| 181 | process_id, | ||
| 182 | register_time, | ||
| 183 | start_time | ||
| 184 | FROM helios_worker_registry_tb | ||
| 185 | WHERE register_time > ? | ||
| 186 | ORDER BY service, host | ||
| 187 | STATUSSQL | ||
| 188 | |||
| 189 | my $sth = $dbh->prepare_cached($sql); | ||
| 190 | $sth->execute($register_threshold); | ||
| 191 | my $rs = $sth->fetchall_arrayref(); | ||
| 192 | $sth->finish(); | ||
| 193 | |||
| 194 | my @collective; | ||
| 195 | my @dbresult; | ||
| 196 | my $current_service; | ||
| 197 | my $first_result = 1; | ||
| 198 | my $last_service = undef; | ||
| 199 | #t print $q->header(); | ||
| 200 | foreach my $result (@$rs) { | ||
| 201 | #t print join("|", @$result),"<br>\n"; | ||
| 202 | if ($first_result) { | ||
| 203 | $last_service = $result->[0]; | ||
| 204 | $first_result = 0; | ||
| 205 | } | ||
| 206 | if ($result->[0] ne $last_service) { | ||
| 207 | push(@collective, $current_service); | ||
| 208 | undef $current_service; | ||
| 209 | $last_service = $result->[0]; | ||
| 210 | } | ||
| 211 | |||
| 212 | my $dhash = $self->parseEpochDate($result->[4]); | ||
| 213 | $current_service->{SERVICE_CLASS} = $result->[0]; | ||
| 214 | |||
| 215 | # calc uptime | ||
| 216 | my $uptime_string = ''; | ||
| 217 | { | ||
| 218 | use integer; | ||
| 219 | my $uptime = time() - $result->[5]; | ||
| 220 | my $uptime_days = $uptime/86400; | ||
| 221 | my $uptime_hours = ($uptime % 86400)/3600; | ||
| 222 | my $uptime_mins = (($uptime % 86400) % 3600)/60; | ||
| 223 | if ($uptime_days != 0) { $uptime_string .= $uptime_days.'d '; } | ||
| 224 | if ($uptime_hours != 0) { $uptime_string .= $uptime_hours.'h '; } | ||
| 225 | if ($uptime_mins != 0) { $uptime_string .= $uptime_mins.'m '; } | ||
| 226 | } | ||
| 227 | |||
| 228 | # max_workers | ||
| 229 | my $max_workers = 1; | ||
| 230 | if ( defined($config->{ $result->[0] }->{ '*' }->{MAX_WORKERS}) ) { | ||
| 231 | $max_workers = $config->{ $result->[0] }->{ '*' }->{MAX_WORKERS}; | ||
| 232 | } | ||
| 233 | if ( defined($config->{ $result->[0] }->{ $result->[1] }->{MAX_WORKERS}) ) { | ||
| 234 | $max_workers = $config->{ $result->[0] }->{ $result->[1] }->{MAX_WORKERS}; | ||
| 235 | } | ||
| 236 | |||
| 237 | # figure out status (normal/overdrive/holding/halting) | ||
| 238 | my $status; | ||
| 239 | my $halt_status = 0; | ||
| 240 | my $hold_status = 0; | ||
| 241 | my $overdrive_status = 0; | ||
| 242 | # determine overdrive status | ||
| 243 | if ( defined($config->{$result->[0]}->{'*'}->{OVERDRIVE}) && ($config->{$result->[0]}->{'*'}->{OVERDRIVE} == 1) ) { | ||
| 244 | $overdrive_status = 1; | ||
| 245 | } | ||
| 246 | if ( defined($config->{$result->[0]}->{$result->[1]}->{OVERDRIVE}) ) { | ||
| 247 | $overdrive_status = $config->{$result->[0]}->{$result->[1]}->{OVERDRIVE}; | ||
| 248 | } | ||
| 249 | |||
| 250 | # determine holding status | ||
| 251 | if ( defined($config->{$result->[0]}->{'*'}->{HOLD}) && ($config->{$result->[0]}->{'*'}->{HOLD} == 1) ) { | ||
| 252 | $hold_status = 1; | ||
| 253 | } | ||
| 254 | if ( defined($config->{$result->[0]}->{$result->[1]}->{HOLD}) ) { | ||
| 255 | $hold_status = $config->{$result->[0]}->{$result->[1]}->{HOLD}; | ||
| 256 | } | ||
| 257 | |||
| 258 | |||
| 259 | # determine halt status; if it's even defined, that means the service instance is HALTing | ||
| 260 | if ( defined( $config->{$result->[0] }->{ '*' }->{HALT}) || | ||
| 261 | defined( $config->{$result->[0]}->{ $result->[1] }->{HALT}) ) { | ||
| 262 | $halt_status = 1; | ||
| 263 | $status = "HALTING"; | ||
| 264 | } | ||
| 265 | push(@{ $current_service->{HOSTS} }, | ||
| 266 | { HOST => $result->[1], | ||
| 267 | SERVICE_CLASS => $result->[0], | ||
| 268 | SERVICE_VERSION => $result->[2], | ||
| 269 | PROCESS_ID => $result->[3], | ||
| 270 | REGISTER_TIME => $dhash->{yyyy}.'-'.$dhash->{mm}.'-'.$dhash->{dd}.' '.$dhash->{hh}.':'.$dhash->{mi}.':'.$dhash->{ss}, | ||
| 271 | UPTIME => $uptime_string, | ||
| 272 | STATUS => $status, | ||
| 273 | MAX_WORKERS => $max_workers, | ||
| 274 | OVERDRIVE => $overdrive_status, | ||
| 275 | HOLDING => $hold_status, | ||
| 276 | HALTING => $halt_status, | ||
| 277 | }); | ||
| 278 | } | ||
| 279 | push(@collective, $current_service); | ||
| 280 | |||
| 281 | my $tmpl = $self->load_tmpl('collective_service.html', die_on_bad_params => 0); | ||
| 282 | $tmpl->param(TITLE => 'Helios - Collective Admin'); | ||
| 283 | $tmpl->param(COLLECTIVE => \@collective); | ||
| 284 | return $tmpl->output(); | ||
| 285 | } | ||
| 286 | |||
| 287 | |||
| 288 | # BEGIN CODE Copyright (C) 2012 Logical Helion, LLC. | ||
| 289 | |||
| 290 | =head2 rm_conf_mod() | ||
| 291 | |||
| 292 | =cut | ||
| 293 | |||
| 294 | sub rm_conf_mod { | ||
| 295 | my $self = shift; | ||
| 296 | my $q = $self->query(); | ||
| 297 | my $dbh = $self->dbh(); | ||
| 298 | |||
| 299 | my $service = $q->param('service'); | ||
| 300 | my $host = defined($q->param('host')) ? $q->param('host') : '*'; | ||
| 301 | my $param_name = $q->param('param'); | ||
| 302 | my $param_value = $q->param('value'); | ||
| 303 | my $action = $q->param('action'); | ||
| 304 | |||
| 305 | unless ($service && $param_name && defined($param_value) && $action) { | ||
| 306 | die("Helios config param modifications require a service, parameter name, value, and action."); | ||
| 307 | } | ||
| 308 | unless ($action eq 'mod' || $action eq 'add' || $action eq 'del') { | ||
| 309 | die("Invalid modParam() action specified (can only be 'add','del','mod')"); | ||
| 310 | } | ||
| 311 | |||
| 312 | $self->modParam($action, $service, $host, $param_name, $param_value); | ||
| 313 | |||
| 314 | return $self->rm_collective(); | ||
| 315 | } | ||
| 316 | |||
| 317 | # END CODE Copyright (C) 2012 Logical Helion, LLC. | ||
| 318 | |||
| 319 | 1; | ||
| 320 | __END__ |