Panoptes/SystemLog.pm untested

filter: Statementstmt 0.0% 0 / 123 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 177 CC 16 SCAR 52.5
CC
16
Cov
0%
rm_system_log
272.0
setup
2.0
BEGIN
2.0
CRAP 189.8
1 package Helios::Panoptes::SystemLog;
2  
3 use 5.008;
4 use strict;
5 use warnings;
6 use base 'Helios::Panoptes::Base';
7 use Data::Dumper;
8  
9 use CGI::Application::Plugin::DBH qw(dbh_config dbh);
10  
11 our $VERSION = '1.51_2820';
12  
13 sub setup {
14 my $self = shift;
15 $self->start_mode('system_log');
16 $self->mode_param('rm');
17 $self->run_modes(
18 system_log => 'rm_system_log'
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_system_log
28  
29 =cut
30  
31 sub rm_system_log {
32 my $self = shift;
33 my $config = $self->getConfig();
34 my $dbh = $self->dbh();
35 my $q = $self->query();
36 my %funcmapbyid = $self->getFuncmapByFuncid();
37 my %funcmapbyname = $self->getFuncmapByFuncname();
38 my @log_levels = $self->getLogLevelList();
39 my %log_level_map = $self->getLogLevelMap();
40 my $sql;
41 my @whereclauses;
42 my @plhdrs;
43 my @log_entries;
44  
45 # get the form values
46 my @form_priorities = $q->param('priorities');
47 my $form_funcid = defined($q->param('service')) ? $q->param('service') : 0;
48 my $form_thorizon = defined($q->param('time')) ? $q->param('time') : 300;
49 my $form_search_field = $q->param('search_field');
50 my $form_search_value = $q->param('search_value');
51 my $form_rpp = defined($q->param('rpp')) ? $q->param('rpp') : 50;
52  
53 # sanity checks
54 if ( $form_thorizon =~ /\D/ ) { $form_thorizon = 300; }
55 if ( $form_funcid =~ /\D/ ) { $form_funcid = 0; }
56 foreach (@form_priorities) {
57 if ( $_ =~ /\D/ || $_ < 0 || $_ > 7) { @form_priorities = (); last; }
58 }
59 if ( $form_rpp =~ /\D/ ) { $form_rpp = 50; }
60  
61 my $selectfrom = q{
62 SELECT
63 log_time,
64 host,
65 process_id,
66 jobid,
67 funcid,
68 job_class,
69 priority,
70 message
71 FROM
72 helios_log_tb
73 };
74
75 # first WHERE condition: time horizon
76 push(@whereclauses,'log_time >= ?');
77 push(@plhdrs, time() - $form_thorizon);
78
79 # service
80 if ( $form_funcid ) {
81 push(@whereclauses, 'funcid = ?');
82 push(@plhdrs, $form_funcid);
83 }
84
85 if ( $form_search_field && $form_search_value ) {
86 SWITCH : {
87 if ($form_search_field eq 'message') {
88 push(@whereclauses, 'message LIKE ?');
89 push(@plhdrs, '%'.$form_search_value.'%');
90 }
91 if ($form_search_field eq 'host') {
92 push(@whereclauses, 'LOWER(host) = ?');
93 push(@plhdrs, lc( $form_search_value ));
94 }
95 if ($form_search_field eq 'jobid') {
96 push(@whereclauses, 'jobid = ?');
97 push(@plhdrs, $form_search_value);
98 }
99 if ($form_search_field eq 'pid') {
100 push(@whereclauses, 'process_id = ?');
101 push(@plhdrs, $form_search_value);
102 }
103 # there is no default
104 # if searchterm doesn't match a valid search field,
105 # we just skip it
106 }
107 }
108
109 if (@form_priorities){
110 if (scalar(@form_priorities) > 1) {
111 push(@whereclauses, ' priority IN ('.join(',', @form_priorities).')');
112 } else {
113 push(@whereclauses, 'priority = ?');
114 push(@plhdrs, $form_priorities[0]);
115 }
116 }
117
118 my $whereclause = 'WHERE '.join(' AND ',@whereclauses);
119 my $orderby = 'ORDER BY log_time DESC';
120 $sql = join(' ',($selectfrom, $whereclause, $orderby));
121  
122  
123 my $sth = $dbh->prepare_cached($sql);
124 $sth->execute(@plhdrs);
125
126 while (my $r = $sth->fetchrow_arrayref() ) {
127 push(@log_entries, {
128 log_time => scalar(localtime($r->[0])),
129 host => $r->[1],
130 pid => $r->[2],
131 jobid => $r->[3],
132 service => $r->[5],
133 priority => $log_levels[ $r->[6] ],
134 message => $r->[7],
135 }
136 );
137 }
138  
139 ## (re)generate form ##
140 my $plabels = {};
141 my $pvalues = [];
142 # while (my ($key, $value) = each %log_level_map ) {
143 # $plabels->{$value} = $key;
144 # }
145 %$plabels = map { $log_level_map{$_} => $_ } keys %log_level_map;
146 @$pvalues = sort values %log_level_map;
147 unshift(@$pvalues, '-1');
148 $plabels->{'-1'} = 'All';
149 my $priorities_field = $q->scrolling_list(
150 -name => 'priorities',
151 -values => $pvalues,
152 -labels => $plabels,
153 -size => 4,
154 -multiple => 'true',
155 -default => \@form_priorities
156 );
157
158 my $slabels = {};
159 my $svalues = [];
160 #[] %$slabels = map { $funcmapbyname{$_} => $_ } keys %funcmapbyname;
161 $slabels = \%funcmapbyid;
162 %$slabels = map { $funcmapbyname{$_} => $_ } keys %funcmapbyname;
163 #[] @$svalues = sort values %funcmapbyname;
164 @$svalues = map { $funcmapbyname{$_} } sort keys %funcmapbyname;
165 unshift(@$svalues, '0');
166 $slabels->{'0'} = 'All';
167 my $service_list = $q->popup_menu(
168 -name => 'service',
169 -values => $svalues,
170 -labels => $slabels,
171 -default => $form_funcid
172 );
173
174 my $search_field_list = $q->popup_menu(
175 -name => 'search_field',
176 -values => ['host', 'jobid', 'message', 'pid'],
177 -labels => { 'host' => 'Host', 'jobid' => 'Jobid', 'message' => 'Message', 'pid' => 'PID'},
178 -default => $form_search_field
179 );
180
181 my $rpp_list = $q->popup_menu(
182 -name => 'rpp',
183 -values => [25, 50, 100, 200, 500, 1000, 2000, 5000, 10000],
184 -default => $form_rpp
185 );
186  
187 my $time_list = $q->popup_menu(
188 -name => 'time',
189 -values => [60, 300, 900, 1800, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 259200, 640800],
190 -labels => {
191 60 => '1 Minute',
192 300 => '5 Minutes',
193 900 => '15 Minutes',
194 1800 => '30 Minutes',
195 3600 => '1 Hour',
196 7200 => '2 Hours',
197 14400 => '4 Hours',
198 28800 => '8 Hours',
199 57600 => '16 Hours',
200 86400 => '1 Day',
201 172800 => '2 Days',
202 259200 => '3 Days',
203 640800 => '1 Week'
204 },
205 -default => $form_thorizon
206 );
207  
208 #t
209 #print $q->header('text/plain');
210 #print Dumper($sql);
211 #print "\n\n";
212 #print Dumper(@plhdrs);
213 #print "\n\n";
214 #print Dumper($form_search_field);
215 #print Dumper($form_search_value);
216 #print Dumper($slabels);
217 #print "\n\n";
218 #print Dumper($svalues);
219 #print "\n\n";
220 #print Dumper(@log_entries);
221  
222
223 my $tmpl = $self->load_tmpl('system_log.html', die_on_bad_params => 0);
224 $tmpl->param(TITLE => "Helios - System Log");
225 $tmpl->param(PRIORITY_LIST => $priorities_field);
226 $tmpl->param(SERVICE_LIST => $service_list);
227 $tmpl->param(SEARCH_FIELD_LIST => $search_field_list);
228 $tmpl->param(SEARCH_VALUE => $form_search_value);
229 $tmpl->param(RPP_LIST => $rpp_list);
230 $tmpl->param(TIME_LIST => $time_list);
231
232 $tmpl->param(LOG_ENTRIES => \@log_entries);
233  
234 return $tmpl->output();
235 }
236  
237  
238  
239 1;
240 __END__