Glen Pitt-Pladdy :: BlogMySQL Performance Graphs on Cacti via SNMP | |||
I regularly end up doing work with MySQL and as I regularly point out in other articles, tuning can give massive performance improvements if done right. The first thing is to when tuning is understand what is going on and where the bottlenecks are. Just running on a faster box is all well and good but it may not actually lead to in proportionate performance increases if the bottlenecks lie elsewhere. These graphs are the product of some tuning work I've been doing over the past few months and are what I use for basic tuning and looking at what impact changes to queries have. Tuning wiselyThere is an awful lot of stuff on the web about measuring miss ratios for things query caches and blindly increasing the size of them if the ratios are not favourable. There are circumstances when this approach is valid, but care is needed when tuning to dig further and understand why miss ratios may be high. A typical example may be when the bulk of queries are simply not cacheable (eg. the same query doesn't repeat or always will return something different). What happens is that caches fill up with non-cacheable queries leaving less space for queries that are cacheable and at the same time introducing overheads of checking caches which are no use. Possible solutions would be to redesign queries (if possible) to be more cacheable, or just explicitly stop trying to cache a query that can be cached: SELECT SQL_NO_CACHE * WHERE parameter = 'random stuff' AND anotherparameter = 'more random stuff'; That can give you the performance boosts you may be looking for where just throwing in some more cache memory may have negliagable effect. This is why hit/miss ratio graphs here have a label saying things like "lower is better... sometimes" as lower may only be better under specific circumstances. They can be very misleading if you don't understand why they are not showing optimum results. One thing that is often worth checking is memory usage. In many cases caches may have high miss ratios and still have plenty of free space in the cache. This is a sure sign that heaping more cache space on is not going to be of any use at all. To be useful, these graphs need to be used wisely and with understanding of what is actually happening. Getting it workingLike with other templates I create, this one relies on SNMP to shift the data. This is a convenient, simple one-stop way of shifting data (with encryption if needed) into Cacti or other monitoring systems and allows one method to be used for all your monitoring irrespective of being local, over the LAN or in a remote locations across the planet. There are already plenty of other templates that run as local scripts or via ssh. This one is simply an extension script for snmpd that grabs (and caches) the data when called. Assuming you put this extension script in /etc/snmp and have a cache directory for my extensions of /var/local/snmp/cache (as described in previous articles), you can add the following to your /etc/snmp/snmpd.conf file and then restart snmpd: extend mysqlqcache /etc/snmp/mysql-stats /var/local/snmp/cache/mysql _qcache_miss_ratio _qcache_hit_ratio qcache_hits qcache_inserts qcache_total_blocks qcache_free_blocks _qcache_used_blocks query_cache_size qcache_free_memory _qcache_used_memory qcache_queries_in_cache The first argument for mysql-stats is the cache file, and from there it simply picks up and outputs the variables specified on the remainder of the line. I will add to this as myself (or perhaps others) need more things graphed. In order to read all these variables from MySQL we need credentials. These are stored in /etc/snmpd/mysql-stats.conf by default and you can modify the script if you need to change that. Typically this would have something along the lines of: data_source: DBI:mysql::localhost Make sure the permissions are set right on this file - we need the snmpd user to be able to read it by nobody else. $ ls -l /etc/snmp/mysql-stats.conf Log into MySQL and execute the following (changing "secret" as appropriate) to give the script permission to monitor: CREATE USER 'monitor'@'localhost' IDENTIFIED BY 'secret'; More privileges may be needed in the longer term as more monitoring is added, but that's enough to monitor a basic single node setup and it's always best to give as little privileges as possible. Download: MySQL monitoring on Cacti is on GitHub Install the Cacti template cacti_host_template_mysql.xml in the usual way and all going well graphs should start appearing. The usual diagnostics apply if data doesn't appear. The graphs |
|||
Disclaimer: This is a load of random thoughts, ideas and other nonsense and is not intended to be taken seriously. I have no idea what I am doing with most of this so if you are stupid and naive enough to believe any of it, it is your own fault and you can live with the consequences. More importantly this blog may contain substances such as humor which have not yet been approved for human (or machine) consumption and could seriously damage your health if taken seriously. If you still feel the need to litigate (or whatever other legal nonsense people have dreamed up now), then please address all complaints and other stupidity to yourself as you clearly "don't get it".
Copyright Glen Pitt-Pladdy 2008-2023
|
Comments:
Can this work with a mysql server that is not on localhost? I tried modifying the line
data_source: DBI:mysql::fqdn.com
but have had no luck....assuming everything else is right, is this even possible? Thank you!
These tutorials are awesome, by the way!
Eric
Glad you find these useful - that's the reason for publishing them!
Technically it should work, but I would suggest making sure you can connect with the "mysql" command line tool first. You will also need to set the user passwords to match the host they are coming from and ensure that MySQL is listening for inbound connections (in many cases it only listens on the local loopback address by default).
As it would be a good thing to be monitoring load, memory, cpu, disk, network etc. as well, it would make sense to be running snmpd on the MySQL box and so there would normally be no need to do anything other than monitoring localhost.
Glen,
many thanks for your treasure trove cacti toolbox!!!
made it easy, really appreciated,
dont work!!
1/09/2015 04:40:04 PM - CMDPHP: Poller[0] Host[8] DS[75] WARNING: Result from SNMP not valid. Partial Result: DBI connect(mysql:lo
01/09/2015 04:40:04 PM - CMDPHP: Poller[0] Host[8] DS[74] WARNING: Result from SNMP not valid. Partial Result: U
01/09/2015 04:40:04 PM - CMDPHP: Poller[0] Host[8] DS[73] WARNING: Result from SNMP not valid. Partial Result: Cant call method pre
Try running the snmpd extension script directly (as same user as snmpd runs as) to see the full error message. I would guess from what I can see that there is a problem with the connection. This could be something like default port/socket configured differently on your system or username/password mismatch between what you have set in the script and the database. Either way the full error message should give you more information on why it is not connecting.
I got the following error:
Use of uninitialized value $fields[1] in pattern match (m//) at mysql-stats line 148.
FATAL - can't rename "/etc/snmp/mysql_cache.TMP" to "/etc/snmp/mysql_cache": Is a directory
Any help to this?
Thank you
This seems a little strange since in the current version there is no regex on line 148. Can you tell me which version (git ref) you are using?
Glen,
where am I going wrong, it seems I'm not reading user/pwd ? or ?
# ./mysql-stats
usage: ./mysql-stats <path to cache file> <variable name> [more variable names.....]
# ./mysql-stats /var/local/snmp/cache/mysql sort_rows sort_range sort_merge_passes sort_scan
DBI connect(':localhost','monitor',...) failed: Access denied for user 'monitor'@'localhost' (using password: NO) at ./mysql-stats line 108.
Can't call method "prepare" on an undefined value at ./mysql-stats line 113.
# ls -al mysql*
-rwxr-xr-x 1 root root 9726 May 27 19:42 mysql-stats
-rw-r--r-- 1 root root 180 May 27 19:28 mysql-stats.conf
#
# mysql -u monitor -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 169990
Server version: 10.2.22-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
I suspect the clue is "using password: NO"
What is the commit ref of this version you are running? The current version in GitHub (years old now) has initialising a hash on line 108. That would not create this error. My guess is your version has the connect happening on line 108.
Glen,
I've d/l few days ago from GitHub, perhaps I picked wrong archive or something?
is this commit ref: '# version 20120426' ?
lines 108/114
my %status;
use DBI;
my $dbh = DBI->connect (
$credentials{'data_source'},
$credentials{'username'},
$credentials{'password'}
);
# head -n 114 mysql-stats
That's weird since the error is complaining about connect failing on line 108, but the connect is from 110.
Experimenting here I find the "using password: NO" appears if there is no matching "password: secret" line in the config. What might be happening here is that this contains characters that are not matched on line 53. The credentials loading is a bit crude and does limit characters that may be used.
I've updated this today so see if that helps. It now picks up anything from the first non-whitespace character to the end of the line.
thanks! I think I'm in:
# ./mysql-stats /var/local/snmp/cache/mysql sort_rows sort_range sort_merge_passes sort_scan
6572737
245398
0
188800