Skip to content

Commit d967ade

Browse files
committed
Zabbix: explicit --zabbix switch; token renamed FOGPING_ZABBIX_TOKEN
The integration is now turned on by --zabbix (Docker: FOGPING_ZABBIX=1), with the token in FOGPING_ZABBIX_TOKEN / FOGPING_ZABBIX_TOKEN_FILE / zabbix-token-file=. Without --zabbix nothing changes: a token set anyway is ignored with a one-line note. --zabbix without a token refuses to start, so the stats endpoint is never open. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01E4SFMB893s7ejgrJ7qosV4
1 parent 4b692f6 commit d967ade

8 files changed

Lines changed: 80 additions & 46 deletions

File tree

‎docker/entrypoint.sh‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ case "${FOGPING_EDIT:-}" in
88
1|true|TRUE|yes|on) set -- --edit "$@" ;;
99
esac
1010

11+
case "${FOGPING_ZABBIX:-}" in
12+
1|true|TRUE|yes|on) set -- --zabbix "$@" ;;
13+
esac
14+
1115
if [ -n "${FOGPING_DAYS:-}" ]; then
1216
set -- "--days=${FOGPING_DAYS}" "$@"
1317
fi

‎docs/GUIDE.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Everything after the image name is passed to fogping as-is (`--edit`, `--days=30
4242
| `FOGPING_USER=admin` / `FOGPING_PASSWD=change-me` | `user=admin passwd=change-me` (comma-separated for several) |
4343
| `FOGPING_PASSWD_FILE=/run/secrets/fogping` | `passwd-file=...` — one password per line; works with Docker secrets |
4444
| `FOGPING_DAYS=30` | `--days=30` (default and maximum 93) |
45+
| `FOGPING_ZABBIX=1` + `FOGPING_ZABBIX_TOKEN=...` | `--zabbix` (optional Zabbix stats, see below) |
4546

4647
`--edit` without a login is refused, as on bare metal. PING targets work without
4748
extra capabilities on Docker 20.10+; `--user` keeps the mounted directory writable by you.
@@ -492,7 +493,8 @@ oscilloscope without cgo.
492493
## Zabbix (optional)
493494

494495
fogping can hand its latency summary to Zabbix: median, p90, p99, loss and loss
495-
bursts per target, discovered automatically. It is off unless you give fogping
496-
a stats token (`FOGPING_STATS_TOKEN` or `FOGPING_STATS_TOKEN_FILE`); without one
496+
bursts per target, discovered automatically. It is off unless you start fogping
497+
with `--zabbix` plus a token (`FOGPING_ZABBIX_TOKEN` or
498+
`FOGPING_ZABBIX_TOKEN_FILE`; in Docker `FOGPING_ZABBIX=1`); without `--zabbix`
497499
nothing changes. The template and step-by-step setup are in
498500
[`zabbix/template_fogping/7.0`](../zabbix/template_fogping/7.0/README.md).

‎help.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ FLAGS
6767
--localhost listen on 127.0.0.1:8518 instead of all interfaces (0.0.0.0:8518)
6868
--days=N days of history to keep (default 93, max 93)
6969
--version print the version and exit
70+
--zabbix serve read-only stats to the Zabbix template (needs a token, see below)
7071
user= passwd= turn on the login page (comma-separated lists)
7172
passwd-file=F read the passwords from file F instead, one per line
7273
@@ -79,9 +80,9 @@ FLAGS
7980
./fogping user=admin passwd-file=/etc/fogping.pw # chmod 600
8081
FOGPING_USER and FOGPING_PASSWD_FILE work the same way.
8182
82-
Optional, off unless set: FOGPING_STATS_TOKEN (or FOGPING_STATS_TOKEN_FILE,
83-
stats-token-file=F) turns on a read-only GET /api/stats for the Zabbix
84-
template in zabbix/ of the repository.
83+
Optional: --zabbix with FOGPING_ZABBIX_TOKEN (or FOGPING_ZABBIX_TOKEN_FILE,
84+
zabbix-token-file=F) serves a read-only GET /api/stats for the Zabbix
85+
template in zabbix/ of the repository. Without --zabbix nothing changes.
8586
8687
Full guide, systemd unit, reverse proxy: https://github.com/githubflyideas/fogping
8788
`

‎main.go‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func main() {
2222
days := flag.Int("days", defaultDays, fmt.Sprintf("days of history to keep (max %d); UI hides windows beyond this", maxDays))
2323
edit := flag.Bool("edit", false, "allow adding/editing/deleting targets from the web UI (default: read-only)")
2424
showVer := flag.Bool("version", false, "print version")
25+
zabbix := flag.Bool("zabbix", false, "serve a read-only GET /api/stats for the Zabbix template (needs FOGPING_ZABBIX_TOKEN)")
2526
args := os.Args[1:]
2627
for _, a := range args {
2728
if a == "help" { // `fogping help` would otherwise be rejected as a bad auth arg
@@ -53,7 +54,7 @@ func main() {
5354
fmt.Fprintf(os.Stderr, "fogping: %v\nRun './fogping --help' for examples.\n", err)
5455
os.Exit(2)
5556
}
56-
statsToken, kvArgs, err := resolveStatsToken(append(authArgs, flag.Args()...), os.Getenv)
57+
statsToken, kvArgs, tokenIgnored, err := resolveZabbixToken(*zabbix, append(authArgs, flag.Args()...), os.Getenv)
5758
if err != nil {
5859
log.Fatalf("%v\nRun './fogping --help' for examples.", err)
5960
}
@@ -64,7 +65,10 @@ func main() {
6465
// The variables have done their job; do not hand them to anything this
6566
// process might start.
6667
os.Unsetenv("FOGPING_PASSWD")
67-
os.Unsetenv("FOGPING_STATS_TOKEN")
68+
os.Unsetenv("FOGPING_ZABBIX_TOKEN")
69+
if tokenIgnored {
70+
log.Printf("note: a Zabbix token is set but --zabbix was not given; the Zabbix stats API stays off")
71+
}
6872
if cliPasswd {
6973
log.Printf("WARNING: passwd= on the command line is visible to every account on this machine (ps, /proc) " +
7074
"and lands in shell history. Prefer FOGPING_PASSWD=... in the environment or passwd-file=PATH.")
@@ -75,7 +79,7 @@ func main() {
7579
cfg.Editable = *edit
7680
cfg.StatsToken = statsToken
7781
if statsToken != "" {
78-
log.Printf("stats API on: GET /api/stats with the stats token (for the Zabbix template)")
82+
log.Printf("zabbix: on — read-only GET /api/stats for the Zabbix template")
7983
}
8084
// An open, writable UI would let anyone who can reach the port make this host
8185
// ping or TCP-connect to arbitrary addresses. Refuse rather than warn.

‎stats.go‎

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
// Read-only statistics for Zabbix (zabbix/README.md).
1313
//
14-
// Off unless a stats token is configured, and off means absent: no route, no
14+
// Off unless fogping runs with --zabbix (and a token), and off means absent: no route, no
1515
// help text the user has to read, nothing in the UI. fogping without it is
1616
// exactly fogping. With a token, GET /api/stats returns the latest summary of
1717
// every target — percentiles, loss, bursts — and nothing else: no hosts, no
@@ -45,45 +45,57 @@ type statsReport struct {
4545
Targets []statsTarget `json:"targets"`
4646
}
4747

48-
// resolveStatsToken takes stats-token-file=PATH out of the key=value arguments,
49-
// then falls back to FOGPING_STATS_TOKEN / FOGPING_STATS_TOKEN_FILE. An empty
50-
// result means the integration is off.
51-
func resolveStatsToken(args []string, getenv func(string) string) (token string, rest []string, err error) {
48+
// resolveZabbixToken takes zabbix-token-file=PATH out of the key=value
49+
// arguments (always, so the login parser never sees it), then reads the token
50+
// from that file or from FOGPING_ZABBIX_TOKEN / FOGPING_ZABBIX_TOKEN_FILE.
51+
//
52+
// Without --zabbix (enabled=false) the token is ignored and the integration is
53+
// off; ignored reports that one was supplied anyway, so main can say so. With
54+
// --zabbix a token is required: an open stats endpoint is never an option.
55+
func resolveZabbixToken(enabled bool, args []string, getenv func(string) string) (token string, rest []string, ignored bool, err error) {
5256
file := ""
5357
for _, a := range args {
54-
if v, ok := strings.CutPrefix(a, "stats-token-file="); ok {
58+
if v, ok := strings.CutPrefix(a, "zabbix-token-file="); ok {
5559
file = v
5660
continue
5761
}
5862
rest = append(rest, a)
5963
}
64+
envTok, envFile := getenv("FOGPING_ZABBIX_TOKEN"), getenv("FOGPING_ZABBIX_TOKEN_FILE")
65+
if !enabled {
66+
return "", rest, file != "" || envTok != "" || envFile != "", nil
67+
}
6068
if file == "" {
61-
if token = getenv("FOGPING_STATS_TOKEN"); token != "" {
62-
if getenv("FOGPING_STATS_TOKEN_FILE") != "" {
63-
return "", rest, errStr("set FOGPING_STATS_TOKEN or FOGPING_STATS_TOKEN_FILE, not both")
64-
}
65-
} else {
66-
file = getenv("FOGPING_STATS_TOKEN_FILE")
69+
switch {
70+
case envTok != "" && envFile != "":
71+
return "", rest, false, errStr("set FOGPING_ZABBIX_TOKEN or FOGPING_ZABBIX_TOKEN_FILE, not both")
72+
case envTok != "":
73+
token = envTok
74+
default:
75+
file = envFile
6776
}
6877
}
6978
if file != "" {
7079
b, err := os.ReadFile(file)
7180
if err != nil {
72-
return "", rest, errStr("stats token file: " + err.Error())
81+
return "", rest, false, errStr("zabbix token file: " + err.Error())
7382
}
7483
token = strings.TrimSpace(string(b))
7584
}
76-
if token != "" && len(token) < 16 {
77-
return "", rest, errStr("stats token must be at least 16 characters")
85+
if token == "" {
86+
return "", rest, false, errStr("--zabbix needs a token: set FOGPING_ZABBIX_TOKEN=... or FOGPING_ZABBIX_TOKEN_FILE=PATH (or zabbix-token-file=PATH)")
87+
}
88+
if len(token) < 16 {
89+
return "", rest, false, errStr("zabbix token must be at least 16 characters")
7890
}
79-
return token, rest, nil
91+
return token, rest, false, nil
8092
}
8193

8294
type errStr string
8395

8496
func (e errStr) Error() string { return string(e) }
8597

86-
// registerStats adds GET /api/stats when a token is configured, and nothing
98+
// registerStats adds GET /api/stats when --zabbix gave a token, and nothing
8799
// otherwise.
88100
func registerStats(mux *http.ServeMux, token string, store *Store, run *Runner) {
89101
if token == "" {

‎stats_test.go‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,28 +137,38 @@ func TestStatsEndpoint(t *testing.T) {
137137
}
138138
}
139139

140-
func TestResolveStatsToken(t *testing.T) {
140+
func TestResolveZabbixToken(t *testing.T) {
141141
dir := t.TempDir()
142142
f := filepath.Join(dir, "tok")
143143
os.WriteFile(f, []byte(testStatsToken+"\n"), 0600)
144144
env := func(m map[string]string) func(string) string { return func(k string) string { return m[k] } }
145+
withTok := env(map[string]string{"FOGPING_ZABBIX_TOKEN": testStatsToken})
145146

146-
if tok, rest, err := resolveStatsToken([]string{"user=a", "passwd=b"}, env(nil)); err != nil || tok != "" || len(rest) != 2 {
147-
t.Errorf("default must be off: %q %v %v", tok, rest, err)
147+
// Default: off, login args untouched.
148+
if tok, rest, ign, err := resolveZabbixToken(false, []string{"user=a", "passwd=b"}, env(nil)); err != nil || tok != "" || ign || len(rest) != 2 {
149+
t.Errorf("default must be off: %q %v %v %v", tok, rest, ign, err)
148150
}
149-
if tok, rest, err := resolveStatsToken([]string{"user=a", "stats-token-file=" + f}, env(nil)); err != nil || tok != testStatsToken || len(rest) != 1 {
150-
t.Errorf("file arg: %q %v %v", tok, rest, err)
151+
// A token without --zabbix is ignored (and reported), never an error.
152+
if tok, rest, ign, err := resolveZabbixToken(false, []string{"user=a", "zabbix-token-file=" + f}, withTok); err != nil || tok != "" || !ign || len(rest) != 1 {
153+
t.Errorf("token without --zabbix: %q %v %v %v", tok, rest, ign, err)
154+
}
155+
// --zabbix without a token refuses to start.
156+
if _, _, _, err := resolveZabbixToken(true, nil, env(nil)); err == nil || !strings.Contains(err.Error(), "--zabbix needs a token") {
157+
t.Errorf("--zabbix without token: %v", err)
151158
}
152-
if tok, _, err := resolveStatsToken(nil, env(map[string]string{"FOGPING_STATS_TOKEN": testStatsToken})); err != nil || tok != testStatsToken {
159+
if tok, _, _, err := resolveZabbixToken(true, nil, withTok); err != nil || tok != testStatsToken {
153160
t.Errorf("env: %q %v", tok, err)
154161
}
155-
if tok, _, err := resolveStatsToken(nil, env(map[string]string{"FOGPING_STATS_TOKEN_FILE": f})); err != nil || tok != testStatsToken {
162+
if tok, rest, _, err := resolveZabbixToken(true, []string{"user=a", "zabbix-token-file=" + f}, env(nil)); err != nil || tok != testStatsToken || len(rest) != 1 {
163+
t.Errorf("file arg: %q %v %v", tok, rest, err)
164+
}
165+
if tok, _, _, err := resolveZabbixToken(true, nil, env(map[string]string{"FOGPING_ZABBIX_TOKEN_FILE": f})); err != nil || tok != testStatsToken {
156166
t.Errorf("env file: %q %v", tok, err)
157167
}
158-
if _, _, err := resolveStatsToken(nil, env(map[string]string{"FOGPING_STATS_TOKEN": "short"})); err == nil {
168+
if _, _, _, err := resolveZabbixToken(true, nil, env(map[string]string{"FOGPING_ZABBIX_TOKEN": "short"})); err == nil {
159169
t.Error("a short token was accepted")
160170
}
161-
if _, _, err := resolveStatsToken(nil, env(map[string]string{"FOGPING_STATS_TOKEN": testStatsToken, "FOGPING_STATS_TOKEN_FILE": f})); err == nil {
171+
if _, _, _, err := resolveZabbixToken(true, nil, env(map[string]string{"FOGPING_ZABBIX_TOKEN": testStatsToken, "FOGPING_ZABBIX_TOKEN_FILE": f})); err == nil {
162172
t.Error("both env sources accepted")
163173
}
164174
}

‎zabbix/template_fogping/7.0/README.md‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,32 +32,33 @@ target in fogping keeps its history.
3232
- `loss burst flagged` (Info), suppressed while the loss trigger is active,
3333
so a dead target raises one problem, not two;
3434
- `no data from {$FOGPING.URL} for 5m` (Warning), on the template itself:
35-
fogping unreachable, wrong token, or stats not enabled.
35+
fogping unreachable, wrong token, or started without `--zabbix`.
3636

3737
A target with no replies in the window (down, or a slow target with no round
3838
yet) gets no latency values rather than zeros.
3939

4040
## Setup
4141

42-
**1. Turn on fogping's stats API.** It is off by default. Give fogping a token
43-
of at least 16 characters, through the environment or a file, never on the
44-
command line:
42+
**1. Start fogping with `--zabbix`.** It is off by default. `--zabbix` needs a
43+
token of at least 16 characters, given through the environment or a file,
44+
never on the command line:
4545

4646
```bash
47-
openssl rand -hex 24 > /etc/fogping.stats-token && chmod 600 /etc/fogping.stats-token
48-
FOGPING_STATS_TOKEN_FILE=/etc/fogping.stats-token ./fogping user=admin
49-
# or: FOGPING_STATS_TOKEN=<token> ./fogping ...
47+
openssl rand -hex 24 > /etc/fogping.zabbix-token && chmod 600 /etc/fogping.zabbix-token
48+
FOGPING_ZABBIX_TOKEN_FILE=/etc/fogping.zabbix-token ./fogping --zabbix user=admin
49+
# or: FOGPING_ZABBIX_TOKEN=<token> ./fogping --zabbix ...
50+
# Docker: -e FOGPING_ZABBIX=1 -e FOGPING_ZABBIX_TOKEN=<token>
5051
```
5152

52-
fogping logs `stats API on` when it starts. Check it from the Zabbix server or
53+
fogping logs `zabbix: on` when it starts. Check it from the Zabbix server or
5354
proxy:
5455

5556
```bash
56-
curl -H "Authorization: Bearer $(cat /etc/fogping.stats-token)" http://<fogping-host>:8518/api/stats
57+
curl -H "Authorization: Bearer $(cat /etc/fogping.zabbix-token)" http://<fogping-host>:8518/api/stats
5758
```
5859

5960
You should see `{"window_sec":300,"targets":[...]}`. A `401` means the token is
60-
wrong; a `404` means fogping was started without a token.
61+
wrong; a `404` means fogping was started without `--zabbix`.
6162

6263
**2. Import the template.** *Data collection → Templates → Import*, choose
6364
`template_fogping.yaml`.

‎zabbix/template_fogping/7.0/template_fogping.yaml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ zabbix_export:
3737
expression: 'nodata(/fogping by HTTP/fogping.stats.get,5m)=1'
3838
name: 'fogping: no data from {$FOGPING.URL} for 5m'
3939
priority: WARNING
40-
description: 'fogping is unreachable, the token is wrong, or the stats API is not enabled.'
40+
description: 'fogping is unreachable, the token is wrong, or fogping runs without --zabbix.'
4141
discovery_rules:
4242
- uuid: e35c45f5b13a4df9ac7213e0aaf41920
4343
name: 'fogping: Targets discovery'
@@ -184,7 +184,7 @@ zabbix_export:
184184
macros:
185185
- macro: '{$FOGPING.TOKEN}'
186186
type: SECRET_TEXT
187-
description: 'The stats token fogping was started with (FOGPING_STATS_TOKEN).'
187+
description: 'The token fogping was started with (FOGPING_ZABBIX_TOKEN).'
188188
- macro: '{$FOGPING.URL}'
189189
value: 'http://fogping.example:8518'
190190
description: 'fogping base URL, no trailing slash.'

0 commit comments

Comments
 (0)