Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
check_mysql_slave
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
nimrod
check_mysql_slave
Commits
7ac34e81
Commit
7ac34e81
authored
4 years ago
by
nimrod
Browse files
Options
Downloads
Patches
Plain Diff
Format with Black.
parent
3129a88d
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
check_mysql_slave/__init__.py
+56
-50
56 additions, 50 deletions
check_mysql_slave/__init__.py
with
56 additions
and
50 deletions
check_mysql_slave/__init__.py
+
56
−
50
View file @
7ac34e81
#!/usr/bin/env python
#!/usr/bin/env python
'''
Check MySQL seconds behind master for Nagios-like monitoring.
'''
"""
Check MySQL seconds behind master for Nagios-like monitoring.
"""
from
__future__
import
(
absolute_import
,
division
,
print_function
,
from
__future__
import
(
unicode_literals
)
absolute_import
,
division
,
print_function
,
unicode_literals
,
)
import
argparse
import
argparse
from
argparse
import
ArgumentParser
from
argparse
import
ArgumentParser
try
:
try
:
from
MySQLdb
import
connect
from
MySQLdb
import
connect
except
ImportError
:
except
ImportError
:
print
(
'
Failed to import MySQLdb. Is mysqlclient installed?
'
)
print
(
"
Failed to import MySQLdb. Is mysqlclient installed?
"
)
exit
(
3
)
exit
(
3
)
def
getSlaveStatus
(
host
,
user
,
passwd
,
port
):
def
getSlaveStatus
(
host
,
user
,
passwd
,
port
):
'''
Returns a dictionary of the
'
SHOW SLAVE STATUS;
'
command output.
'''
"""
Returns a dictionary of the
'
SHOW SLAVE STATUS;
'
command output.
"""
try
:
try
:
conn
=
connect
(
user
=
user
,
passwd
=
passwd
,
host
=
host
,
port
=
port
)
conn
=
connect
(
user
=
user
,
passwd
=
passwd
,
host
=
host
,
port
=
port
)
except
BaseException
as
e
:
except
BaseException
as
e
:
print
(
'
Failed to connect.
'
)
print
(
"
Failed to connect.
"
)
exit
(
3
)
exit
(
3
)
cur
=
conn
.
cursor
()
cur
=
conn
.
cursor
()
cur
.
execute
(
'''
SHOW SLAVE STATUS;
'''
)
cur
.
execute
(
"""
SHOW SLAVE STATUS;
"""
)
keys
=
[
desc
[
0
]
for
desc
in
cur
.
description
]
keys
=
[
desc
[
0
]
for
desc
in
cur
.
description
]
values
=
cur
.
fetchone
()
values
=
cur
.
fetchone
()
return
dict
(
zip
(
keys
,
values
))
return
dict
(
zip
(
keys
,
values
))
def
main
():
def
main
():
parser
=
ArgumentParser
(
description
=
'
Check MySQL seconds behind master for Nagios-like monitoring.
'
)
parser
=
ArgumentParser
(
parser
.
add_argument
(
'
-u
'
,
description
=
"
Check MySQL seconds behind master for Nagios-like monitoring.
"
'
--user
'
,
)
help
=
'
Login username
'
,
parser
.
add_argument
(
required
=
True
,
"
-u
"
,
"
--user
"
,
help
=
"
Login username
"
,
required
=
True
,
nargs
=
"
?
"
nargs
=
'
?
'
)
)
parser
.
add_argument
(
'
-p
'
,
parser
.
add_argument
(
'
--password
'
,
"
-p
"
,
"
--password
"
,
help
=
"
Login password
"
,
required
=
True
,
nargs
=
"
?
"
help
=
'
Login password
'
,
)
required
=
True
,
parser
.
add_argument
(
nargs
=
'
?
'
)
"
--host
"
,
help
=
"
Login host
"
,
nargs
=
"
?
"
,
default
=
"
localhost
"
parser
.
add_argument
(
'
--host
'
,
)
help
=
'
Login host
'
,
parser
.
add_argument
(
nargs
=
'
?
'
,
"
--port
"
,
help
=
"
Login port
"
,
nargs
=
1
,
type
=
int
,
default
=
3306
default
=
'
localhost
'
)
)
parser
.
add_argument
(
'
--port
'
,
parser
.
add_argument
(
help
=
'
Login port
'
,
"
warning_threshold
"
,
nargs
=
1
,
help
=
"
Warning threshold (defaults to 60)
"
,
type
=
int
,
default
=
3306
)
parser
.
add_argument
(
'
warning_threshold
'
,
help
=
'
Warning threshold (defaults to 60)
'
,
default
=
60
,
default
=
60
,
type
=
int
,
type
=
int
,
nargs
=
'
?
'
)
nargs
=
"
?
"
,
parser
.
add_argument
(
'
critical_threshold
'
,
)
help
=
'
Critical threshold (defualts to 300)
'
,
parser
.
add_argument
(
"
critical_threshold
"
,
help
=
"
Critical threshold (defualts to 300)
"
,
default
=
300
,
default
=
300
,
type
=
int
,
type
=
int
,
nargs
=
'
?
'
)
nargs
=
"
?
"
,
)
args
=
parser
.
parse_args
()
args
=
parser
.
parse_args
()
status
=
getSlaveStatus
(
host
=
args
.
host
,
status
=
getSlaveStatus
(
user
=
args
.
user
,
host
=
args
.
host
,
user
=
args
.
user
,
passwd
=
args
.
password
,
port
=
args
.
port
passwd
=
args
.
password
,
)
port
=
args
.
port
)
if
(
if
not
'
Slave_IO_Running
'
in
status
or
not
'
Slave_SQL_Running
'
in
status
or
not
status
[
not
"
Slave_IO_Running
"
in
status
'
Slave_IO_Running
'
]
==
'
Yes
'
or
not
status
[
or
not
"
Slave_SQL_Running
"
in
status
'
Slave_SQL_Running
'
]
==
'
Yes
'
:
or
not
status
[
"
Slave_IO_Running
"
]
==
"
Yes
"
print
(
'
Replication is turned off.
'
)
or
not
status
[
"
Slave_SQL_Running
"
]
==
"
Yes
"
):
print
(
"
Replication is turned off.
"
)
exit
(
0
)
exit
(
0
)
lag
=
status
[
'
Seconds_Behind_Master
'
]
lag
=
status
[
"
Seconds_Behind_Master
"
]
if
lag
>
args
.
critical_threshold
:
if
lag
>
args
.
critical_threshold
:
print
(
'
Seconds behind master is above the critical threshold.
'
)
print
(
"
Seconds behind master is above the critical threshold.
"
)
exit
(
2
)
exit
(
2
)
elif
lag
>
args
.
warning_threshold
:
elif
lag
>
args
.
warning_threshold
:
print
(
'
Seconds behind master is above the warning threshold.
'
)
print
(
"
Seconds behind master is above the warning threshold.
"
)
exit
(
1
)
exit
(
1
)
else
:
else
:
print
(
'
Seconds behind master is below the warning threshold.
'
)
print
(
"
Seconds behind master is below the warning threshold.
"
)
exit
(
0
)
exit
(
0
)
if
__name__
==
'
__main__
'
:
if
__name__
==
"
__main__
"
:
main
()
main
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment