my (undef,undef,@fields) = split(/\,/,shift @rows);
my %stats = ();
for my $r(@rows){
my ($pxname,$svname,@values) = split /\,/,$r;
for my $f(@fields){
$stats{$pxname}{$svname}{$f} = shift @values;
}
}
I will explain a bit of the scope for those interested. This is designed to be a plugin that will rip apart a Proxy status page for use as a Nagios plugin.
At this time I am working through the last bit of pulling various keys from the associative array in order to allow for CLI metric availability on demand. This is in conjunction with the standards utilized with Nagios' API.
<p>Sample HTML to use is as follows:</p>
<body>
<p><h2>Profile</h2>
<table cellspacing='10'>
<th>Function Name</th><th>Calls</th><th>Total Time</th><th>Avg. Ti
+me</th>
<tr><td>some.function</td><td>0</td><td>0</td><td>0</td></tr>
</table>
</body>
<p><h2>Cache Status</h2>
0 items cached, 0 cache hits, 0 cache misses (0.0%)
I was able to clean this up to meet our purposes of a CSV style file utilizing the following series of regular expressions. This is not clean, but I am also new to perl, and the final product meets my requirements.
$htmlcontent =~ s/ //g; #remove tabs
$htmlcontent =~ s/<[^>]*>/,/g; #convert html tags into commas
$htmlcontent =~ s/,,/,/g; #Convert double commas to single
$htmlcontent =~ s/,\n,/\n/g; #remove leading and trailing commas aroun
+d newlines
$htmlcontent =~ s/,Function Name,Calls,Total Time,Avg\. Time/Function
+Name,Calls,Total Time,Avg\. Time/g; #Clean up column headers
$htmlcontent =~ s/,Cache Status,\n//; #nuke the cache status line
$htmlcontent =~ s/Profile\n//; #nuke the profile line
$htmlcontent =~ s/\n+/\n/g; #delete blank lines
$htmlcontent =~ s/,\n//g; #nuke lines with a comma followed by a new l
+ine
$htmlcontent =~ s/^(?:.*\n){0,1}//; #remove first line
$htmlcontent =~ s/,$//g; #nuke that pesky last line
The code segment in question, in it's current state, appears as such:
my $stats = $htmlcontent;
@rows = split(/\n/,$stats);
#Debug Line - prints the cleaned up data after dumping it to an array
#foreach (@rows) {
# print "ROW DATA : $_\n";
#}
#Make sure 'good' data is pulled
if ( $rows[0] !~ m/Function Name/ ) {
$np->nagios_exit("UNKNOWN", "Can't find csv header!\n");
exit $ERRORS{"UNKNOWN"}
}
my (undef,undef,@fields) = split(/\,/,shift @rows);
my %stats = ();
for my $r(@rows){
my ($pxname,$svname,@values) = split /\,/,$r;
for my $f(@fields){
$stats{$pxname}{$svname}{$f} = shift @values;
}
}
At this time I am working through accessing the various function names in the associative array by assigning a variable to the 'Function' column, and then using a regex match in order to "search" for it. Any suggestions are of course welcome as I am still a perl newbie.
Thanks again to everyone who chimed in and provided the fantastic suggestions provided here. I look forward to being around more often as I work through more plugins
Best!
bp |