diff options
Diffstat (limited to '')
-rw-r--r-- | src/go/plugin/go.d/modules/mysql/collect_version.go | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/go/plugin/go.d/modules/mysql/collect_version.go b/src/go/plugin/go.d/modules/mysql/collect_version.go new file mode 100644 index 000000000..b85922e2c --- /dev/null +++ b/src/go/plugin/go.d/modules/mysql/collect_version.go @@ -0,0 +1,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 +} |