Linux Advanced Routing and Traffic Control list
 help / color / mirror / Atom feed
From: "Adam Towarnyckyj" <adamt@commspeed.net>
To: lartc@vger.kernel.org
Subject: [LARTC] Perl Script for pulling information from a mysql database
Date: Tue, 22 Jun 2004 17:22:01 +0000	[thread overview]
Message-ID: <020f01c4587d$6eb02390$903113d8@uranus> (raw)

Here's the script. I would have posted it to a website, but I figure
this'll be better so it is always on the list for people in the future
in case they wanted to see it.
If you have any questions about any of it, please let me know. Mike and
I aren't the cleanest of programmers. Cron the script to run whenever
you need it to. :)

#!/usr/bin/perl
#
# TC Helper Script: Written by Mike Davis & Adam Towarnyckyj
#
#   Syncronizes data rates with MySQL server and applies hourly.
#

### Configuration Section ###


$dev = "eth1";
$tc = "/sbin/tc";
$mysql_host = "host";
$mysql_db = "database";
$mysql_user = "user";
$mysql_pass = "password";


### END Configuration Section ###

use POSIX qw(strftime);
use DBI;

# Database connect and define subroutines

$dsn = "DBI:mysql:database=$mysql_db;hostname=$mysql_host";
$dbh = DBI->connect($dsn, $mysql_user, $mysql_pass) || die "Can't
connect to database: " . DBI->errstr;

sub SelectSQL {
  my($sql) = @_;
  my @MATCHES, $hash;
  $sth = $dbh->prepare("$sql");
  $sth->execute();
  while ($hash = $sth->fetchrow_hashref) {
        push @MATCHES, $hash;
  }
  return @MATCHES;
}

sub SelectSingleSQL {
  my($sql) = @_;
  my($gotit, $return, $hash);
  $sth = $dbh->prepare("$sql");
  $sth->execute();
  while ($hash = $sth->fetchrow_array) {
        unless ($gotit) {
          $return = $hash;
          $gotit++;
        } else { warn "got multiple SQL returns when exepecting only
one"; }
  }
  return $return;
}

sub SimpleSQL {
  my($sql) = $_[0];
  my $rows_affected;
  $rows_affected = $dbh->do($sql);
  return $rows_affected;
}

sub Action {
  my($action) = @_;
#  print"Performing: $action\n";
  $warn=`$action 2>&1`;
  if ($warn) {
    chomp($warn);
    $prepare = "ERROR: $warn. Command was: $action";
#    print"WHOOPS: $warn\n";
    push @WARNING, $prepare;
  }
}

### Ok, now we start having fun.  Let's rebuild the tc tree.

# Remove existing tree and add the root.

Action("$tc qdisc del dev $dev root");
Action("$tc qdisc add dev $dev root handle 10:0 cbq bandwidth 200mbit
avpkt 1000");
Action("$tc class add dev $dev parent 10: classid 10:1 cbq bandwidth
200Mbit rate 200Mbit allot 1514 weight 2Mbit prio 8 maxburst 10 avpkt
1000");

# Get our list of accounts

@MODEMS = SelectSQL("SELECT mid, dsrate FROM modems");

# Figure out account IPs and put 'em in!

foreach $modem (@MODEMS) {
        if ($$modem{dsrate} = "0" || $$modem{dsrate} = "1") {
                next;
        }
        my @COMPUTERS = SelectSQL("SELECT ipid FROM computers WHERE
mid='$$modem{mid}'");
        foreach $computer (@COMPUTERS) {
                my $ip = SelectSingleSQL("SELECT ipaddr FROM ips WHERE
ipid='$$computer{ipid}'");
                my $rate = $$modem{dsrate} . "kbit";
                my $classid = sprintf("%X", $$computer{ipid});
                Action("$tc class add dev $dev parent 10: classid
10:$classid cbq bandwidth 200Mbit rate $rate allot 1514 prio 5 maxburst
20 avpkt 1000 

bounded");
                Action("$tc qdisc add dev $dev parent 10:$classid sfq
quantum 1514 perturb 15");
                Action("$tc filter add dev $dev parent 10:0 protocol ip
prio 25 u32 match ip dst $ip flowid 10:$classid");
        }
}

if (@WARNING) {
  print"WARNING: TCHELPER produced errors!  See below:\n @WARNING\n";
}

#
# Cool, everyone is now limited.

# Exit Nice and clean.

$dbh->disconnect;
exit(0);

_______________________________________________
LARTC mailing list / LARTC@mailman.ds9a.nl
http://mailman.ds9a.nl/mailman/listinfo/lartc HOWTO: http://lartc.org/

                 reply	other threads:[~2004-06-22 17:22 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='020f01c4587d$6eb02390$903113d8@uranus' \
    --to=adamt@commspeed.net \
    --cc=lartc@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox