Projet

Général

Profil

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

root / plugins / mssql / microsoft-sql @ b0b39b01

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

1 7a37bfb1 Lars Kruse
#!/usr/bin/env ruby
2 b0b39b01 Lars Kruse
3
=begin
4
5
Munin Plugin for MSSQL - transaction monitoring
6
7
Author: Wilfred Chau <openapp.developer@gmail.com>
8
Date: 2011-05-18
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_transaction.rb /etc/munin/plugins/mssql_transaction.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 c0dc3b1b Wilfred Chau
#%# family=auto
47
#%# capabilities=autoconf
48
49 b0b39b01 Lars Kruse
=end
50
51
52 c0dc3b1b Wilfred Chau
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 b0b39b01 Lars Kruse
dbh = DBI.connect("DBI:ODBC:#{dsn}", sqluser, sqlpass)
65 c0dc3b1b Wilfred Chau
66 17f78427 Lars Kruse
instance_name_query = "SELECT distinct instance_name
67
                       FROM sys.dm_os_performance_counters
68 c0dc3b1b Wilfred Chau
                       WHERE instance_name = '#{instance}'
69
		       and object_name = 'SQLServer:Databases'
70
                       order by instance_name"
71
72
transaction_query = "select cntr_value from sys.dm_os_performance_counters
73
                     where counter_name = 'transactions/sec'
74
                     and object_name = 'SQLServer:Databases'
75
                     and instance_name = ?"
76
77 b0b39b01 Lars Kruse
all_instance_names = Array.new
78 c0dc3b1b Wilfred Chau
sth = dbh.execute(instance_name_query)
79
sth.fetch do |row|
80 b0b39b01 Lars Kruse
  all_instance_names.push(row[0].strip)
81 c0dc3b1b Wilfred Chau
end
82
sth.finish
83
84
#
85
# autoconf
86
#
87
if ARGV[0] == "autoconf"
88 b0b39b01 Lars Kruse
  if all_instance_names.length > 1 && sqluser.length > 1 && sqlpass.length > 1
89
    puts "yes"
90
  else
91
    puts "no"
92
    puts "Usage: #{__FILE__} autoconf|conf"
93
  end
94
  exit 0
95 c0dc3b1b Wilfred Chau
#
96
# config definition
97
#
98
elsif ARGV[0] == "config"
99 b0b39b01 Lars Kruse
  puts "graph_args --base 1000 -r --lower-limit 0"
100
  puts "graph_title MSSQL Transactions/s"
101
  puts "graph_category db"
102
  puts "graph_info This graph shows transactions/s"
103
  puts "graph_vlabel transactions/s"
104
  puts "graph_scale no"
105
  puts "graph_period second"
106
107
  all_instance_names.sort.each do |s|
108
    puts "#{s}.label #{s}"
109
    puts "#{s}.info INSTANCE: #{s}"
110
    puts "#{s}.type DERIVE"
111
    puts "#{s}.draw LINE1"
112
  end
113
114
  exit 0
115 c0dc3b1b Wilfred Chau
end
116
117
#
118
# Getting data
119
#
120
sth = dbh.prepare(transaction_query)
121
all_instance_names.sort.each do |k|
122 b0b39b01 Lars Kruse
  sth.execute(k)
123
  sth.fetch do |row|
124
    # since type is DERIVE, need to convert value to integer then to string
125
    puts "#{k.to_s}.value #{row[0].to_i.to_s}"
126
  end
127 c0dc3b1b Wilfred Chau
end
128
sth.finish
129
dbh.disconnect