Thanks for everyone who made recommendations and tried to help, I tried the
various suggestions and I still get the same results. Maybe I should have
included more example code to show how I built the initial arrayreference.
So here goes.
The idea behind this is my website www.securitysaint.com which monitors
other websites. Each week I want to generate a report and send it to the
users.
#!/usr/local/bin/perl -w
use strict;
use DBI;
use Mail::Mailer qw(qmail);
my $dbh =
DBI->connect("DBI:mysql:database=saint;host=localhost","username","password"
, {'RaiseError' => 1});
my $current_date = getdate();
print "$current_date\n";
my $url_id_list = get_url_id_list();
foreach my $url_id(@$url_id_list) {
$url_id = @$url_id[0];
my $config_details = get_config_details($url_id);
my $chk_freq = @$config_details[0]->[0];
my $url_timeout = @$config_details[0]->[1];
my $url = @$config_details[0]->[2];
my $url_protocol = @$config_details[0]->[3];
my $mc_email = @$config_details[0]->[4];
my $full_detail = get_full_detail($url_id);
#foreach my $full_detail(@$full_detail) { # Just testing here to make
sure it's the real thing and it is
# print
"@$full_detail[0],@$full_detail[1],@$full_detail[2],@$full_detail[3]\n";
#}
my $i;
my @day;
for($i = 1; $i <= 7; $i++) {
$day[$i] = getdate($i);
}
my $durations = get_durations(\@day);
my $avg_resp_time = get_avg_resp_time($durations, $url_id);
# foreach my $avg_resp(@$avg_resp_time) { # Just testing here again to
make sure it's the real thing
# print "AVG = $avg_resp\n";
# }
my $sum_errors = get_sum_errors($durations, $url_id);
# foreach my $sum_error(@$sum_errors) { # Just testing here again, I am
compulsive
# print "ERROR COUNT = $sum_error\n";
# }
my $sum_checks = get_sum_checks($durations, $url_id);
# foreach my $sum_check(@$sum_checks) { #Just testing again, ok so I have
a problem with testing things
# print "SUM CHECK = $sum_check\n";
# }
my $pct_up_time = get_pct_up_time($sum_checks, $sum_errors);
# foreach my $pct_up_time(@$pct_up_time) { # Last test, before I seek
12-step help
# print "% Uptime = $pct_up_time\n";
# }
print "------------\n";
my $email_vars = { chk_freq => $chk_freq,
url_timeout => $url_timeout,
url => $url,
url_protocol => $url_protocol,
mc_email => $mc_email,
full_detail => $full_detail,
avg_resp_time => [ qw(@$avg_resp_time[0]
@$avg_resp_time[1]
@$avg_resp_time[2]
@$avg_resp_time[3]
@$avg_resp_time[4]
@$avg_resp_time[5]
@$avg_resp_time[6]) ],
sum_errors => $sum_errors,
pct_up_time => $pct_up_time,
};
# Below is just some testing to make sure I am getting the variables I think
I am before parsing w/HTML::Template
# Ok I relapsed and tested again.
print "DEBUG TEST @$avg_resp_time[0]\n";
print "URL: $email_vars->{url_protocol}://$email_vars->{url}\n";
print "CHK FREQ: $email_vars->{chk_freq}\n";
print "TIMEOUT: $email_vars->{url_timeout}\n";
print "EMAIL: $email_vars->{mc_email}\n";
print "AVG RESP:\n";
print "DAY1: $email_vars->{avg_resp_time}[0]\n";
# End of test
}
exit();
sub getdate($days) {
my $days = $_[0];
if(! $days) { $days = 0; };
my ($day, $month, $year) = (localtime (time - $days * 86400))[3,4,5];
my $date = sprintf("%04d-%02d-%02d", $year+1900, $month+1, $day);
return($date);
}
sub get_url_id_list {
my $select_query = "SELECT url_id FROM monitor_check WHERE active = 1";
my $url_id_list = $dbh->selectall_arrayref($select_query)
or die "Can't execute statement: $DBI::errstr";
return($url_id_list);
}
sub get_config_details($url_id) {
my $url_id = $_[0];
my $select_query = "SELECT monitor_check.chk_freq,
monitor_check.url_timeout,
monitor_url.url, monitor_url.url_protocol,
monitor_customers.mc_email
FROM monitor_url
LEFT OUTER JOIN monitor_customers
ON monitor_url.mc_id =
monitor_customers.mc_primary_key
LEFT OUTER JOIN monitor_check
ON monitor_url.url_id = monitor_check.url_id
WHERE monitor_url.url_id = ?";
my $config_details = $dbh->selectall_arrayref($select_query, undef,
$url_id)
or die "Can't execute statement: $DBI::errstr";
return($config_details);
}
sub get_full_detail($url_id) {
my $url_id = $_[0];
my $select_query = "SELECT monitor_report.toc_epoch,
monitor_report.chk_status,
monitor_report.resp_sec,
monitor_response_codes.mnemonic
FROM monitor_report
LEFT JOIN monitor_response_codes
ON monitor_report.chk_status =
monitor_response_codes.code
WHERE TO_DAYS(NOW()) -
TO_DAYS(monitor_report.toc_epoch) <= 7
AND monitor_report.url_id = ?";
my $full_detail = $dbh->selectall_arrayref($select_query, undef, $url_id)
or die "Can't execute statement: $DBI::errstr";
return($full_detail);
}
sub get_durations($days) {
my $day_array = $_[0];
my $now = $current_date;
my $day1 = @$day_array[1];
my $day2 = @$day_array[2];
my $day3 = @$day_array[3];
my $day4 = @$day_array[4];
my $day5 = @$day_array[5];
my $day6 = @$day_array[6];
my $day7 = @$day_array[7];
my @durations = ( [$now, $day1], [$day1, $day2], [$day2, $day3], [$day3,
$day4],
[$day4, $day5], [$day5, $day6], [$day6, $day7] );
return(\@durations);
}
sub get_avg_resp_time($durations,$url_id) {
my $durations = $_[0];
my $url_id = $_[1];
my @avg_resp_time;
foreach my $duration(@$durations) {
my @bind_values = (@$duration[1], @$duration[0], $url_id);
my $select_query = "SELECT AVG(monitor_report.resp_sec)
FROM monitor_report
WHERE monitor_report.toc_epoch between ? AND ? AND
monitor_report.url_id = ?";
my $avg_resp_time = $dbh->selectrow_arrayref($select_query, undef,
@bind_values)
or die "Can't execute statement: $DBI::errstr";
push(@avg_resp_time, @$avg_resp_time);
}
return(\@avg_resp_time);
}
sub get_sum_errors($durations, $url_id) {
my $durations = $_[0];
my $url_id = $_[1];
my @sum_errors;
foreach my $duration(@$durations) {
my @bind_values = (@$duration[1], @$duration[0], $url_id);
my $select_query = "SELECT COUNT(*) FROM monitor_report
WHERE (monitor_report.toc_epoch between ? AND ? AND
monitor_report.url_id = ?)
AND chk_status > 202";
my $sum_error = $dbh->selectrow_arrayref($select_query, undef,
@bind_values)
or die "Can't execute statement: $DBI::errstr";
push(@sum_errors, @$sum_error);
}
return(\@sum_errors);
}
sub get_sum_checks($durations, $url_id) {
my $durations = $_[0];
my $url_id = $_[1];
my @sum_checks;
foreach my $duration(@$durations) {
my @bind_values = (@$duration[1], @$duration[0], $url_id);
my $select_query = "SELECT COUNT(*) FROM monitor_report
WHERE (monitor_report.toc_epoch between ? AND ? AND
monitor_report.url_id = ?)";
my $sum_check = $dbh->selectrow_arrayref($select_query, undef,
@bind_values)
or die "Can't execute statement: $DBI::errstr";
push(@sum_checks, @$sum_check);
}
return(\@sum_checks);
}
sub get_pct_up_time($sum_checks, $sum_errors) {
my $sum_checks = $_[0];
my $sum_errors = $_[1];
my @pct_up_time;
for(my $i = 0; $i <= 6; $i++) {
if(@$sum_checks[$i] == 0) {next;};
my $sum_good = @$sum_checks[$i] - @$sum_errors[$i];
$pct_up_time[$i] = (($sum_good * 100) / @$sum_checks[$i]);
}
return(\@pct_up_time);
}
----- Original Message -----
From: "Timothy Johnson" <[EMAIL PROTECTED]>
To: "'Zachary Buckholz'" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Sent: Wednesday, July 10, 2002 1:28 AM
Subject: RE: help dereferencing arrayref so I can put the value into a hash
>
> First of all, don't forget to 'use strict;' at the top of your script.
>
> But here's a couple of scenarios. The last two are what you are looking
> for. Always remember to put curly braces around the reference when you
are
> dereferencing, and you won't run into this problem. Below are some common
> scenarios that people put to Perl, and what it sees. Hopefully it helps
you
> to see why your original attempts were incorrect.
>
> ###########################################
>
> You type:
> "@$avg_resp_time[0]" or "@{$avg_resp_time[0]}"
> Perl sees:
> @avg_resp_time is an array, whose first element is an array reference.
> Dereference it.
>
> You type:
> @$avg_resp_time->[0]
> Perl sees:
> $avg_resp_time is an array reference. Dereference it and find the first
> element, which is also an array reference. Dereference it.
>
> You type:
> @{$avg_resp_time}[0]
> Perl sees:
> $avg_resp_time is an array reference. Dereference it, and then get a
hash
> slice that has only the first element.
>
> You type:
> ${$avg_resp_time}[0]
> Perl sees:
> $avg_resp_time is an array reference. Get the first element of the
> dereferenced array.
>
> You type:
> $avg_resp_time->[0]
> Perl sees:
> $avg_resp_time is an array reference. Get the first element of the
> dereferenced array.
>
> ##############################################
>
> -----Original Message-----
> From: Zachary Buckholz [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, July 09, 2002 11:31 PM
> To: [EMAIL PROTECTED]
> Subject: help dereferencing arrayref so I can put the value into a hash
>
>
> I understand how to use a foreach on a reference to an array as follows:
>
> my $avg_resp_time = get_avg_resp_time($durations, $url_id);
> foreach my $avg_resp(@$avg_resp_time) {
> print "AVG = $avg_resp\n";
> }
>
> But how do I directly access one array value from the reference to the
> array?
>
> print "DEBUG TEST @$avg_resp_time[0]\n"; Fails
> print "DEBUG TEST @$avg_resp_time->[0]\n"; Fails
> print "DEBUG TEST @{$avg_resp_time[0]}\n"; Fails
>
>
> I have bookmarked ch04_07.htm of the Perl CD "Data Structure Code
Examples"
> but I just don't understand it.
>
> I am trying to setup a hash for use in HTML::Template as follows
>
> my $email_vars = { chk_freq => $chk_freq,
> url_timeout => $url_timeout,
> url => $url,
> url_protocol => $url_protocol,
> mc_email => $mc_email,
> full_detail => $full_detail,
> avg_resp_time => [ qw(@$avg_resp_time[0]
> @$avg_resp_time[1]
> @$avg_resp_time[2]
> @$avg_resp_time[3]
> @$avg_resp_time[4]
> @$avg_resp_time[5]
>
@$avg_resp_time[6]) ],
> sum_errors => $sum_errors,
> pct_up_time => $pct_up_time,
> }
> Thanks
> zack
>
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]