Commit a27f8b56 authored by nimrod's avatar nimrod
Browse files

pre-commit.

- Use common Python project pre-commit config.
- Address issues.
- Use the same description in the CLI and the package.
- Fail on importing the MySQL client inside the function (allow
importing without errors).
parent 7ac34e81
Loading
Loading
Loading
Loading
+87 −0
Original line number Diff line number Diff line
---
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
+21 −18
Original line number Diff line number Diff line
@@ -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


DESCRIPTION = "Check MySQL seconds behind master for Nagios-like monitoring."


def getSlaveStatus(host, user, passwd, port):
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__":
+30 −24
Original line number Diff line number Diff line
#!/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',
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 :: 2', 'Topic :: Utilities',
          'License :: OSI Approved :: MIT License'
        "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',
    keywords="nagios mysql slave replication monitoring",
    packages=find_packages(),
      install_requires=['MySQL-python'],
    install_requires=["MySQL-python"],
    entry_points={
          'console_scripts': [
              'check_mysql_slave=check_mysql_slave:main'
          ],
      }, )
        "console_scripts": ["check_mysql_slave=check_mysql_slave:main"],
    },
)