Skip to content

Commit 4d497b9

Browse files
committed
added prometheus to version check
1 parent 3e86ace commit 4d497b9

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

common/versionCheck/asterisk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func AsteriskCheck() (string, error) {
2525
version := string(out)
2626

2727
if version == "" {
28-
errMsg := "`asterisk --version` returns empty"
28+
errMsg := "`asterisk -V` returns empty"
2929
log.Error().Str("output", version).Msg(errMsg)
3030
addToVersionErrors(fmt.Errorf(errMsg))
3131
return "", fmt.Errorf(errMsg)

common/versionCheck/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ func VersionCheck(cmd *cobra.Command, args []string) {
144144

145145
AsteriskCheck()
146146

147+
PrometheusCheck()
148+
147149
PrintList()
148150
}
149151

common/versionCheck/prometheus.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

Comments
 (0)