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)
|
||||
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
25
db.go
25
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
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -44,7 +44,7 @@
|
|||
<td>{{.TS.Format "2006-01-02 15:04"}}
|
||||
</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>
|
||||
</tr>
|
||||
{{end}}
|
||||
|
|
|
@ -27,14 +27,6 @@
|
|||
<label for="AlertTimeout">Alert Timeout:</label>
|
||||
<input type="number" id="AlertTimeout" name="AlertTimeout">
|
||||
</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>
|
||||
<input type="submit" value="Insert">
|
||||
</li>
|
||||
|
@ -104,12 +96,6 @@
|
|||
|
||||
<dt>Alerted At</dt>
|
||||
<dd>{{.AlertedAt.Format "2006-01-02 15:04"}}</dd>
|
||||
|
||||
<dt>Log Action</dt>
|
||||
<dd>{{.LogAction}}</dd>
|
||||
|
||||
<dt>Alert Action</dt>
|
||||
<dd>{{.AlertAction}}</dd>
|
||||
</dl>
|
||||
</section>
|
||||
|
||||
|
@ -145,14 +131,6 @@
|
|||
<label for="AlertTimeout">Alert Timeout:</label>
|
||||
<input type="number" id="AlertTimeout" name="AlertTimeout" value="{{.Source.AlertTimeout}}">
|
||||
</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>
|
||||
<input type="submit" value="Update">
|
||||
</li>
|
||||
|
|
24
tmpl_gen.go
24
tmpl_gen.go
|
@ -81,7 +81,7 @@ var tmpls = `
|
|||
<td>{{.TS.Format "2006-01-02 15:04"}}
|
||||
</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>
|
||||
</tr>
|
||||
{{end}}
|
||||
|
@ -123,14 +123,6 @@ var tmpls = `
|
|||
<label for="AlertTimeout">Alert Timeout:</label>
|
||||
<input type="number" id="AlertTimeout" name="AlertTimeout">
|
||||
</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>
|
||||
<input type="submit" value="Insert">
|
||||
</li>
|
||||
|
@ -200,12 +192,6 @@ var tmpls = `
|
|||
|
||||
<dt>Alerted At</dt>
|
||||
<dd>{{.AlertedAt.Format "2006-01-02 15:04"}}</dd>
|
||||
|
||||
<dt>Log Action</dt>
|
||||
<dd>{{.LogAction}}</dd>
|
||||
|
||||
<dt>Alert Action</dt>
|
||||
<dd>{{.AlertAction}}</dd>
|
||||
</dl>
|
||||
</section>
|
||||
|
||||
|
@ -241,14 +227,6 @@ var tmpls = `
|
|||
<label for="AlertTimeout">Alert Timeout:</label>
|
||||
<input type="number" id="AlertTimeout" name="AlertTimeout" value="{{.Source.AlertTimeout}}">
|
||||
</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>
|
||||
<input type="submit" value="Update">
|
||||
</li>
|
||||
|
|
3
types.go
3
types.go
|
@ -15,8 +15,6 @@ type Source struct {
|
|||
LastSeenAt time.Time
|
||||
AlertTimeout int64 // In seconds.
|
||||
AlertedAt time.Time // Timeout alert time.
|
||||
LogAction string // Override log action.
|
||||
AlertAction string // Override alert action.
|
||||
}
|
||||
|
||||
func (s Source) TimedOut() bool {
|
||||
|
@ -37,6 +35,7 @@ type Entry struct {
|
|||
|
||||
type EntryListRow struct {
|
||||
LogID int64
|
||||
SourceID string
|
||||
SourceName string
|
||||
TS time.Time
|
||||
Alert bool
|
||||
|
|
|
@ -223,8 +223,6 @@ func handleSourceInsert(w http.ResponseWriter, r *http.Request) {
|
|||
Name: r.Form.Get("Name"),
|
||||
Description: r.Form.Get("Description"),
|
||||
AlertTimeout: formGetInt(r, "AlertTimeout"),
|
||||
LogAction: r.Form.Get("LogAction"),
|
||||
AlertAction: r.Form.Get("AlertAction"),
|
||||
}
|
||||
|
||||
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.AlertTimeout = formGetInt(r, "AlertTimeout")
|
||||
s.LogAction = r.Form.Get("LogAction")
|
||||
s.AlertAction = r.Form.Get("AlertAction")
|
||||
|
||||
if err := db.SourceUpdate(s); err != nil {
|
||||
execTmpl(w, "Error", err)
|
||||
|
|
Reference in New Issue