Code cleanup
parent
ce3f3f1417
commit
052f7373fc
29
actions.go
29
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)
|
log.Printf("[%s] %s", sourceName, text)
|
||||||
|
|
||||||
if action == "" {
|
dir, err := os.Getwd()
|
||||||
dir, err := os.Getwd()
|
if err != nil {
|
||||||
if err != nil {
|
log.Printf("Failed to get working directory: %v", err)
|
||||||
log.Printf("Failed to get working directory: %v", err)
|
return
|
||||||
return
|
|
||||||
}
|
|
||||||
action = filepath.Join(dir, "log-action")
|
|
||||||
}
|
}
|
||||||
|
action := filepath.Join(dir, "log-action")
|
||||||
|
|
||||||
run(action, sourceName, text)
|
run(action, sourceName, text)
|
||||||
}
|
}
|
||||||
|
|
||||||
func runAlertAction(action, sourceName, text string) {
|
func runAlertAction(sourceName, text string) {
|
||||||
log.Printf("[%s] ALERT %s", sourceName, text)
|
log.Printf("[%s] ALERT %s", sourceName, text)
|
||||||
if action == "" {
|
|
||||||
dir, err := os.Getwd()
|
dir, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to get working directory: %v", err)
|
log.Printf("Failed to get working directory: %v", err)
|
||||||
return
|
return
|
||||||
}
|
|
||||||
action = filepath.Join(dir, "alert-action")
|
|
||||||
}
|
}
|
||||||
|
action := filepath.Join(dir, "alert-action")
|
||||||
run(action, sourceName, text)
|
run(action, sourceName, text)
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ func processTimeouts() {
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = db.LogInsert(e)
|
_ = db.LogInsert(e)
|
||||||
runAlertAction(src.AlertAction, src.Name, e.Text)
|
runAlertAction(src.Name, e.Text)
|
||||||
_ = db.SourceUpdateAlertedAt(src.SourceID)
|
_ = db.SourceUpdateAlertedAt(src.SourceID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
25
db.go
25
db.go
|
@ -177,12 +177,10 @@ func (db *dbal) SourceInsert(s *Source) error {
|
||||||
|
|
||||||
_, err := db.Exec(`INSERT INTO sources(`+
|
_, err := db.Exec(`INSERT INTO sources(`+
|
||||||
` SourceID,Name,APIKey,Description,`+
|
` SourceID,Name,APIKey,Description,`+
|
||||||
` LastSeenAt,AlertTimeout,AlertedAt,`+
|
` LastSeenAt,AlertTimeout,AlertedAt`+
|
||||||
` LogAction,AlertAction`+
|
`)VALUES(?,?,?,?,?,?,?)`,
|
||||||
`)VALUES(?,?,?,?,?,?,?,?,?)`,
|
|
||||||
s.SourceID, s.Name, s.APIKey, s.Description,
|
s.SourceID, s.Name, s.APIKey, s.Description,
|
||||||
s.LastSeenAt, s.AlertTimeout, s.AlertedAt,
|
s.LastSeenAt, s.AlertTimeout, s.AlertedAt)
|
||||||
s.LogAction, s.AlertAction)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
db.logf("Failed to insert source: %v", err)
|
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,` +
|
const sourceCols = `SourceID,Name,APIKey,Description,` +
|
||||||
`LastSeenAt,AlertTimeout,AlertedAt,` +
|
`LastSeenAt,AlertTimeout,AlertedAt`
|
||||||
`LogAction,AlertAction`
|
|
||||||
|
|
||||||
func (db *dbal) scanSource(
|
func (db *dbal) scanSource(
|
||||||
row interface{ Scan(...interface{}) error },
|
row interface{ Scan(...interface{}) error },
|
||||||
) (s Source, err error) {
|
) (s Source, err error) {
|
||||||
err = row.Scan(
|
err = row.Scan(
|
||||||
&s.SourceID, &s.Name, &s.APIKey, &s.Description,
|
&s.SourceID, &s.Name, &s.APIKey, &s.Description,
|
||||||
&s.LastSeenAt, &s.AlertTimeout, &s.AlertedAt,
|
&s.LastSeenAt, &s.AlertTimeout, &s.AlertedAt)
|
||||||
&s.LogAction, &s.AlertAction)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
db.logf("Failed to scan source: %v", err)
|
db.logf("Failed to scan source: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -234,12 +230,12 @@ func (db *dbal) SourceList() (l []Source, err error) {
|
||||||
return l, nil
|
return l, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Updates Description, AlertTimeout, LogAction, AlertAction.
|
// Updates Description and AlertTimeout.
|
||||||
func (db *dbal) SourceUpdate(s Source) error {
|
func (db *dbal) SourceUpdate(s Source) error {
|
||||||
_, err := db.Exec(`UPDATE sources `+
|
_, err := db.Exec(`UPDATE sources `+
|
||||||
`SET Description=?,AlertTimeout=?,LogAction=?,AlertAction=? `+
|
`SET Description=?,AlertTimeout=? `+
|
||||||
`WHERE SourceID=?`,
|
`WHERE SourceID=?`,
|
||||||
s.Description, s.AlertTimeout, s.LogAction, s.AlertAction, s.SourceID)
|
s.Description, s.AlertTimeout, s.SourceID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
db.logf("Failed to update source %s: %v", s.Name, err)
|
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{}{}
|
qArgs := []interface{}{}
|
||||||
query := `SELECT ` +
|
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`
|
`FROM log l JOIN sources s ON l.SourceID=s.SourceID WHERE 1`
|
||||||
if args.BeforeID != 0 {
|
if args.BeforeID != 0 {
|
||||||
query += " AND LogID<?"
|
query += " AND LogID<?"
|
||||||
|
@ -340,7 +336,8 @@ func (db *dbal) LogList(args LogListArgs) (l []EntryListRow, err error) {
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
e := EntryListRow{}
|
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 {
|
if err != nil {
|
||||||
db.logf("Failed to scan entry list row: %v", err)
|
db.logf("Failed to scan entry list row: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -14,9 +14,7 @@ CREATE TABLE IF NOT EXISTS sources(
|
||||||
Description TEXT NOT NULL,
|
Description TEXT NOT NULL,
|
||||||
LastSeenAt TIMESTAMP NOT NULL,
|
LastSeenAt TIMESTAMP NOT NULL,
|
||||||
AlertedAt TIMESTAMP NOT NULL,
|
AlertedAt TIMESTAMP NOT NULL,
|
||||||
AlertTimeout BIGINT NOT NULL,
|
AlertTimeout BIGINT NOT NULL
|
||||||
LogAction TEXT NOT NULL,
|
|
||||||
AlertAction TEXT NOT NULL
|
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS log(
|
CREATE TABLE IF NOT EXISTS log(
|
||||||
|
|
|
@ -34,7 +34,7 @@ func handleReport(w http.ResponseWriter, r *http.Request) {
|
||||||
SourceID: src.SourceID,
|
SourceID: src.SourceID,
|
||||||
Text: text,
|
Text: text,
|
||||||
})
|
})
|
||||||
runLogAction(src.LogAction, src.Name, text)
|
runLogAction(src.Name, text)
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
case "alert":
|
case "alert":
|
||||||
|
@ -46,7 +46,7 @@ func handleReport(w http.ResponseWriter, r *http.Request) {
|
||||||
Text: text,
|
Text: text,
|
||||||
Alert: true,
|
Alert: true,
|
||||||
})
|
})
|
||||||
runAlertAction(src.AlertAction, src.Name, text)
|
runAlertAction(src.Name, text)
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -44,7 +44,7 @@
|
||||||
<td>{{.TS.Format "2006-01-02 15:04"}}
|
<td>{{.TS.Format "2006-01-02 15:04"}}
|
||||||
</td>
|
</td>
|
||||||
<td>{{if .Alert}}<b>!</b>{{end}}</td>
|
<td>{{if .Alert}}<b>!</b>{{end}}</td>
|
||||||
<td>{{.SourceName}}</td>
|
<td><a href="/source/view/{{.SourceID}}">{{.SourceName}}</a></td>
|
||||||
<td style="width:100%;">{{.Text}}</td>
|
<td style="width:100%;">{{.Text}}</td>
|
||||||
</tr>
|
</tr>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
|
@ -27,14 +27,6 @@
|
||||||
<label for="AlertTimeout">Alert Timeout:</label>
|
<label for="AlertTimeout">Alert Timeout:</label>
|
||||||
<input type="number" id="AlertTimeout" name="AlertTimeout">
|
<input type="number" id="AlertTimeout" name="AlertTimeout">
|
||||||
</li>
|
</li>
|
||||||
<li>
|
|
||||||
<label for="LogAction">Log Action:</label>
|
|
||||||
<input type="text" id="LogAction" name="LogAction">
|
|
||||||
</li>
|
|
||||||
<li>
|
|
||||||
<label for="AlertAction">Alert Action:</label>
|
|
||||||
<input type="text" id="AlertAction" name="AlertAction">
|
|
||||||
</li>
|
|
||||||
<li>
|
<li>
|
||||||
<input type="submit" value="Insert">
|
<input type="submit" value="Insert">
|
||||||
</li>
|
</li>
|
||||||
|
@ -104,12 +96,6 @@
|
||||||
|
|
||||||
<dt>Alerted At</dt>
|
<dt>Alerted At</dt>
|
||||||
<dd>{{.AlertedAt.Format "2006-01-02 15:04"}}</dd>
|
<dd>{{.AlertedAt.Format "2006-01-02 15:04"}}</dd>
|
||||||
|
|
||||||
<dt>Log Action</dt>
|
|
||||||
<dd>{{.LogAction}}</dd>
|
|
||||||
|
|
||||||
<dt>Alert Action</dt>
|
|
||||||
<dd>{{.AlertAction}}</dd>
|
|
||||||
</dl>
|
</dl>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
@ -145,14 +131,6 @@
|
||||||
<label for="AlertTimeout">Alert Timeout:</label>
|
<label for="AlertTimeout">Alert Timeout:</label>
|
||||||
<input type="number" id="AlertTimeout" name="AlertTimeout" value="{{.Source.AlertTimeout}}">
|
<input type="number" id="AlertTimeout" name="AlertTimeout" value="{{.Source.AlertTimeout}}">
|
||||||
</li>
|
</li>
|
||||||
<li>
|
|
||||||
<label for="LogAction">Log Action:</label>
|
|
||||||
<input type="text" id="LogAction" name="LogAction" value="{{.Source.LogAction}}">
|
|
||||||
</li>
|
|
||||||
<li>
|
|
||||||
<label for="AlertAction">Alert Action:</label>
|
|
||||||
<input type="text" id="AlertAction" name="AlertAction" value="{{.Source.AlertAction}}">
|
|
||||||
</li>
|
|
||||||
<li>
|
<li>
|
||||||
<input type="submit" value="Update">
|
<input type="submit" value="Update">
|
||||||
</li>
|
</li>
|
||||||
|
|
24
tmpl_gen.go
24
tmpl_gen.go
|
@ -81,7 +81,7 @@ var tmpls = `
|
||||||
<td>{{.TS.Format "2006-01-02 15:04"}}
|
<td>{{.TS.Format "2006-01-02 15:04"}}
|
||||||
</td>
|
</td>
|
||||||
<td>{{if .Alert}}<b>!</b>{{end}}</td>
|
<td>{{if .Alert}}<b>!</b>{{end}}</td>
|
||||||
<td>{{.SourceName}}</td>
|
<td><a href="/source/view/{{.SourceID}}">{{.SourceName}}</a></td>
|
||||||
<td style="width:100%;">{{.Text}}</td>
|
<td style="width:100%;">{{.Text}}</td>
|
||||||
</tr>
|
</tr>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
@ -123,14 +123,6 @@ var tmpls = `
|
||||||
<label for="AlertTimeout">Alert Timeout:</label>
|
<label for="AlertTimeout">Alert Timeout:</label>
|
||||||
<input type="number" id="AlertTimeout" name="AlertTimeout">
|
<input type="number" id="AlertTimeout" name="AlertTimeout">
|
||||||
</li>
|
</li>
|
||||||
<li>
|
|
||||||
<label for="LogAction">Log Action:</label>
|
|
||||||
<input type="text" id="LogAction" name="LogAction">
|
|
||||||
</li>
|
|
||||||
<li>
|
|
||||||
<label for="AlertAction">Alert Action:</label>
|
|
||||||
<input type="text" id="AlertAction" name="AlertAction">
|
|
||||||
</li>
|
|
||||||
<li>
|
<li>
|
||||||
<input type="submit" value="Insert">
|
<input type="submit" value="Insert">
|
||||||
</li>
|
</li>
|
||||||
|
@ -200,12 +192,6 @@ var tmpls = `
|
||||||
|
|
||||||
<dt>Alerted At</dt>
|
<dt>Alerted At</dt>
|
||||||
<dd>{{.AlertedAt.Format "2006-01-02 15:04"}}</dd>
|
<dd>{{.AlertedAt.Format "2006-01-02 15:04"}}</dd>
|
||||||
|
|
||||||
<dt>Log Action</dt>
|
|
||||||
<dd>{{.LogAction}}</dd>
|
|
||||||
|
|
||||||
<dt>Alert Action</dt>
|
|
||||||
<dd>{{.AlertAction}}</dd>
|
|
||||||
</dl>
|
</dl>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
@ -241,14 +227,6 @@ var tmpls = `
|
||||||
<label for="AlertTimeout">Alert Timeout:</label>
|
<label for="AlertTimeout">Alert Timeout:</label>
|
||||||
<input type="number" id="AlertTimeout" name="AlertTimeout" value="{{.Source.AlertTimeout}}">
|
<input type="number" id="AlertTimeout" name="AlertTimeout" value="{{.Source.AlertTimeout}}">
|
||||||
</li>
|
</li>
|
||||||
<li>
|
|
||||||
<label for="LogAction">Log Action:</label>
|
|
||||||
<input type="text" id="LogAction" name="LogAction" value="{{.Source.LogAction}}">
|
|
||||||
</li>
|
|
||||||
<li>
|
|
||||||
<label for="AlertAction">Alert Action:</label>
|
|
||||||
<input type="text" id="AlertAction" name="AlertAction" value="{{.Source.AlertAction}}">
|
|
||||||
</li>
|
|
||||||
<li>
|
<li>
|
||||||
<input type="submit" value="Update">
|
<input type="submit" value="Update">
|
||||||
</li>
|
</li>
|
||||||
|
|
3
types.go
3
types.go
|
@ -15,8 +15,6 @@ type Source struct {
|
||||||
LastSeenAt time.Time
|
LastSeenAt time.Time
|
||||||
AlertTimeout int64 // In seconds.
|
AlertTimeout int64 // In seconds.
|
||||||
AlertedAt time.Time // Timeout alert time.
|
AlertedAt time.Time // Timeout alert time.
|
||||||
LogAction string // Override log action.
|
|
||||||
AlertAction string // Override alert action.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Source) TimedOut() bool {
|
func (s Source) TimedOut() bool {
|
||||||
|
@ -37,6 +35,7 @@ type Entry struct {
|
||||||
|
|
||||||
type EntryListRow struct {
|
type EntryListRow struct {
|
||||||
LogID int64
|
LogID int64
|
||||||
|
SourceID string
|
||||||
SourceName string
|
SourceName string
|
||||||
TS time.Time
|
TS time.Time
|
||||||
Alert bool
|
Alert bool
|
||||||
|
|
|
@ -223,8 +223,6 @@ func handleSourceInsert(w http.ResponseWriter, r *http.Request) {
|
||||||
Name: r.Form.Get("Name"),
|
Name: r.Form.Get("Name"),
|
||||||
Description: r.Form.Get("Description"),
|
Description: r.Form.Get("Description"),
|
||||||
AlertTimeout: formGetInt(r, "AlertTimeout"),
|
AlertTimeout: formGetInt(r, "AlertTimeout"),
|
||||||
LogAction: r.Form.Get("LogAction"),
|
|
||||||
AlertAction: r.Form.Get("AlertAction"),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := db.SourceInsert(&s); err != nil {
|
if err := db.SourceInsert(&s); err != nil {
|
||||||
|
@ -271,8 +269,6 @@ func handleSourceUpdate(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
s.Description = r.Form.Get("Description")
|
s.Description = r.Form.Get("Description")
|
||||||
s.AlertTimeout = formGetInt(r, "AlertTimeout")
|
s.AlertTimeout = formGetInt(r, "AlertTimeout")
|
||||||
s.LogAction = r.Form.Get("LogAction")
|
|
||||||
s.AlertAction = r.Form.Get("AlertAction")
|
|
||||||
|
|
||||||
if err := db.SourceUpdate(s); err != nil {
|
if err := db.SourceUpdate(s); err != nil {
|
||||||
execTmpl(w, "Error", err)
|
execTmpl(w, "Error", err)
|
||||||
|
|
Reference in New Issue