Projet

Général

Profil

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

root / plugins / mssql / microsoft-sql-log-file-size @ 809639ab

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

1
#!/usr/bin/env ruby
2

    
3
=begin
4

    
5
Munin Plugin for MSSQL - log files 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
Prerequistes:
25
  1) /etc/odbc.ini and /etc/freetds.conf
26
  2) rubygems
27
  3) ruby-dbi
28

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

    
35
Parameters:
36
  autoconf
37
  config (required)
38

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

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

    
48
=end
49

    
50
require 'rubygems'
51
require 'dbi'
52

    
53
sqluser  = 'rubyuser'
54
sqlpass  = 'rubyuser'
55
dsn      = 'TESTSQL'
56
instance = 'AdventureWorks'
57

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

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

    
68
logfilesize_query = "SELECT cntr_value/1024.0 from sys.dm_os_performance_counters
69
                     WHERE counter_name = 'Log File(s) Size (KB)'
70
                     AND object_name = 'SQLServer:Databases'
71
                     AND instance_name = ?"
72

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

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

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

    
111
  exit 0
112
end
113

    
114
sth = dbh.prepare(logfilesize_query)
115
all_instance_names.sort.each do |k|
116
  sth.execute(k)
117
  sth.fetch do |row|
118
    puts "#{k}.value #{row[0]}"
119
  end
120
end
121
sth.finish
122
dbh.disconnect