Make Python compatability a flag
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
9c21880efa
commit
00029a6327
10
alert.go
10
alert.go
@ -51,16 +51,20 @@ func (alert *Alert) BuildTemplates() error {
|
|||||||
if alert.commandTemplate == nil && alert.Command.Command != nil {
|
if alert.commandTemplate == nil && alert.Command.Command != nil {
|
||||||
alert.commandTemplate = []*template.Template{}
|
alert.commandTemplate = []*template.Template{}
|
||||||
for i, cmdPart := range alert.Command.Command {
|
for i, cmdPart := range alert.Command.Command {
|
||||||
|
if PyCompat {
|
||||||
cmdPart = legacy.Replace(cmdPart)
|
cmdPart = legacy.Replace(cmdPart)
|
||||||
|
}
|
||||||
alert.commandTemplate = append(alert.commandTemplate, template.Must(
|
alert.commandTemplate = append(alert.commandTemplate, template.Must(
|
||||||
template.New(alert.Name+string(i)).Parse(cmdPart),
|
template.New(alert.Name+string(i)).Parse(cmdPart),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
} else if alert.commandShellTemplate == nil && alert.Command.ShellCommand != "" {
|
} else if alert.commandShellTemplate == nil && alert.Command.ShellCommand != "" {
|
||||||
|
shellCmd := alert.Command.ShellCommand
|
||||||
|
if PyCompat {
|
||||||
|
shellCmd = legacy.Replace(shellCmd)
|
||||||
|
}
|
||||||
alert.commandShellTemplate = template.Must(
|
alert.commandShellTemplate = template.Must(
|
||||||
template.New(alert.Name).Parse(
|
template.New(alert.Name).Parse(shellCmd),
|
||||||
legacy.Replace(alert.Command.ShellCommand),
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("No template provided for alert %s", alert.Name)
|
return fmt.Errorf("No template provided for alert %s", alert.Name)
|
||||||
|
@ -34,6 +34,7 @@ func TestAlertSend(t *testing.T) {
|
|||||||
expectedOutput string
|
expectedOutput string
|
||||||
expectErr bool
|
expectErr bool
|
||||||
name string
|
name string
|
||||||
|
pyCompat bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
Alert{Command: CommandOrShell{Command: []string{"echo", "{{.MonitorName}}"}}},
|
Alert{Command: CommandOrShell{Command: []string{"echo", "{{.MonitorName}}"}}},
|
||||||
@ -41,6 +42,7 @@ func TestAlertSend(t *testing.T) {
|
|||||||
"test\n",
|
"test\n",
|
||||||
false,
|
false,
|
||||||
"Command with template",
|
"Command with template",
|
||||||
|
false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Alert{Command: CommandOrShell{ShellCommand: "echo {{.MonitorName}}"}},
|
Alert{Command: CommandOrShell{ShellCommand: "echo {{.MonitorName}}"}},
|
||||||
@ -48,6 +50,7 @@ func TestAlertSend(t *testing.T) {
|
|||||||
"test\n",
|
"test\n",
|
||||||
false,
|
false,
|
||||||
"Command shell with template",
|
"Command shell with template",
|
||||||
|
false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Alert{Command: CommandOrShell{Command: []string{"echo", "{{.Bad}}"}}},
|
Alert{Command: CommandOrShell{Command: []string{"echo", "{{.Bad}}"}}},
|
||||||
@ -55,6 +58,7 @@ func TestAlertSend(t *testing.T) {
|
|||||||
"",
|
"",
|
||||||
true,
|
true,
|
||||||
"Command with bad template",
|
"Command with bad template",
|
||||||
|
false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Alert{Command: CommandOrShell{ShellCommand: "echo {{.Bad}}"}},
|
Alert{Command: CommandOrShell{ShellCommand: "echo {{.Bad}}"}},
|
||||||
@ -62,6 +66,7 @@ func TestAlertSend(t *testing.T) {
|
|||||||
"",
|
"",
|
||||||
true,
|
true,
|
||||||
"Command shell with bad template",
|
"Command shell with bad template",
|
||||||
|
false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Alert{Command: CommandOrShell{ShellCommand: "echo {alert_message}"}},
|
Alert{Command: CommandOrShell{ShellCommand: "echo {alert_message}"}},
|
||||||
@ -69,11 +74,14 @@ func TestAlertSend(t *testing.T) {
|
|||||||
"test check has failed 1 times\n",
|
"test check has failed 1 times\n",
|
||||||
false,
|
false,
|
||||||
"Command shell with legacy template",
|
"Command shell with legacy template",
|
||||||
|
true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
log.Printf("Testing case %s", c.name)
|
log.Printf("Testing case %s", c.name)
|
||||||
|
// Set PyCompat to value of compat flag
|
||||||
|
PyCompat = c.pyCompat
|
||||||
c.alert.BuildTemplates()
|
c.alert.BuildTemplates()
|
||||||
output, err := c.alert.Send(c.notice)
|
output, err := c.alert.Send(c.notice)
|
||||||
hasErr := (err != nil)
|
hasErr := (err != nil)
|
||||||
@ -85,6 +93,8 @@ func TestAlertSend(t *testing.T) {
|
|||||||
t.Errorf("Send(%v err), expected=%v actual=%v", c.name, "Err", err)
|
t.Errorf("Send(%v err), expected=%v actual=%v", c.name, "Err", err)
|
||||||
log.Printf("Case failed: %s", c.name)
|
log.Printf("Case failed: %s", c.name)
|
||||||
}
|
}
|
||||||
|
// Set PyCompat back to default value
|
||||||
|
PyCompat = false
|
||||||
log.Println("-----")
|
log.Println("-----")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -118,15 +118,16 @@ func LoadConfig(filePath string) (config Config, err error) {
|
|||||||
log.Printf("DEBUG: Config values:\n%v\n", config)
|
log.Printf("DEBUG: Config values:\n%v\n", config)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add log alert if not present
|
||||||
|
if PyCompat {
|
||||||
// Intialize alerts list if not present
|
// Intialize alerts list if not present
|
||||||
if config.Alerts == nil {
|
if config.Alerts == nil {
|
||||||
config.Alerts = map[string]*Alert{}
|
config.Alerts = map[string]*Alert{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add log alert if not present
|
|
||||||
if _, ok := config.Alerts["log"]; !ok {
|
if _, ok := config.Alerts["log"]; !ok {
|
||||||
config.Alerts["log"] = NewLogAlert()
|
config.Alerts["log"] = NewLogAlert()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if !config.IsValid() {
|
if !config.IsValid() {
|
||||||
err = errors.New("Invalid configuration")
|
err = errors.New("Invalid configuration")
|
||||||
|
@ -10,23 +10,29 @@ func TestLoadConfig(t *testing.T) {
|
|||||||
configPath string
|
configPath string
|
||||||
expectErr bool
|
expectErr bool
|
||||||
name string
|
name string
|
||||||
|
pyCompat bool
|
||||||
}{
|
}{
|
||||||
{"./test/valid-config.yml", false, "Valid config file"},
|
{"./test/valid-config.yml", false, "Valid config file", false},
|
||||||
{"./test/valid-default-log-alert.yml", false, "Valid config file with default log alert"},
|
{"./test/valid-default-log-alert.yml", false, "Valid config file with default log alert PyCompat", true},
|
||||||
{"./test/does-not-exist", true, "Invalid config path"},
|
{"./test/valid-default-log-alert.yml", true, "Invalid config file no log alert", false},
|
||||||
{"./test/invalid-config-type.yml", true, "Invalid config type for key"},
|
{"./test/does-not-exist", true, "Invalid config path", false},
|
||||||
{"./test/invalid-config-missing-alerts.yml", true, "Invalid config missing alerts"},
|
{"./test/invalid-config-type.yml", true, "Invalid config type for key", false},
|
||||||
{"./test/invalid-config-unknown-alert.yml", true, "Invalid config unknown alert"},
|
{"./test/invalid-config-missing-alerts.yml", true, "Invalid config missing alerts", false},
|
||||||
|
{"./test/invalid-config-unknown-alert.yml", true, "Invalid config unknown alert", false},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
log.Printf("Testing case %s", c.name)
|
log.Printf("Testing case %s", c.name)
|
||||||
|
// Set PyCompat based on compatibility mode
|
||||||
|
PyCompat = c.pyCompat
|
||||||
_, err := LoadConfig(c.configPath)
|
_, err := LoadConfig(c.configPath)
|
||||||
hasErr := (err != nil)
|
hasErr := (err != nil)
|
||||||
if hasErr != c.expectErr {
|
if hasErr != c.expectErr {
|
||||||
t.Errorf("LoadConfig(%v), expected_error=%v actual=%v", c.name, c.expectErr, err)
|
t.Errorf("LoadConfig(%v), expected_error=%v actual=%v", c.name, c.expectErr, err)
|
||||||
log.Printf("Case failed: %s", c.name)
|
log.Printf("Case failed: %s", c.name)
|
||||||
}
|
}
|
||||||
|
// Set PyCompat to default value
|
||||||
|
PyCompat = false
|
||||||
log.Println("-----")
|
log.Println("-----")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
4
main.go
4
main.go
@ -18,6 +18,9 @@ var (
|
|||||||
// Metrics contains all active metrics
|
// Metrics contains all active metrics
|
||||||
Metrics = NewMetrics()
|
Metrics = NewMetrics()
|
||||||
|
|
||||||
|
// PyCompat enables support for legacy Python templates
|
||||||
|
PyCompat = false
|
||||||
|
|
||||||
// version of minitor being run
|
// version of minitor being run
|
||||||
version = "dev"
|
version = "dev"
|
||||||
)
|
)
|
||||||
@ -83,6 +86,7 @@ func main() {
|
|||||||
// Get debug flag
|
// Get debug flag
|
||||||
flag.BoolVar(&LogDebug, "debug", false, "Enables debug logs (default: false)")
|
flag.BoolVar(&LogDebug, "debug", false, "Enables debug logs (default: false)")
|
||||||
flag.BoolVar(&ExportMetrics, "metrics", false, "Enables prometheus metrics exporting (default: false)")
|
flag.BoolVar(&ExportMetrics, "metrics", false, "Enables prometheus metrics exporting (default: false)")
|
||||||
|
flag.BoolVar(&PyCompat, "py-compat", false, "Enables support for legacy Python Minitor config. Will eventually be removed. (default: false)")
|
||||||
var showVersion = flag.Bool("version", false, "Display the version of minitor and exit")
|
var showVersion = flag.Bool("version", false, "Display the version of minitor and exit")
|
||||||
var configPath = flag.String("config", "config.yml", "Alternate configuration path (default: config.yml)")
|
var configPath = flag.String("config", "config.yml", "Alternate configuration path (default: config.yml)")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
Loading…
Reference in New Issue
Block a user