TT/Input/CSV.pm

filter: Statementstmt 100% 63 / 63 Branchbran 80.0% 8 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 97.6% 82 / 84 CC 11 SCAR 15.6
CC
11
Cov
97%
read
8.0
get_types
4.0
BEGIN
1.0
CRAP 4.8
« TT/Object/Host.pm ↑ Summary TT/Utils.pm »
1 package Monitoring::TT::Input::CSV;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Monitoring::TT::Log qw/error warn info debug trace/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Monitoring::TT::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 ▶ my($class, %options) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
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 'montt' => $options{'montt'},
39 };
40 1 bless $self, $class;
41 1 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 ▶ my($self, $folders) = @_;
Subroutine get_types: called CC=4 SCAR=13.9
Pod documented
57 1 my $types = [];
58 1 for my $dir (@{$folders}) {
59 1 for my $type (sort keys %{$self->{'fields'}}) {
60 2 my $pattern = $dir.'/'.$type.'*.csv';
61 2 trace('looking for csv file: '.$pattern);
62 2 my @files = glob($pattern);
63 2 for my $f (@files) {
64 2 debug('found csv file: '.$f);
65 2 push @{$types}, $type;
66 }
67 }
68 }
69 1 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 ▶ my($self, $dir, $type) = @_;
Subroutine read: called CC=8 SCAR=20.8
Pod documented
81 1 my $data = [];
82 1 my $pattern = $dir.'/'.$type.'*.csv';
83 1 my @files = glob($pattern);
84 1 for my $file (@files) {
85 2 info("reading $type from $file");
86 2 ▶ open(my $fh, '<', $file) or die('cannot read '.$file.': '.$!);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die 'cannot read ' . $file . ': ' . $! 0 2 2
87 2 ▶ while(my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 7 2 9
88 7 ▶ next if substr($line, 0, 1) eq '#';
Branch: 1 branch
BranchTrueFalseTotal
if substr($line, 0, 1) eq "#" 2 5 7
89 5 chomp($line);
90 5 ▶ next if $line =~ m/^\s*$/gmx;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/gmx 3 2 5
91 2 my @d = split(/\s*;\s*/mx, $line);
92 2 my $d = {};
93 2 my $x = 0;
94 2 for my $k (@{$self->{'fields'}->{$type}}) {
95 14 $d->{$k} = $d[$x];
96 14 $x++;
97 }
98  
99 2 $d->{'file'} = $file;
100 2 $d->{'line'} = $.;
101  
102 # bring tags in shape
103 2 $d->{'tags'} = Monitoring::TT::Utils::parse_tags($d->{'tags'});
104  
105 # bring groups in shape
106 2 $d->{'groups'} = Monitoring::TT::Utils::parse_groups($d->{'groups'});
107  
108 # bring apps in shape
109 2 ▶ $d->{'apps'} = Monitoring::TT::Utils::parse_tags($d->{'apps'}) if $type eq 'hosts';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "hosts" 2 0 2
110  
111 2 push @{$data}, $d;
112 }
113 2 close($fh);
114 2 debug("read ".(scalar @{$data})." $type from $file");
115 }
116 1 return $data;
117 }
118  
119 #####################################################################
120  
121 =head1 AUTHOR
122  
123 Sven Nierlein, 2013, <sven.nierlein@consol.de>
124  
125 =cut
126  
127 1 1;
« TT/Object/Host.pm ↑ Summary TT/Utils.pm »