Connecting Tech Pros Worldwide Forums | Help | Site Map

mysql health check script

Newbie
 
Join Date: Oct 2007
Posts: 4
#1: Nov 17 '07
Hi,

I am coding a mysql health check script. The logic is to execute the commands (only once)show status,show slave status,show variables and fetch the variable name and value in a hash refer or any other fetch machanism and dynamically use the values to do calculation like the following

Expand|Select|Wrap|Line Numbers
  1. threhold values($uptime > 10800) && (Handler_read_rnd_next > 4000) && ((100-(((Handler_read_rnd_next + Handler_read_rnd) / (##Handler_read_rnd_next + Handler_read_rnd + Handler_read_first + Handler_read_next + Handler_read_key + Handler_read_prev))*100))
  2.  
if any one can code using hash reference and use cloumn name to do calculation..
Show variables would result the following output.There are other 189 variables which will be used to do the above calculation.
mysql> show variables;
Variable_name | Value
-------------------------------------------------------
back_log 50
basedir /
binlog_cache_size 32768
bulk_insert_buffer_size 8388608
character_set_client latin1
character_set_connection latin1
character_set_database latin1
character_set_results latin1
character_set_server latin1
character_set_system utf8

my current code is

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. # Check the health of a mysql server.
  3. #
  4. use Getopt::Long;
  5. use DBI;
  6. #use strict;
  7. #use warning;
  8.  
  9. my $Variable_name = '';
  10. my $status = '';
  11. # --
  12. # Print out the usage message
  13. # --
  14. sub usage {
  15.     print "usage: check_mysqlhealth.pl -H <host> -u <user> -p <password> \n";
  16.     print "       Optional parameters:\n";
  17.     print "       --port <port> \n";
  18. }
  19.  
  20. $|=1;
  21.  
  22. # --
  23. # Parse arguments and read Configuration
  24. # --
  25. my ($host, $user, $password, $port);
  26. GetOptions (
  27.     'host=s' => \$host,
  28.     'H=s' => \$host,
  29.     'user=s' => \$user,
  30.     'u=s' => \$user,
  31.     'password=s' => \$password,
  32.     'p:s' => \$password,
  33.     'port=i' => \$port,
  34. );
  35.  
  36. if (!$host || !$user) {
  37.     usage();
  38.     exit(1);
  39. }
  40.  
  41. if (!$port) {
  42.     $port = 3306;
  43. }
  44.  
  45. my $totalTime = time();
  46.  
  47. # --
  48. # Establish connection
  49. # --
  50. my $state = "OK";
  51. my $dbh;
  52. eval {
  53.     $dbh = DBI->connect("DBI:mysql:host=$host;port=$port", $user, $password, {'RaiseError' => 1});
  54. print "connected\n";
  55. };
  56.  
  57. if ($@) {
  58.     my $status = $@;
  59.     print 'CRITICAL: Connect failed with reason ' . $status . "\n";
  60.     exit 2;
  61. }
  62.  
  63. &shvar;
  64.  
  65. sub shvar
  66.  {
  67.     my $sgv = $dbh->prepare("show variables");
  68.     $sgv->execute();
  69.    %MySQL_Variables = ();
  70.     while (my ($name,$value) = $sgv->fetchrow_array())
  71. {
  72.       print "$MySQL_Variables{lc($name)} = $value\n";
  73.  
  74.     }
  75. }
  76.  
This code is listing the variables, now how can i fetch a particular variable to do computation..

Need help desperately...
eWish's Avatar
Moderator
 
Join Date: Jul 2007
Location: Arkansas
Posts: 900
#2: Dec 30 '07

re: mysql health check script


Instead of using "Show Variable" just do a "Select" statement.

Expand|Select|Wrap|Line Numbers
  1. my (@rows);
  2.  
  3. my $select = $dbh->prepare('SELECT column1, column2, column3 FROM table_name WHERE column1 = ?');
  4.    $select->execute($value_for_column_criteria);
  5.  
  6.    while (@rows = $select->fetchrow_array) {
  7.        print join("\n", @rows);
  8.        }       
--Kevin
Reply