| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package mysql |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "fmt" |
| 8 | "regexp" |
| 9 | "strings" |
| 10 | |
| 11 | "github.com/blang/semver/v4" |
| 12 | ) |
| 13 | |
| 14 | const queryShowVersion = ` |
| 15 | SHOW GLOBAL VARIABLES |
| 16 | WHERE |
| 17 | Variable_name LIKE 'version' |
| 18 | OR Variable_name LIKE 'version_comment';` |
| 19 | |
| 20 | var reVersionCore = regexp.MustCompile(`^\d+\.\d+\.\d+`) |
| 21 | |
| 22 | func (c *Collector) collectVersion(ctx context.Context) error { |
| 23 | // https://mariadb.com/kb/en/version/ |
| 24 | q := queryShowVersion |
| 25 | c.Debugf("executing query: '%s'", queryShowVersion) |
| 26 | |
| 27 | var name, version, versionComment string |
| 28 | _, err := c.collectQuery(ctx, q, func(column, value string, _ bool) { |
| 29 | switch column { |
| 30 | case "Variable_name": |
| 31 | name = value |
| 32 | case "Value": |
| 33 | switch name { |
| 34 | case "version": |
| 35 | version = value |
| 36 | case "version_comment": |
| 37 | versionComment = value |
| 38 | } |
| 39 | } |
| 40 | }) |
| 41 | if err != nil { |
| 42 | return err |
| 43 | } |
| 44 | |
| 45 | c.Infof("application version: '%s', version_comment: '%s'", version, versionComment) |
| 46 | |
| 47 | // version string is not always valid semver (ex.: 8.0.22-0ubuntu0.20.04.2) |
| 48 | s := reVersionCore.FindString(version) |
| 49 | if s == "" { |
| 50 | return fmt.Errorf("couldn't parse version string '%s'", version) |
| 51 | } |
| 52 | |
| 53 | ver, err := semver.New(s) |
| 54 | if err != nil { |
| 55 | return fmt.Errorf("couldn't parse version string '%s': %v", s, err) |
| 56 | } |
| 57 | |
| 58 | c.version = ver |
| 59 | c.isMariaDB = strings.Contains(version, "MariaDB") || strings.Contains(versionComment, "mariadb") |
| 60 | c.isPercona = strings.Contains(versionComment, "Percona") |
| 61 | |
| 62 | return nil |
| 63 | } |