diff --git a/config.yaml b/config.yaml index db750ea..6a3100a 100644 --- a/config.yaml +++ b/config.yaml @@ -163,23 +163,23 @@ disks: last_active: 50m standby_after: 1h -templates: - zabbix: - source: "zabbix_template.go.txt" +# templates: +# zabbix: +# source: "zabbix_template.go.txt" -tasks: - zabbix_sender: - command: "zabbix_sender -z 127.0.0.1 -s \"Zabbix server\" -i -" - timeout: 5s - stdin: - template: zabbix +# tasks: +# zabbix_sender: +# command: "zabbix_sender -z 127.0.0.1 -s \"Zabbix server\" -i -" +# timeout: 5s +# stdin: +# template: zabbix -schedule: - zabbix: - task: zabbix_sender - when: - - startup - - every 5m +# schedule: +# zabbix: +# task: zabbix_sender +# when: +# - startup +# - every 5m telemetry: prometheus: diff --git a/lib/global.go b/lib/global.go index 64b1ae1..3cdbd73 100644 --- a/lib/global.go +++ b/lib/global.go @@ -84,10 +84,12 @@ func NewGlobal(config cfg.Config) (*Global, error) { templates = append(templates, source) } - // Now load the templates - global.templ, err = template.ParseFiles(templates...) - if err != nil { - return global, err + if len(templates) > 0 { + // Now load the templates + global.templ, err = template.ParseFiles(templates...) + if err != nil { + return global, err + } } simulationRand := rand.New(rand.NewPCG(config.Seed1, config.Seed2)) @@ -165,6 +167,10 @@ func (g *Global) GetStartupTasks() []*Task { tasks := make([]*Task, 0, len(g.Schedules)) for _, schedule := range g.Schedules { if schedule.OnStartup() { + if schedule.Task == nil { + clog.Error("startup task not found") + continue + } tasks = append(tasks, schedule.Task) } } @@ -176,6 +182,10 @@ func (g *Global) GetTimerTasks() []Timer { for _, schedule := range g.Schedules { duration := schedule.OnTimer() if duration > 0 { + if schedule.Task == nil { + clog.Error("scheduled task not found") + continue + } timers = append(timers, Timer{schedule.Task, duration}) } } @@ -191,7 +201,7 @@ func (g *Global) StartTimers() { clog.Debugf("running timer task %s", timer.task.Name) err := timer.task.Execute() if err != nil { - clog.Error(err) + clog.Errorf("timer task %s failed: %s", timer.task.Name, err) } } }(timer) diff --git a/lib/task.go b/lib/task.go index c759400..1565b3c 100644 --- a/lib/task.go +++ b/lib/task.go @@ -2,6 +2,7 @@ package lib import ( "bytes" + "fmt" "io" "time" @@ -50,6 +51,9 @@ func NewTask(global *Global, name string, config cfg.Task, simulate bool) (*Task func (t *Task) Execute() error { var stdin io.Reader if t.InputTemplate != "" { + if t.global.templ == nil || t.global.Templates[t.InputTemplate] == nil { + return fmt.Errorf("template %s not found", t.InputTemplate) + } input := &bytes.Buffer{} err := t.global.templ.ExecuteTemplate(input, t.global.Templates[t.InputTemplate].ID, t.global) if err != nil { diff --git a/main.go b/main.go index 40bd672..80d4805 100644 --- a/main.go +++ b/main.go @@ -82,7 +82,7 @@ func main() { clog.Debugf("running startup task %s", task.Name) err := task.Execute() if err != nil { - clog.Error(err) + clog.Errorf("startup task %s failed: %s", task.Name, err) continue } }