forked from auxesis/cucumber-nagios
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mysql_steps.rb
236 lines (192 loc) · 7.3 KB
/
mysql_steps.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
require 'rubygems'
require 'mysql'
# simple functions to get at the number of currently running processes
# on a mysql server
def get_process_count()
my = Mysql.connect(@host, @username, @password)
results = my.query('SHOW PROCESSLIST;')
@processes = results.size
end
# lots of helper funtions to retrieve information from the GLOBAL STATUS
# table where MySQL stores information about it's current state
def get_connection_count()
my = Mysql.connect(@host, @username, @password)
results = my.query("SHOW GLOBAL STATUS where Variable_name = 'Connections';")
@connections = results.size
end
def get_threads_connected_count()
my = Mysql.connect(@host, @username, @password)
results = my.query("SHOW GLOBAL STATUS where Variable_name = 'Threads_connected';")
@threads_connected = results.size
end
def get_threads_running_count()
my = Mysql.connect(@host, @username, @password)
results = my.query("SHOW GLOBAL STATUS where Variable_name = 'Threads_running';")
@threads_running = results.size
end
def get_cached_queries_count()
my = Mysql.connect(@host, @username, @password)
results = my.query("SHOW GLOBAL STATUS where Variable_name = 'Qcache_queries_in_cache';")
@cached_queries = results.size
end
def get_queries_per_second()
my = Mysql.connect(@host, @username, @password)
query = "SHOW GLOBAL STATUS where Variable_name = 'Com_update' or Variable_name = 'Com_select' or Variable_name = 'Com_delete' or Variable_name = 'Com_insert';"
results = my.query(query)
starting = results.fetch_row[1].to_i + results.fetch_row[1].to_i + results.fetch_row[1].to_i + results.fetch_row[1].to_i
sleep 10
results = my.query(query)
ending = results.fetch_row[1].to_i + results.fetch_row[1].to_i + results.fetch_row[1].to_i + results.fetch_row[1].to_i
# now calculate the difference and divide by 10
(ending.to_i-starting.to_i)/10
end
def get_results_per_second(query)
my = Mysql.connect(@host, @username, @password)
results = my.query(query)
starting = results.fetch_row[1]
sleep 10
results = my.query(query)
ending = results.fetch_row[1]
# now calculate the difference and divide by 10
(ending.to_i-starting.to_i)/10
end
def get_updates_per_second()
@updates_per_second = get_results_per_second("SHOW GLOBAL STATUS where Variable_name = 'Com_update';")
end
def get_selects_per_second()
@selects_per_second = get_results_per_second("SHOW GLOBAL STATUS where Variable_name = 'Com_select';")
end
def get_deletes_per_second()
@deletes_per_second = get_results_per_second("SHOW GLOBAL STATUS where Variable_name = 'Com_delete';")
end
def get_inserts_per_second()
@inserts_per_second = get_results_per_second("SHOW GLOBAL STATUS where Variable_name = 'Com_insert';")
end
def get_cache_hits_per_second()
@cache_hits_per_second = get_results_per_second("SHOW GLOBAL STATUS where Variable_name = 'Qcache_hots';")
end
def get_questions_per_second()
@questions_per_second = get_results_per_second("SHOW GLOBAL STATUS where Variable_name = 'Questions';")
end
def get_slow_queries_per_second()
@slow_queries_per_second = get_results_per_second("SHOW GLOBAL STATUS where Variable_name = 'Slow_queries';")
end
# specify the username and password if we're testing a default local install
Before do
@username = 'root'
@password = ''
end
# Step definitions for testing the state of the MySQL server
Given /I have a MySQL server on (.+)$/ do |host|
@host = host
end
And /I use the username (.+)$/ do |username|
@username = username
end
And /I use the password (.+)$/ do |password|
@password = password
end
Then /it should have less than (\d+) processes$/ do |processes|
get_process_count()
@processes.should < processes.to_i
end
Then /it should have less than (\d+) connections$/ do |connections|
get_connection_count()
@connections.should < connections.to_i
end
Then /it should have less than (\d+) threads running$/ do |threads|
get_threads_running_count()
@threads_running.should < threads.to_i
end
Then /it should have less than (\d+) threads connected$/ do |threads|
get_threads_connected_count()
@threads_connected.should < threads.to_i
end
Then /it should have at least (\d+) queries cached$/ do |cached_queries|
get_cached_queries_count()
@cached_queries.should >= cached_queries.to_i
end
Then /it should have less than (\d+) update queries per second$/ do |queries|
get_updates_per_second()
@updates_per_second.should < queries.to_i
end
Then /it should have less than (\d+) insert queries per second$/ do |queries|
get_inserts_per_second()
@inserts_per_second.should < queries.to_i
end
Then /it should have less than (\d+) delete queries per second$/ do |queries|
get_deletes_per_second()
@deletes_per_second.should < queries.to_i
end
Then /it should have less than (\d+) selects queries per second$/ do |queries|
get_selects_per_second()
@selects_per_second.should < queries.to_i
end
Then /it should have less than (\d+) queries per second$/ do |queries|
get_queries_per_second()
@queries_per_second.should < queries.to_i
end
Then /it should have less than (\d+) cache hits per second$/ do |queries|
get_cache_hits_per_second()
@cache_hits_per_second.should < queries.to_i
end
Then /it should have less than (\d+) slow queries per second$/ do |queries|
get_slow_queries_per_second()
@slow_queries_per_second.should < queries.to_i
end
Then /it should have less than (\d+) questions per second$/ do |queries|
get_questions_per_second()
@questions_per_second.should < queries.to_i
end
Then /it should have at least (\d+) processes$/ do |processes|
get_process_count()
@processes.should >= processes.to_i
end
Then /it should have at least (\d+) connections$/ do |connections|
get_connection_count()
@connections.should >= connections.to_i
end
Then /it should have at least (\d+) threads running$/ do |threads|
get_threads_running_count()
@threads_running.should >= threads.to_i
end
Then /it should have at least (\d+) threads connected$/ do |threads|
get_threads_connected_count()
@threads_connected.should >= threads.to_i
end
Then /it should have at least (\d+) queries cached$/ do |cached_queries|
get_cached_queries_count()
@cached_queries.should >= cached_queries.to_i
end
Then /it should have at least (\d+) update queries per second$/ do |queries|
get_updates_per_second()
@updates_per_second.should >= queries.to_i
end
Then /it should have at least (\d+) insert queries per second$/ do |queries|
get_inserts_per_second()
@inserts_per_second.should >= queries.to_i
end
Then /it should have at least (\d+) delete queries per second$/ do |queries|
get_deletes_per_second()
@deletes_per_second.should >= queries.to_i
end
Then /it should have at least (\d+) selects queries per second$/ do |queries|
get_selects_per_second()
@selects_per_second.should >= queries.to_i
end
Then /it should have at least (\d+) queries per second$/ do |queries|
get_queries_per_second()
@queries_per_second.should >= queries.to_i
end
Then /it should have at least (\d+) cache hits per second$/ do |queries|
get_cache_hits_per_second()
@cache_hits_per_second.should >= queries.to_i
end
Then /it should have at least (\d+) slow queries per second$/ do |queries|
get_slow_queries_per_second()
@slow_queries_per_second.should >= queries.to_i
end
Then /it should have at least (\d+) questions per second$/ do |queries|
get_questions_per_second()
@questions_per_second.should >= queries.to_i
end