| 1 | package Helios::Panoptes::CtrlPanel; | ||
| 2 | |||
| 3 | use 5.008; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use base qw(Helios::Panoptes::Base); | ||
| 7 | use Data::Dumper; | ||
| 8 | |||
| 9 | our $VERSION = '1.51_3070'; | ||
| 10 | |||
| 11 | # we have to do setup to establish runmode(s) | ||
| 12 | sub setup { | ||
| 13 | my $self = shift; | ||
| 14 | $self->start_mode('ctrlpanel'); | ||
| 15 | $self->mode_param('rm'); | ||
| 16 | $self->run_modes( | ||
| 17 | ctrlpanel => 'rm_ctrlpanel', | ||
| 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_ctrlpanel() | ||
| 28 | |||
| 29 | This method controls the rendering of the Ctrl Panel view, used to display | ||
| 30 | Helios configuration parameters. | ||
| 31 | =cut | ||
| 32 | |||
| 33 | sub rm_ctrlpanel { | ||
| 34 | my $self = shift; | ||
| 35 | my $dbh = $self->dbh(); | ||
| 36 | my $q = $self->query(); | ||
| 37 | |||
| 38 | my $output; | ||
| 39 | |||
| 40 | my $sql = <<PNLSQL; | ||
| 41 | SELECT worker_class, | ||
| 42 | host, | ||
| 43 | param, | ||
| 44 | value | ||
| 45 | FROM helios_params_tb | ||
| 46 | ORDER BY worker_class, host, param | ||
| 47 | PNLSQL | ||
| 48 | |||
| 49 | my $sth = $dbh->prepare_cached($sql); | ||
| 50 | $sth->execute(); | ||
| 51 | my $rs = $sth->fetchall_arrayref(); | ||
| 52 | $sth->finish(); | ||
| 53 | |||
| 54 | my $classes; | ||
| 55 | my $hosts; | ||
| 56 | my $params = []; | ||
| 57 | my $last_host; | ||
| 58 | my $last_class; | ||
| 59 | my $current_host; | ||
| 60 | my $current_class; | ||
| 61 | my $first_result = 1; | ||
| 62 | foreach my $result (@$rs) { | ||
| 63 | if ($first_result) { | ||
| 64 | $last_class = $result->[0]; | ||
| 65 | $last_host = $result->[1]; | ||
| 66 | $first_result = 0; | ||
| 67 | } | ||
| 68 | if ($result->[0] ne $last_class) { | ||
| 69 | $current_host->{PARAMS} = $params; | ||
| 70 | $current_host->{HOST} = $last_host; | ||
| 71 | $current_host->{SERVICE_CLASS} = $last_class; | ||
| 72 | push(@$hosts, $current_host); | ||
| 73 | undef $params; | ||
| 74 | undef $current_host; | ||
| 75 | $last_host = $result->[1]; | ||
| 76 | |||
| 77 | $current_class->{HOSTS} = $hosts; | ||
| 78 | $current_class->{SERVICE_CLASS} = $last_class; | ||
| 79 | push(@$classes, $current_class); | ||
| 80 | undef $hosts; | ||
| 81 | undef $current_class; | ||
| 82 | $last_class = $result->[0]; | ||
| 83 | } | ||
| 84 | if ($result->[1] ne $last_host) { | ||
| 85 | $current_host->{PARAMS} = $params; | ||
| 86 | $current_host->{HOST} = $last_host; | ||
| 87 | $current_host->{SERVICE_CLASS} = $last_class; | ||
| 88 | push(@$hosts, $current_host); | ||
| 89 | undef $params; | ||
| 90 | undef $current_host; | ||
| 91 | $last_host = $result->[1]; | ||
| 92 | } | ||
| 93 | |||
| 94 | push(@$params, | ||
| 95 | { | ||
| 96 | 'service_class' => $result->[0], | ||
| 97 | 'host' => $result->[1], | ||
| 98 | 'param' => $result->[2], | ||
| 99 | 'value' => $result->[3] | ||
| 100 | } | ||
| 101 | ); | ||
| 102 | } | ||
| 103 | |||
| 104 | $current_host->{PARAMS} = $params; | ||
| 105 | $current_host->{HOST} = $last_host; | ||
| 106 | $current_host->{SERVICE_CLASS} = $last_class; | ||
| 107 | push(@$hosts, $current_host); | ||
| 108 | |||
| 109 | $current_class->{HOSTS} = $hosts; | ||
| 110 | $current_class->{SERVICE_CLASS} = $last_class; | ||
| 111 | push(@$classes, $current_class); | ||
| 112 | |||
| 113 | #t | ||
| 114 | #print $q->header('text/plain'); | ||
| 115 | #print Dumper($classes); | ||
| 116 | |||
| 117 | my $tmpl = $self->load_tmpl('ctrl_panel.html', die_on_bad_params => 0); | ||
| 118 | $tmpl->param(TITLE => "Helios - Ctrl Panel"); | ||
| 119 | |||
| 120 | # only fill in parameters if we actually have some | ||
| 121 | if ( $classes->[0]->{HOSTS}->[0]->{HOST} ) { | ||
| 122 | $tmpl->param(CLASSES => $classes); | ||
| 123 | } | ||
| 124 | return $tmpl->output(); | ||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | |||
| 129 | |||
| 130 | # BEGIN CODE Copyright (C) 2012 Logical Helion, LLC. | ||
| 131 | |||
| 132 | =head2 rm_conf_mod() | ||
| 133 | |||
| 134 | =cut | ||
| 135 | |||
| 136 | sub rm_conf_mod { | ||
| 137 | my $self = shift; | ||
| 138 | my $q = $self->query(); | ||
| 139 | my $dbh = $self->dbh(); | ||
| 140 | |||
| 141 | my $service = $q->param('service'); | ||
| 142 | my $host = defined($q->param('host')) ? $q->param('host') : '*'; | ||
| 143 | my $param_name = $q->param('param'); | ||
| 144 | my $param_value = $q->param('value'); | ||
| 145 | my $action = $q->param('action'); | ||
| 146 | |||
| 147 | unless ($service && $param_name && defined($param_value) && $action) { | ||
| 148 | die("Helios config param modifications require a service, parameter name, value, and action."); | ||
| 149 | } | ||
| 150 | unless ($action eq 'mod' || $action eq 'add' || $action eq 'del') { | ||
| 151 | die("Invalid modParam() action specified (can only be 'add','del','mod')"); | ||
| 152 | } | ||
| 153 | |||
| 154 | $self->modParam($action, $service, $host, $param_name, $param_value); | ||
| 155 | |||
| 156 | return $self->rm_ctrlpanel(); | ||
| 157 | } | ||
| 158 | |||
| 159 | # END CODE Copyright (C) 2012 Logical Helion, LLC. | ||
| 160 | |||
| 161 | |||
| 162 | 1; | ||
| 163 | __END__ |