Dobra to opisze jak i co a wy mi powiec cie czy dobrze
w katalogu /urs/local/bin/ jest sam plik stat.pl i wyglada tak
#! /usr/bin/perl
#--------Ustawienie sta³ych u¿ytkownika-------------------
my $path = '/home/httpd/html/stats'; # katalog ze statystyk±
my $ipt = '/etc/iptables'; # lokalizacja iptables
my $hosts_names = 1; # nazwy zamiast IP (domy¶lnie: wy³±czone)
# ----------Tutaj nie nale¿y ju¿ nic zmieniaæ-------------
# --------------------------------------------------------
my $month = int(`date +%m`);
my $year = int(`date +%Y`);
my $file = "$path/$year$month";
my $now = time();
my @info;
# Odczytanie danych z firewalla
if($hosts_names)
{
@info = `$ipt -t mangle -L STAT -vxZ`;
}
else
{
@info = `$ipt -t mangle -L STAT -vnxZ`;
}
my %upload;
my %download;
my %activity;
foreach my $line (@info)
{
chomp($line);
my $host = "";
my $bytes = 0;
my $pkts = 0;
if($hosts_names)
{
if($line =~ /^ +([0-9]+) +([0-9]+).*any +anywhere +(.*)/ )
{
$line =~ s/^ +([0-9]+) +([0-9]+).*any +anywhere +(.*)/$1 $2 $3/g;
($pkts, $bytes, $host) = split ' ',$line;
$download{$host} = $bytes;
}
elsif($line =~ /^ +([0-9]+) +([0-9]+).*any +(.*) +anywhere +/ )
{
$line =~ s/^ +([0-9]+) +([0-9]+).*any +(.*) +anywhere +/$1 $2 $3/g;
($pkts, $bytes, $host) = split ' ',$line;
$upload{$host} = $bytes;
}
if($download{$host} || $upload{$host})
{
$activity{$host} = $now;
}
}
else
{
if($line =~ /^[ ]+([0-9]+)[ ]+([0-9]+).*all.* 0\.0\.0\.0\/0[ ]+([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3
{
$line =~ s/^[ ]+([0-9]+)[ ]+([0-9]+).*all.* 0\.0\.0\.0\/0[ ]+([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-
($pkts, $bytes, $host) = split ' ',$line;
$download{$host} = $bytes;
}
elsif($line =~ /^[ ]+([0-9]+)[ ]+([0-9]+).*all.* ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})[ ]+0\.0\.0\
{
$line =~ s/^[ ]+([0-9]+)[ ]+([0-9]+).*all.* ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})[ ]+0\.0\
($pkts, $bytes, $host) = split ' ',$line;
$upload{$host} = $bytes;
}
if($download{$host} || $upload{$host})
{
$activity{$host} = $now;
}
}
}
# Odczytanie danych z poprzedniego wykonania skryptu
if( open(LOGFILE, $file) )
my(@lines) = <LOGFILE>;
close(LOGFILE);
foreach my $line (@lines)
{
chomp($line);
if( $line =~ /^(.*)\t+([0-9]+)\t+([0-9]+)\t+([0-9]+)/ ||
$line =~ /^(.*)\t+([0-9]+)\t+([0-9]+)/ )
{
chomp $line;
my ($host, $up, $down, $activ) = split(/\t+/, $line);
$activity{$host} = $activity{$host} || $activ;
$download{$host} = $download{$host} + $down;
$upload{$host} = $upload{$host} + $up;
}
}
}
# zapis danych
open(OUTFILE, ">$file") or die("Fatal error: Unable to write '$file'. Exiting.\n");
foreach my $host (keys %upload)
{
print OUTFILE "$host\t$upload{$host}\t$download{$host}\t$activity{$host}\n";
}
close(OUTFILE);
/home/httpd/html/stats i tu jest wszystko
/home/httpd/html/stats/stat.php
<?php
$path = "/home/httpd/html/stats"; //¶cie¿ka do plików ze statystyk±
$hosts_names = 1; //u¿ywaæ nazw hostów zamiast IP
$default_sorting = 'download'; //domy¶lne sortowanie ('download' 'upload', 'host')
$skip_zeroes = 1; //pomija komputery z zerowymi danymi
$time_limit = 300; //czas (w sekundach), po którym komputery zostan±
//uznane za nieaktywne
/*******************************************************************/
$title = 'Statystyka wykorzystania ³±cza v.0.9.5';
$footer = 'Copyright © 2003-2005 A.L.E.C (<a href="http://alec.pl">www.alec.pl</a>)';
$months = array ('styczeñ','luty','marzec','kwiecieñ','maj','czerwiec','lipiec','sierpieñ','wrzesieñ','pa¼dziernik','listopad
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-2">
<TITLE><? echo $title; ?></TITLE>
<LINK rel="stylesheet" href="images/style.css" type="text/css">
</HEAD>
<BODY>
<?
function setunits($data)
{
if ( !($data < (1024*1024*1000)) ) {
$number = $data / (1024*1024*1024);
$unit = "GB";
} elseif ( !($data < (1024*1000) ) ) {
$number = $data / (1024*1024);
$unit = "MB";
} else {
$number = $data / 1024;
$unit = "KB";
}
return array($number, $unit);
}
$now = time();
$date = getdate();
$month = ( $_POST['month'] ? $_POST['month'] : $date['mon'] );
$year = ( $_POST['year'] ? $_POST['year'] : $date['year'] );
$sort = ( $_POST['sort'] ? $_POST['sort'] : $default_sorting );
/****************************************************************/
print '<P class="header">'.$title.'</P>';
print '<P><FORM class="form" name="traffic" method="post" action="stat.php">';
print 'miesi±c:<SELECT class="select" name="month" OnChange="javascript:document.traffic.submit();">';
for ( $i=1; $i<=12; $i++ )
{
print '<OPTION value="'.$i.'"';
if ( $i==$month )
print 'SELECTED';
print '>'.$months[$i-1].'</OPTION>';
};
print '</SELECT> ';
print 'rok:<SELECT class="select" name="year" OnChange="javascript:document.traffic.submit();">';
for ( $i=0; $i<=10; $i++ )
{
print '<OPTION value="'.($i+2000).'"';
if ( $i==$year-2000 )
print 'SELECTED';
print '>'.($i+2000).'</OPTION>';
};
print '</SELECT> ';
print 'sortowanie:<SELECT class="select" name="sort" OnChange="javascript:document.traffic.submit();">';
print '<OPTION value="download"'.($sort=='download' ? 'SELECTED' : '').'>download</OPTION>';
print '<OPTION value="upload"'.($sort=='upload' ? 'SELECTED' : '').'>upload</OPTION>';
print '<OPTION value="host"'.($sort=='host' ? 'SELECTED' : '').'>host</OPTION>';
print '</SELECT>';
print '</FORM>';
//otwieramy plik ************************
if( !($file = @fopen($path.'/'.$year.$month,'r')) )
{
print '<P class="error"><BR><BR>Brak statystyk z wybranego okresu!</P>';
die;
}
//czytamy dane
while (!feof($file))
$lines[] = fgets($file,100);
fclose($file);
foreach ($lines as $line)
{
$tab = preg_split('/[ \t]+/', $line);
$host = trim($tab[0]);
$up = trim($tab[1]);
$down = trim($tab[2]);
$atime = trim($tab[3]);
if(!$host || ($skip_zeroes && !($up || $down)) )
{
continue;
}
$hosts[] = ($hosts_names ? $host : ip2long($host));
$upload[] = $up;
$download[] = $down;
$active[] = $atime>$now-$time_limit ? 1 : 0;
}
//skalowanie wykresów ********************
$maks_up = max($upload);
$maks_down = max($download);
$maks = ( $maks_up > $maks_down ? $maks_up : $maks_down );
if ($maks == 0)
$maks = 1; //co by nie by³o dzielenia przez zero
//sortowanie *****************************
switch($sort)
{
case 'download':
array_multisort($download, SORT_DESC, $download, $upload, $hosts, $active);
break;
case 'upload':
array_multisort($upload, SORT_DESC, $upload, $download, $hosts, $active);
break;
case 'host':
array_multisort($hosts, SORT_ASC, $hosts, $download, $upload, $active);
break;
}
print '<P><TABLE cellpadding="3" align="center"><TR><TD>';
//upload *********************************
$sum_upload = array_sum($upload);
print '<TABLE cellpadding="0" class="tables" ALIGN="right">';
print '<TR><TD colspan="2" align="center"><B>Upload</B></TD></TR>';
foreach ($upload as $idx => $data)
{
list($number, $unit) = setunits($upload[$idx]);
$d = round($upload[$idx]*200/$maks);
print '<TR>';
print '<TD align="right" nowrap> '.($active[$idx] ? '<B>' : '').($hosts_names ? $hosts[$idx] : long2ip($hosts[$i
print '<TD><IMG src="images/bluepx.gif" height="12" width="'.($d ? $d : '1').'" align="absmiddle" alt="" border="1">&
printf('%0.2f %s (%0.1f%%)</TD>', $number, $unit, ($upload[$idx]*100/$sum_upload));
print '</TR>';
}
$sum = setunits($sum_upload);
print '<TR><TD colspan="2" align="center"><B>RAZEM: '.sprintf('%0.2f',$sum[0]).' '.$sum[1].'</B></TD></TR>';
print '</TABLE>';
print '</TD><TD>';
//download *******************************
$sum_download = array_sum($download);
print '<TABLE cellpadding="0" class="tables" align="center">';
print '<TR><TD colspan="2" align="center"><B>Download</B></TD></TR>';
foreach ($download as $idx => $data)
{
list($number, $unit) = setunits($download[$idx]);
$d = round($download[$idx]*200/$maks);
print '<TR>';
print '<TD align="right" nowrap> '.($active[$idx] ? '<B>' : '').($hosts_names ? $hosts[$idx] : long2ip($hosts[$i
print '<TD><IMG src="images/redpx.gif" height="12" width="'.($d ? $d : '1').'" alt="" align="absmiddle" border="1">&n
printf('%0.2f %s (%0.1f%%)</TD>', $number, $unit, ($download[$idx]*100/$sum_download));
print '</TR>';
}
$sum = setunits($sum_download);
print '<TR><TD colspan="2" align="center"><B>RAZEM: '.sprintf('%0.2f',$sum[0]).' '.$sum[1].'</B></TD></TR>';
print '</TABLE>';
print '</TD></TR><TR><TD colspan="2" align="center" class="footer"><P>'.$footer.'</P></TD></TR></TABLE>';
?>
/etc/rc.d/rc.local a jak to uruchamiam to wyskakuje
NND: Uruchamianie programow uzytkownika [Trwa....] ./rc.local: line 10: /etc/rc.d/reguly: Nie ma takiego pliku ani katalogu
[Wykonane]
------------------------------------------------------------------------------
#!/bin/sh
#
# /etc/rc.d/rc.local: Local system initialization script.
#
. /etc/rc.conf
. /etc/rc.d/functions
stat_busy "Uruchamianie programow uzytkownika"
# Tutaj umiesc komendy uruchamiajace twoje programy.
/etc/rc.d/bałwan jestem
# Programy, ktore musza startowac z usera
# linijka stat_done musi zostac
stat_done
/etc/iptables/reguly zrobilem to tak pico /rtc/iptables/reguly
i wpisalem tam tak
iptables -t mangle -D FORWARD -i eth1 -j STAT >/dev/null 2>&1
iptables -t mangle -D FORWARD -o eth1 -j STAT >/dev/null 2>&1
iptables -t mangle -F STAT >/dev/null 2>&1
iptables -t mangle -X STAT >/dev/null 2>&1
iptables -t mangle -N STAT
iptables -t mangle -I FORWARD -o eth1 -j STAT
iptables -t mangle -I FORWARD -i eth1 -j STAT
# i dla kazdego komputera po dwie reguly
iptables -t mangle -A STAT -d 192.168.0.2
iptables -t mangle -A STAT -s 192.168.0.2
iptables -t mangle -A STAT -d 192.168.0.6
iptables -t mangle -A STAT -s 192.168.0.6
iptables -t mangle -A STAT -d 192.168.0.9
iptables -t mangle -A STAT -s 192.168.0.9
iptables -t mangle -A STAT -d 192.168.0.11
iptables -t mangle -A STAT -s 192.168.0.11
iptables -t mangle -A STAT -d 192.168.0.40
iptables -t mangle -A STAT -s 192.168.0.40
tam pisze eth1 a ja mam eth0 mam to zmienic ?
i co jeszcze mam zrobic ??
|