Permalink
Browse files

First commit. Includes an apache vhost monitor and a mysql plugin to …

…monitor the key cache
  • Loading branch information...
0 parents commit e7909b41fa67eae069fba936c62e27b8a628a313 @aaronpk committed Oct 11, 2010
Showing with 177 additions and 0 deletions.
  1. +32 −0 README.md
  2. +60 −0 apache_vhosts.php
  3. +85 −0 mysql_keycache.php
@@ -0,0 +1,32 @@
+
+ABOUT
+=====
+
+This project provides several munin plugins to monitor various aspects of a web server. Most
+of these work by linking a script in this folder to /etc/munin/plugins/plugin_name. They have
+instructions at the top of the file indicating the name to link the script as.
+
+
+LICENSE
+=======
+
+Copyright (c) 2010 Aaron Parecki
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
@@ -0,0 +1,60 @@
+#!/usr/bin/php
+<?php
+/**
+ * README
+ *
+ * Add an additional line to each vhost you want to track with the domain name of the host
+ * This will write a period for every hit to the vhost
+ * CustomLog /web/logs/munin/example.com "."
+ */
+
+$basedir = '/web/logs/munin';
+
+$files = scandir($basedir);
+
+// arg1 is blank or 'config' when munin runs it
+$mode = array_key_exists(1, $argv) ? $argv[1] : '';
+
+
+if($mode == 'config')
+{
+ echo 'graph_title Apache vhosts' . "\n";
+
+ echo 'graph_info Shows the requests per second for each vhost.' . "\n";
+ echo 'graph_vlabel requests / ${graph_period}' . "\n";
+ echo 'graph_category apache' . "\n";
+ echo 'graph_args --lower-limit 0' . "\n";
+ echo 'graph_scale yes' . "\n";
+
+ $first = TRUE;
+ foreach($files as $domain)
+ {
+ if(substr($domain, 0, 1) != '.')
+ {
+ $key = generate_munin_key($domain);
+ echo $key . '.label ' . $domain . "\n";
+ echo $key . '.min 0' . "\n";
+ echo $key . '.type DERIVE' . "\n";
+ echo $key . '.draw ' . ($first ? 'AREA' : 'STACK') . "\n";
+ $first = FALSE;
+ }
+ }
+}
+else
+{
+ foreach($files as $domain)
+ {
+ if(substr($domain, 0, 1) != '.')
+ {
+ $key = generate_munin_key($domain);
+ $value = filesize($basedir . '/' . $domain) / 2;
+ echo $key . '.value ' . $value . "\n";
+ }
+ }
+}
+
+function generate_munin_key($domain)
+{
+ return preg_replace(array('/\./', '/[^a-z0-9_]/'), array('_', ''), $domain);
+}
+?>
@@ -0,0 +1,85 @@
+#!/usr/bin/php
+<?php
+/**
+ * README
+ *
+ * Link this file as mysql_keycache_ratae and mysql_keycache in the plugins folder.
+ * Define your database settings in the PDO variables.
+ */
+
+define('PDO_DSN', 'mysql:dbname=information_schema;host=localhost');
+define('PDO_USER', 'munin');
+define('PDO_PASS', 'munin');
+
+
+// config or blank
+$mode = array_key_exists(1, $argv) ? $argv[1] : '';
+
+$section = explode('_', $argv[0]);
+$section = array_key_exists(2, $section) ? $section[2] : '';
+
+$variables = array(
+ 'key_reads' => 'The number of physical reads of a key block from disk.',
+ 'key_read_requests' => 'The number of requests to read a key block from the cache.',
+ 'key_writes' => 'The number of physical writes of a key block to disk.',
+ 'key_write_requests' => 'The number of requests to write a key block to the cache.'
+);
+
+$status = d()->query('SHOW STATUS');
+$status->execute();
+$info = array();
+while($s = $status->fetch())
+ if(in_array(strtolower($s['Variable_name']), array_keys($variables)))
+ $info[strtolower($s['Variable_name'])] = $s['Value'];
+
+if($section == 'rate')
+{
+ if($mode == 'config')
+ {
+ echo 'graph_title MySQL Key Read and Request Rate' . "\n";
+ echo 'graph_vlabel per ${graph_period}' . "\n";
+ echo 'graph_category mysql' . "\n";
+ echo 'graph_period minute' . "\n";
+ foreach($info as $k=>$n)
+ {
+ echo $k . '.label ' . $k . "\n";
+ echo $k . '.type DERIVE' . "\n";
+ echo $k . '.min 0' . "\n";
+ echo $k . '.info ' . $variables[$k] . "\n";
+ }
+ }
+ else
+ {
+ foreach($info as $k=>$n)
+ echo $k . '.value ' . $n . "\n";
+ }
+}
+else
+{
+ if($mode == 'config')
+ {
+ echo 'graph_title MySQL Key Cache Effectiveness' . "\n";
+ echo 'graph_category mysql' . "\n";
+ echo 'graph_period minute' . "\n";
+ echo 'key_read_cache.label Key Read Cache Miss %' . "\n";
+ echo 'key_read_cache.info The number of physical key reads over the number of requests. Less than 1% is ideal.' . "\n";
+ echo 'key_write_cache.label Key Write Cache Miss %' . "\n";
+ echo 'key_write_cache.info The number of physical key writes over the number of requests.' . "\n";
+ }
+ else
+ {
+ echo 'key_read_cache.value ' . round(($info['key_reads'] * 100) / $info['key_read_requests'], 3) . "\n";
+ echo 'key_write_cache.value ' . round(($info['key_writes'] * 100) / $info['key_write_requests'], 3) . "\n";
+ }
+}
+
+
+function d()
+{
+ static $DB;
+ if(!isset($DB))
+ $DB = new PDO(PDO_DSN, PDO_USER, PDO_PASS);
+ return $DB;
+}
+
+?>

0 comments on commit e7909b4

Please sign in to comment.