Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / mssql / microsoft-sql-data-file-sizes @ b0b39b01

Historique | Voir | Annoter | Télécharger (3,04 ko)

1
#!/usr/bin/env ruby
2

    
3
=begin
4

    
5
Munin Plugin for MSSQL - Data file size monitoring
6

    
7
Author: Wilfred Chau <openapp.developer@gmail.com>
8
Date: 2011-05-19
9
Version: 1.0
10

    
11
This program is free software; you can redistribute it and/or modify
12
it under the terms of the GNU General Public License version 2
13
as published by the Free Software Foundation.
14

    
15
This program is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
GNU General Public License for more details.
19

    
20
You should have received a copy of the GNU General Public License along
21
with this program; if not, write to the Free Software Foundation, Inc.,
22
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23

    
24

    
25
Prerequistes:
26
	1) /etc/odbc.ini and /etc/freetds.conf
27
	2) rubygems
28
	3) ruby-dbi
29

    
30
Usage:
31
	1) copy this script to the munin install plugins directory (e.g. /usr/share/munin/plugins)
32
	2) chmod to allow executable to others
33
	3) create symbolic link in /etc/munin/plugins
34
           ln -s /usr/share/munin/plugins/mssql_datafilesizes.rb /etc/munin/plugins/mssql_datafilesizes.rb
35

    
36
Parameters:
37
	autoconf
38
	config (required)
39

    
40
Config variables:
41
	sqluser : mssql user who has view server state privilege
42
	sqlpass : password for the mssql user
43
	dsn     : datasource name as defined in /etc/odbc.ini
44
	instance: instance to monitor
45

    
46
#%# family=auto
47
#%# capabilities=autoconf
48

    
49
=end
50

    
51

    
52
require 'rubygems'
53
require 'dbi'
54

    
55
sqluser  = 'rubyuser'
56
sqlpass  = 'rubyuser'
57
dsn      = 'TESTSQL'
58
instance = 'AdventureWorks'
59

    
60
#
61
# Queries
62
#
63
#
64
dbh = DBI.connect("DBI:ODBC:#{dsn}", sqluser, sqlpass)
65

    
66
instance_name_query = "SELECT distinct instance_name
67
                       FROM sys.dm_os_performance_counters
68
		       WHERE instance_name = '#{instance}'"
69

    
70
transaction_query = "select cntr_value/1024.0 from sys.dm_os_performance_counters
71
                     where counter_name = 'Data File(s) Size (KB)'
72
                     and object_name = 'SQLServer:Databases'
73
                     and instance_name = ?"
74

    
75
all_instance_names = Array.new
76
sth = dbh.execute(instance_name_query)
77
sth.fetch do |row|
78
  all_instance_names.push(row[0].strip)
79
end
80
sth.finish
81

    
82
#
83
# autoconf
84
#
85
if ARGV[0] == "autoconf"
86
  if all_instance_names.length > 1 && sqluser.length > 1 && sqlpass.length > 1
87
    puts "yes"
88
  else
89
    puts "no"
90
    puts "Usage: #{__FILE__} autoconf|conf"
91
  end
92
  exit 0
93
#
94
# config definition
95
#
96
elsif ARGV[0] == "config"
97
  puts "graph_args --base 1024k -r --lower-limit 0"
98
  puts "graph_title MSSQL DB File Sizes"
99
  puts "graph_category db"
100
  puts "graph_info This graph shows DB File Sizes (MB)"
101
  puts "graph_vlabel MB"
102
  puts "graph_scale no"
103
  puts "graph_period second"
104

    
105
  all_instance_names.sort.each do |s|
106
    puts "#{s}.label #{s}"
107
    puts "#{s}.info INSTANCE: #{s}"
108
    puts "#{s}.type GAUGE"
109
    puts "#{s}.draw LINE1"
110
  end
111

    
112
  exit 0
113
end
114

    
115
#
116
# Getting data
117
#
118
sth = dbh.prepare(transaction_query)
119
all_instance_names.sort.each do |k|
120
  sth.execute(k)
121
  sth.fetch do |row|
122
    puts "#{k.to_s}.value #{row[0].to_s}"
123
  end
124
end
125
sth.finish
126
dbh.disconnect