Code cleanup, remove unnecessary features.
parent
cd332ce483
commit
2c039ffb1c
|
@ -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},
|
||||
})
|
||||
|
|
15
cmd/am/am.go
15
cmd/am/am.go
|
@ -13,23 +13,22 @@ func main() {
|
|||
usage := func() {
|
||||
fmt.Fprintf(
|
||||
os.Stderr,
|
||||
"\n\n%s <URL> <api_key> <action> [text]\n\n"+
|
||||
"\n\n%s <report URL> <action> [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:], " "))
|
||||
|
|
24
db.go
24
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)
|
||||
|
|
2
main.go
2
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)
|
||||
|
|
|
@ -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
|
||||
);
|
||||
|
|
|
@ -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 {
|
||||
|
||||
|
|
|
@ -62,9 +62,7 @@
|
|||
{{range . -}}
|
||||
<li>
|
||||
<a href="/source/view/{{.SourceID}}">
|
||||
{{.Name}}
|
||||
{{if .TimedOut}}✖{{end}}
|
||||
{{if .Ignore}}∥{{end}}
|
||||
{{.Name}} {{if .TimedOut}}✖{{end}}
|
||||
</a>
|
||||
</li>
|
||||
{{- end}}
|
||||
|
@ -107,9 +105,6 @@
|
|||
<dt>Alerted At</dt>
|
||||
<dd>{{.AlertedAt.Format "2006-01-02 15:04"}}</dd>
|
||||
|
||||
<dt>Ignore</dt>
|
||||
<dd>{{if .Ignore}}True{{else}}False{{end}}</dd>
|
||||
|
||||
<dt>Log Action</dt>
|
||||
<dd>{{.LogAction}}</dd>
|
||||
|
||||
|
@ -140,10 +135,6 @@
|
|||
<input type="hidden" name="SourceID" value="{{.Source.SourceID}}">
|
||||
|
||||
<ul class="form-list">
|
||||
<li>
|
||||
<input type="checkbox" name="Ignore" id="Ignore" {{if .Source.Ignore}}Checked{{end}}>
|
||||
<label for="Ignore">Ignore</label>
|
||||
</li>
|
||||
<li>
|
||||
<label for="APIKey">API Key:</label>
|
||||
<input type="text" id="APIKey" name="APIKey" value="{{.Source.APIKey}}">
|
||||
|
|
11
tmpl_gen.go
11
tmpl_gen.go
|
@ -158,9 +158,7 @@ var tmpls = `
|
|||
{{range . -}}
|
||||
<li>
|
||||
<a href="/source/view/{{.SourceID}}">
|
||||
{{.Name}}
|
||||
{{if .TimedOut}}✖{{end}}
|
||||
{{if .Ignore}}∥{{end}}
|
||||
{{.Name}} {{if .TimedOut}}✖{{end}}
|
||||
</a>
|
||||
</li>
|
||||
{{- end}}
|
||||
|
@ -203,9 +201,6 @@ var tmpls = `
|
|||
<dt>Alerted At</dt>
|
||||
<dd>{{.AlertedAt.Format "2006-01-02 15:04"}}</dd>
|
||||
|
||||
<dt>Ignore</dt>
|
||||
<dd>{{if .Ignore}}True{{else}}False{{end}}</dd>
|
||||
|
||||
<dt>Log Action</dt>
|
||||
<dd>{{.LogAction}}</dd>
|
||||
|
||||
|
@ -236,10 +231,6 @@ var tmpls = `
|
|||
<input type="hidden" name="SourceID" value="{{.Source.SourceID}}">
|
||||
|
||||
<ul class="form-list">
|
||||
<li>
|
||||
<input type="checkbox" name="Ignore" id="Ignore" {{if .Source.Ignore}}Checked{{end}}>
|
||||
<label for="Ignore">Ignore</label>
|
||||
</li>
|
||||
<li>
|
||||
<label for="APIKey">API Key:</label>
|
||||
<input type="text" id="APIKey" name="APIKey" value="{{.Source.APIKey}}">
|
||||
|
|
1
types.go
1
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.
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
Reference in New Issue