root / plugins / mssql / microsoft-sql-data-file-sizes @ 17f78427
Historique | Voir | Annoter | Télécharger (3,05 ko)
| 1 |
#!/usr/bin/env ruby |
|---|---|
| 2 |
# |
| 3 |
# Munin Plugin for MSSQL - Data file size monitoring |
| 4 |
# |
| 5 |
# Author: Wilfred Chau <openapp.developer@gmail.com> |
| 6 |
# Date: 2011-05-19 |
| 7 |
# Version: 1.0 |
| 8 |
# |
| 9 |
# This program is free software; you can redistribute it and/or modify |
| 10 |
# it under the terms of the GNU General Public License version 2 |
| 11 |
# as published by the Free Software Foundation. |
| 12 |
# |
| 13 |
# This program is distributed in the hope that it will be useful, |
| 14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
# GNU General Public License for more details. |
| 17 |
# |
| 18 |
# You should have received a copy of the GNU General Public License along |
| 19 |
# with this program; if not, write to the Free Software Foundation, Inc., |
| 20 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 |
# |
| 22 |
# |
| 23 |
# Prerequistes: |
| 24 |
# 1) /etc/odbc.ini and /etc/freetds.conf |
| 25 |
# 2) rubygems |
| 26 |
# 3) ruby-dbi |
| 27 |
# |
| 28 |
# Usage: |
| 29 |
# 1) copy this script to the munin install plugins directory (e.g. /usr/share/munin/plugins) |
| 30 |
# 2) chmod to allow executable to others |
| 31 |
# 3) create symbolic link in /etc/munin/plugins |
| 32 |
# ln -s /usr/share/munin/plugins/mssql_datafilesizes.rb /etc/munin/plugins/mssql_datafilesizes.rb |
| 33 |
# |
| 34 |
# Parameters: |
| 35 |
# autoconf |
| 36 |
# config (required) |
| 37 |
# |
| 38 |
# Config variables: |
| 39 |
# sqluser : mssql user who has view server state privilege |
| 40 |
# sqlpass : password for the mssql user |
| 41 |
# dsn : datasource name as defined in /etc/odbc.ini |
| 42 |
# instance: instance to monitor |
| 43 |
# |
| 44 |
#%# family=auto |
| 45 |
#%# capabilities=autoconf |
| 46 |
|
| 47 |
require 'rubygems' |
| 48 |
require 'dbi' |
| 49 |
|
| 50 |
sqluser = 'rubyuser' |
| 51 |
sqlpass = 'rubyuser' |
| 52 |
dsn = 'TESTSQL' |
| 53 |
instance = 'AdventureWorks' |
| 54 |
|
| 55 |
# |
| 56 |
# Queries |
| 57 |
# |
| 58 |
# |
| 59 |
dbh = DBI.connect("DBI:ODBC:#{dsn}",sqluser,sqlpass)
|
| 60 |
|
| 61 |
instance_name_query = "SELECT distinct instance_name |
| 62 |
FROM sys.dm_os_performance_counters |
| 63 |
WHERE instance_name = '#{instance}'"
|
| 64 |
|
| 65 |
transaction_query = "select cntr_value/1024.0 from sys.dm_os_performance_counters |
| 66 |
where counter_name = 'Data File(s) Size (KB)' |
| 67 |
and object_name = 'SQLServer:Databases' |
| 68 |
and instance_name = ?" |
| 69 |
|
| 70 |
all_instance_names = Array.new |
| 71 |
sth = dbh.execute(instance_name_query) |
| 72 |
sth.fetch do |row| |
| 73 |
all_instance_names.push(row[0].strip) |
| 74 |
end |
| 75 |
sth.finish |
| 76 |
|
| 77 |
# |
| 78 |
# autoconf |
| 79 |
# |
| 80 |
if ARGV[0] == "autoconf" |
| 81 |
if all_instance_names.length > 1 && sqluser.length > 1 && sqlpass.length > 1 |
| 82 |
puts "yes" |
| 83 |
else |
| 84 |
puts "no" |
| 85 |
puts "Usage: #{__FILE__} autoconf|conf"
|
| 86 |
end |
| 87 |
exit 0 |
| 88 |
# |
| 89 |
# config definition |
| 90 |
# |
| 91 |
elsif ARGV[0] == "config" |
| 92 |
puts "graph_args --base 1024k -r --lower-limit 0" |
| 93 |
puts "graph_title MSSQL DB File Sizes" |
| 94 |
puts "graph_category db" |
| 95 |
puts "graph_info This graph shows DB File Sizes (MB)" |
| 96 |
puts "graph_vlabel MB" |
| 97 |
puts "graph_scale no" |
| 98 |
puts "graph_period second" |
| 99 |
|
| 100 |
all_instance_names.sort.each do |s| |
| 101 |
puts "#{s}.label #{s}"
|
| 102 |
puts "#{s}.info INSTANCE: #{s}"
|
| 103 |
puts "#{s}.type GAUGE"
|
| 104 |
puts "#{s}.draw LINE1"
|
| 105 |
end |
| 106 |
|
| 107 |
exit 0 |
| 108 |
end |
| 109 |
|
| 110 |
# |
| 111 |
# Getting data |
| 112 |
# |
| 113 |
sth = dbh.prepare(transaction_query) |
| 114 |
all_instance_names.sort.each do |k| |
| 115 |
sth.execute(k) |
| 116 |
sth.fetch do |row| |
| 117 |
puts "#{k.to_s}.value #{row[0].to_s}"
|
| 118 |
end |
| 119 |
end |
| 120 |
sth.finish |
| 121 |
dbh.disconnect |
