MachineInfo/SimpleMetric.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 114 CC 13 SCAR 31.1
CC
13
Cov
0%
get_via_cmd
56.0
get_via_file
20.0
get
12.0
CRAP 22.3
1 =head1 NAME
2  
3 Probe::MachineInfo::SimpleMetric - Simple Metric Collector
4  
5 =head1 SYNOPSIS
6  
7 blah
8  
9 =head1 DESCRIPTION
10  
11 blah
12  
13 =head1 PUBLIC INTERFACE
14  
15 =cut
16  
17 package Probe::MachineInfo::SimpleMetric;
18  
19  
20 # pragmata
21 use base qw(Probe::MachineInfo::Metric);
22 use strict;
23 use warnings;
24  
25  
26 # Standard Perl Library and CPAN modules
27 use English;
28 use Proc::Reliable;
29  
30 #
31 # CLASS ATTRIBUTES
32 #
33  
34 #
35 # CONSTRUCTOR
36 #
37  
38  
39 =head2 get
40  
41 get()
42  
43 =cut
44  
45 sub get {
46 my($self) = @_;
47  
48 return $self->get_via_cmd() if($self->command());
49 return $self->get_via_file() if($self->filename());
50  
51 }
52  
53 =head2 get_via_cmd
54  
55 get_via_cmd()
56  
57 =cut
58  
59 sub get_via_cmd {
60 my ($self) = @_;
61  
62 my $cmd = $self->command();
63 return unless $cmd;
64  
65 my($executable) = $cmd =~ m/^(\S+)/;
66  
67 if(not -r $executable) {
68 $self->log->warn("$executable is not readable\n");
69 return;
70 }
71 elsif(not -x _) {
72 $self->log->warn("$executable is not executable\n");
73 return;
74 }
75  
76 my $proc = Proc::Reliable->new();
77  
78 my $output = $proc->run($cmd);
79  
80 if($proc->status()) {
81 my $err = $proc->stderr();
82 $self->log->warn("$cmd exited with non zero exit status $err\n");
83 return;
84 }
85  
86 unless($output) {
87 $self->log->warn("$cmd returned no output\n");
88 return;
89 }
90  
91 if($self->linenumber()) {
92 my $n = $self->linenumber();
93 my @lines = split("\n", $output);
94 $output = $lines[$n];
95 }
96  
97 return $self->filter_through_regex($output);
98 }
99  
100 =head2 get_via_file
101  
102 get_via_file()
103  
104 =cut
105  
106 sub get_via_file {
107 my($self) = @_;
108  
109 my $file = $self->filename();
110 return unless $file;
111  
112 if(! -r $file) {
113 $self->log->warn("$file is not readable\n");
114 return;
115 }
116  
117 open (my $fh, '<', $file) or return;
118 my @contents = <$fh>;
119 close $fh;
120  
121 if(my $n = $self->linenumber()) {
122 return->filter_through_regex($contents[$n])
123 }
124 else {
125 return->filter_through_regex(join("\n", @contents))
126 }
127 }
128  
129 =head2 filter_through_regex
130  
131 filter_through_regex($value)
132  
133 =cut
134  
135 sub filter_through_regex {
136 my($self, $value) = @_;
137  
138  
139 chomp $value;
140  
141 if(my $regex = $self->regex()) {
142 my($value) = $value =~ m/$regex/;
143 return $value;
144 }
145  
146 return $value;
147 }
148  
149 =head2 command
150  
151 command()
152  
153 =cut
154  
155 sub command {
156 my($self) = @_;
157  
158 return;
159 }
160  
161  
162 =head2 linenumber
163  
164 linenumber()
165  
166 =cut
167  
168 sub linenumber {
169 my($self) = @_;
170  
171 return;
172 }
173  
174 =head2 regex
175  
176 regex()
177  
178 =cut
179  
180 sub regex {
181 my($self) = @_;
182  
183 return;
184 }
185  
186 1;
187  
188 =head1 AUTHOR
189  
190 Sagar R. Shah
191  
192 =cut