/
nkimark_log2tsv.pl
executable file
·144 lines (112 loc) · 5.13 KB
/
nkimark_log2tsv.pl
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
#!/usr/bin/env perl
# nkimark_log2tsv.pl
# see: perldoc -t nkimark_log2tsv.pl
# NetKids iMarkの生ログは次の形をしていると想定:
# | NetKids iMark Log File
# | [2015/06/01 01:00:01] Ping応答時間 192.168.1.1 チェック:OK, 2, 2 msec
# | [2015/06/02 02:00:02] SNMPV2データ取得_1.2.3.4.5.6.78901.2.3.4 192.168.1.1 チェック:OK, 25, 15 msec
# | [2015/06/03 03:00:03] HTTP応答時間_http://www.example.com/test.html 192.168.1.1 チェック:OK, 166, 166 msec
# | [2015/06/04 04:00:04] HTTP応答コード_/test.html 192.168.1.1 チェック:OK, 200, 273 msec
# | [2015/06/05 05:00:05] 通知エラー:テンプレート型メール送信 が行えません. ファイルが見つかりません
use strict;
use warnings;
use utf8;
use Encode;
binmode STDIN, ':encoding(cp932)'; # for Excel/Windows
binmode STDOUT, ':encoding(cp932)'; # for Excel/Windows
#binmode STDOUT, ':encoding(utf-8)';
binmode STDERR, ':encoding(utf-8)';
use Getopt::Std;
my %separator = ('tsv' => "\t", 'csv' => ',', '' => "\t");
my %options = ();
Getopt::Std::getopts('s:', \%options);
if (exists($options{'s'})) {
if (!exists($separator{$options{'s'}})) {
print STDERR sprintf('ERROR: option -s "%s" is not defined.', $options{'s'}) . "\n";
exit 1;
}
} else {
$options{'s'} = 'tsv';
}
my $separator = $separator{$options{'s'}};
my $regex_firstline = 'NetKids iMark Log File';
my $regex_datetime = '\[(\d{4}/\d{2}/\d{2}) (\d{2}:\d{2}:\d{2})\]';
my $regex_ipaddress = '(\d+\.\d+\.\d+\.\d+)';
my $regex_status = '(\S+), (\d+), (\d+) msec';
my $regex_core_ping = 'Ping応答時間';
my $regex_core_snmpv2 = 'SNMPV2データ取得\_(\S+)';
my $regex_core_httptime = 'HTTP応答時間\_(\S+)';
my $regex_core_httpcode = 'HTTP応答コード\_(\S+)';
#my $regex_core_noticeerror = '(通知エラー:テンプレート型メール送信 が行えません. ファイルが見つかりません)';
my $regex_core_noticeerror = '(通知エラー:テンプレート型メール送信 が行えません. \S+)';
my $count = 1;
my $error = 0;
my $line_output = join($separator, '#日付', '#時刻', '#項目1', '#項目2', '#IPアドレス', '#状態', '#値1', '#値2(msec)');
print $line_output . "\n";
while(defined(my $line = <STDIN>)) {
chomp($line);
if ($line =~ /$regex_firstline/) {
# 最初の行
$line_output = '';
} elsif ($line =~ /$regex_core_ping/) {
# Ping応答時間
$line =~ /$regex_datetime $regex_core_ping $regex_ipaddress $regex_status/;
my($date, $time, $ipaddress, $status_1, $status_2, $status_3) = ($1, $2, $3, $4, $5, $6);
$line_output = join($separator, $date, $time, 'Ping応答時間', '', $ipaddress, $status_1, $status_2, $status_3);
} elsif ($line =~ /$regex_core_snmpv2/) {
# SNMPV2データ取得
$line =~ /$regex_datetime $regex_core_snmpv2 $regex_ipaddress $regex_status/;
my($date, $time, $snmpv2, $ipaddress, $status_1, $status_2, $status_3) = ($1, $2, $3, $4, $5, $6, $7);
$line_output = join($separator, $date, $time, 'SNMPV2データ取得', $snmpv2, $ipaddress, $status_1, $status_2, $status_3);
} elsif ($line =~ /$regex_core_httptime/) {
# HTTP応答時間
$line =~ /$regex_datetime $regex_core_httptime $regex_ipaddress $regex_status/;
my($date, $time, $httptime, $ipaddress, $status_1, $status_2, $status_3) = ($1, $2, $3, $4, $5, $6, $7);
$line_output = join($separator, $date, $time, 'HTTP応答時間', $httptime, $ipaddress, $status_1, $status_2, $status_3);
} elsif ($line =~ /$regex_core_httpcode/) {
# HTTP応答コード
$line =~ /$regex_datetime $regex_core_httpcode $regex_ipaddress $regex_status/;
my($date, $time, $httpcode, $ipaddress, $status_1, $status_2, $status_3) = ($1, $2, $3, $4, $5, $6, $7);
$line_output = join($separator, $date, $time, 'HTTP応答コード', $httpcode, $ipaddress, $status_1, $status_2, $status_3);
} elsif ($line =~ /$regex_core_noticeerror/) {
# 通知エラー
$line =~ /$regex_datetime $regex_core_noticeerror/;
my($date, $time, $noticeerror) = ($1, $2, $3);
$line_output = join($separator, $date, $time, $noticeerror);
} else {
# 上記以外の例外
$error = 1;
$line_output = sprintf('ERROR [%d]: %s', $count, $line);
}
if ($error) {
print STDERR $line_output . "\n";
} elsif ($line_output) {
print $line_output . "\n";
}
$count ++;
$error = 0;
}
exit;
=pod
=encoding utf8
=head1 NAME
nkimark_log2tsv.pl - Log Converter for NetKids iMark
=head1 VERSION
ver.20160404
=head1 DESCRIPTION
サーバ監視ツール『NetKids iMark』が保存する生ログのファイルを、
TSV/CSV形式のファイルに変換します。
標準入力/標準出力のパイプとして動作します。
対応している生ログの形式は、ソースファイルを参照してください。
=head1 USAGE
nkimark_log2tsv.pl -s tsv < NKIMARKyymm.LOG > NKIMARKyymm.tsv
=head2 OPTION
=over
=item -s [tsv|csv] (default = tsv)
=back
=head1 AUTHOR
大久保 正彦 (Masahiko OHKUBO) <mah@remoteroom.jp> <https://twitter.com/mah_jp>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2016 by Masahiko OHKUBO.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
=cut