#!/usr/bin/php
<?php

/*
   MyStatsQL 0.1Beta (c) 2006 Andrew Rose <rose.andrew@gmail.com>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

Installation:

1. Be sure /path/to/php is correct above.

2.  Create the tables below, adding indexs as and when needed.

    CREATE TABLE `mystatsql_stats` (
      `id` int(11) NOT NULL auto_increment,
      `server` int(11) NOT NULL,
      `date` int(11) default NULL,
      `time` int(11) default NULL,
      `Innodb_data_reads` int(11) default NULL,
      `Innodb_data_writes` int(11) default NULL,
      `Qcache_hits` int(11) default NULL,
      `Qcache_inserts` int(11) default NULL,
      `Questions` int(11) default NULL,
      `Open_files` int(11) default NULL,
      `Open_tables` int(11) default NULL,
      PRIMARY KEY  (`id`)
    );

    CREATE TABLE `mystatsql_stats_last` (
      `id` int(11) NOT NULL auto_increment,
      `server` int(11) NOT NULL,
      `Innodb_data_reads` int(11) default NULL,
      `Innodb_data_writes` int(11) default NULL,
      `Qcache_hits` int(11) default NULL,
      `Qcache_inserts` int(11) default NULL,
      `Questions` int(11) default NULL,
      `Open_files` int(11) default NULL,
      `Open_tables` int(11) default NULL,
      UNIQUE (`server`),
      PRIMARY KEY  (`id`)
    );

3. Configure serverid, mysql_connect and mysql_select_db below.
Severid must be numeric and unique for each server this script runs on.

4. `insert into mystatsql_stats_last(server) values(<serverid 1>),(<serverid 2>),...;`
    The serverids are what are set below, for instance if you have a cluster of 10
    MySQL servers you would need to assign them all a unique id in this script 1-10
    and initialize mystatsql_stats_last accordingly. 

5. Place this script into your cron.hourly/ or wherever you feel fit and `chmod +x` it.

****Allow a few hours for the stats to steady out***

--

Modifying:

1. Run `show status` via mysql commandline client.

2. Add anything you want to the view array below as it's appropriate type.

3. `alter table mystatsql_stats add column <Variable_name> int default 0;`
   `alter table mystatsql_stats_last add column <Variable_name> int default 0;`

*/

$serverid 1;
mysql_connect('localhost''''');
mysql_select_db('');

$view = array(
'Innodb_data_reads' => 'tally',
'Innodb_data_writes' => 'tally',
'Qcache_hits' => 'tally',
'Qcache_inserts' => 'tally',
'Questions' => 'tally',
'Open_files' =>  'snapshot',
'Open_tables' => 'snapshot'
);

$cols = array();
foreach(
$view as $k => $v$cols[]=$k;

$query_stats "insert into mystatsql_stats set server = '".$serverid."', date = '".date('Ymd')."', time = '".date('Hi')."'";
$query_stats_last "update mystatsql_stats_last set server = '".$serverid."'";

$result mysql_query("select ".implode(','$cols)." from mystatsql_stats_last where server = '".$serverid."'");
if(
mysql_num_rows($result)==1)
{
    
$stats_last mysql_fetch_assoc($result);

    
$result mysql_query("show status");
    while(
$stats mysql_fetch_assoc($result))
    {
        
$tally=0;
        if(isset(
$view[$stats['Variable_name']]))
        {
            if(
$view[$stats['Variable_name']]=='tally')
            {
                
$figure $stats['Value'] - $stats_last[$stats['Variable_name']];
            }
            else
            {
                
$figure $stats['Value'];
            }
            
$query_stats .= ", ".$stats['Variable_name']." = '".$figure."'";
            
$query_stats_last .= ", ".$stats['Variable_name']." = '".$stats['Value']."'";
        }
    }

    
mysql_query($query_stats);
    
mysql_query($query_stats_last " where server = '".$serverid."'");
}