~ubuntu-branches/ubuntu/raring/octopussy/raring

« back to all changes in this revision

Viewing changes to usr/sbin/octo_syslog2iso8601.pl

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-09-25 10:27:37 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120925102737-1hgntjl557w3sjqh
Tags: 1.0.6-0ubuntu1
* New upstream release (LP: #1056016).
* d/control: bumped Standards-Version: 3.9.3, no changes.
* d/{copyright,control}: Updated upstream URL.
* d/README.Debian: Fixed minor typo.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/perl
2
 
 
3
 
=head1 NAME
4
 
 
5
 
syslog2iso8601.pl - Octopussy program to convert 'syslog format' logs to 'iso8601' logs
6
 
 
7
 
=head1 SYNOPSIS
8
 
 
9
 
syslog2iso8601.pl --device <device> --service <service> 
10
 
 
11
 
=head1 DESCRIPTION
12
 
 
13
 
=cut
14
 
 
15
 
use strict;
16
 
use warnings;
17
 
use Readonly;
18
 
 
19
 
use Getopt::Long;
20
 
 
21
 
use DateTime;
22
 
use DateTime::Format::Strptime;
23
 
 
24
 
use AAT::Utils qw( ARRAY );
25
 
use Octopussy;
26
 
use Octopussy::Logs;
27
 
 
28
 
Readonly my $PROG_NAME    => 'syslog2iso8601.pl';
29
 
Readonly my $PROG_VERSION => Octopussy::Version();
30
 
 
31
 
my $help;
32
 
my ($opt_device, $opt_service, $opt_timezone) = (undef, undef, undef);
33
 
 
34
 
=head1 FUNCTIONS
35
 
 
36
 
=head2 Help()
37
 
 
38
 
Prints Help
39
 
 
40
 
=cut
41
 
 
42
 
sub Help
43
 
{
44
 
    my $help_str = <<"EOF";
45
 
 
46
 
$PROG_NAME (version $PROG_VERSION)
47
 
 
48
 
 Usage: $PROG_NAME --device <device> --service <service> --timezone <+/-HH:MM>
49
 
 
50
 
EOF
51
 
 
52
 
    print $help_str;
53
 
    if (!defined $opt_device)
54
 
    {
55
 
        print ' ' . Octopussy::Device::String_List(undef) . "\n";
56
 
    }
57
 
    elsif (!defined $opt_service)
58
 
    {
59
 
        print ' '
60
 
            . Octopussy::Device::String_Services(ARRAY($opt_device)) . "\n";
61
 
    }
62
 
    print "\n";
63
 
 
64
 
    exit;
65
 
}
66
 
 
67
 
#
68
 
# MAIN
69
 
#
70
 
 
71
 
my $status = GetOptions(
72
 
    'h'         => \$help,
73
 
    'help'      => \$help,
74
 
    'device=s'  => \$opt_device,
75
 
    'service=s' => \$opt_service,
76
 
    'timezone=s' => \$opt_timezone,
77
 
);
78
 
 
79
 
Help()
80
 
    if ((!$status)
81
 
    || ($help)
82
 
    || (!defined $opt_device)
83
 
    || (!defined $opt_service)
84
 
    || (!defined $opt_timezone));
85
 
 
86
 
my ($files, $total) =
87
 
    Octopussy::Logs::Get_TimePeriod_Files($opt_device, $opt_service, '197001010000', '202001010000');
88
 
 
89
 
my $strp = new DateTime::Format::Strptime(pattern => '%Y %h %e %T');
90
 
 
91
 
foreach my $min (sort keys %{$files})
92
 
{
93
 
    my @logs = ();
94
 
    foreach my $f (@{$files->{$min}})
95
 
    {
96
 
        if ($f =~ /\/(\d{4})\/\d{2}\/\d{2}\/msg_/)
97
 
        {
98
 
            my $year = $1;
99
 
            if (defined open my $FILE, '-|', "zcat \"$f\"")
100
 
            {
101
 
                while (<$FILE>)
102
 
                { 
103
 
                   if ($_ =~ /^(\w{3}) \s?(\d{1,2}) (\d\d:\d\d:\d\d) (.*)$/)
104
 
                   {
105
 
                       my $dt = $strp->parse_datetime("$year $1 $2 $3");
106
 
                       my $str = sprintf("%sT%s.000000%s %s", $dt->ymd('-'), $dt->hms(':'), $opt_timezone, $4);         
107
 
                       push @logs, $str;
108
 
                   }
109
 
                }
110
 
                close $FILE;
111
 
                if (defined open my $TMPFILE, '|-', "gzip >> ${f}.tmp")
112
 
                {
113
 
                    foreach my $log (@logs) 
114
 
                        { print {$TMPFILE} "$log\n"; }
115
 
                    close $TMPFILE;
116
 
                    rename "${f}.tmp", $f   if (scalar @logs);
117
 
                    @logs = ();
118
 
                }
119
 
            }
120
 
        }
121
 
    }
122
 
}
123
 
 
124
 
=head1 AUTHOR
125
 
 
126
 
Sebastien Thebert <octo.devel@gmail.com>
127
 
 
128
 
=cut
129
 
 
130
 
1;
 
 
b'\\ No newline at end of file'