PluginPortrait/mysql_cache_memory: mysql_cachememory

File mysql_cachememory, 2.2 KB (added by lordelph@…, at 2006-08-22T23:05:01+02:00)

mysql_cache_memory plugin perl script

Line 
1#!/usr/bin/perl
2#
3# mysql_cachememory - written by Paul Dixon (paul@elphin.com)
4# adapted from mysql_queries by Per Andreas Buer
5#
6# This munin plugin graphs the total available memory for the
7# mysql query cache, show the proportion that has been used
8#
9# Parameters:
10#
11#   config
12#   autoconf
13#
14# Configuration variables
15#
16#   mysqlopts     - Options to pass to mysql
17#   mysqladmin    - Override location of mysqladmin
18#
19#%# family=auto
20#%# capabilities=autoconf
21
22use strict;
23
24my $MYSQLADMIN = $ENV{mysqladmin} || "mysqladmin";
25my $COMMAND    =      "$MYSQLADMIN $ENV{mysqlopts} extended-status variables";
26
27#status variables we want to fetch...
28my %mysql_status = ( 
29               "Qcache_free_memory"  => 0, 
30               "query_cache_size" => 0,
31             );
32
33my $arg = shift();
34
35if ($arg eq 'config') {
36    print_config();
37    exit();
38} elsif ($arg eq 'autoconf') {
39    unless (test_service() ) {
40        print "yes\n";
41    } else {
42        print "no\n";
43    }
44    exit;
45}
46
47
48#no special argument to process, let's gather our data!
49
50open(SERVICE, "$COMMAND |")
51  or die("Coult not execute '$COMMAND': $!");
52
53while (<SERVICE>) {
54    my ($k, $v) = (m/(\w+).*?(\d+(?:\.\d+)?)/);
55    next unless ($k);
56    if (exists $mysql_status{$k} ) {
57         $mysql_status{$k}=$v;
58    }
59}
60
61close(SERVICE);
62
63
64my $mb_size = $mysql_status{'query_cache_size'}/(1024*1024);
65my $mb_free = $mysql_status{'Qcache_free_memory'}/(1024*1024);
66my $mb_used = $mb_size - $mb_free;
67
68print "free.value $mb_free\n";
69print "used.value $mb_used\n";
70
71exit;
72
73
74
75sub print_config {
76
77    my $num = 0;
78
79    print("graph_title MySQL Query Cache Memory
80graph_args -l 0
81graph_vlabel MB
82graph_category mysql
83graph_info This graph shows the size of the cache and the amount being used
84used.label Used memory (MB)
85used.type GAUGE
86used.draw AREA
87free.label Free memory (MB)
88free.type GAUGE
89free.draw STACK
90");
91
92   
93}
94
95
96sub test_service {
97
98    my $return = 1;
99
100    system ("$MYSQLADMIN --version >/dev/null 2>/dev/null");
101    if ($? == 0)
102    {
103        system ("$COMMAND >/dev/null 2>/dev/null");
104        if ($? == 0)
105        {
106            print "yes\n";
107            $return = 0;
108        }
109        else
110        {
111            print "no (could not connect to mysql)\n";
112        }
113    }
114    else
115    {
116        print "no (mysqadmin not found)\n";
117    }
118    exit $return;
119}