File: logmon.pl

package info (click to toggle)
libterm-visual-perl 0.06-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 200 kB
  • ctags: 113
  • sloc: perl: 2,281; makefile: 44
file content (163 lines) | stat: -rw-r--r-- 4,554 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/perl -w
use strict;
use POE;
use POE qw(Wheel::FollowTail Filter::Line Driver::SysRW);
use Term::Visual;

my @files = qw(/var/log/messages /var/log/kernel.log /var/log/maillog
 /var/log/ftp.log /var/log/lpd-errs);

# Start Term::Visual.

my $vt = Term::Visual->new( Alias => "interface" );
                                        

$vt->set_palette( mycolor       => "magenta on black",
                  statcolor     => "green on black",
                  sockcolor     => "cyan on black",
                  ncolor        => "white on black",
                  st_frames     => "bright cyan on blue",
                  st_values     => "bright white on blue",
                  stderr_bullet => "bright white on red",
                  stderr_text   => "bright yellow on black",
                  err_input     => "bright white on red",
                  help          => "white on black",
                  help_cmd      => "bright white on black" );

my $window_id = $vt->create_window(
       Window_Name => "logmon",

       Status => { 0 =>
                   { format => "\0(st_frames)" .
                               " [" .
                               "\0(st_values)" .
                               "%8.8s" .
                               "\0(st_frames)" .
                               "] " .
                               "\0(st_values)" .
                               "%s",
                     fields => [qw( time name )] },
                   1 =>
                   { format => " Observing: %s",
                     fields => [ qw( file ) ] },
                 },

       Buffer_Size => 1000,
       History_Size => 50,

       Title => "System Log Monitor" );

POE::Session->create
  (inline_states =>
    { _start         => \&start_guts,
      got_term_input => \&handle_term_input,
      update_time    => \&update_time,
      update_file    => \&update_file,
      input_tail     => \&input_tail,
    }
  );

## Initialize the back-end guts of the "client".

sub start_guts {
  my ($kernel, $heap) = @_[KERNEL, HEAP];

  # Tell the terminal to send me input as "got_term_input".
  $kernel->post( interface => send_me_input => "got_term_input" );

  $kernel->yield( "update_time" );

  my $window_name = $vt->get_window_name($window_id);
  $vt->set_status_field( $window_id, name => $window_name );


  my $filist = join(" ",@files);
  $filist =~ s!/var/log/!!g;
  $vt->set_status_field( $window_id, file => $filist);
#  $kernel->yield( update_file => $filist);


  # Start Following a File
  foreach my $file (@files) {
    $heap->{"wheel_$file"} = POE::Wheel::FollowTail->new
      ( Filename     => $file,
        Driver       => POE::Driver::SysRW->new(),
        Filter       => POE::Filter::Line->new(),
        PollInterval => 1,
        InputEvent   => "input_tail",
        ErrorEvent   => \&error_tail,
        ResetEvent   => \&reset_tail,
      );

  }
}

### The main input handler for this program.  This would be supplied
### by the guts of the client program.

sub handle_term_input {
  my ($kernel, $heap, $input, $exception) = @_[KERNEL, HEAP, ARG0, ARG1];

  # Got an exception.  These are interrupt (^C) or quit (^\).
  if (defined $exception) {
    warn "got exception: $exception";
    $vt->delete_window($window_id);
    exit;
  }

  if ($input =~ s{^/\s*(\S+)\s*}{}) {
    my $cmd = uc($1);

    if ($cmd eq 'QUIT') {
     # might close open filehandles here.
      $vt->delete_window($window_id);
      exit;
    }
    # Unknown command?
    warn "Unknown command: $cmd";
    return;
  }

}

### Update the time on the status bar.

sub update_time {
  my ($kernel, $heap) = @_[KERNEL, HEAP];
  # New time format.
  use POSIX qw(strftime);

  $vt->set_status_field( $window_id, time => strftime("%I:%M %p", localtime) );

  # Schedule another time update for the next minute.  This is more
  # accurate than using delay() because it schedules the update at the
  # beginning of the minute.
  $kernel->alarm( update_time => int(time() / 60) * 60 + 60 );
}

sub update_file {
  my ($kernel,$file) = @_[KERNEL,ARG0];

  $vt->set_status_field( $window_id, file => $file );
}

sub input_tail {
    my ($heap, $input, $wheel_id) = @_[HEAP, ARG0, ARG1];
  $vt->print( $window_id, $input );
}

sub error_tail {
    my ($operation, $errnum, $errstr, $wheel_id) = @_[ARG0..ARG3];
    warn "Wheel $wheel_id generated $operation error $errnum: $errstr";
}

sub reset_tail {
    my $wheel_id = $_[ARG0];
    $vt->print( $window_id, "Reset $wheel_id");
}

$poe_kernel->run();
$vt->shutdown;
exit 0;