|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os/exec" |
| 6 | + "regexp" |
| 7 | + |
| 8 | + "github.com/rs/zerolog/log" |
| 9 | +) |
| 10 | + |
| 11 | +var prometheusVersionRegex = regexp.MustCompile(`version ([^ ]+)`) |
| 12 | + |
| 13 | +func PrometheusCheck() (string, error) { |
| 14 | + _, err := exec.LookPath("prometheus") |
| 15 | + if err != nil { |
| 16 | + addToNotInstalled("Prometheus") |
| 17 | + return "", nil |
| 18 | + } |
| 19 | + |
| 20 | + out, err := exec.Command("prometheus", "--version").Output() |
| 21 | + if err != nil { |
| 22 | + errMsg := "Error getting Prometheus version: " + err.Error() |
| 23 | + log.Error().Msg(errMsg) |
| 24 | + addToVersionErrors(fmt.Errorf(errMsg)) |
| 25 | + return "", fmt.Errorf("%s", errMsg) |
| 26 | + } |
| 27 | + |
| 28 | + outputStr := string(out) |
| 29 | + |
| 30 | + if outputStr == "" { |
| 31 | + errMsg := "`prometheus --version` returns empty" |
| 32 | + log.Error().Str("output", outputStr).Msg(errMsg) |
| 33 | + addToVersionErrors(fmt.Errorf(errMsg)) |
| 34 | + return "", fmt.Errorf(errMsg) |
| 35 | + } |
| 36 | + |
| 37 | + matches := prometheusVersionRegex.FindStringSubmatch(outputStr) |
| 38 | + if len(matches) < 2 { |
| 39 | + errMsg := "Could not parse Prometheus version from output" |
| 40 | + log.Error().Str("output", outputStr).Msg(errMsg) |
| 41 | + addToVersionErrors(fmt.Errorf(errMsg)) |
| 42 | + return "", fmt.Errorf(errMsg) |
| 43 | + } |
| 44 | + version := matches[1] |
| 45 | + |
| 46 | + log.Debug().Str("version", version).Msg("Detected Prometheus version") |
| 47 | + |
| 48 | + oldVersion := GatherVersion("prometheus") |
| 49 | + |
| 50 | + if oldVersion != "" && oldVersion == version { |
| 51 | + log.Debug().Msg("Prometheus version unchanged.") |
| 52 | + addToNotUpdated(AppVersion{Name: "Prometheus", OldVersion: oldVersion, NewVersion: version}) |
| 53 | + } else if oldVersion != "" && oldVersion != version { |
| 54 | + log.Debug().Str("old_version", oldVersion).Str("new_version", version).Msg("Prometheus has been updated") |
| 55 | + addToUpdated(AppVersion{Name: "Prometheus", OldVersion: oldVersion, NewVersion: version}) |
| 56 | + CreateNews("Prometheus", oldVersion, version, false) |
| 57 | + } else { |
| 58 | + log.Debug().Msg("Storing initial Prometheus version: " + version) |
| 59 | + addToNotUpdated(AppVersion{Name: "Prometheus", OldVersion: version}) |
| 60 | + } |
| 61 | + |
| 62 | + StoreVersion("prometheus", version) |
| 63 | + return version, nil |
| 64 | +} |
0 commit comments