[Sat Dec 19 17:36:52 2009] [error] [client 10.46.16.211] , referer:
https://10.46.16.1/stat.pl?pp=T1-435304263WZ5w!wNAfQOPUXgZNEjHiQ&a=101&id=124&a=302
[Sat Dec 19 17:36:52 2009] [error] [client 10.46.16.211] Couldn't connect to 10.46.16.1:14120 : IO::Socket::INET: connect: Connection refused, referer: https
[Sat Dec 19 17:36:52 2009] [error] [client 10.46.16.211] Premature end of script headers: stat.pl, referer:
https://10.46.16.1/stat.pl?pp=T1-435304263W[Sat Dec 19 17:36:52 2009] [error] [client 10.46.16.211] File does not exist: /usr/local/www/stats/data/error, referer:
https://10.46.16.1/stat.pl?pp=TВот такие ошыбки в логах апача при нажатии кнопку обновить. Проблема в соединении на сколько я понял, но мак адрес же отображается правильно когда нажымать пинговать!
Sarping.pl
sub ARP_main{
# так как конфижить всё это добро в конфиге биллинга нельзя, прийдёться это сделать один раз здесь.
my %ARP_config = (
"server" => "10.46.16.1",
"pingport" => "14120",
"updport" => "14121",
"proto" => "tcp",
);
Кусок с настройками.
В чем может быть проблемма?
arp_refresh.pl
#!/usr/bin/perl -w
use POSIX;
use IO::Socket;
use IO::Select;
use Fcntl;
use Tie::RefHash;
use DBI;
my %config = (
"pid" => "/var/run/arp_refresh.pid",
"arp" => "/usr/sbin/arp",
"count" => 5,
"ip" => "10.46.16.1",
"port" => "14121",
"proto" => "tcp",
"MySQL" => {
"sqlfirst" => "DBI:mysql:bill;127.0.0.1",
"sqllogin" => "***",
"sqlpasswd" => "***",
},
"dopfield_id" => 4,
);
open PID, ">".$config{"pid"} or $err=$!;
if ($err==0) {
print PID $$;
close PID;
}
sub trim {
my($string)=@_;
for ($string) {
s/^\s+//;
s/\s+$//;
}
return $string;
}
# handle($socket) deals with all pending requests for $client
sub handle {
# requests are in $ready{$client}
# send output to $outbuffer{$client}
my $client = shift;
my $request;
foreach $request (@{$ready{$client}}) {
# $request is the text of the request
# put text of reply into $outbuffer{$client}
$str = trim($request);
# Do we have ip??
if( $str =~ /^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.[0-9]{1,3}/) {
$first = $1;
$second = $2;
$net = $3;
if($first eq 10 ){
if($second eq 46){
if($net eq 16 ||
$net eq 17 ||
$net eq 18 ||
$net eq 19) {
##################################
undef $dbh;
undef $myst;
# Коннектимся к Мускл
$dbh=DBI->connect($config{"MySQL"}{"sqlfirst"},$config{"MySQL"}{"sqllogin"},$config{"MySQL"}{"sqlpasswd"}) or print "Error $! !\n";
# Запрашиваем МАС
$query="SELECT d.field_value AS mac FROM users u, dopvalues d, (
SELECT u.ip AS ip, MAX(d.revision) AS rev FROM users u, dopvalues d
WHERE u.id=d.parent_id AND d.dopfield_id=".$config{'dopfield_id'}." AND u.ip='".$str."'
GROUP BY ip
) AS tmp
WHERE u.id=d.parent_id
AND d.dopfield_id=".$config{'dopfield_id'}."
AND u.ip=tmp.ip
AND d.revision=tmp.rev
AND TRIM(d.field_value) != ''";
# Готовим запрос
$myst = $dbh->prepare($query);
# Делаем экзекуцию запросу
$myst->execute;
# Выдёргиваем МАС
($mac) = $myst->fetchrow;
# Если вдруг мака нету, генерим какой-то фэйковый
if($mac eq ''){
$mac = sprintf("%x:%x:%x:%x:%x:%x", int(rand(255)), int(rand(255)), int(rand(255)), int(rand(255)), int(rand(255)),int(rand(255)));
$outbuffer{$client} .= "Generated FAKE mac!\n";
}
# Делаем привязку IP <-> MAC
system($config{"arp"}." -s ".$str." ".$mac);
$outbuffer{$client} .= $str." ".$mac."\n";
# Закрываем MySQL соединение
$myst->finish;
$dbh->disconnect;
#####################################
} else {
$outbuffer{$client} .= "Wrong third octet **.**.".$net.".**\n";
}
} else {
$outbuffer{$client} .= "Wrong second octet **.".$second.".**.**\n";
}
} else {
$outbuffer{$client} .= "Wrong first octet ".$first.".**.**.**\n";
}
} else {
print "Garbage from ".$client->peerhost().":".$client->peerport().": ".$str."\n";
}
#################################################
}
delete $ready{$client};
}
# nonblock($socket) puts socket into nonblocking mode
sub nonblock {
my $socket = shift;
my $flags;
$flags = fcntl($socket, F_GETFL, 0)
or die "Can't get flags for socket: $!\n";
fcntl($socket, F_SETFL, $flags | O_NONBLOCK)
or die "Can't make socket nonblocking: $!\n";
}
# Создаём сокет
my $server = IO::Socket::INET->new (
LocalAddr => $config{"ip"},
LocalPort => $config{"port"},
Proto => $config{"proto"},
Listen => 10,
Reuse => 1,
) or die "Can't bind : $@\n";
nonblock($server);
my $select = new IO::Select->new($server);
# begin with empty buffers
%inbuffer = ();
%outbuffer = ();
%ready = ();
tie %ready, 'Tie::RefHash';
while(1){
my $client;
my $rv;
my $data;
# смотрим какая инфа у нас есть?
# может что-то принять, обработать??
foreach $client ($select->can_read(1)) {
if ($client == $server) {
# Принимаем коннект
$client = $server->accept();
$select->add($client);
nonblock($client);
}else{
# читаем данные
$data = '';
$rv = $client->recv($data, POSIX::BUFSIZ, 0);
unless (defined($rv) && length $data) {
# Наверное от клиента ничего больше не будет
delete $inbuffer{$client};
delete $outbuffer{$client};
delete $ready{$client};
$select->remove($client);
close $client;
#print "Client had gone
\n";
next;
}
$inbuffer{$client} .= $data;
# test whether the data in the buffer or the data we
# just read means there is a complete request waiting
# to be fulfilled. If there is, set $ready{$client}
# to the requests waiting to be fulfilled.
while ($inbuffer{$client} =~ s/(.*\n)//) {
push( @{$ready{$client}}, $1 );
}
}
}
# Any complete requests to process?
foreach $client (keys %ready) {
handle($client);
}
# Buffers to flush?
foreach $client ($select->can_write(1)) {
# Skip this client if we have nothing to say
next unless exists $outbuffer{$client};
$rv = $client->send($outbuffer{$client}, 0);
unless (defined $rv) {
# Whine, but move on.
warn "I was told I could write, but I can't.\n";
next;
}
if ($rv == length $outbuffer{$client} || $! == POSIX::EWOULDBLOCK ) {
substr($outbuffer{$client}, 0, $rv) = '';
delete $outbuffer{$client} unless length $outbuffer{$client};
} else {
# Couldn't write all the data, and it wasn't because
# it would have blocked. Shutdown and move on.
delete $inbuffer{$client};
delete $outbuffer{$client};
delete $ready{$client};
$select->remove($client);
close($client);
next;
}
}
# Out of band data?
foreach $client ($select->has_exception(0)) { # arg is timeout
# Deal with out-of-band data here, if you want to.
}
}
exit 0;
arping_serv.pl
#!/usr/bin/perl -w
use POSIX;
use IO::Socket;
use IO::Select;
use Fcntl;
use Tie::RefHash;
my %config = (
"pid" => "/var/run/arping_serv.pid",
"arping" => "/usr/local/sbin/arping",
"count" => 5,
"ip" => "10.46.16.1",
"port" => "14120",
"proto" => "tcp",
"cards" => {
"16" => "fxp0",
"17" => "fxp0",
"18" => "fxp0",
"19" => "fxp0"
},
);
sub trim {
my($string)=@_;
for ($string) {
s/^\s+//;
s/\s+$//;
}
return $string;
}
# handle($socket) deals with all pending requests for $client
sub handle {
# requests are in $ready{$client}
# send output to $outbuffer{$client}
my $client = shift;
my $request;
foreach $request (@{$ready{$client}}) {
# $request is the text of the request
# put text of reply into $outbuffer{$client}
$str = trim($request);
# Do we have ip??
if( $str =~ /^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.[0-9]{1,3}/) {
$first = $1;
$second = $2;
$net = $3;
if($first eq 10 ){
if($second eq 46){
if(exists($config{"cards"}{$net})) {
#print $new_sock "$arping -i$card2x -c$count $ip\n";
foreach(`$config{"arping"} -i$config{"cards"}{$net} -c$config{"count"} $str`){
#print "$_\n";
if(/\w+ bytes from \w+:\w+:\w+:\w+:\w+:\w+ .+/){
($mac) = /\w+ bytes from (\w+:\w+:\w+:\w+:\w+:\w+) .+/;
$outbuffer{$client} .= "$mac\n";
}
}
} else {
$outbuffer{$client} .= "Wrong third octet **.**.".$net.".**\n";
}
} else {
$outbuffer{$client} .= "Wrong second octet **.".$second.".**.**\n";
}
} else {
$outbuffer{$client} .= "Wrong first octet ".$first.".**.**.**\n";
}
} else {
print "Garbage from ".$client->peerhost().":".$client->peerport().": ".$str."\n";
}
#################################################
}
$outbuffer{$client} .= "Operation Completed!\n";
delete $ready{$client};
}
# nonblock($socket) puts socket into nonblocking mode
sub nonblock {
my $socket = shift;
my $flags;
$flags = fcntl($socket, F_GETFL, 0)
or die "Can't get flags for socket: $!\n";
fcntl($socket, F_SETFL, $flags | O_NONBLOCK)
or die "Can't make socket nonblocking: $!\n";
}
open PID, ">".$config{"pid"} or $err=$!;
if ($err==0) {
print PID $$;
close PID;
}
my $server = IO::Socket::INET->new (
LocalAddr => $config{"ip"},
LocalPort => $config{"port"},
Proto => $config{"proto"},
Listen => 10,
Reuse => 1,
) or die "Can't bind : $@\n";
nonblock($server);
my $select = new IO::Select->new($server);
# begin with empty buffers
%inbuffer = ();
%outbuffer = ();
%ready = ();
tie %ready, 'Tie::RefHash';
# Arping Server main code
while(1){
my $client;
my $rv;
my $data;
# смотрим какая инфа у нас есть?
# может что-то принять, обработать??
foreach $client ($select->can_read(1)) {
if ($client == $server) {
# Принимаем коннект
$client = $server->accept();
$select->add($client);
nonblock($client);
}else{
# читаем данные
$data = '';
$rv = $client->recv($data, POSIX::BUFSIZ, 0);
unless (defined($rv) && length $data) {
# Наверное от клиента ничего больше не будет
delete $inbuffer{$client};
delete $outbuffer{$client};
delete $ready{$client};
$select->remove($client);
close $client;
#print "Client had gone
\n";
next;
}
$inbuffer{$client} .= $data;
# test whether the data in the buffer or the data we
# just read means there is a complete request waiting
# to be fulfilled. If there is, set $ready{$client}
# to the requests waiting to be fulfilled.
while ($inbuffer{$client} =~ s/(.*\n)//) {
push( @{$ready{$client}}, $1 );
}
}
}
# Any complete requests to process?
foreach $client (keys %ready) {
handle($client);
}
# Buffers to flush?
foreach $client ($select->can_write(1)) {
# Skip this client if we have nothing to say
next unless exists $outbuffer{$client};
$rv = $client->send($outbuffer{$client}, 0);
unless (defined $rv) {
# Whine, but move on.
warn "I was told I could write, but I can't.\n";
next;
}
if ($rv == length $outbuffer{$client} || $! == POSIX::EWOULDBLOCK ) {
substr($outbuffer{$client}, 0, $rv) = '';
delete $outbuffer{$client} unless length $outbuffer{$client};
} else {
# Couldn't write all the data, and it wasn't because
# it would have blocked. Shutdown and move on.
delete $inbuffer{$client};
delete $outbuffer{$client};
delete $ready{$client};
$select->remove($client);
close($client);
next;
}
}
# Out of band data?
foreach $client ($select->has_exception(0)) { # arg is timeout
# Deal with out-of-band data here, if you want to.
}
}
exit 0;