summaryrefslogtreecommitdiff
path: root/docs/upload-client.md
blob: dff3abf1cdd6734d503b525c825ad7081ecfd430 (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# Upload client installation

`scripts/goprecords-upload-client.sh` is a POSIX `sh` script that uploads
uptimed record files to a running `goprecords` daemon. It works on FreeBSD,
Linux, and OpenBSD, and runs as root or as a regular user.

`scripts/goprecords-upload-client-darwin.fish` is a fish-shell variant for
macOS (Darwin). It handles both Intel (`/usr/local/var/uptimed`) and Apple
Silicon (`/opt/homebrew/var/uptimed`) Homebrew prefixes and uses macOS-native
commands (`sw_vers`, `sysctl`) for OS and CPU identification.

A copy of the POSIX script is also kept in `contrib/` for backward compatibility.

## Prerequisites

Install `curl` and `uptimed` on every client host and ensure `uptimed` is
running before setting up uploads.

| OS | Package manager |
|----|-----------------|
| FreeBSD | `pkg install curl uptimed` |
| Rocky Linux / Fedora | `sudo dnf install curl uptimed` |
| OpenBSD | `pkg_add curl uptimed` |
| macOS (Intel) | `brew install curl uptimed` |
| macOS (Apple Silicon) | `brew install curl uptimed` |

## Token path

| Privilege | Path |
|-----------|------|
| root | `/etc/goprecords-upload.token` (mode `600`) |
| non-root | `${XDG_CONFIG_HOME:-$HOME/.config}/goprecords-upload-<HOST>/token` (mode `600`) |

Override with `GOPRECORDS_TOKEN_FILE`.

## Environment variables

| Variable | Default (root) | Default (non-root) |
|----------|----------------|--------------------|
| `GOPRECORDS_HOST` | **required** | **required** |
| `GOPRECORDS_TOKEN_FILE` | `/etc/goprecords-upload.token` | `$XDG_CONFIG_HOME/goprecords-upload-<HOST>/token` |
| `GOPRECORDS_BASE_URL` | `https://goprecords.f3s.buetow.org` | same |

## Step 1 — Issue a token on the server

`HOSTNAME` must match the short stats name used in every upload URL (e.g.
`f0`, `pi2`, `earth`, `blowfish`).

```bash
# daemon running in Kubernetes
kubectl exec -n services deployment/goprecords -- \
  goprecords --create-client-key HOSTNAME -stats-dir=/data/stats

# daemon running locally
goprecords --create-client-key HOSTNAME -stats-dir=/var/lib/goprecords/stats
```

The plaintext token is printed once. Store it only on the client.
Re-running `--create-client-key` for the same hostname replaces the previous token.

## Step 2 — Install the script

**FreeBSD**

```sh
doas install -m 755 scripts/goprecords-upload-client.sh \
  /usr/local/bin/goprecords-upload-client.sh
```

**Linux (root)**

```sh
sudo install -m 755 scripts/goprecords-upload-client.sh \
  /usr/local/bin/goprecords-upload-client.sh
```

**OpenBSD**

```sh
doas install -m 755 scripts/goprecords-upload-client.sh \
  /usr/local/bin/goprecords-upload-client.sh
```

**Linux (user session, e.g. earth)**

```sh
install -m 700 scripts/goprecords-upload-client.sh \
  ~/.local/bin/goprecords-upload-client.sh
```

**macOS (user session)**

The Darwin variant is a fish script and runs as a regular user (no root needed
for Homebrew-installed `uptimed`):

```sh
install -m 700 scripts/goprecords-upload-client-darwin.fish \
  ~/.local/bin/goprecords-upload-client-darwin.fish
```

Ensure `fish` is on `PATH` (it is when installed via Homebrew). The script
requires `curl`, `uptimed`, and `uprecords` — all provided by `brew install uptimed`.
Set `GOPRECORDS_RECORDS_FILE` only when your `uptimed` records file is outside
the standard Homebrew paths.

## Step 3 — Store the token

**FreeBSD / Linux (root)**

```sh
# FreeBSD
umask 077
echo 'TOKEN' | doas tee /etc/goprecords-upload.token
doas chmod 600 /etc/goprecords-upload.token

# Linux
umask 077
echo 'TOKEN' | sudo tee /etc/goprecords-upload.token
sudo chmod 600 /etc/goprecords-upload.token
```

**OpenBSD**

```sh
umask 077
echo 'TOKEN' | doas tee /etc/goprecords-upload.token
doas chmod 600 /etc/goprecords-upload.token
```

**Linux (user session)**

```sh
mkdir -p ~/.config/goprecords-upload-earth
umask 077
echo 'TOKEN' > ~/.config/goprecords-upload-earth/token
```

**macOS (user session)**

Replace `mymac` with the `GOPRECORDS_HOST` value you chose:

```sh
mkdir -p ~/.config/goprecords-upload-mymac
chmod 700 ~/.config/goprecords-upload-mymac
echo 'TOKEN' > ~/.config/goprecords-upload-mymac/token
chmod 600 ~/.config/goprecords-upload-mymac/token
```

## Step 4 — Automate

### FreeBSD — hourly cron

Add to root's crontab (`crontab -e` as root, or `/etc/crontab`). `curl` must
be on `PATH` for cron:

```cron
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
0 * * * * root /usr/bin/env GOPRECORDS_HOST=f0 /usr/local/bin/goprecords-upload-client.sh
```

Repeat with the matching `GOPRECORDS_HOST` value on each host (`f1`, `f2`, `f3`, …).

### Linux (Rocky/Fedora) — hourly systemd timer, system-wide

`/etc/goprecords-upload.env` (mode `644`):

```ini
GOPRECORDS_HOST=pi0
```

`/etc/systemd/system/goprecords-upload.service`:

```ini
[Unit]
Description=Upload uptimed stats to goprecords

[Service]
Type=oneshot
EnvironmentFile=/etc/goprecords-upload.env
Environment=GOPRECORDS_TOKEN_FILE=/etc/goprecords-upload.token
ExecStart=/usr/local/bin/goprecords-upload-client.sh
```

`/etc/systemd/system/goprecords-upload.timer`:

```ini
[Unit]
Description=Hourly uptimed upload to goprecords

[Timer]
OnCalendar=hourly
OnActiveSec=90s
RandomizedDelaySec=300
Persistent=true

[Install]
WantedBy=timers.target
```

```sh
sudo systemctl daemon-reload
sudo systemctl enable --now goprecords-upload.timer
```

Use a different `GOPRECORDS_HOST` in `/etc/goprecords-upload.env` on each Pi
(`pi1`, `pi2`, `pi3`).

### Linux — hourly systemd timer, user session (earth)

`~/.config/systemd/user/goprecords-upload-earth.service`:

```ini
[Unit]
Description=Upload uptimed stats to goprecords

[Service]
Type=oneshot
Environment=GOPRECORDS_HOST=earth
ExecStart=%h/.local/bin/goprecords-upload-client.sh
```

`~/.config/systemd/user/goprecords-upload-earth.timer`:

```ini
[Unit]
Description=Hourly uptimed upload to goprecords (earth)

[Timer]
OnCalendar=hourly
OnActiveSec=90s
RandomizedDelaySec=300
Persistent=true

[Install]
WantedBy=timers.target
```

Enable lingering so uploads continue without an active login session:

```sh
sudo loginctl enable-linger "$USER"
systemctl --user daemon-reload
systemctl --user enable --now goprecords-upload-earth.timer
```

### OpenBSD — daily cron via /etc/daily.local

OpenBSD's `uptimed` only updates its records file once per day, so a daily run
is sufficient. Add to `/etc/daily.local`:

```sh
GOPRECORDS_HOST=blowfish /usr/local/bin/goprecords-upload-client.sh
```

Adjust `GOPRECORDS_HOST` for each OpenBSD host (`fishfinger`, etc.).

### macOS — hourly LaunchAgent

Create `~/Library/LaunchAgents/org.buetow.goprecords-upload.plist`.
Replace `mymac` with your `GOPRECORDS_HOST` value and adjust the path to
`fish` if your Homebrew prefix differs:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
  "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>org.buetow.goprecords-upload</string>
  <key>ProgramArguments</key>
  <array>
    <string>/opt/homebrew/bin/fish</string>
    <string>/Users/paul/.local/bin/goprecords-upload-client-darwin.fish</string>
  </array>
  <key>EnvironmentVariables</key>
  <dict>
    <key>GOPRECORDS_HOST</key>
    <string>mymac</string>
    <key>PATH</key>
    <string>/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin</string>
  </dict>
  <key>StartInterval</key>
  <integer>3600</integer>
  <key>StandardOutPath</key>
  <string>/tmp/goprecords-upload.log</string>
  <key>StandardErrorPath</key>
  <string>/tmp/goprecords-upload.err</string>
  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>
```

Load it with:

```sh
launchctl load ~/Library/LaunchAgents/org.buetow.goprecords-upload.plist
```

For Intel Macs, replace `/opt/homebrew/bin/fish` with `/usr/local/bin/fish`.

### macOS — fish shell supersync integration

If you use the `supersync` fish function from the dotfiles, you can call the
upload script directly from your shell session by adding a call to your
`supersync` function or running it manually:

```fish
GOPRECORDS_HOST=mymac fish ~/.local/bin/goprecords-upload-client-darwin.fish
```

## Step 5 — Test one run

**FreeBSD**

```sh
doas env GOPRECORDS_HOST=f0 /usr/local/bin/goprecords-upload-client.sh
```

**Linux (root)**

```sh
sudo env GOPRECORDS_HOST=pi0 /usr/local/bin/goprecords-upload-client.sh
```

**OpenBSD**

```sh
doas env GOPRECORDS_HOST=blowfish /usr/local/bin/goprecords-upload-client.sh
```

**Linux (user session)**

```sh
GOPRECORDS_HOST=earth ~/.local/bin/goprecords-upload-client.sh
```

**macOS (user session)**

```sh
GOPRECORDS_HOST=mymac fish ~/.local/bin/goprecords-upload-client-darwin.fish
```