Hallo, ik heb de volgende code gemaakt:
Alleen als ik dit draai werkt alles goed, alleen insert of update ie niks, waarom is dit??
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
| #!/usr/bin/perl -w
# use strict;
use DBI();
$log_file = '/scripts/access.log';
#$output = '/scripts/output.log';
$www_files = 0;
$files_cached = 0;
($null,$minute,$hour,$day,$month,$year,$null,$null,$null) = localtime(time);
$year = $year + 1900;
$month = $month + 1;
$insert_count = 0;
$update_count = 0;
my $dbh = DBI->connect("DBI:mysql:database=squidlog;host=localhost","root","***", {'RaiseError' => 1});
open (SQUIDLOG,$log_file) or die "Can't open $log_file: $!";
while (<SQUIDLOG>) {
$internal_ip="";
$linkvisited="";
$sitevisited="";
$linkdate="";
$human_date="";
$minute="";
$hour="";
$tijd="";
$cached_line="";
$rightnow=time();
$rightnow -= (3600 * 24 * 7);
$rec_count = 0;
$logfile_line=$_;
$logfile_line =~ s/ /\+/g;
next if ! $logfile_line ;
@line = split /\+/, $logfile_line;
next if $rightnow > $line[0];
foreach $tempvar (@line) {
if (($tempvar =~ m/\d+\.\d+/) and ( ! $linkdate) ) {
# This is the date stamp; 982962881.790
($null, $minute, $hour, $day, $month, $year, $null, $null, $null)= localtime($tempvar);
$month++;
$year=$year+1900;
if ($minute < 10) { $minute = '0'.$minute;}
if ($month < 10) { $month = '0'.$month;}
if ($day < 10) { $day = '0'.$day;}
$logday="${year}-${month}-${day}";
#$accumdate{"$logday"} += 1;
$linkdate=$tempvar;
$human_date=$logday;
$tijd = $hour.":".$minute;
}
if (($tempvar =~ m/192\.168\./) and (! $internal_ip) ) {
$ip = $tempvar;
#$accumip{$ip} += 1;
$internal_ip=$ip;
}
if (($tempvar =~ m/http:\/\//) and (! $linkvisited) )
{
$document = $tempvar;
$document =~ s/.*http:..//;
$document =~ s/\/.*//;
$linkvisited=$document;
@comp=split(/\./,$document);
$cnt2=@comp;
$document="$comp[$cnt2 - 2].$comp[$cnt2 - 1]";
$document=$linkvisited if ($document =~ /^\d+\.\d+$/ );
$sitevisited=$document;
#$accumurl{"$document"} +=1;
}
if (($tempvar =~ m/TCP/) and (!$cached_line) )
{
$cache = $tempvar;
if ($cache =~ m/HIT/) {
$files_cached++;
}
if ($cache =~ m/MISS/) {
$www_files++;
}
}
}
$human_date="unknown" if not $human_date;
$linkvisited="unknown" if not $linkvisited;
$internal_ip="unknown" if not $internal_ip;
$sitevisted="unknown" if not $sitevisted;
$minute="unknown" if not $minute;
$hour="unknown" if not $hour;
my $sth = $dbh->prepare("SELECT * FROM logdata_1 WHERE ip_adres = '$internal_ip'");
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()) {
$rec_count++;
}
if ($rec_count == 0) {
$dbh->do("INSERT INTO logdata_1 VALUES ('','$human_date','$tijd','$internal_ip','$linkvisited','1')");
$insert_count++;
}
else {
$dbh->do("UPDATE logdata_1 SET last_visit_date='$human_date', last_visit_time='$tijd', visit_count=visit_count+1 WHERE ip_adres='$internal_ip' AND website='$linkvisited'");
$update_count++;
}
}
print "All done!\n";
print "$insert_count records inserted\n";
print "$update_count records updated\n";
$dbh->disconnect(); |
Alleen als ik dit draai werkt alles goed, alleen insert of update ie niks, waarom is dit??
Hail to the king baby!