diff --git a/amclient/client.go b/amclient/client.go index e8a265e..769bed5 100644 --- a/amclient/client.go +++ b/amclient/client.go @@ -5,20 +5,22 @@ import ( "io/ioutil" "net/http" "net/url" + "time" ) type Client struct { - addr string - apiKey string + reportURL string } // addr is the full address, for example: https://x.io/report. -func New(addr, apiKey string) Client { - return Client{addr, apiKey} +func New(reportURL string) Client { + return Client{reportURL} } -func (cl Client) post(values url.Values) error { - resp, err := http.PostForm(cl.addr, values) +func (client Client) post(values url.Values) error { + cl := http.Client{Timeout: 30 * time.Second} + + resp, err := cl.PostForm(client.reportURL, values) if err != nil { return err } @@ -35,14 +37,12 @@ func (cl Client) post(values url.Values) error { func (cl Client) Ping() error { return cl.post(url.Values{ - "key": []string{cl.apiKey}, "action": []string{"ping"}, }) } func (cl Client) Log(text string) error { return cl.post(url.Values{ - "key": []string{cl.apiKey}, "action": []string{"log"}, "text": []string{text}, }) @@ -50,7 +50,6 @@ func (cl Client) Log(text string) error { func (cl Client) Alert(text string) error { return cl.post(url.Values{ - "key": []string{cl.apiKey}, "action": []string{"alert"}, "text": []string{text}, }) diff --git a/cmd/am/am.go b/cmd/am/am.go index 6787d94..2b3caba 100644 --- a/cmd/am/am.go +++ b/cmd/am/am.go @@ -13,23 +13,22 @@ func main() { usage := func() { fmt.Fprintf( os.Stderr, - "\n\n%s [text]\n\n"+ + "\n\n%s [text]\n\n"+ "Action is one of `ping`, `log`, or `alert`.\n"+ "The `log` and `alert` actions require `text`.\n\n", os.Args[0]) os.Exit(1) } - if len(os.Args) < 4 { + if len(os.Args) < 3 { usage() } - url := os.Args[1] - apiKey := os.Args[2] + reportURL := os.Args[1] - cl := amclient.New(url, apiKey) + cl := amclient.New(reportURL) - action := os.Args[3] + action := os.Args[2] var err error @@ -38,13 +37,13 @@ func main() { err = cl.Ping() case "log": - if len(os.Args) < 5 { + if len(os.Args) < 4 { usage() } err = cl.Log(strings.Join(os.Args[4:], " ")) case "alert": - if len(os.Args) < 5 { + if len(os.Args) < 4 { usage() } err = cl.Alert(strings.Join(os.Args[4:], " ")) diff --git a/db.go b/db.go index cab8ba7..838ad9d 100644 --- a/db.go +++ b/db.go @@ -178,11 +178,11 @@ func (db *dbal) SourceInsert(s *Source) error { _, err := db.Exec(`INSERT INTO sources(`+ ` SourceID,Name,APIKey,Description,`+ ` LastSeenAt,AlertTimeout,AlertedAt,`+ - ` Ignore,LogAction,AlertAction`+ - `)VALUES(?,?,?,?,?,?,?,?,?,?)`, + ` LogAction,AlertAction`+ + `)VALUES(?,?,?,?,?,?,?,?,?)`, s.SourceID, s.Name, s.APIKey, s.Description, s.LastSeenAt, s.AlertTimeout, s.AlertedAt, - s.Ignore, s.LogAction, s.AlertAction) + s.LogAction, s.AlertAction) if err != nil { db.logf("Failed to insert source: %v", err) } @@ -191,7 +191,7 @@ func (db *dbal) SourceInsert(s *Source) error { const sourceCols = `SourceID,Name,APIKey,Description,` + `LastSeenAt,AlertTimeout,AlertedAt,` + - `Ignore,LogAction,AlertAction` + `LogAction,AlertAction` func (db *dbal) scanSource( row interface{ Scan(...interface{}) error }, @@ -199,7 +199,7 @@ func (db *dbal) scanSource( err = row.Scan( &s.SourceID, &s.Name, &s.APIKey, &s.Description, &s.LastSeenAt, &s.AlertTimeout, &s.AlertedAt, - &s.Ignore, &s.LogAction, &s.AlertAction) + &s.LogAction, &s.AlertAction) if err != nil { db.logf("Failed to scan source: %v", err) } @@ -251,16 +251,8 @@ func (db *dbal) SourceUpdate(s Source) error { return err } -func (db *dbal) SourceUpdateIgnore(id string, b bool) error { - _, err := db.Exec(`UPDATE sources SET Ignore=? WHERE SourceID=?`, b, id) - if err != nil { - db.logf("Failed to update source ignore %s: %v", id, err) - } - return err -} - func (db *dbal) SourceUpdateLastSeenAt(id string) error { - t := time.Now() + t := time.Now().UTC() _, err := db.Exec(`UPDATE sources SET LastSeenAt=? WHERE SourceID=?`, t, id) if err != nil { db.logf("Failed to update source last seen at %s: %v", id, err) @@ -269,7 +261,7 @@ func (db *dbal) SourceUpdateLastSeenAt(id string) error { } func (db *dbal) SourceUpdateAlertedAt(id string) error { - t := time.Now() + t := time.Now().UTC() _, err := db.Exec(`UPDATE sources SET AlertedAt=? WHERE SourceID=?`, t, id) if err != nil { db.logf("Failed to update source alerted at %s: %v", id, err) @@ -290,7 +282,7 @@ func (db *dbal) SourceDelete(id string) error { *******/ func (db *dbal) LogInsert(e Entry) error { - e.TS = time.Now() + e.TS = time.Now().UTC() _, err := db.Exec(`INSERT INTO log`+ `(SourceID,TS,Alert,Text)VALUES(?,?,?,?)`, e.SourceID, e.TS, e.Alert, e.Text) diff --git a/main.go b/main.go index 3e5c3ac..220dcd3 100644 --- a/main.go +++ b/main.go @@ -49,7 +49,7 @@ func Main() { http.HandleFunc("/", handleRoot) // API requests. - http.HandleFunc("/report", handleReport) + http.HandleFunc("/report/", handleReport) // User routes. handle_admin("/user/insert", handleUserInsert) diff --git a/migration.go b/migration.go index 997b996..1a5a8b8 100644 --- a/migration.go +++ b/migration.go @@ -15,7 +15,6 @@ CREATE TABLE IF NOT EXISTS sources( LastSeenAt TIMESTAMP NOT NULL, AlertTimeout BIGINT NOT NULL, AlertedAt TIMESTAMP NOT NULL, - Ignore BOOLEAN NOT NULL, LogAction TEXT NOT NULL, AlertAction TEXT NOT NULL ); diff --git a/report-handler.go b/report-handler.go index 7023bca..0031149 100644 --- a/report-handler.go +++ b/report-handler.go @@ -2,12 +2,11 @@ package am import ( "net/http" + "path/filepath" ) func handleReport(w http.ResponseWriter, r *http.Request) { - r.ParseForm() - - apiKey := r.Form.Get("key") + apiKey := filepath.Base(r.URL.Path) src, err := db.SourceGetByKey(apiKey) if err != nil { @@ -15,6 +14,8 @@ func handleReport(w http.ResponseWriter, r *http.Request) { return } + r.ParseForm() + action := r.Form.Get("action") switch action { diff --git a/templates/sources.html b/templates/sources.html index feb7431..01193c1 100644 --- a/templates/sources.html +++ b/templates/sources.html @@ -62,9 +62,7 @@ {{range . -}}
  • - {{.Name}} - {{if .TimedOut}}✖{{end}} - {{if .Ignore}}∥{{end}} + {{.Name}} {{if .TimedOut}}✖{{end}}
  • {{- end}} @@ -107,9 +105,6 @@
    Alerted At
    {{.AlertedAt.Format "2006-01-02 15:04"}}
    -
    Ignore
    -
    {{if .Ignore}}True{{else}}False{{end}}
    -
    Log Action
    {{.LogAction}}
    @@ -140,10 +135,6 @@
      -
    • - - -
    • diff --git a/tmpl_gen.go b/tmpl_gen.go index 71a3128..ada753c 100644 --- a/tmpl_gen.go +++ b/tmpl_gen.go @@ -158,9 +158,7 @@ var tmpls = ` {{range . -}}
    • - {{.Name}} - {{if .TimedOut}}✖{{end}} - {{if .Ignore}}∥{{end}} + {{.Name}} {{if .TimedOut}}✖{{end}}
    • {{- end}} @@ -203,9 +201,6 @@ var tmpls = `
      Alerted At
      {{.AlertedAt.Format "2006-01-02 15:04"}}
      -
      Ignore
      -
      {{if .Ignore}}True{{else}}False{{end}}
      -
      Log Action
      {{.LogAction}}
      @@ -236,10 +231,6 @@ var tmpls = `
        -
      • - - -
      • diff --git a/types.go b/types.go index 9675107..beaaccc 100644 --- a/types.go +++ b/types.go @@ -15,7 +15,6 @@ type Source struct { LastSeenAt time.Time AlertTimeout int64 // In seconds. AlertedAt time.Time // Timeout alert time. - Ignore bool // Don't trigger alerts. LogAction string // Override log action. AlertAction string // Override alert action. } diff --git a/user-handlers.go b/user-handlers.go index c1ca0b9..9cac53d 100644 --- a/user-handlers.go +++ b/user-handlers.go @@ -280,14 +280,6 @@ func handleSourceUpdate(w http.ResponseWriter, r *http.Request) { return } - ignore := r.Form.Get("Ignore") != "" - if ignore != s.Ignore { - if err := db.SourceUpdateIgnore(s.SourceID, ignore); err != nil { - execTmpl(w, "Error", err) - return - } - } - respondRedirect(w, r, "/source/view/%s", s.SourceID) }