Repository navigation
Expand file tree
/
Copy pathstats.go
More file actions
141 lines (131 loc) · 4.96 KB
/
Copy pathstats.go
File metadata and controls
141 lines (131 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"crypto/subtle"
"log"
"net/http"
"os"
"strings"
"time"
)
// Read-only statistics for Zabbix. Same contract and same template as
// fogping: https://github.com/githubflyideas/fogping/tree/main/zabbix
//
// Off unless pingping runs with --zabbix (and a token), and off means absent: no route, no
// help text the user has to read, nothing in the UI. pingping without it is
// exactly pingping. With a token, GET /api/stats returns the latest summary of
// every target — percentiles, loss, bursts — and nothing else: no hosts, no
// ports, no version, no raw samples. It never writes.
//
// The token is its own secret, not a login: Zabbix's HTTP agent cannot hold a
// session. Like the password it is read from the environment or a file, never
// from an argument every local account can see in ps.
// statsWindow is how far back the summary looks. Five minutes covers several
// rounds of a fast or normal target; a slow one (300 s) may have none, which is
// reported as rounds=0 with no percentiles rather than as zeros.
const statsWindow = 5 * time.Minute
// statsTarget is the contract with the Zabbix template. Fields may be added;
// none may be renamed or change meaning.
type statsTarget struct {
ID int64 `json:"id"`
Name string `json:"name"`
Rounds int `json:"rounds"`
P50 *float64 `json:"p50,omitempty"` // ms
P90 *float64 `json:"p90,omitempty"`
P99 *float64 `json:"p99,omitempty"`
LossPct *float64 `json:"loss_pct,omitempty"`
Bursts int `json:"bursts"` // loss bursts flagged in the window (the chart's ◆)
Up *int `json:"up,omitempty"` // 1 if the latest round got any reply, else 0
}
type statsReport struct {
WindowSec int `json:"window_sec"`
Targets []statsTarget `json:"targets"`
}
// resolveZabbixToken reads the token from --zabbix-token-file or from
// PINGPING_ZABBIX_TOKEN / PINGPING_ZABBIX_TOKEN_FILE.
//
// Without --zabbix (enabled=false) the token is ignored and the integration is
// off; ignored reports that one was supplied anyway, so the caller can say so.
// With --zabbix a token is required: an open stats endpoint is never an option.
func resolveZabbixToken(enabled bool, file string, getenv func(string) string) (token string, ignored bool, err error) {
envTok, envFile := getenv("PINGPING_ZABBIX_TOKEN"), getenv("PINGPING_ZABBIX_TOKEN_FILE")
if !enabled {
return "", file != "" || envTok != "" || envFile != "", nil
}
if file == "" {
switch {
case envTok != "" && envFile != "":
return "", false, errStr("set PINGPING_ZABBIX_TOKEN or PINGPING_ZABBIX_TOKEN_FILE, not both")
case envTok != "":
token = envTok
default:
file = envFile
}
}
if file != "" {
b, err := os.ReadFile(file)
if err != nil {
return "", false, errStr("zabbix token file: " + err.Error())
}
token = strings.TrimSpace(string(b))
}
if token == "" {
return "", false, errStr("--zabbix needs a token: set PINGPING_ZABBIX_TOKEN=... or PINGPING_ZABBIX_TOKEN_FILE=PATH (or --zabbix-token-file PATH)")
}
if len(token) < 16 {
return "", false, errStr("zabbix token must be at least 16 characters")
}
return token, false, nil
}
type errStr string
func (e errStr) Error() string { return string(e) }
// registerStats adds GET /api/stats when --zabbix gave a token, and nothing
// otherwise.
func registerStats(mux *http.ServeMux, token string, store *Store, run *Runner) {
if token == "" {
return
}
throttle := newLoginThrottle() // wrong tokens are guesses too
mux.HandleFunc("GET /api/stats", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-store")
key := loginClientKey(r)
if wait, ok := throttle.begin(key); !ok {
w.Header().Set("Retry-After", retryAfter(wait))
jsonErr(w, http.StatusTooManyRequests, "too many failed attempts")
return
}
got, isBearer := strings.CutPrefix(r.Header.Get("Authorization"), "Bearer ")
if !isBearer || subtle.ConstantTimeCompare([]byte(got), []byte(token)) != 1 {
if lock := throttle.fail(key); lock > 0 {
log.Printf("stats: bad token from %s; address locked for %v", r.RemoteAddr, lock)
}
jsonErr(w, http.StatusUnauthorized, "bad or missing token")
return
}
throttle.success(key)
writeJSON(w, buildStats(store, run.Targets(), time.Now()))
})
}
func buildStats(store *Store, targets []TargetCfg, now time.Time) statsReport {
out := statsReport{WindowSec: int(statsWindow / time.Second), Targets: []statsTarget{}}
since := now.Add(-statsWindow).Unix()
for _, t := range targets {
rec := store.Recent(t.Name, since)
st := calcStats(rec)
item := statsTarget{ID: t.ID, Name: t.Name, Rounds: st.Rounds, Bursts: st.Bursts}
if st.Rounds > 0 {
loss := st.LossPct
item.LossPct = &loss
up := 0
if rec[len(rec)-1].R > 0 {
up = 1
}
item.Up = &up
if st.LossPct < 100 { // some replies, so the percentiles are real
p50, p90, p99 := st.P50, st.P90, st.P99
item.P50, item.P90, item.P99 = &p50, &p90, &p99
}
}
out.Targets = append(out.Targets, item)
}
return out
}