Skip to content

Commit 87f27bb

Browse files
authored
Merge pull request #4 from WeOps-Lab/cursor/bklite-script-timeout-984f
feat(inputs.bklite_script): support configurable timeout capped at interval-1s
2 parents 1e878e3 + 1074213 commit 87f27bb

4 files changed

Lines changed: 53 additions & 10 deletions

File tree

‎plugins/inputs/bklite_script/README.md‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ BlueKing Lite script-monitoring input.
77
`inputs.exec` is unchanged.
88

99
BKLite child configs should only set the user-facing fields below. Sandbox
10-
limits, timeout, and root policy are **fixed in the plugin** (not toggles).
10+
limits and root policy are **fixed in the plugin** (not toggles). Optional
11+
`timeout` is capped at interval-1s.
1112

1213
## Global configuration options <!-- @/docs/includes/plugin_config.md -->
1314

@@ -26,15 +27,20 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
2627
# Telegraf pipeline; failures are reported via bklite_script health metrics.
2728
#
2829
# Platform-enforced (not configurable in this plugin):
29-
# - timeout = interval - 1s (set interval >= 60s)
30+
# - timeout is capped at interval-1s (default = cap; set interval >= 60s)
3031
# - Linux: refuse to run as root; drop to run_as when Telegraf is root
3132
# - Linux: CPU / memory / process limits (cgroup v2 and/or setrlimit)
3233
# - per-instance file lock (no overlapping runs)
3334
# - script body is never placed in process argv
3435
[[inputs.bklite_script]]
35-
## Collection interval. Must be at least 60s. Child timeout is interval-1s.
36+
## Collection interval. Must be at least 60s.
3637
interval = "60s"
3738

39+
## Optional script timeout. Unset or "0s" keeps interval-1s (same as
40+
## previous behavior). Effective timeout is min(timeout, interval-1s);
41+
## values above the cap are clamped and logged once at startup.
42+
# timeout = "30s"
43+
3844
## Optional name used as the "script" tag on health and parsed metrics.
3945
# script_name = ""
4046

@@ -75,21 +81,24 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
7581
| Field | Notes |
7682
|-------|--------|
7783
| `interval` | Required, **≥ 60s**. Telegraf collection interval for this input. |
84+
| `timeout` | Optional. Unset or `0` uses `interval - 1s`. Effective value is `min(timeout, interval - 1s)`. |
7885
| `script` / `script_env` | Script body. Written to a private file; **not** in argv. |
7986
| `interpreter` / `params` | How the file is executed. |
8087
| `environment` | Secrets (`KEY=value`) for the child. |
8188
| `run_as` | Linux non-root user. Windows: not supported (service account). |
8289

8390
### Platform-enforced behavior (fixed)
8491

85-
- **Timeout** = `interval - 1s`. There is no user `timeout` knob.
92+
- **Timeout cap** = `interval - 1s`. Configured `timeout` cannot exceed this.
93+
Unset or `0` keeps that default (same as previous behavior). Exceeding the
94+
cap is clamped and logged once at Init.
8695
- **Linux root**: Init fails if the child would run as root: empty `run_as`
8796
while Telegraf is root, `run_as = "root"`, or any account whose uid is 0.
8897
There is no `allow_root` flag. When Telegraf is root, set `run_as` to a
8998
non-root account; the child is launched with `setuid`/`setgid`.
9099
- **Linux resource limits** (always applied, no disable flag):
91100
- memory: 256MiB (`cgroup v2 memory.max` when writable, plus `RLIMIT_AS`)
92-
- CPU time: `RLIMIT_CPU` equal to the derived timeout (seconds)
101+
- CPU time: `RLIMIT_CPU` equal to the effective timeout (seconds)
93102
- processes: 64 via `cgroup v2 pids.max` when writable; if cgroup setup
94103
fails, `RLIMIT_NPROC=256` (per-UID ceiling) is applied so a fork bomb
95104
cannot unbounded-spawn. Windows has no CPU/memory/nproc quotas.

‎plugins/inputs/bklite_script/bklite_script.go‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var sampleConfig string
2626
// BkliteScript runs a user script and collects Prometheus metrics from stdout.
2727
type BkliteScript struct {
2828
Interval config.Duration `toml:"interval"`
29+
Timeout config.Duration `toml:"timeout"`
2930
ScriptName string `toml:"script_name"`
3031
Interpreter string `toml:"interpreter"`
3132
Params []string `toml:"params"`
@@ -36,8 +37,9 @@ type BkliteScript struct {
3637
RunAs string `toml:"run_as"`
3738
Log telegraf.Logger `toml:"-"`
3839

39-
// runTimeout is interval-1s after Init. Tests may set it before Init to
40-
// avoid waiting a full minute.
40+
// runTimeout is the effective child timeout after Init:
41+
// min(configured timeout, interval-1s). Unset or zero timeout keeps
42+
// interval-1s. Tests may set it before Init to avoid waiting a full minute.
4143
runTimeout time.Duration
4244
lockPath string
4345
hasCredential bool
@@ -59,7 +61,7 @@ func (b *BkliteScript) Init() error {
5961
if err := validateInterval(time.Duration(b.Interval)); err != nil {
6062
return err
6163
}
62-
b.runTimeout = derivedTimeout(time.Duration(b.Interval))
64+
b.applyTimeout(time.Duration(b.Interval))
6365
}
6466

6567
if !b.usesScriptBody() {
@@ -247,13 +249,29 @@ func (b *BkliteScript) emitHealth(acc telegraf.Accumulator, h healthResult) {
247249
}, map[string]string{"script": b.scriptTag()})
248250
}
249251

252+
func (b *BkliteScript) applyTimeout(interval time.Duration) {
253+
configured := time.Duration(b.Timeout)
254+
limit := derivedTimeout(interval)
255+
b.runTimeout = effectiveTimeout(configured, interval)
256+
if configured > limit {
257+
b.logWarnf("timeout %s exceeds cap %s (interval-1s); using %s", configured, limit, limit)
258+
}
259+
}
260+
250261
func (b *BkliteScript) logErrorf(format string, args ...interface{}) {
251262
if b.Log == nil {
252263
return
253264
}
254265
b.Log.Errorf(format, args...)
255266
}
256267

268+
func (b *BkliteScript) logWarnf(format string, args ...interface{}) {
269+
if b.Log == nil {
270+
return
271+
}
272+
b.Log.Warnf(format, args...)
273+
}
274+
257275
type healthResult struct {
258276
up int
259277
exitCode int

‎plugins/inputs/bklite_script/root.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ func derivedTimeout(interval time.Duration) time.Duration {
3434
return interval - time.Second
3535
}
3636

37+
// effectiveTimeout is min(configured, interval-1s). Unset or zero configured
38+
// keeps the historical interval-1s default. Interval <= 1s is guarded the
39+
// same way as derivedTimeout.
40+
func effectiveTimeout(configured, interval time.Duration) time.Duration {
41+
limit := derivedTimeout(interval)
42+
if configured <= 0 || configured > limit {
43+
return limit
44+
}
45+
return configured
46+
}
47+
3748
func validateInterval(interval time.Duration) error {
3849
if interval < minInterval {
3950
return fmt.Errorf("interval must be at least %s (got %s)", minInterval, interval)

‎plugins/inputs/bklite_script/sample.conf‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@
33
# Telegraf pipeline; failures are reported via bklite_script health metrics.
44
#
55
# Platform-enforced (not configurable in this plugin):
6-
# - timeout = interval - 1s (set interval >= 60s)
6+
# - timeout is capped at interval-1s (default = cap; set interval >= 60s)
77
# - Linux: refuse to run as root; drop to run_as when Telegraf is root
88
# - Linux: CPU / memory / process limits (cgroup v2 and/or setrlimit)
99
# - per-instance file lock (no overlapping runs)
1010
# - script body is never placed in process argv
1111
[[inputs.bklite_script]]
12-
## Collection interval. Must be at least 60s. Child timeout is interval-1s.
12+
## Collection interval. Must be at least 60s.
1313
interval = "60s"
1414

15+
## Optional script timeout. Unset or "0s" keeps interval-1s (same as
16+
## previous behavior). Effective timeout is min(timeout, interval-1s);
17+
## values above the cap are clamped and logged once at startup.
18+
# timeout = "30s"
19+
1520
## Optional name used as the "script" tag on health and parsed metrics.
1621
# script_name = ""
1722

0 commit comments

Comments
 (0)