Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion mysqlchk.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import "net/http"
var db *sql.DB
var wsrepStmt *sql.Stmt
var readOnlyStmt *sql.Stmt
var maxConnStmt *sql.Stmt
var connUsedStmt *sql.Stmt

var username = flag.String("username", "clustercheckuser", "MySQL Username")
var password = flag.String("password", "clustercheckpassword!", "MySQL Password")
Expand All @@ -32,6 +34,8 @@ func init() {
func checkHandler(w http.ResponseWriter, r *http.Request) {
var fieldName, readOnly string
var wsrepState int
var maxConn int
var connUsed int

if _, err := os.Stat(*forceUpFile); err == nil {
fmt.Fprint(w, "Cluster node OK by manual override\n")
Expand All @@ -43,7 +47,25 @@ func checkHandler(w http.ResponseWriter, r *http.Request) {
return
}

err := wsrepStmt.QueryRow().Scan(&fieldName, &wsrepState)
err := maxConnStmt.QueryRow().Scan(&fieldName, &maxConn)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

err = connUsedStmt.QueryRow().Scan(&fieldName, &connUsed)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

if maxConn <= (connUsed + 10) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add runtime flags for this feature. Either one to enable connection count checking and another to set the limit (10 by default), or just a limit (0 by default, meaning the check is disabled).

http.Error(w, "Cluster node is unavailable Max connections", http.StatusServiceUnavailable)
} else {
log.Println("Number of free connections", maxConn-connUsed)
}

err = wsrepStmt.QueryRow().Scan(&fieldName, &wsrepState)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down Expand Up @@ -96,6 +118,16 @@ func main() {
log.Fatal(err)
}

maxConnStmt, err = db.Prepare("show variables like 'max_connections'")
if err != nil {
log.Fatal(err)
}

connUsedStmt, err = db.Prepare("show status where variable_name = 'Threads_connected'")
if err != nil {
log.Fatal(err)
}

log.Println("Listening...")
http.HandleFunc("/", checkHandler)
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", *bindAddr, *bindPort), nil))
Expand Down
10 changes: 10 additions & 0 deletions mysqlchk.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Unit]
Description=Mysqlchk check
After=mariadb.service

[Service]
ExecStart=/usr/bin/mysqlchk

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably have a way to pass arguments to mysqlchk so people can override the default settings. How about using EnvironmentFile and passing something like $MYSQLCHK_ARGS on this line.

We should also drop privileges to nobody or something, there's no reason for this to run as root.

Restart=always

[Install]
WantedBy=multi-user.target