#!/usr/bin/perl # # $Id$ # # mysql.pl # TiChou # my $user = "root"; my $pass = "petitchat"; my $host = "localhost"; my $version = "1.0"; my $cmdname = (split('/', $0))[-1]; my $rrdlib = "/var/lib/rrd"; my $mysqladmin = "/usr/bin/mysqladmin"; use Getopt::Long; use RRDs; sub usage($) { my $usage = "Usage: $cmdname [options] Options: -f, --file=FILE specify RRD file (default $rrdlib/mysql.rrd) -h, --help display this help and exit -v, --version display version Report bugs to .\n"; if (shift) { print STDOUT $usage; exit 0; } else { print STDERR $usage; exit 1; } } sub version { print "$cmdname $version\n"; exit 0; } my $rrdfile = "$rrdlib/mysql.rrd"; Getopt::Long::Configure ("bundling"); GetOptions("f|file=s" => \$rrdfile, "h|help" => \&usage, "v|version" => \&version) or &usage(0); if (not -f $rrdfile) { RRDs::create $rrdfile, qw(DS:in:COUNTER:600:0:U DS:out:COUNTER:600:0:U DS:cnx:COUNTER:600:0:U DS:query:COUNTER:600:0:U RRA:AVERAGE:0.5:1:288 RRA:AVERAGE:0.5:6:336 RRA:AVERAGE:0.5:24:360 RRA:AVERAGE:0.5:288:365 RRA:MIN:0.5:1:288 RRA:MIN:0.5:6:336 RRA:MIN:0.5:24:360 RRA:MIN:0.5:288:365 RRA:MAX:0.5:1:288 RRA:MAX:0.5:6:336 RRA:MAX:0.5:24:360 RRA:MAX:0.5:288:365); my $error = RRDs::error; die "$0: $error\n" if $error; } my ($in,$out,$cnx,$query) = ("U") x 4; open(SQL, "$mysqladmin --host=$host --user=$user".($pass ? " --password=$pass" : "")." extended-status|") or die "$0: can't run $mysqladmin: $!\n"; while () { /^\| Bytes_received\s+\| (\d+)\s+\|$/ && ($in = $1); /^\| Bytes_sent\s+\| (\d+)\s+\|$/ && ($out = $1); /^\| Connections\s+\| (\d+)\s+\|$/ && ($cnx = $1); /^\| Questions\s+\| (\d+)\s+\|$/ && ($query = $1); }; close(SQL); RRDs::update $rrdfile, "N:$in:$out:$cnx:$query"; my $error = RRDs::error; die "$0: $error\n" if $error;