diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000000000000000000000000000000000000..5f769c018664bb72fb78fdf20da523f900513221 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,87 @@ +--- +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.4.0 + hooks: + - id: check-executables-have-shebangs + - id: check-merge-conflict + - id: check-toml + files: Pipfile + - id: trailing-whitespace + + - repo: https://github.com/Yelp/detect-secrets + rev: v0.14.3 + hooks: + - id: detect-secrets + + - repo: https://github.com/adrienverge/yamllint + rev: v1.25.0 + hooks: + - id: yamllint + + - repo: https://github.com/amperser/proselint/ + rev: 0.10.2 + hooks: + - id: proselint + types: [plain-text] + exclude: LICENSE + + - repo: https://github.com/ambv/black + rev: 20.8b1 + hooks: + - id: black + args: + - | + --line-length=79 + + - repo: https://github.com/Lucas-C/pre-commit-hooks-markup + rev: v1.0.1 + hooks: + - id: rst-linter + + - repo: https://github.com/myint/rstcheck.git + rev: master + hooks: + - id: rstcheck + + - repo: https://github.com/PyCQA/prospector + rev: 1.3.1 + hooks: + - id: prospector + args: + - |- + --max-line-length=79 + - |- + --with-tool=pyroma + - |- + --with-tool=bandit + - |- + --without-tool=pep257 + - |- + --doc-warnings + - |- + --test-warnings + - |- + --full-pep8 + - |- + --strictness=high + - |- + --no-autodetect + additional_dependencies: + - bandit + - pyroma + + - repo: https://gitlab.com/pycqa/flake8.git + rev: 3.8.4 + hooks: + - id: flake8 + args: + - |- + --doctests + additional_dependencies: + - flake8-bugbear + + - repo: https://github.com/mgedmin/check-manifest + rev: '0.45' + hooks: + - id: check-manifest diff --git a/check_mysql_slave/__init__.py b/check_mysql_slave/__init__.py index 69ee4bdb39610432f3cfd8cbb831f4b7b51f8094..9914b0dba509eebc3e09f5bfb23db48a46792739 100644 --- a/check_mysql_slave/__init__.py +++ b/check_mysql_slave/__init__.py @@ -7,23 +7,28 @@ from __future__ import ( print_function, unicode_literals, ) -import argparse from argparse import ArgumentParser +import sys try: from MySQLdb import connect except ImportError: - print("Failed to import MySQLdb. Is mysqlclient installed?") - exit(3) + pass -def getSlaveStatus(host, user, passwd, port): +DESCRIPTION = "Check MySQL seconds behind master for Nagios-like monitoring." + + +def get_slave_status(host, user, passwd, port): """Returns a dictionary of the 'SHOW SLAVE STATUS;' command output.""" try: conn = connect(user=user, passwd=passwd, host=host, port=port) - except BaseException as e: + except NameError: + print("Failed to import MySQLdb. Is mysqlclient installed?") + sys.exit(3) + except BaseException: # pylint: disable=broad-except print("Failed to connect.") - exit(3) + sys.exit(3) cur = conn.cursor() cur.execute("""SHOW SLAVE STATUS;""") keys = [desc[0] for desc in cur.description] @@ -32,9 +37,7 @@ def getSlaveStatus(host, user, passwd, port): def main(): - parser = ArgumentParser( - description="Check MySQL seconds behind master for Nagios-like monitoring." - ) + parser = ArgumentParser(description=DESCRIPTION) parser.add_argument( "-u", "--user", help="Login username", required=True, nargs="?" ) @@ -62,27 +65,27 @@ def main(): nargs="?", ) args = parser.parse_args() - status = getSlaveStatus( + status = get_slave_status( host=args.host, user=args.user, passwd=args.password, port=args.port ) if ( - not "Slave_IO_Running" in status - or not "Slave_SQL_Running" in status - or not status["Slave_IO_Running"] == "Yes" - or not status["Slave_SQL_Running"] == "Yes" + "Slave_IO_Running" not in status + or "Slave_SQL_Running" not in status + or status["Slave_IO_Running"] != "Yes" + or status["Slave_SQL_Running"] != "Yes" ): print("Replication is turned off.") - exit(0) + sys.exit(0) lag = status["Seconds_Behind_Master"] if lag > args.critical_threshold: print("Seconds behind master is above the critical threshold.") - exit(2) + sys.exit(2) elif lag > args.warning_threshold: print("Seconds behind master is above the warning threshold.") - exit(1) + sys.exit(1) else: print("Seconds behind master is below the warning threshold.") - exit(0) + sys.exit(0) if __name__ == "__main__": diff --git a/setup.py b/setup.py index 51c24cea8d6e22a89f75e4c038da00d34cfd4f0d..3b1f589b4ecc46e6d2addd3f263e7e637e74ec54 100644 --- a/setup.py +++ b/setup.py @@ -1,27 +1,33 @@ #!/usr/bin/env python from setuptools import setup, find_packages +from check_mysql_slave import DESCRIPTION -setup(name='check_mysql_slave', - version=open('VERSION', 'r').read(), - description='''Check MySQL seconds behind master for Nagios-like - monitoring.''', - long_description=open('README.rst', 'r').read(), - url='https://www.shore.co.il/git/check_mysql_slave', - author='Nimrod Adar', - author_email='nimrod@shore.co.il', - license='MIT', - classifiers=[ - 'Development Status :: 4 - Beta', - 'Intended Audience :: System Administrators', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 2', 'Topic :: Utilities', - 'License :: OSI Approved :: MIT License' - ], - keywords='nagios mysql slave replication monitoring', - packages=find_packages(), - install_requires=['MySQL-python'], - entry_points={ - 'console_scripts': [ - 'check_mysql_slave=check_mysql_slave:main' - ], - }, ) +setup( + name="check_mysql_slave", + version=open("VERSION", "r").read(), + description=DESCRIPTION, + long_description=open("README.rst", "r").read(), + url="https://www.shore.co.il/git/check_mysql_slave", + author="Nimrod Adar", + author_email="nimrod@shore.co.il", + license="MIT", + classifiers=[ + "Development Status :: 4 - Beta", + "Intended Audience :: System Administrators", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 2", + "Programming Language :: Python :: 2.7", + "Topic :: Utilities", + "License :: OSI Approved :: MIT License", + ], + keywords="nagios mysql slave replication monitoring", + packages=find_packages(), + install_requires=["MySQL-python"], + entry_points={ + "console_scripts": ["check_mysql_slave=check_mysql_slave:main"], + }, +)