From 3400167c98c78a2cf104cd4be3907450e01c56ff Mon Sep 17 00:00:00 2001 From: Gibheer Date: Thu, 29 Nov 2018 10:56:20 +0100 Subject: monfront - add initial work for unhandled links This will return the unhandled groups/checks/whatever but is still work in progress. --- cmd/monfront/main.go | 103 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 93 insertions(+), 10 deletions(-) (limited to 'cmd') diff --git a/cmd/monfront/main.go b/cmd/monfront/main.go index 544d4f3..ec3eeb1 100644 --- a/cmd/monfront/main.go +++ b/cmd/monfront/main.go @@ -10,6 +10,7 @@ import ( "io/ioutil" "log" "net/http" + "strings" "time" "github.com/lib/pq" @@ -47,6 +48,9 @@ func main() { http.HandleFunc("/", showChecks) http.HandleFunc("/action", checkAction) + http.HandleFunc("/unhandled/checks", showChecks) + http.HandleFunc("/unhandled/hosts", showUnhandledHosts) + http.HandleFunc("/unhandled/groups", showUnhandledGroups) http.ListenAndServe(config.Listen, nil) } @@ -122,13 +126,22 @@ func checkAction(w http.ResponseWriter, r *http.Request) { log.Printf("could not adjust checks %#v: %s", checks, err) return } - w.Header()["Location"] = []string{"/"} + ref, found := r.Header["Referrer"] + if found { + w.Header()["Location"] = ref + } else { + w.Header()["Location"] = []string{"/"} + } w.WriteHeader(http.StatusSeeOther) return } func showChecks(w http.ResponseWriter, r *http.Request) { - rows, err := DB.Query(SQLShowChecks) + query := SQLShowChecks + if strings.HasPrefix(r.URL.Path, "/unhandled") { + query = SQLShowUnhandledChecks + } + rows, err := DB.Query(query) if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("problems with the database")) @@ -137,7 +150,7 @@ func showChecks(w http.ResponseWriter, r *http.Request) { } type check struct { - Name string + NodeName string CommandName string CheckID int64 State int @@ -150,7 +163,7 @@ func showChecks(w http.ResponseWriter, r *http.Request) { checks := []check{} for rows.Next() { c := check{} - err := rows.Scan(&c.CheckID, &c.Name, &c.CommandName, &c.State, &c.Notify, &c.Enabled, &c.Notice, &c.NextTime, &c.Msg) + err := rows.Scan(&c.CheckID, &c.NodeName, &c.CommandName, &c.State, &c.Notify, &c.Enabled, &c.Notice, &c.NextTime, &c.Msg) if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("problems with the database")) @@ -176,6 +189,51 @@ func showChecks(w http.ResponseWriter, r *http.Request) { return } +func showUnhandledHosts(w http.ResponseWriter, r *http.Request) { +} +func showUnhandledGroups(w http.ResponseWriter, r *http.Request) { + rows, err := DB.Query(SQLShowUnhandledGroups) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("problems with the database")) + log.Printf("could not get check list: %s", err) + return + } + + type check struct { + GroupName string + NodeName string + State int + } + checks := []check{} + for rows.Next() { + c := check{} + err := rows.Scan(&c.GroupName, &c.NodeName, &c.State) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("problems with the database")) + log.Printf("could not get check list: %s", err) + return + } + checks = append(checks, c) + } + tmpl, err := template.New("checklist").Parse(TmplUnhandledGroups) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("problems with a template")) + log.Printf("could not parse template: %s", err) + return + } + w.Header()["Content-Type"] = []string{"text/html"} + if err := tmpl.Execute(w, checks); err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("problem with a template")) + log.Printf("could not execute template: %s", err) + return + } + return +} + var ( SQLShowChecks = `select c.id, n.name, co.name, ac.states[1] as state, ac.notify, ac.enabled, ac.notice, ac.next_time, ac.msg @@ -184,6 +242,22 @@ var ( join nodes n on c.node_id = n.id join commands co on c.command_id = co.id order by n.name, co.name;` + SQLShowUnhandledChecks = `select c.id, n.name, co.name, ac.states[1] as state, ac.notify, + ac.enabled, ac.notice, ac.next_time, ac.msg + from active_checks ac + join checks c on ac.check_id = c.id + join nodes n on c.node_id = n.id + join commands co on c.command_id = co.id + where ac.states[1] > 0 + order by n.name, co.name;` + SQLShowUnhandledGroups = `select g.name, n.name, max(ac.state[1]) + from groups g + join nodes_groups ng on g.id = ng.group_id + join nodes n on ng.node_id = n.id + join checks c on n.id = c.node_id + join active_checks ac on c.id = ac.check_id + where ac.states[1] > 0 + group by g.name, n.name;` ) var ( @@ -199,10 +273,10 @@ var ( justify-content: center; align-items: flex-start; } - form menu { order: 1; } + form nav { order: 1; } form content { order: 2; flex-grow: 1; } - form menu { display: flex; flex-direction: column; } - form menu > * { margin: 0.5em; } + form nav { display: flex; flex-direction: column; } + form nav > * { margin: 0.5em; } table td, table th { padding: 0.5em; } td.state-0 { background: green; } td.state-1 { background: orange; } @@ -211,9 +285,17 @@ var ( +
- + + @@ -239,7 +321,7 @@ var ( {{ range . }} - + @@ -251,4 +333,5 @@ var ( ` + TmplUnhandledGroups = `TODO` ) -- cgit v1.2.3-70-g09d2
hoststatusnext checkmessage
{{ .Name }}{{ .NodeName }} {{ .CommandName }} - {{ .State }} {{ .NextTime.Format "2006.01.02 15:04:05" }}
{{ .Msg }}