| 1 | package Helios::Panoptes::ErrorLog; | ||
| 2 | |||
| 3 | use 5.008000; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use base qw(CGI::Application); | ||
| 7 | use Data::Dumper; | ||
| 8 | use Helios::Panoptes::Helper; | ||
| 9 | use HTML::Template::Expr; | ||
| 10 | |||
| 11 | use Error qw(:try); | ||
| 12 | |||
| 13 | our $VERSION = '1.40'; | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | Helios::Panoptes::ErrorLog - Helios::Panoptes extension to handle the Error Log view | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | Helios::Panoptes::ErrorLog handles the display and user interaction for the Panoptes Error Log | ||
| 22 | run mode. | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | sub setup { | ||
| 27 | my $self = shift; | ||
| 28 | $self->start_mode('error_log'); | ||
| 29 | $self->run_modes( | ||
| 30 | error_log => 'error_log', | ||
| 31 | ); | ||
| 32 | |||
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | sub teardown { | ||
| 37 | my $self = shift; | ||
| 38 | } | ||
| 39 | |||
| 40 | |||
| 41 | =head1 VIEW METHODS | ||
| 42 | |||
| 43 | These methods define code that back the particular application pages. | ||
| 44 | |||
| 45 | =head2 error_log() | ||
| 46 | |||
| 47 | The error_log() method handles the display and user interaction for the Error Log page. | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | |||
| 52 | sub error_log { | ||
| 53 | my $self = shift; | ||
| 54 | my $q = $self->query(); | ||
| 55 | |||
| 56 | my $sql = ""; | ||
| 57 | |||
| 58 | my $time_horizon = 300; | ||
| 59 | my $per_page = 100; | ||
| 60 | my $cur_page = 0; | ||
| 61 | my @log_display = (); | ||
| 62 | |||
| 63 | my @priority; | ||
| 64 | my @priority_hash; | ||
| 65 | |||
| 66 | my $message = ""; | ||
| 67 | my $search = "message"; | ||
| 68 | |||
| 69 | my %priority = Helios::Panoptes::Helper::option_priority(); | ||
| 70 | my %search = Helios::Panoptes::Helper::option_search(); | ||
| 71 | my %time_horizon = Helios::Panoptes::Helper::option_time_horizon(); | ||
| 72 | my %limit = Helios::Panoptes::Helper::option_limit(); | ||
| 73 | |||
| 74 | $search = "message" if !$search{$search}; | ||
| 75 | |||
| 76 | my @funcmap = Helios::Panoptes::Helper::option_function_map(); | ||
| 77 | |||
| 78 | my $function = 0; | ||
| 79 | |||
| 80 | if ( defined($q->param('time')) ) { $time_horizon = $q->param('time'); } | ||
| 81 | if ( defined($q->param('priority')) ) | ||
| 82 | { | ||
| 83 | foreach($q->param('priority')) | ||
| 84 | { | ||
| 85 | if($_ > -1) | ||
| 86 | { | ||
| 87 | push @priority_hash, { 'option' => $_ }; | ||
| 88 | push @priority, $_; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | } | ||
| 92 | if ( defined($q->param('function')) ) { $function = $q->param('function'); } | ||
| 93 | if ( defined($q->param('message')) ) { $message = $q->param('message'); } | ||
| 94 | if ( defined($q->param('search')) ) { $search = $q->param('search'); } | ||
| 95 | if ( defined($q->param('page')) ) { $cur_page = $q->param('page'); } | ||
| 96 | if ( defined($q->param('limit')) ) { $per_page = $q->param('limit'); } | ||
| 97 | |||
| 98 | my $message_sql = "AND ?"; | ||
| 99 | my $message_value = "1"; | ||
| 100 | |||
| 101 | my $priority_sql = "AND 1"; | ||
| 102 | |||
| 103 | my $function_sql = "AND ?"; | ||
| 104 | my $function_value = "1"; | ||
| 105 | |||
| 106 | $message_sql = "AND $search LIKE ?" if($message); | ||
| 107 | $message_value = "%$message%" if($message); | ||
| 108 | |||
| 109 | $priority_sql = "AND priority IN ('" . (join "','",@priority) . "')" if(@priority); | ||
| 110 | |||
| 111 | $function_sql = "AND funcid = ?" if($function); | ||
| 112 | $function_value = $function if($function); | ||
| 113 | |||
| 114 | $sql = " | ||
| 115 | SELECT | ||
| 116 | COUNT(*) AS num | ||
| 117 | FROM | ||
| 118 | helios_log_tb | ||
| 119 | WHERE | ||
| 120 | log_time >= UNIX_TIMESTAMP() - ? | ||
| 121 | $function_sql | ||
| 122 | $message_sql | ||
| 123 | $priority_sql | ||
| 124 | "; | ||
| 125 | |||
| 126 | my $total_entries = Helios::Panoptes::Helper::db_fetch_row($sql,$time_horizon, $function_value, $message_value); | ||
| 127 | $total_entries = $total_entries->{num}; | ||
| 128 | |||
| 129 | $sql = " | ||
| 130 | SELECT | ||
| 131 | * | ||
| 132 | FROM | ||
| 133 | helios_log_tb | ||
| 134 | WHERE | ||
| 135 | log_time >= UNIX_TIMESTAMP() - ? | ||
| 136 | $function_sql | ||
| 137 | $message_sql | ||
| 138 | $priority_sql | ||
| 139 | ORDER BY | ||
| 140 | log_time DESC | ||
| 141 | LIMIT ?,? | ||
| 142 | "; | ||
| 143 | |||
| 144 | my @logs = Helios::Panoptes::Helper::db_fetch_all($sql,$time_horizon, $function_value, $message_value,$cur_page * $per_page,$per_page); | ||
| 145 | |||
| 146 | my @colors = qw[EFEFEF EAEAEA]; | ||
| 147 | |||
| 148 | # http://search.cpan.org/~saper/Sys-Syslog-0.24/Syslog.pm | ||
| 149 | |||
| 150 | |||
| 151 | my $j = 0; | ||
| 152 | foreach(@logs) | ||
| 153 | { | ||
| 154 | my $date_parts = Helios::Panoptes::Helper::splitEpochDate($_->{log_time}); | ||
| 155 | $_->{created_at} = $date_parts->{YYYY}.'-'.$date_parts->{MM}.'-'.$date_parts->{DD}.' '.$date_parts->{HH24}.':'.$date_parts->{MI}.':'.$date_parts->{SS}; | ||
| 156 | $_->{color} = $colors[$j]; | ||
| 157 | $_->{priority} = $priority{$_->{priority}} || $_->{priority}; | ||
| 158 | $j=1-$j; | ||
| 159 | } | ||
| 160 | |||
| 161 | my $params = $ENV{SCRIPT_NAME} . "?time=$time_horizon&function=$function&search=$search&message=$message&limit=$per_page"; | ||
| 162 | |||
| 163 | my $tmpl = HTML::Template::Expr->new(filename => 'tmpl/error_log.html', die_on_bad_params => 0, global_vars => 1); | ||
| 164 | $tmpl->param(TITLE=>"Helios - Error Log"); | ||
| 165 | $tmpl->param(PRIORITY_SELECTED => \@priority_hash); | ||
| 166 | $tmpl->param(PRIORITY_OPTIONS => Helios::Panoptes::Helper::hash_to_pair(\%priority)); | ||
| 167 | $tmpl->param(TIME => $time_horizon); | ||
| 168 | $tmpl->param(TIME_OPTIONS => Helios::Panoptes::Helper::hash_to_pair(\%time_horizon)); | ||
| 169 | $tmpl->param(SEARCH => $search); | ||
| 170 | $tmpl->param(SEARCH_OPTIONS => Helios::Panoptes::Helper::hash_to_pair(\%search,'text')); | ||
| 171 | $tmpl->param(FUNCTION => $function); | ||
| 172 | $tmpl->param(FUNCTION_OPTIONS => \@funcmap); | ||
| 173 | $tmpl->param(MESSAGE => $message); | ||
| 174 | $tmpl->param(LOG_ENTRIES => \@logs); | ||
| 175 | |||
| 176 | $tmpl->param(PER_PAGE => $per_page); | ||
| 177 | $tmpl->param(CUR_PAGE => $cur_page); | ||
| 178 | $tmpl->param(LIMIT_OPTIONS => Helios::Panoptes::Helper::hash_to_pair(\%limit)); | ||
| 179 | $tmpl->param(PAGINATION => Helios::Panoptes::Helper::pagination($cur_page,$per_page,$total_entries,$params)); | ||
| 180 | |||
| 181 | foreach(@priority) | ||
| 182 | { | ||
| 183 | $tmpl->param('PRIORITY_' . $_,1); | ||
| 184 | } | ||
| 185 | |||
| 186 | return $tmpl->output(); | ||
| 187 | |||
| 188 | } | ||
| 189 | |||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | 1; | ||
| 194 | __END__ |