I'm wrapping up the final bit of work on a plugin that reads in a CSV formatted file, aggregates matching sets of results from a certain column together, and then runs calculations on the coinciding values for each set. I have this currently working fine but am at a bit of a loss as to how to save and present the data. In particular the challenge is that this is designed for implementation as a Nagios monitoring plugin. In the other plugins I have written (mind you I am a rookie), I have directly referenced the values. I am a bit flustered establishing how to save the output of a foreach loop to a data structure and then re-referencing it in a scalable dynamic way? (the set(s) that the calculations are run against will be differ in both count as well as name.

The script looks like this presently (again I am a rookie, so any advice on streamlining would be appreciated). My primary question regards saving the output of the foreach loop at the end and then re-referencing it. I have also included the syntax that I am used to for returning Nagios friendly results (InfoData and PerfData). The big picture is to access the values of the foreach in this section.

My script:

# Calculations to be run on the array values 181 sub average { 182 @_ == 1 or die ('Sub usage: $average = average(\@array);'); 183 my ($array_ref) = @_; 184 my $sum; 185 my $count = scalar @$array_ref; 186 foreach (@$array_ref) { $sum += $_; } 187 return $sum / $count; 188 } 189 190 sub median { 191 @_ == 1 or die ('Sub usage: $median = median(\@array);'); 192 my ($array_ref) = @_; 193 my $count = scalar @$array_ref; 194 my @array = sort { $a <=> $b } @$array_ref; 195 if ($count % 2) { 196 return $array[int($count/2)]; 197 } else { 198 return ($array[$count/2] + $array[$count/2 - 1]) / 2; 199 } 200 } 201 202 sub min { 203 @_ == 1 or die ('Sub usage: $min = min(\@array);'); 204 my ($array_ref) = @_; 205 my @array = sort { $a <=> $b } @$array_ref; 206 return $array[0]; 207 } 208 209 sub max { 210 @_ == 1 or die ('Sub usage: $max = max(\@array);'); 211 my ($array_ref) = @_; 212 my @array = sort { $a <=> $b } @$array_ref; 213 return $array[-1]; 214 } 215 216 sub samplecount { 217 @_ == 1 or die ('Sub usage: $sample_count = samplecount(\@array);' +); 218 my ($array_ref) = @_; 219 my $count = scalar @$array_ref; 220 return $count; 221 } 222 223 224 #Modification for Jenkins syncronized metrics 225 226 my %seen = (); 227 228 open (FH, "< $resultsFile"); 229 my @lines = <FH>; 230 foreach my $line (@lines) { 231 my @values = split ',', $line; 232 233 my $col1_val = $values[1]; 234 my $key = $values[2]; 235 my $errors = $values[7]; 236 237 $key =~ s/LOGGED IN //; 238 $key =~ s/ GET //; 239 $key =~ s/ POST //; 240 241 $seen{$key}->{error_count}++ if $errors eq 'false'; 242 push @{$seen{$key}->{col1_vals}}, $col1_val; 243 244 } 245 246 foreach my $key (keys %seen) { 247 my $sample_count = samplecount($seen{$key}->{col1_vals}); 248 my $error_count = ((($seen{$key}->{error_count})/($sample_coun +t))*100); 249 my $median = median($seen{$key}->{col1_vals}); 250 my $average = average($seen{$key}->{col1_vals}); 251 my $minimum = min($seen{$key}->{col1_vals}); 252 my $maximum = max($seen{$key}->{col1_vals}); 253 print <<HERE; 254 Key: $key 255 Samples: $sample_count 256 ErrorPercent: $error_count 257 Median: $median 258 Average: $average 259 Minimum: $minimum 260 Maximum: $maximum 261 HERE 262 } 263 264 print Dumper \%seen; 265 266 close (FH);

Nagios Output Sample:

691 $InfoData = sprintf ("$targetname Avg.Time= $o_target"); 692 $PerfData = sprintf ("value1=%f value2=%f value3=%f",($value1),( +$value2),($value3)); 693 }

Thanks always for your assistance, insights, and wisdom monks!

-bp-


In reply to Returning foreach results in a Nagios-Friendly Fashion by bpthatsme

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.