line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Monitoring::TT::Input::CSV; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1013
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
23
|
|
4
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
0
|
|
|
1
|
|
|
|
|
18
|
|
5
|
1
|
|
|
1
|
|
2
|
use utf8; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
3
|
|
6
|
1
|
|
|
1
|
|
20
|
use Monitoring::TT::Log qw/error warn info debug trace/; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
47
|
|
7
|
1
|
|
|
1
|
|
3
|
use Monitoring::TT::Utils; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
437
|
|
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
|
327
|
my($class, %options) = @_; |
33
|
|
|
|
|
|
|
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
|
1
|
|
|
|
|
5
|
'montt' => $options{'montt'}, |
39
|
|
|
|
|
|
|
}; |
40
|
1
|
|
|
|
|
2
|
bless $self, $class; |
41
|
1
|
|
|
|
|
2
|
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
|
|
|
|
|
1
|
for my $dir (@{$folders}) { |
|
1
|
|
|
|
|
2
|
|
59
|
1
|
|
|
|
|
1
|
for my $type (sort keys %{$self->{'fields'}}) { |
|
1
|
|
|
|
|
9
|
|
60
|
2
|
|
|
|
|
5
|
my $pattern = $dir.'/'.$type.'*.csv'; |
61
|
2
|
|
|
|
|
6
|
trace('looking for csv file: '.$pattern); |
62
|
2
|
|
|
|
|
106
|
my @files = glob($pattern); |
63
|
2
|
|
|
|
|
4
|
for my $f (@files) { |
64
|
2
|
|
|
|
|
10
|
debug('found csv file: '.$f); |
65
|
2
|
|
|
|
|
2
|
push @{$types}, $type; |
|
2
|
|
|
|
|
5
|
|
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} |
69
|
1
|
|
|
|
|
3
|
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
|
511
|
my($self, $dir, $type) = @_; |
81
|
1
|
|
|
|
|
1
|
my $data = []; |
82
|
1
|
|
|
|
|
3
|
my $pattern = $dir.'/'.$type.'*.csv'; |
83
|
1
|
|
|
|
|
39
|
my @files = glob($pattern); |
84
|
1
|
|
|
|
|
3
|
for my $file (@files) { |
85
|
2
|
|
|
|
|
6
|
info("reading $type from $file"); |
86
|
2
|
50
|
|
|
|
43
|
open(my $fh, '<', $file) or die('cannot read '.$file.': '.$!); |
87
|
2
|
|
|
|
|
13
|
while(my $line = <$fh>) { |
88
|
7
|
100
|
|
|
|
15
|
next if substr($line, 0, 1) eq '#'; |
89
|
5
|
|
|
|
|
6
|
chomp($line); |
90
|
5
|
100
|
|
|
|
21
|
next if $line =~ m/^\s*$/gmx; |
91
|
2
|
|
|
|
|
11
|
my @d = split(/\s*;\s*/mx, $line); |
92
|
2
|
|
|
|
|
2
|
my $d = {}; |
93
|
2
|
|
|
|
|
2
|
my $x = 0; |
94
|
2
|
|
|
|
|
2
|
for my $k (@{$self->{'fields'}->{$type}}) { |
|
2
|
|
|
|
|
5
|
|
95
|
14
|
|
|
|
|
12
|
$d->{$k} = $d[$x]; |
96
|
14
|
|
|
|
|
12
|
$x++; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
2
|
|
|
|
|
4
|
$d->{'file'} = $file; |
100
|
2
|
|
|
|
|
4
|
$d->{'line'} = $.; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# bring tags in shape |
103
|
2
|
|
|
|
|
5
|
$d->{'tags'} = Monitoring::TT::Utils::parse_tags($d->{'tags'}); |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# bring groups in shape |
106
|
2
|
|
|
|
|
4
|
$d->{'groups'} = Monitoring::TT::Utils::parse_groups($d->{'groups'}); |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# bring apps in shape |
109
|
2
|
50
|
|
|
|
5
|
$d->{'apps'} = Monitoring::TT::Utils::parse_tags($d->{'apps'}) if $type eq 'hosts'; |
110
|
|
|
|
|
|
|
|
111
|
2
|
|
|
|
|
2
|
push @{$data}, $d; |
|
2
|
|
|
|
|
9
|
|
112
|
|
|
|
|
|
|
} |
113
|
2
|
|
|
|
|
9
|
close($fh); |
114
|
2
|
|
|
|
|
2
|
debug("read ".(scalar @{$data})." $type from $file"); |
|
2
|
|
|
|
|
8
|
|
115
|
|
|
|
|
|
|
} |
116
|
1
|
|
|
|
|
3
|
return $data; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
##################################################################### |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head1 AUTHOR |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Sven Nierlein, 2013, |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=cut |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
1; |