Archive for the ‘Nagios Script’ Category

Nagios script to monitor memory uses

Thursday, June 24th, 2010
#!/bin/bash
 
#Version 1.0
#######################################
#Nagios scrept to check memory status##
#Commands : free -m#####################
#######################################
 
 
#Status check for nagios script
 
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4
 
 
#Define All the variables for commands
 
declare -rx SCRIPT=${0##*/}
declare -rx CMD_AWK="/bin/awk"
declare  -rx CMD_CAT="/bin/cat"
declare  -rx CMD_FREE="/usr/bin/free"
#####Section 1.1 :Definning function for free memory checking########
#Definning function to check free memory status#####################
#####################################################################
 
function FUNC_FREE_CMD
 
{
 
MEM_STATUS=$( $CMD_FREE -m | grep buffers/cache | awk '{print $4}')
 
 
########Checking if Current memory is critial or normal ######
 
if [ $MEM_STATUS -le 325 ]
then
 
#echo "Critical,Memory Level: $MEM_STATUS"
echo "Critical,Memory Level: $MEM_STATUS|Memory_level=$MEM_STATUS;350;325;0"
exit $STATE_CRITICAL
fi
 
if [ $MEM_STATUS -le 350 ]
then
 
echo "Warnings,Memory Level: $MEM_STATUS|Memory_level=$MEM_STATUS;350;325;0"
exit $STATE_WARNING
 
else
echo "Memory Seems Ok,Total Memory is: $MEM_STATUS|Memory_level=$MEM_STATUS;350;325;0"
#echo "Critical,Memory Level: $MEM_STATUS|Memory_level=$MEM_STATUS"
exit $STATE_OK
fi
 
}
 
#############Section 1.2 calling  the function###############
######## And processing data from this fucntion##############
FUNC_FREE_CMD

Thanks

Nagios script to monitor memory uses

Friday, April 23rd, 2010

Purpose:
###########################################
Develop a nagios script, which will monitor Linux memory uses.
###########################################

This script will check following :
#############################################
#1.If free memory is more then the defined memory as free: Status Done
#2.If System is using swap memory : Status:Done
##############################################

#!/bin/bash
 
#Version 1.0
#######################################
#Nagios scrept to check memory status##
#Commands : fre -m#####################
#######################################
 
 
#Status check for nagios script
 
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4
 
 
#Define All the variables for commands
 
declare -rx SCRIPT=${0##*/}
declare -rx CMD_AWK="/bin/awk"
declare  -rx CMD_CAT="/bin/cat"
declare  -rx CMD_FREE="/usr/bin/free"
declare  -rx CMD_VMSTAT="/usr/bin/vmstat"
declare  -rx CMD_GREP="/bin/grep"
 
#####Section 1.1 :Definning function for free memory checking########
#Definning function to check free memory status#####################
##########################################
 
function FUNC_FREE_CMD
 
{
 
MEM_STATUS=$( $CMD_FREE -m | grep buffers/cache | awk '{print $4}')
 
 
########Checking if Current memory is critial or normal ######
 
if [ $MEM_STATUS -le 325 ]
then
 
#echo "Critical,Memory Level: $MEM_STATUS"
echo "Critical,Memory Level: $MEM_STATUS|Memory_level=$MEM_STATUS;350;325;0"
exit $STATE_CRITICAL
fi
 
if [ $MEM_STATUS -le 350 ]
then
 
echo "Warnings,Memory Level: $MEM_STATUS|Memory_level=$MEM_STATUS;350;325;0"
exit $STATE_WARNING
 
else
echo "Memory Seems Ok,Total Memory is: $MEM_STATUS|Memory_level=$MEM_STATUS;350;325;0"
#echo "Critical,Memory Level: $MEM_STATUS|Memory_level=$MEM_STATUS"
#exit $STATE_OK
fi
 
}
 
#####Section 2.1 Definning function for checking swap uses###########
#### Commands: free -m | grep Swap | awk '{print $3}################
###################################################################
 
function FUNC_FREE_SWAP_CMD
{
 
SWAP_STATUS=$( $CMD_FREE -m | grep Swap | awk '{print $3}')
 
if [ $SWAP_STATUS -ne 0 ]
then
echo "System is using swap:$SWAP_STATUS"
echo "Lets Try to find out how much swap system using by using vmstat output"
 
fi
 
}
 
######Section 3.1, Definning funtion , to check how much swap in and swap out for  5 seconds####
#####Commands : vmstat
###############################################################################################
 
 
 
function FUNC_VMSTAT_CMD
{
 
#echo $( $CMD_VMSTAT 3 5 | $CMD_GREP "^[ ][0-9]"|  $CMD_AWK 'BEGIN{for(n=1;n<=8;n++){printf("%s ", "Average Uses:" [3]/5)}}')
echo $( $CMD_VMSTAT 3 5 | $CMD_GREP "^[ ][0-9]"|  $CMD_AWK 'BEGIN{for(n=1;n<=8;n++){printf("Average Uses:" s[3]/5)}}')
 
}
 
 
#############Section 3.1 calling  all  functions###############
###Function from section 1.1:To Calculate Free memory##############
###Funciton from section 2.1:To calucate  Swap uses ###############
FUNC_FREE_CMD
FUNC_FREE_SWAP_CMD
FUNC_VMSTAT_CMD

nagios script for checking mysql server replication status between 4 servers

Monday, February 22nd, 2010

Ref : http://onlamp.com/pub/a/onlamp/2006/04/20/advanced-mysql-replication.html?page=2

Date:22/02/2010
This Script is still under development.

Purpose:

Develop a nagios script, which would be able to check replication status between 4 Master/Master Server.

This scripy will check following :

#1.Each Mysql servers are online : Stats: Done
#2.If Slave process is running : Status:Done
#3.If Slave IO process is running : Status:Done
#4.If There is any bin log position difference between Master/Slave :Status:Done
#5.Check time in processlist for(Has read all relay log; waiting for the slave I/O thread to update it) for further repliation related problems. Status: Under Development
#6.If problem found,change Master server info in Slave, and connect to differerent Master server. :Status :Under Development
#7. Change Dns record(A record) to stop comming http request to the problematic server:Status:Under Development

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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
 
 
#!/bin/bash
################################################################################
#SVN Version: 28
#Script Version 28
#Purpose of this script:
#Check Bellow options between 4 mysql Master/Master Replication
#1.Every Mysql servers are online: Status:Done
#2.If Slave process is running:    Status:Done
#3.If Slave IO process is running: Status:Done
#4.If There is any bin log position difference between Master/Slave: Status:Done
#5.Check time in processlist for:Status: In Development
#6.If problem found change  Master server info in Slave
#7. Change Dns record(A record) to stop comming http request to the problematic sever
#################################################################################
 
#Status check for nagios script
 
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4
 
 
#Define All the variables
 
declare -rx SCRIPT=${0##*/}
declare -rx CMD_AWK="/bin/awk"
declare -rx CMD_MYSQL="/usr/local/mysql/bin/mysql"
declare -rx CMD_GREP="/bin/grep"
 
##Define value for nagios is to fire for certain conditions for log check
 
CRITICAL_VALUE=100
CRITICAL_VALUE=50
NORMAL_VALUE=0
 
#######################################################################
#####Define All the servers Ip, username and password for mysql server#
#######################################################################
declare -rx SLV_MSTRServerA="Ip.of.your.server"
declare -rx SLV_MSTRServerA_USER="noslave"
declare -rx SLV_MSTRServerA_PASSWD="password"
 
#Slave/Master ServerB (NodeB-web.yourdomian.co.uk)
 
declare -rx SLV_MSTRServerB="ip.of.your.server"
declare -rx SLV_MSTRServerB_USER="noslave"
declare -rx SLV_MSTRServerB_PASSWD="password"
 
#Slave/Master ServerC (NodeC-http.yourdomain.local)
 
declare -rx SLV_MSTRServerC="localhost"
declare -rx SLV_MSTRServerC_USER="noslave"
declare -rx SLV_MSTRServerC_PASSWD="password"
 
 
#Slave/Master ServerD (NodeD-beaver.yourdomain.local)
 
declare -rx SLV_MSTRServerD="ip.of.your.server"
declare -rx SLV_MSTRServerD_USER="noslave"
declare -rx SLV_MSTRServerD_PASSWD="password"
 
#Global Variables
declare -a result
 
 
#Definning Variable for Array
 
declare -a SLV_MSTRServerS=($SLV_MSTRServerA $SLV_MSTRServerB $SLV_MSTRServerC $SLV_MSTRServerD)
declare -a SLV_MSTRServerS_USERS=($SLV_MSTRServerA_USER $SLV_MSTRServerB_USER $SLV_MSTRServerC_USER $SLV_MSTRServerD_USER)
declare -a SLV_MSTRServerS_PASSWD=($SLV_MSTRServerA_PASSWD $SLV_MSTRServerB_PASSWD $SLV_MSTRServerC_PASSWD $SLV_MSTRServerD_PASSWD )
declare -a SLV_ServerAppLS=("SLV_MAIL" "SLV_WEB" "SLV_HTTP" "SLV_BEVR")
declare -a MSTR_ServerAppLS=("MSTR_MAIL" "MSTR_WEB" "MSTR_HTTP" "MSTR_BEVR")
 
 
###########################################################
#Section-1.0:function-Command: My Mysql Servers are online#
###########################################################
 
function FUNC_CHECK_SERVER_ONLINE_CMD
{
 
$CMD_MYSQL -h "$1"  -u"$2" -p"$3" -e "show slave status\G" >/dev/null 2>&1
 
return $?
 
}
 
 
###########################################################
#Section-2.0: Function-Command:If Slave_IO_Running#########
###########################################################
 
function FUNC_CHK_SLV_IO_RUN_CMD
{
 
echo $($CMD_MYSQL -h "$1"  -u"$2" -p"$3" -e "show slave status\G" | $CMD_GREP Slave_IO_Running | awk '{ print $2 }' )
 
}
 
 
##########################################################
#Section-3.0: Function-Command:If Slave_SQL_running#######
##########################################################
 
function FUNC_CHK_SLV_SQL_RUN_CMD
{
 
echo $($CMD_MYSQL -h "$1"  -u"$2" -p"$3" -e "show slave status\G" | $CMD_GREP Slave_SQL_Running | awk '{ print $2 }' )
 
}
 
##########################################################
#Section-4.0: Function-Command:#######
##########################################################
 
function FUNC_CHK_SLV_LOG_POS_CMD
{
 
echo $($CMD_MYSQL -h "$1"  -u"$2" -p"$3" -e "show slave status\G" | $CMD_GREP Read_Master_Log_Pos | awk '{ print $2}' )
 
 
}
 
 
 
##########################################################
#Section-5.0: Function-Command:#######
##########################################################
 
function FUNC_CHK_MSTR_LOG_POS_CMD
{
 
echo $($CMD_MYSQL -h "$1"  -u"$2" -p"$3" -e "show master status" | $CMD_GREP bin | cut -f2 )
 
 
}
 
 
###########################################################
#Section-1.1: If all Mysql Server is Online################
##Implementing Secton 1.0(ref:FUNC_CHECK_SERVER_ONLINE_CMD#
###########################################################
 
function FUNC_CHECK_SERVER_ONLINE()
{
i=0
COUNT=${#SLV_MSTRServerS[*]}
while [ $i -lt $COUNT ]
do
 
if ! $(FUNC_CHECK_SERVER_ONLINE_CMD "${SLV_MSTRServerS[$i]}" "${SLV_MSTRServerS_USERS[$i]}" "${SLV_MSTRServerS_PASSWD[$i]}"  )
 
then
echo " Server IP: ${SLV_MSTRServerS[$i]},is not running "
exit $STATE_CRITICAL
exit 99
fi
 
 
i=$(($i+1))
 
done
 
#echo "All Server are Online"
#exit $STATE_OK
 
}
 
 
###########################################################
#Section-2.1: If Slave_IO_Running IS RUNNING OR NOT########
##Implementing Secton 2.0(ref:FUNC_CHK_SLV_IO_RUN_CMD######
###########################################################
 
 
 
function FUNC_CHK_SLV_IO_RUN()
{
 
i=0
COUNT=${#SLV_MSTRServerS[*]}
while [ $i -lt $COUNT ]
do
result[$i]=$(FUNC_CHK_SLV_IO_RUN_CMD "${SLV_MSTRServerS[$i]}" "${SLV_MSTRServerS_USERS[$i]}" "${SLV_MSTRServerS_PASSWD[$i]}" )
i=$(($i+1))
done
 
 
i=0
while [ $i -lt $COUNT ]
do
if [ ${result[$i]} != "Yes" ]
then
echo "In Server IP: ${SLV_MSTRServerS[$i]},Slave_IO_running is not running "
exit $STATE_CRITICAL
 
fi
i=$(($i + 1 ))
done
#echo "ALL Servers are running fine"
#exit $STATE_OK
 
}
 
###########################################################
#Section-3.1: IF Slave_SQL_Running OR NOT#################
##Implementing Secton 3.0(ref:FUNC_CHK_SLV_SQL_RUN_CMD#####
###########################################################
 
function FUNC_CHK_SLV_SQL_RUN()
 
{
i=0
COUNT=${#SLV_MSTRServerS[*]}
while [ $i -lt $COUNT ]
 
do
result[$i]=$(FUNC_CHK_SLV_SQL_RUN_CMD "${SLV_MSTRServerS[$i]}" "${SLV_MSTRServerS_USERS[$i]}" "${SLV_MSTRServerS_PASSWD[$i]}" )
i=$(($i+1))
done
 
 
i=0
while [ $i -lt $COUNT ]
do
if [ ${result[$i]} != "Yes" ]
then
echo "In Server IP: ${SLV_MSTRServerS[$i]},Slave_SQL_Running is not running "
exit $STATE_CRITICAL
 
fi
i=$(($i + 1 ))
done
 
 
}
 
###########################################################
#Section-4.1: GETTING MASTER LOG POSITION FROM EACH SLAVE##
##Implementing Secton 4.0(ref:FUNC_CHK_SLV_LOG_POS_CMD#####
###########################################################
 
 
function FUNC_CHK_SLV_LOG_POS ()
 
{
i=0
COUNT=${#SLV_MSTRServerS[*]}
while [ $i -lt $COUNT ]
 
do
result[$i]=$(FUNC_CHK_SLV_LOG_POS_CMD "${SLV_MSTRServerS[$i]}" "${SLV_MSTRServerS_USERS[$i]}" "${SLV_MSTRServerS_PASSWD[$i]}" )
i=$(($i+1))
done
 
i=0
while [ $i -lt $COUNT ]
do
eval ${SLV_ServerAppLS[$i]}=${result[$i]}
 
 
i=$(($i + 1 ))
done
 
 
}
 
###########################################################
#Section-5.1: GETTING MASTER LOG POSITION FROM EACH MASTER#
##Implementing Secton 5.0(ref:FUNC_CHK_MSTR_LOG_POS_CMD#####
###########################################################
 
 
 
function FUNC_CHK_MSTR_LOG_POS ()
 
{
i=0
COUNT=${#SLV_MSTRServerS[*]}
while [ $i -lt $COUNT ]
 
do
result[$i]=$(FUNC_CHK_MSTR_LOG_POS_CMD "${SLV_MSTRServerS[$i]}" "${SLV_MSTRServerS_USERS[$i]}" "${SLV_MSTRServerS_PASSWD[$i]}" )
i=$(($i+1))
done
 
 
i=0
while [ $i -lt $COUNT ]
do
eval ${MSTR_ServerAppLS[$i]}=${result[$i]}
 
 
i=$(($i + 1 ))
done
 
 
}
 
#########################################################################################
 
#Section-4.1/5.1: FIND OUT LOG POSITION DIFFERENCE#######################################
##Implementing Secton 4.1 and 5.1(ref:FUNC_CHK_SLV_LOG_POS) AND   FUNC_CHK_MSTR_LOG_POS##
#########################################################################################
 
 
function FUNC_FIND_LOG_POS_DIFF ()
 
{
################################################################
##Calling FUNC_CHK_SRV_LOG_POS and FUNC_CHK_MSTR_LOG_POS########
## To Get All the output from Servers###########################
################################################################
 
FUNC_CHK_SLV_LOG_POS
FUNC_CHK_MSTR_LOG_POS
 
echo "Master log position from each master(show master status)"
echo "mail: $MSTR_MAIL"
echo "web: $MSTR_WEB"
echo "http: $MSTR_HTTP"
echo " beaver: $MSTR_BEVR"
 
 
 
echo "MAster log position from each  Slave(show slave status) "
echo "mail: $SLV_MAIL"
echo "web: $SLV_WEB"
echo "http: $SLV_HTTP"
echo "beaver: $SLV_BEVR"
 
 
 
 
### note : $SLV_MAIL will be equal to $MSTR_BEVR
### Note : $SLV_WEB will be equal to $MSTR_MAIL
### Note : $SLV_HTTP will be equal to $MSTR_WEB
### Note : $SLV_BEVR will be equal to $MSTR_HTTP
 
if [ $SLV_MAIL -ne $MSTR_BEVR ]
 
then
echo "Problem between Server Mail: $SLV_MAIL and Server BEVR:$MSTR_BEVR"
exit $STATE_CRITICAL
fi
 
if [ $SLV_WEB -ne $MSTR_MAIL ]
 
then
 
echo "Problem between Server WEB:$SLV_WEB and Server MAIL:$MSTR_MAIL"
exit $STATE_CRITICAL
fi
 
if [ $SLV_HTTP -ne $MSTR_WEB ]
 
then
echo "Problem between Server HTTP:$SLV_HTTP  and Server WEB:$MSTR_WEB "
exit $STATE_CRITICAL
fi
 
if [ $SLV_BEVR -ne $MSTR_HTTP ]
 
then
echo "Problem between Server BEVR:$SLV_BEVR and Server HTTP:$MSTR_HTTP"
exit $STATE_CRITICAL
fi
 
 
}
 
 
######################################################################
#Calling all function from  section [1.1,2.1,3.1,4.1,5.1]#############
######################################################################
 
FUNC_CHECK_SERVER_ONLINE
FUNC_CHK_SLV_IO_RUN
FUNC_CHK_SLV_SQL_RUN
FUNC_FIND_LOG_POS_DIFF
 
 
#####################################################################
## If there is not any error from section[1.1,2.1,3.1,4.1,5.1]#######
## then show bellow commands to ensure nagios all OK ################
#####################################################################
 
 
 
#echo $SLV_HTTP
#echo $SLV_BEVR
 
#echo $MSTR_MAIL
#echo $MSTR_WEB
#echo $MSTR_HTTP
#echo $MSTR_BEVR
 
echo "ALL Servers are running fine"
exit $STATE_OK