Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 15 additions & 5 deletions lib/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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})
}
}
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions lib/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lib

import (
"bytes"
"fmt"
"io"
"time"

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
Loading