[Perl] Geen INSERT en UPDATES

Pagina: 1
Acties:
  • 35 views sinds 30-01-2008

  • urk_forever
  • Registratie: Juni 2001
  • Laatst online: 27-08 16:32
Hallo, ik heb de volgende code gemaakt:

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!


  • whoami
  • Registratie: December 2000
  • Laatst online: 19:35
Tja, misschien moet je het geval maar eens debuggen en kijken of hij wel aan die queries raakt...

https://fgheysels.github.io/


  • urk_forever
  • Registratie: Juni 2001
  • Laatst online: 27-08 16:32
Hier werkt ie niet op:
next if $rightnow > $line[0];

Hail to the king baby!


  • Janoz
  • Registratie: Oktober 2000
  • Laatst online: 12:00

Janoz

Moderator Devschuur®

!litemod

Grapjas... Zelf overgetikt zeker : http://www.aplawrence.com/Unix/squidlog.html

We leveren hier geen support op scripts van 3en.. Vraag maar bij hun (zie ook verschillende stickies bovenaan in /14)

Ken Thompson's famous line from V6 UNIX is equaly applicable to this post:
'You are not expected to understand this'


Dit topic is gesloten.