diff --git a/actions.go b/actions.go index f5bd9d1..568d525 100644 --- a/actions.go +++ b/actions.go @@ -15,30 +15,27 @@ func run(cmd, name, text string) { } } -func runLogAction(action, sourceName, text string) { +func runLogAction(sourceName, text string) { log.Printf("[%s] %s", sourceName, text) - if action == "" { - dir, err := os.Getwd() - if err != nil { - log.Printf("Failed to get working directory: %v", err) - return - } - action = filepath.Join(dir, "log-action") + dir, err := os.Getwd() + if err != nil { + log.Printf("Failed to get working directory: %v", err) + return } + action := filepath.Join(dir, "log-action") run(action, sourceName, text) } -func runAlertAction(action, sourceName, text string) { +func runAlertAction(sourceName, text string) { log.Printf("[%s] ALERT %s", sourceName, text) - if action == "" { - dir, err := os.Getwd() - if err != nil { - log.Printf("Failed to get working directory: %v", err) - return - } - action = filepath.Join(dir, "alert-action") + + dir, err := os.Getwd() + if err != nil { + log.Printf("Failed to get working directory: %v", err) + return } + action := filepath.Join(dir, "alert-action") run(action, sourceName, text) } diff --git a/background.go b/background.go index 738e4e2..07b437c 100644 --- a/background.go +++ b/background.go @@ -20,7 +20,7 @@ func processTimeouts() { } _ = db.LogInsert(e) - runAlertAction(src.AlertAction, src.Name, e.Text) + runAlertAction(src.Name, e.Text) _ = db.SourceUpdateAlertedAt(src.SourceID) } } diff --git a/db.go b/db.go index 2188a5d..99e7ef4 100644 --- a/db.go +++ b/db.go @@ -177,12 +177,10 @@ func (db *dbal) SourceInsert(s *Source) error { _, err := db.Exec(`INSERT INTO sources(`+ ` SourceID,Name,APIKey,Description,`+ - ` LastSeenAt,AlertTimeout,AlertedAt,`+ - ` LogAction,AlertAction`+ - `)VALUES(?,?,?,?,?,?,?,?,?)`, + ` LastSeenAt,AlertTimeout,AlertedAt`+ + `)VALUES(?,?,?,?,?,?,?)`, s.SourceID, s.Name, s.APIKey, s.Description, - s.LastSeenAt, s.AlertTimeout, s.AlertedAt, - s.LogAction, s.AlertAction) + s.LastSeenAt, s.AlertTimeout, s.AlertedAt) if err != nil { db.logf("Failed to insert source: %v", err) } @@ -190,16 +188,14 @@ func (db *dbal) SourceInsert(s *Source) error { } const sourceCols = `SourceID,Name,APIKey,Description,` + - `LastSeenAt,AlertTimeout,AlertedAt,` + - `LogAction,AlertAction` + `LastSeenAt,AlertTimeout,AlertedAt` func (db *dbal) scanSource( row interface{ Scan(...interface{}) error }, ) (s Source, err error) { err = row.Scan( &s.SourceID, &s.Name, &s.APIKey, &s.Description, - &s.LastSeenAt, &s.AlertTimeout, &s.AlertedAt, - &s.LogAction, &s.AlertAction) + &s.LastSeenAt, &s.AlertTimeout, &s.AlertedAt) if err != nil { db.logf("Failed to scan source: %v", err) } @@ -234,12 +230,12 @@ func (db *dbal) SourceList() (l []Source, err error) { return l, nil } -// Updates Description, AlertTimeout, LogAction, AlertAction. +// Updates Description and AlertTimeout. func (db *dbal) SourceUpdate(s Source) error { _, err := db.Exec(`UPDATE sources `+ - `SET Description=?,AlertTimeout=?,LogAction=?,AlertAction=? `+ + `SET Description=?,AlertTimeout=? `+ `WHERE SourceID=?`, - s.Description, s.AlertTimeout, s.LogAction, s.AlertAction, s.SourceID) + s.Description, s.AlertTimeout, s.SourceID) if err != nil { db.logf("Failed to update source %s: %v", s.Name, err) } @@ -313,7 +309,7 @@ func (db *dbal) LogList(args LogListArgs) (l []EntryListRow, err error) { qArgs := []interface{}{} query := `SELECT ` + - `l.LogID,s.Name,l.TS,l.Alert,l.Text ` + + `l.LogID,s.SourceID,s.Name,l.TS,l.Alert,l.Text ` + `FROM log l JOIN sources s ON l.SourceID=s.SourceID WHERE 1` if args.BeforeID != 0 { query += " AND LogID" @@ -340,7 +336,8 @@ func (db *dbal) LogList(args LogListArgs) (l []EntryListRow, err error) { for rows.Next() { e := EntryListRow{} - err := rows.Scan(&e.LogID, &e.SourceName, &e.TS, &e.Alert, &e.Text) + err := rows.Scan( + &e.LogID, &e.SourceID, &e.SourceName, &e.TS, &e.Alert, &e.Text) if err != nil { db.logf("Failed to scan entry list row: %v", err) return nil, err diff --git a/migration.go b/migration.go index 6da28e2..3948f74 100644 --- a/migration.go +++ b/migration.go @@ -14,9 +14,7 @@ CREATE TABLE IF NOT EXISTS sources( Description TEXT NOT NULL, LastSeenAt TIMESTAMP NOT NULL, AlertedAt TIMESTAMP NOT NULL, - AlertTimeout BIGINT NOT NULL, - LogAction TEXT NOT NULL, - AlertAction TEXT NOT NULL + AlertTimeout BIGINT NOT NULL ); CREATE TABLE IF NOT EXISTS log( diff --git a/report-handler.go b/report-handler.go index 0031149..df3f51a 100644 --- a/report-handler.go +++ b/report-handler.go @@ -34,7 +34,7 @@ func handleReport(w http.ResponseWriter, r *http.Request) { SourceID: src.SourceID, Text: text, }) - runLogAction(src.LogAction, src.Name, text) + runLogAction(src.Name, text) w.WriteHeader(http.StatusOK) case "alert": @@ -46,7 +46,7 @@ func handleReport(w http.ResponseWriter, r *http.Request) { Text: text, Alert: true, }) - runAlertAction(src.AlertAction, src.Name, text) + runAlertAction(src.Name, text) w.WriteHeader(http.StatusOK) default: diff --git a/templates/log.html b/templates/log.html index 51dc965..1a39a4d 100644 --- a/templates/log.html +++ b/templates/log.html @@ -44,7 +44,7 @@