diff options
author | Gibheer <gibheer+git@zero-knowledge.org> | 2018-12-11 12:37:30 +0100 |
---|---|---|
committer | Gibheer <gibheer+git@zero-knowledge.org> | 2018-12-11 12:37:30 +0100 |
commit | dece1ac2dc4554f66f8ec194269cd91d330edae1 (patch) | |
tree | 046475e3dcc818efd14d59ddc149e15192442fc9 /cmd | |
parent | c62916c46e1cdfb0e04a9f17dd72ff7048924c99 (diff) |
add level mappings
This allows to map the command exit codes to any other output level
which can then be reported by the notification plugin.
With the provided colors, the frontend will show them accordingly.
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/monfront/main.go | 96 | ||||
-rw-r--r-- | cmd/monwork/main.go | 10 |
2 files changed, 84 insertions, 22 deletions
diff --git a/cmd/monfront/main.go b/cmd/monfront/main.go index eaf1d94..7d80c65 100644 --- a/cmd/monfront/main.go +++ b/cmd/monfront/main.go @@ -26,6 +26,29 @@ type ( DB string `json:"db"` Listen string `json:"listen"` } + + Context struct { + Mappings map[int]map[int]MapEntry + Checks []check + } + + MapEntry struct { + Title string + Color string + } + + check struct { + NodeName string + CommandName string + CheckID int64 + MappingId int + State int + Notify bool + Enabled bool + Notice sql.NullString + NextTime time.Time + Msg string + } ) func main() { @@ -149,21 +172,10 @@ func showChecks(w http.ResponseWriter, r *http.Request) { return } - type check struct { - NodeName string - CommandName string - CheckID int64 - State int - Notify bool - Enabled bool - Notice sql.NullString - NextTime time.Time - Msg string - } checks := []check{} for rows.Next() { c := check{} - err := rows.Scan(&c.CheckID, &c.NodeName, &c.CommandName, &c.State, &c.Notify, &c.Enabled, &c.Notice, &c.NextTime, &c.Msg) + err := rows.Scan(&c.CheckID, &c.NodeName, &c.CommandName, &c.MappingId, &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")) @@ -181,8 +193,17 @@ func showChecks(w http.ResponseWriter, r *http.Request) { log.Printf("could not parse template: %s", err) return } + con := Context{ + Checks: checks, + } + if err := loadMappings(&con); err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("problem with the mappings")) + log.Printf("could not load mappings: %s", err) + return + } w.Header()["Content-Type"] = []string{"text/html"} - if err := tmpl.Execute(w, checks); err != nil { + if err := tmpl.Execute(w, con); err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("problem with a template")) log.Printf("could not execute template: %s", err) @@ -193,6 +214,7 @@ func showChecks(w http.ResponseWriter, r *http.Request) { func showUnhandledHosts(w http.ResponseWriter, r *http.Request) { } + func showUnhandledGroups(w http.ResponseWriter, r *http.Request) { rows, err := DB.Query(SQLShowUnhandledGroups) if err != nil { @@ -236,8 +258,40 @@ func showUnhandledGroups(w http.ResponseWriter, r *http.Request) { return } +func loadMappings(c *Context) error { + c.Mappings = map[int]map[int]MapEntry{} + rows, err := DB.Query(SQLShowMappings) + if err != nil { + return err + } + + for rows.Next() { + if rows.Err() != nil { + return rows.Err() + } + var ( + mapId int + target int + title string + color string + ) + if err := rows.Scan(&mapId, &target, &title, &color); err != nil { + return err + } + ma, found := c.Mappings[mapId] + if !found { + ma = map[int]MapEntry{} + c.Mappings[mapId] = ma + } + ma[target] = MapEntry{Title: title, Color: color} + } + return nil +} + var ( - SQLShowChecks = `select c.id, n.name, co.name, ac.states[1] as state, ac.notify, + SQLShowMappings = `select mapping_id, target, title, color + from mapping_level` + SQLShowChecks = `select c.id, n.name, co.name, ac.mapping_id, 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 @@ -280,10 +334,15 @@ var ( form nav { display: flex; flex-direction: column; } form nav > * { margin: 0.5em; } table td, table th { padding: 0.5em; } - td.state-0 { background: green; } + {{ range $mapId, $mapping := .Mappings }} + {{ range $target, $val := $mapping }} + td.state-{{ $mapId }}-{{ $target }} { background: {{ $val.Color }}; } + {{ end }} + {{ end }} + /* td.state-0 { background: green; } td.state-1 { background: orange; } td.state-2 { background: red; } - td.state-99 { background: gray; } + td.state-99 { background: gray; } */ </style> </head> <body> @@ -321,11 +380,12 @@ var ( <thead><tr><th></th><th>host</th><th>status</th><th>next check</th><th>message</th></tr></thead> <tbody> {{ $current := "" }} - {{ range . }} + {{ $mapping := .Mappings }} + {{ range .Checks }} <tr> <td><input type="checkbox" name="checks" value="{{ .CheckID }}" /></td> <td>{{ if ne $current .NodeName }}{{ $current = .NodeName }}{{ .NodeName }}{{ end }}</td> - <td class="state-{{ .State }}">{{ .CommandName }} - {{ .State }}</td> + <td class="state-{{ .MappingId }}-{{ .State }}">{{ .CommandName }} - {{ (index $mapping .MappingId .State).Title }}</td> <td>{{ .NextTime.Format "2006.01.02 15:04:05" }}</td> <td><pre>{{ .Msg }}</pre></td> </tr> diff --git a/cmd/monwork/main.go b/cmd/monwork/main.go index 5c71a60..13c4f39 100644 --- a/cmd/monwork/main.go +++ b/cmd/monwork/main.go @@ -218,11 +218,13 @@ var ( where c.last_refresh < c.updated or c.last_refresh is null limit 1 for update of c skip locked;` - SQLRefreshActiveCheck = `insert into active_checks(check_id, cmdline, intval, enabled, notify, msg) -select id, $2, c.intval, c.enabled, c.notify, case when ac.msg is null then '' else ac.msg end from checks c + SQLRefreshActiveCheck = `insert into active_checks(check_id, cmdline, intval, enabled, notify, msg, mapping_id) +select c.id, $2, c.intval, c.enabled, c.notify, case when ac.msg is null then '' else ac.msg end, case when c.mapping_id is not null then c.mapping_id when n.mapping_id is not null then n.mapping_id else 1 end +from checks c left join active_checks ac on c.id = ac.check_id -where id = $1 +left join nodes n on c.node_id = n.id +where c.id = $1 on conflict(check_id) -do update set cmdline = $2, intval = excluded.intval, enabled = excluded.enabled, notify = excluded.notify;` +do update set cmdline = $2, intval = excluded.intval, enabled = excluded.enabled, notify = excluded.notify, mapping_id = excluded.mapping_id;` SQLUpdateLastRefresh = `update checks set last_refresh = now() where id = $1;` ) |