summaryrefslogtreecommitdiffstats
path: root/src/go/plugin/go.d/modules/mysql/collect_version.go
blob: b85922e2c6e1330c9ee606dcc4338d9a22f4153e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// SPDX-License-Identifier: GPL-3.0-or-later

package mysql

import (
	"fmt"
	"regexp"
	"strings"

	"github.com/blang/semver/v4"
)

const queryShowVersion = `
SHOW GLOBAL VARIABLES 
WHERE 
  Variable_name LIKE 'version'
  OR Variable_name LIKE 'version_comment';`

var reVersionCore = regexp.MustCompile(`^\d+\.\d+\.\d+`)

func (m *MySQL) collectVersion() error {
	// https://mariadb.com/kb/en/version/
	q := queryShowVersion
	m.Debugf("executing query: '%s'", queryShowVersion)

	var name, version, versionComment string
	_, err := m.collectQuery(q, func(column, value string, _ bool) {
		switch column {
		case "Variable_name":
			name = value
		case "Value":
			switch name {
			case "version":
				version = value
			case "version_comment":
				versionComment = value
			}
		}
	})
	if err != nil {
		return err
	}

	m.Infof("application version: '%s', version_comment: '%s'", version, versionComment)

	// version string is not always valid semver (ex.: 8.0.22-0ubuntu0.20.04.2)
	s := reVersionCore.FindString(version)
	if s == "" {
		return fmt.Errorf("couldn't parse version string '%s'", version)
	}

	ver, err := semver.New(s)
	if err != nil {
		return fmt.Errorf("couldn't parse version string '%s': %v", s, err)
	}

	m.version = ver
	m.isMariaDB = strings.Contains(version, "MariaDB") || strings.Contains(versionComment, "mariadb")
	m.isPercona = strings.Contains(versionComment, "Percona")

	return nil
}