]> git.entuzijast.net Git - freeradius-dialup-admin.git/commitdiff
In log_badlogins create a separate sql input file for each sql server and append...
authorkkalev <kkalev>
Wed, 1 Jan 2003 23:30:29 +0000 (23:30 +0000)
committerkkalev <kkalev>
Wed, 1 Jan 2003 23:30:29 +0000 (23:30 +0000)
sql command succeeds we delete the corresponding input file. That way if an sql server is down we store the
accounting info in the input file and then send it all when it comes back up.

Changelog
bin/log_badlogins

index ecc8dccdf5f99e8bab864b810d8fb6d0db503815..8456889edadec45f0cf0c2e7e313e83340582210 100644 (file)
--- a/Changelog
+++ b/Changelog
@@ -21,6 +21,9 @@ Ver 1.62:
   on the sql database).
 * Add more error messages when interacting with the SQL database
 * Add sql_connect_timeout and sql_extra_servers configuration directives to be used by the log_badlogins script
+* In log_badlogins create a separate sql input file for each sql server and append sql commands to it. If the
+  sql command succeeds we delete the corresponding input file. That way if an sql server is down we store the
+  accounting info in the input file and then send it all when it comes back up.
 Ver 1.61:
 * Add a string encoder for greek
 * If general_decode_normal_attributes is set then encode attributes in lib/ldap/change_info. In the near future
index 88632cebd0f69caa7bdadfaf5b13a95ffb1455a0..ae7cae187c8afda0e0c27c071317c60916f87799 100755 (executable)
@@ -24,7 +24,7 @@ $all_file=shift||'no';
 #
 $domain='company.com';
 $mysql='/usr/local/mysql/bin/mysql';
-$tmpfile='/tmp/mysql.input';
+$tmpfile='/var/tmp/mysql.input';
 #
 #
 
@@ -48,9 +48,11 @@ close CONF;
 $realm_del = '@' if ($realm_del eq '');
 $realm_for = 'suffix' if ($realm_for eq '');
 $pass = ($sql_password ne '') ? "-p$sql_password" : '';
+die "SQL server not defined\n" if ($sql_server eq '');
 
 $opt = "-O connect_timeout=$sql_timeout" if ($sql_timeout);
-@extras = (split /\s+/,$sql_extra) if ($sql_extra ne '');
+@servers = (split /\s+/,$sql_extra) if ($sql_extra ne '');
+unshift @servers, $sql_server;
 
 open LOG, "<$file"
        or die "Could not open file $file\n";
@@ -93,16 +95,16 @@ for(;;){
                                        ($year,$mon,$mday,$hour,$min,$sec)=UnixDate($date2,'%Y','%m','%d','%H','%M','%S');
                                }
                                $time = "$year-$mon-$mday $hour:$min:$sec";
-                               if (/\[([\w\-]+?)\]\s+\(from (.+?)\)/){
+                               if (/\[([\w\-\.]+?)\]\s+\(from (.+?)\)/){
                                        $user = $1;
                                        ($nas,$port,$caller) = (split /\s+/,$2)[1,3,5];
                                }
-                               elsif (/\[([\w\-]+?)\/.+?\]\s+\(from (.+?)\)/){
+                               elsif (/\[([\w\-\.]+?)\/.+?\]\s+\(from (.+?)\)/){
                                        $user = $1;
                                        ($nas,$port,$caller) = (split /\s+/,$2)[1,3,5];
                                }
                                $caller='' if (!defined($caller));
-                               $user =~s/[^\w\-\.]//g;
+                               $user =~s/[^\w\-\.\d]//g;
                                $nas =~s/[^\w\.]//g;
                                if ($nas ne 'localhost' && $nas !~ /\.$domain$/){
                                        $nas .= ".$domain";
@@ -118,13 +120,16 @@ for(;;){
                                                $user = ($realm_for eq 'suffix') ? $one : $two;
                                        }
                                }
-                               open TMP, ">$tmpfile"
-                                       or die "Could not open temporary file\n";
-                               print TMP "INSERT INTO $sql_accounting_table (UserName,NASIPAddress,NASPortId,AcctStartTime,AcctStopTime,AcctSessionTime,AcctInputOctets,AcctOutputOctets,CallingStationId,AcctTerminateCause) VALUES ('$user','$addr','$port','$time','$time','0','0','0','$caller','$cause');";
-                               close TMP;
-                               `$mysql -h$sql_server $opt -u$sql_username $pass $sql_database <$tmpfile`;
-                               foreach $extra (@extras){
-                                       `$mysql -h$extra $opt -u$sql_username $pass $sql_database <$tmpfile`;
+                               foreach $server (@servers){
+                                       unlink "$tmpfile.$server" if ($delete{$server});
+                                       open TMP, ">>$tmpfile.$server"
+                                               or die "Could not open temporary file\n";
+                                       print TMP "INSERT INTO $sql_accounting_table (UserName,NASIPAddress,NASPortId,AcctStartTime,AcctStopTime,AcctSessionTime,AcctInputOctets,AcctOutputOctets,CallingStationId,AcctTerminateCause) VALUES ('$user','$addr','$port','$time','$time','0','0','0','$caller','$cause');";
+                                       close TMP;
+                                       `$mysql -h$server $opt -u$sql_username $pass $sql_database <$tmpfile.$server`;
+                                       $exit = $? >> 8;
+                                       $delete{$server} = ($exit == 0) ? 1 : 0;
+                                       print "ERROR: SQL query failed for host $server\n" if ($exit != 0);
                                }
                        }
                }