Собственно сам скрипт (perl):
#!/usr/bin/perl
use DBI;
use DBD::mysql;
use Net::Rsh;
use Time::Local;
use strict;
my ($db,$user,$pw) = ("UTM","billing","xxxxx");
my $logfile = "/var/log/shapeme.log";
my $dbh = DBI->connect("DBI:mysql

atabase=$db;host=localhost",$user,$pw);
if (!$dbh)
{
exit(1);
}
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime (time);
my $day_start = timelocal (0, 0, 0, $mday, $mon, $year);
my $day_end = timelocal (59, 59, 23, $mday, $mon, $year);
my $query_user = "SELECT inet_ntoa(ip) ip,quota,t_class FROM sm_users order by ip";
my $sth_user = $dbh->prepare($query_user);
$sth_user->execute();
while (my $str_user = $sth_user->fetchrow_hashref())
{
my $query_utm =
"
SELECT
us.login login,
inet_ntoa(case when ig.ip > 0 then ig.ip else 4294967296+ig.ip end) ip,
SUM(dtia.bytes) bytes
FROM ip_groups ig
INNER JOIN service_links sl
ON sl.id = ig.ip_group_id
INNER JOIN users us
ON us.id = sl.user_id
INNER JOIN discount_transactions_iptraffic_all dtia
ON dtia.account_id = sl.account_id
WHERE
ig.is_deleted=0
AND dtia.discount_date>="$day_start"
AND dtia.discount_date <="$day_end"
AND dtia.t_class="$str_user->{"t_class"}"
AND inet_ntoa(case when ig.ip > 0 then ig.ip else 4294967296+ig.ip end) = "$str_user->{"ip"}"
GROUP BY
dtia.t_class,
dtia.base_cost,
dtia.account_id,
dtia.slink_id
";
my $sth_utm = $dbh->prepare($query_utm);
$sth_utm->execute();
my $str_utm = $sth_utm->fetchrow_hashref();
if ($str_utm->{"bytes"} > $str_user->{"quota"})
{
#send rsh
debug ("user ".$str_utm->{"login"}." exceeded limit: available ".$str_user->{"quota"}.", downloaded ".$str_utm->{"bytes"});
my $cisco_ip = $str_user->{"ip"};
$cisco_ip =~ s/(^\d{1,3}\.\d{1,3}\.\d{1,3}\.).*/$1."61"/ge;
my $rsh=Net::Rsh->new();
if (!$rsh)
{
debug ("can\"t send command using Net::Rsh");
}
else
{
my $local_user="root";
my $remote_user="netup";
my $cmd="access-template 180 shaper any host $str_user->{"ip"}";
my @c=$rsh->rsh($cisco_ip,$local_user,$remote_user,$cmd);
foreach(@c)
{
if (length ($_) > 3)
{
debug("cisco@".$cisco_ip.": ".$_);
}
}
}
}
}
sub debug()
{
open (LOG,">>$logfile");
print LOG scalar localtime(time()),": ",$_[0],"\n";
close LOG;
}