Categorygithub.com/danclough/mysql-healthcheck
modulepackage
1.1.0
Repository: https://github.com/danclough/mysql-healthcheck.git
Documentation: pkg.go.dev

# README

mysql-healthcheck

Go Go Report Card reviewdog Build Release License Downloads Issues

A self-contained binary to run health checks on MySQL and MariaDB clusters. Supports Percona XtraDB, Galera, and other wsrep-based clustering applications.

Installation

Linux

  1. Download and extract the appropriate binary for your architecture to /usr/local/bin/
  2. Make the binary executable with chmod +x /usr/local/bin/mysql-healthcheck
  3. Create a configuration file as instructed below.
  4. For systemd installation:
    1. Place the mysql-healthcheck.service unit file in your systemd system service directory (usually /etc/systemd/system/).
    2. Enable the service with systemd enable mysql-healthcheck.service
  5. Run the application from the command line, or run systemd start mysql-healthcheck to start the service.

Windows

  1. Download and unzip the appropriate binary to any filesystem location.
  2. Create a configuration file as instructed below.
  3. Run the application from the command line, or follow the appropriate steps for your platform to run it as an OS service.

Other Platforms

Compile a binary for your target OS and architecture with go build -ldflags="-X 'main.version=custom'".

Usage

  -V    Print version and exit
  -d    Run as a daemon and listen for HTTP connections on a socket
  -v    Verbose (debug) logging

The application will default to standalone mode, running one check and sending the result to stdout and setting the exit code accordingly. This can be used for non-HTTP-based health checking needs, or to test changes to your config file.

Example:

root@database01:~# mysql-healthcheck
time="2020-04-26T05:00:30Z" level=info msg="MySQL cluster node is ready."

root@database01:~# mysql-healthcheck -v
time="2020-04-26T05:00:31Z" level=debug msg="Config loaded from /etc/default/mysql-healthcheck.yaml"
time="2020-04-26T05:00:31Z" level=debug msg="Constructed DSN for MySQL: testuser:<redacted>@tcp(database01.mydomain.net:3306)/?timeout=1s&tls=true"
time="2020-04-26T05:00:31Z" level=debug msg="Running standalone health check."
time="2020-04-26T05:00:31Z" level=info msg="MySQL cluster node is ready."

Configuration

Location

Config files must be located in one of the following locations:

  • Linux
    • /etc/sysconfig/
    • /etc/default/
    • /etc/
    • $HOME/.config/
  • Windows
    • %PROGRAMFILES%
    • %LOCALAPPDATA%
  • All Platforms
    • Current working directory of the application

Syntax

Config files can be stored in any format supported by Viper, including JSON, TOML, YAML, and more.

The config file must be named mysql-healthcheck followed by the appropriate suffix for the file format (e.g. .yaml, .json)

Parameters

  • connection: Parameters pertaining to the database connection
    • host: The hostname or IP address of the database server (default: localhost)
    • port: The port to connect to MySQL (default: 3306)
    • user: A username to authenticate to the database server (optional)
    • password: The password of the configured user (optional)
    • tls: Parameters pertaining to connection-level encryption
      • required: If true, require TLS encryption on the connection (default: false)
      • skip-verify: If true, accept any certificate without question (default: false)
      • ca: File path to a trusted CA certificate in PEM format (optional)
      • cert: File path to a client certificate in PEM format (optional)
      • key: File path to a client private key in PEM format (optional)
  • http: Parameters pertaining to running mysql-healthcheck as a service with the -d flag
    • addr: Address to listen on (default: :: (All v4/v6 addresses))
    • port: Port to bind to (default: 5678)
    • path: URI path to serve health checks at - for example, /status or /health (default: /)
  • options: Parameters pertaining to health checks
    • available_when_donor: If true, nodes that are donors for SST will be reported as available (default: false)
    • available_when_readonly: If true, nodes that are in read-only mode due to donor activities will be reported as available (default: false)

Example

connection:
  host: database01.mydomain.net
  user: testuser
  password: WA68fARS1TZz2NkK
  tls:
    required: true
    skip-verify: false
    ca: /etc/ssl/certs/my_ca.pem
    cert: /etc/ssl/certs/client_cert.pem
    key: /etc/ssl/private/client_key.pem
http:
  addr: 10.24.20.48
  port: 8080
  path: /status
options:
  available_when_donor: false
  available_when_readonly: false

# Functions

BuildDSN constructs a MySQL DSN from the provided connection config.
CreateConfig creates a new config instance.
CreateDBHandler instantiates a new DBHandler struct to hold the database connection and associated options.
NewHTTPServerHandler creates a new HTTPServerHandler with the supplied config and dbHandlers.
RunStatusCheck queries the current state of the database and returns a boolean and status message indicating if the database is available.

# Constants

AppName defines the name this application.
Available means the node is ready to serve requests.
Donor means the node is providing SST to a joining node.
nolint // Not explicitly used yet, but here for reference.
Joining means the node is in process of joining the cluster.
NotReady means the node is not available or not ready to serve requests.
ReadOnly means the node is in read-only mode.
Synced means the node is in the cluster and fully operational.
Unavailable means we are unable to connect to the node.

# Structs

DBHandler encapsulates all required objects to manage a database connection and run status checks.
HTTPServerHandler encapsulates all required objects to manage an HTTP server instance.

# Type aliases

ServerStatus represents the state of the database server.
WsrepStatus represents the state of the wsrep process on the database server.