Ребята у меня это делается 2 скриптами:
1)
#!/bin/sh
. /usr/local/stat/etc/stat.conf
ym=`date '+%Y%m'`${ipacc} c
${ipacc} s a c f $filter >> $last
case $1 in
'to_billing')
$quant -c $collector_id -q $quant_size -t ${ym}Details $last > ${last}.sql
( echo "CREATE TABLE IF NOT EXISTS ${ym}Details (
collector tinyint unsigned not null,
id int unsigned not null default '0',
source int unsigned not null,
dest int unsigned not null,
type tinyint unsigned not null default '0',
bytes int unsigned not null,
time int unsigned not null,
new tinyint unsigned not null default '1',
PRIMARY KEY (collector,source,dest,bytes,time),
KEY (collector),
KEY (source),
KEY (dest),
KEY (time),
KEY (type),
KEY (id)
) TYPE=MyISAM;" ;
cat ${last}.sql ) | $mysql_program --host=${mysql_host} --port=${mysql_port} \
--user=${mysql_user} --password="${mysql_password}" ${stat_db}
if [ $? -eq 0 ]; then
rm ${last} ${last}.sql
$good
else
$bad
fi;
;;
*)
;;
esac
2)
#!/usr/bin/perl
$collector=1;
$quant_size=100;
$action="INSERT IGNORE";
$table_name=sprintf('%02d%02dDetails',(localtime)[5]+1900,(localtime)[4]+1);
foreach $name (split('\/',$0)) { $basename=$name };
$i=0;
foreach $arg (@ARGV) {
if ($arg=~/-/) {
$param=$arg;
$i++;
if ($param eq "-r") { $i=0; $action="REPLACE"; }
}
else {
if ($i>0) {
$table_name=$arg if ($param eq "-t");
$quant_size=$arg if ($param eq "-q");
$collector=$arg if ($param eq "-c");
}
else {
$file=$arg;
}
$i=0;
}
}
if (!$file) {
print "Usage: $basename [options] file\n";
print "Options:\n\t-c - collector id (default=$collector)\n\t-q - quant size (default=$quant_size)\n\t-t - table name (default=$table_name)\n\t-r - sql: replace (default=insert ignore)\n\n";
exit 1;
}
open (STAT,"<$file") || die "Can't open file: $!\n";
$i=1;
$entries=0;
while (chomp($_=<STAT>)) {
next if !($_=~'^[0-9 \t]+$');
($src,$dst,$p,$bytes,$time)=split;
$entries++;
if ($i==1 || $quant_size==1) {
print ("$action INTO $table_name (collector,source,dest,bytes,time) VALUES ($collector,$src,$dst,$bytes,$time)");
$i++;
next;
}
print (",($collector,$src,$dst,$bytes,$time)") if ($i<=$quant_size);
if ($i==$quant_size) { printf (";\n"); $i=1; next; }
$i++;
}
print ";" if ($quant_size!=1);
print "INSERT INTO log.UploadLog VALUES (unix_timestamp(),$collector,$entries);";
close STAT;
+ переделанный ipacc под 4.11 c добавлением unix_timestamp первого пакета, выводом ip в виде inet_ntoa и возвращением условленного кода ошибки при вызове ipacc s s в случае 85% заполнения таблицы. Для последнего по крону раз в 1 минуту:
#!/bin/sh
$ipacc s s > /dev/null
if [ $? -eq 10 ]; then
$statsave
fi
Итого у меня стоит пара-тройка таких считалок, что по сети (через stunnel) заливают статистику на центральную биллинговую машинку. А там работает простенький скрипт на основе запросов, что разделяет трафики на типы в зависимости от адресов получателя-отправителя и подводит итоги в другой табличке уже по моим ip.
ЗЫ: скрипт квант вставляет делает по несколько сотен строк в один insert, что значительно ускоряет процесс :)