line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Monitoring::TT::Input::CSV; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1620
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
24
|
|
5
|
1
|
|
|
1
|
|
4
|
use utf8; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
6
|
1
|
|
|
1
|
|
28
|
use Monitoring::TT::Log qw/error warn info debug trace/; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
61
|
|
7
|
1
|
|
|
1
|
|
5
|
use Monitoring::TT::Utils; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
617
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
##################################################################### |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 NAME |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Monitoring::TT::Input::CSV - Input CSV data |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 DESCRIPTION |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
CSV Input for Hosts and Contacts |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=cut |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
##################################################################### |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 CONSTRUCTOR |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head2 new |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
new(%options) |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=cut |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub new { |
32
|
1
|
|
|
1
|
1
|
405
|
my($class, %options) = @_; |
33
|
1
|
|
|
|
|
6
|
my $self = { |
34
|
|
|
|
|
|
|
'fields' => { |
35
|
|
|
|
|
|
|
'hosts' => [qw/name alias address type tags groups apps/], |
36
|
|
|
|
|
|
|
'contacts' => [qw/name alias email roles tags groups/], |
37
|
|
|
|
|
|
|
}, |
38
|
|
|
|
|
|
|
'montt' => $options{'montt'}, |
39
|
|
|
|
|
|
|
}; |
40
|
1
|
|
|
|
|
3
|
bless $self, $class; |
41
|
1
|
|
|
|
|
3
|
return $self; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
##################################################################### |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=head1 METHODS |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=head2 get_types |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
get_types($list_of_folders) |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
return supported types for this input type |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=cut |
55
|
|
|
|
|
|
|
sub get_types { |
56
|
1
|
|
|
1
|
1
|
4
|
my($self, $folders) = @_; |
57
|
1
|
|
|
|
|
1
|
my $types = []; |
58
|
1
|
|
|
|
|
2
|
for my $dir (@{$folders}) { |
|
1
|
|
|
|
|
2
|
|
59
|
1
|
|
|
|
|
2
|
for my $type (sort keys %{$self->{'fields'}}) { |
|
1
|
|
|
|
|
10
|
|
60
|
2
|
|
|
|
|
4
|
my $pattern = $dir.'/'.$type.'*.csv'; |
61
|
2
|
|
|
|
|
10
|
trace('looking for csv file: '.$pattern); |
62
|
2
|
|
|
|
|
164
|
my @files = glob($pattern); |
63
|
2
|
|
|
|
|
4
|
for my $f (@files) { |
64
|
2
|
|
|
|
|
12
|
debug('found csv file: '.$f); |
65
|
2
|
|
|
|
|
3
|
push @{$types}, $type; |
|
2
|
|
|
|
|
6
|
|
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} |
69
|
1
|
|
|
|
|
4
|
return Monitoring::TT::Utils::get_uniq_sorted($types); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
##################################################################### |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head2 read |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
read csv file |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=cut |
79
|
|
|
|
|
|
|
sub read { |
80
|
1
|
|
|
1
|
1
|
577
|
my($self, $dir, $type) = @_; |
81
|
1
|
|
|
|
|
2
|
my $data = []; |
82
|
1
|
|
|
|
|
3
|
my $pattern = $dir.'/'.$type.'*.csv'; |
83
|
1
|
|
|
|
|
59
|
my @files = glob($pattern); |
84
|
1
|
|
|
|
|
3
|
for my $file (@files) { |
85
|
2
|
|
|
|
|
11
|
info("reading $type from $file"); |
86
|
2
|
50
|
|
|
|
83
|
open(my $fh, '<', $file) or die('cannot read '.$file.': '.$!); |
87
|
2
|
|
|
|
|
44
|
while(my $line = <$fh>) { |
88
|
7
|
100
|
|
|
|
19
|
next if substr($line, 0, 1) eq '#'; |
89
|
5
|
|
|
|
|
7
|
chomp($line); |
90
|
5
|
100
|
|
|
|
34
|
next if $line =~ m/^\s*$/gmx; |
91
|
2
|
|
|
|
|
15
|
my @d = split(/\s*;\s*/mx, $line); |
92
|
2
|
|
|
|
|
3
|
my $d = {}; |
93
|
2
|
|
|
|
|
4
|
my $x = 0; |
94
|
2
|
|
|
|
|
2
|
for my $k (@{$self->{'fields'}->{$type}}) { |
|
2
|
|
|
|
|
6
|
|
95
|
14
|
|
|
|
|
23
|
$d->{$k} = $d[$x]; |
96
|
14
|
|
|
|
|
14
|
$x++; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
2
|
|
|
|
|
5
|
$d->{'file'} = $file; |
100
|
2
|
|
|
|
|
9
|
$d->{'line'} = $.; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# bring tags in shape |
103
|
2
|
|
|
|
|
8
|
$d->{'tags'} = Monitoring::TT::Utils::parse_tags($d->{'tags'}); |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# bring groups in shape |
106
|
2
|
|
|
|
|
6
|
$d->{'groups'} = Monitoring::TT::Utils::parse_groups($d->{'groups'}); |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# bring apps in shape |
109
|
2
|
50
|
|
|
|
11
|
$d->{'apps'} = Monitoring::TT::Utils::parse_tags($d->{'apps'}) if $type eq 'hosts'; |
110
|
|
|
|
|
|
|
|
111
|
2
|
|
|
|
|
2
|
push @{$data}, $d; |
|
2
|
|
|
|
|
14
|
|
112
|
|
|
|
|
|
|
} |
113
|
2
|
|
|
|
|
19
|
close($fh); |
114
|
2
|
|
|
|
|
3
|
debug("read ".(scalar @{$data})." $type from $file"); |
|
2
|
|
|
|
|
10
|
|
115
|
|
|
|
|
|
|
} |
116
|
1
|
|
|
|
|
4
|
return $data; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
##################################################################### |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head1 AUTHOR |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Sven Nierlein, 2013, |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=cut |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
1; |