Может кому надо ...
Доброго времени суток comp,
Если кому-то надо считать трафик национальный и зарубежный ....
Обновление диапазона адресов:
#!/usr/local/bin/php
<?
error_reporting (E_PARSE+E_ERROR+E_WARNING);
#$INFILE="http://www.dg.net.ua/noc/ua-list.txt";
$INFILE="http://noc.ix.net.ua/ua-list.txt";
$dblocation ="localhost";
$dbname ="squid";
$dbuser="имя пользователя";
$dbpasswd="пароль пользователя";
$dbcnx=@mysql_connect($dblocation,$dbuser,$dbpasswd);
if ($dbcnx){
if (@mysql_select_db($dbname,$dbcnx)){
$file = fopen ($INFILE, "r");
if ($file) {
$ix = array();
/* Read from http://noc.ix.net.ua/ua-list.txt and put IP-Addresses
to array $ix */
while (!feof ($file)) {
$line = fgets ($file, 1024);
if (preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/i",$line))
{
array_push($ix,trim($line));
}
}
fclose($file);
}else{/* If can't open remote file with IP-List*/
echo "Unable to open remote file ".$INFILE."\n";
exit;
}
}else{/* If can't select database on MySQL-server */
echo "Unable to select databane ".$dbname.".\n";
exit;
}
}else{/* If can't connect to MySQL-server */
echo "Unable connect to MySQL server ".$dblocation."\n";
exit;
}
$ip = mysql_query("select ipaddr from uaix");
if (!$ip){
@mysql_close($dbcnx);
echo "Unable get list of IPADDR. \n";
exit;
}
/* Get IPADDR from uaix */
$ip_db = array();
while($ip_tbl = mysql_fetch_array($ip)){
array_push($ip_db, trim($ip_tbl['ipaddr']));
}
# search for NEW ipaddr
$diff=array_diff($ix,$ip_db);
#search for "NEED DELETE IPADDR"
$diff_old=array_diff($ip_db,$ix);
if (count($ix)>1) {
// Delete "NEED FOR DELETE IPADDR"
foreach($diff_old as $val){
mysql_query("delete from uaix where ipaddr='".trim($val)."'");
echo "DEL ".$val."\n";
}
//Insert NEW IPADDR
foreach($diff as $val){
echo "ADD ".$val."\n";
mysql_query("insert into uaix(ipaddr,flag) values('".trim($val)."',1)");
}
}
mysql_close($dbcnx);
Скрипт для запуска Squid'a
#!/bin/sh
case "$1" in
start)
/usr/local/sbin/squid2mysql < /var/log/squid/access.log &
# rm /var/log/squid/squid.pid
/usr/local/sbin/squid -f /usr/local/etc/squid/squid.conf
echo "Squid starting\n"
;;
stop)
/usr/local/sbin/squid -k shutdown
echo "Squid stoping\n"
sleep 30
;;
*)
;;
esac
exit 0
Скрипт для архивирования БД в конце месяца ...
#!/usr/bin/perl
use DBI;
use DBI::DBD;
my $mysqluser="имя пользователя";
my $mysqlpass="пароль пользователя";
my $mysqlserv="localhost";
my $mysqlport="3306";
my $mysqldbas="kan_squid";
my $from_tbl="protocol";
if (`date -v+1d +%d` == 01 ){
$squidtable=$mysqldbas.`date +"%Y%m"`;
$dbh=DBI->connect("DBI:mysql:database=$mysqldbas;host=$mysqlserv;port=$mysqlport",$mysqluser,$mysqlpass,{AutoCommit=>1,RaiseError=>0,PrintError=>1})||&errorcon;
$dbh->do("CREATE TABLE IF NOT EXISTS $squidtable (datework date default
NULL, time time default NULL, code tinyint(3) unsigned NOT NULL default '0',
status decimal(3,0) default NULL, bytes double default NULL, url char(255) default
NULL, userident char(15) default NULL,host char(15) default NULL, ua tinyint(1)
NOT NULL default '0')");
$dbh->do("INSERT INTO $squidtable SELECT * FROM $from_tbl");
$dbh->do("TRUNCATE TABLE $from_tbl");
$dbh->disconnect;
};
В атаче структура базы...
crontab -l
50 23 * * * /path/to/file/squid_clear_proto # архивирование
10 */1 * * * /path/to/file/parse_ix-ua.php # обновление национального диапазона
--
С наилучшими пожеланиями,
Колесник Андрей
mailto:kolesn***@e*****.ua,
ICQ: 208861095