line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::Sysadmin::Log::Simple; |
2
|
4
|
|
|
4
|
|
202559
|
use strict; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
183
|
|
3
|
4
|
|
|
4
|
|
21
|
use warnings; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
122
|
|
4
|
4
|
|
|
4
|
|
50
|
use v5.10.1; |
|
4
|
|
|
|
|
13
|
|
|
4
|
|
|
|
|
288
|
|
5
|
|
|
|
|
|
|
# ABSTRACT: application class for managing a simple sysadmin log |
6
|
|
|
|
|
|
|
our $VERSION = '0.009'; # VERSION |
7
|
|
|
|
|
|
|
|
8
|
4
|
|
|
4
|
|
3714
|
use autodie qw(:file :filesys); |
|
4
|
|
|
|
|
77453
|
|
|
4
|
|
|
|
|
26
|
|
9
|
4
|
|
|
4
|
|
18289
|
use DateTime; |
|
4
|
|
|
|
|
1143641
|
|
|
4
|
|
|
|
|
161
|
|
10
|
4
|
|
|
4
|
|
42
|
use Carp; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
409
|
|
11
|
|
|
|
|
|
|
use Module::Pluggable |
12
|
4
|
|
|
|
|
42
|
search_path => [__PACKAGE__], |
13
|
4
|
|
|
4
|
|
4544
|
instantiate => 'new'; |
|
4
|
|
|
|
|
51745
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub new { |
17
|
4
|
|
|
4
|
1
|
3131
|
my $class = shift; |
18
|
4
|
|
|
|
|
17
|
my %opts = @_; |
19
|
4
|
|
|
|
|
35
|
my $today = DateTime->now; |
20
|
4
|
100
|
|
|
|
1626
|
if ($opts{date}) { |
21
|
3
|
|
|
|
|
16
|
my ($in_year, $in_month, $in_day) = split(m{/}, $opts{date}); |
22
|
3
|
50
|
|
|
|
19
|
my $in_date = DateTime->new( |
23
|
|
|
|
|
|
|
year => $in_year, |
24
|
|
|
|
|
|
|
month => $in_month, |
25
|
|
|
|
|
|
|
day => $in_day, |
26
|
|
|
|
|
|
|
) or croak "Couldn't understand your date - use YYYY/MM/DD\n"; |
27
|
3
|
50
|
|
|
|
791
|
croak "Cannot use a date in the future\n" if $in_date > $today; |
28
|
3
|
|
|
|
|
725
|
$today = $in_date; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
4
|
|
50
|
|
|
152
|
return bless { |
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
100
|
|
|
|
|
32
|
|
|
|
|
|
|
do_twitter => $opts{do_twitter} // 0, |
33
|
|
|
|
|
|
|
do_file => $opts{do_file} // 1, |
34
|
|
|
|
|
|
|
do_http => $opts{do_http} // 0, |
35
|
|
|
|
|
|
|
do_udp => $opts{do_udp} // 1, |
36
|
|
|
|
|
|
|
logdir => $opts{logdir}, |
37
|
|
|
|
|
|
|
date => $today, |
38
|
|
|
|
|
|
|
user => $opts{user} || $ENV{SUDO_USER} || $ENV{USER}, |
39
|
|
|
|
|
|
|
in => $opts{read_from} || \*STDIN, |
40
|
|
|
|
|
|
|
udp => $opts{udp}, |
41
|
|
|
|
|
|
|
http => $opts{http}, |
42
|
|
|
|
|
|
|
}, $class; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub run { |
47
|
5
|
|
|
5
|
1
|
660135
|
my $self = shift; |
48
|
5
|
|
|
|
|
14
|
my $cmd = shift; |
49
|
|
|
|
|
|
|
|
50
|
5
|
|
100
|
|
|
35
|
$cmd ||= 'log'; |
51
|
5
|
|
|
|
|
26
|
$self->run_command($cmd); |
52
|
4
|
|
|
|
|
96
|
return; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub run_command { |
56
|
5
|
|
|
5
|
0
|
12
|
my $self = shift; |
57
|
5
|
|
|
|
|
10
|
my $cmd = shift; |
58
|
|
|
|
|
|
|
|
59
|
5
|
|
|
|
|
45
|
my $s = $self->can("run_command_$cmd"); |
60
|
5
|
50
|
|
|
|
25
|
die "Unknown command '$cmd'" unless $s; |
61
|
5
|
|
|
|
|
21
|
return $self->$s(); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub run_command_log { |
65
|
3
|
|
|
3
|
0
|
6
|
my $self = shift; |
66
|
3
|
|
|
|
|
148
|
say 'Log entry:'; |
67
|
3
|
|
|
|
|
17
|
my $in = $self->{in}; |
68
|
3
|
|
|
|
|
192
|
my $logentry = <$in>; # one line |
69
|
3
|
|
|
|
|
257
|
chomp $logentry; |
70
|
3
|
100
|
|
|
|
212
|
croak 'A log entry is needed' unless $logentry; |
71
|
|
|
|
|
|
|
|
72
|
2
|
|
|
|
|
18
|
PLUGIN: foreach my $plugin ( $self->plugins(app => $self) ) { |
73
|
6
|
50
|
|
|
|
95
|
next PLUGIN unless $plugin->can('log'); |
74
|
6
|
|
|
|
|
35
|
my $r = $plugin->log($logentry); |
75
|
6
|
100
|
|
|
|
85
|
if ($r) { |
76
|
4
|
|
|
|
|
11
|
my $name = ref $plugin; |
77
|
4
|
|
|
|
|
8
|
my $re = __PACKAGE__ . '::'; |
78
|
4
|
|
|
|
|
46
|
$name =~ s/^$re//; |
79
|
4
|
|
|
|
|
76
|
say sprintf '[%-8s] %s', $name, $r; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub run_command_view { |
85
|
2
|
|
|
2
|
0
|
6
|
my $self = shift; |
86
|
2
|
|
|
|
|
17
|
PLUGIN: foreach my $plugin ( $self->plugins(app => $self) ) { |
87
|
6
|
100
|
|
|
|
103
|
next PLUGIN unless $plugin->can('view'); |
88
|
2
|
|
|
|
|
12
|
$plugin->view(); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
1; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
__END__ |