diff --git a/README.md b/README.md index 502f2b0..263813a 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ Configuration is via the following environment variables: | `TARGET_LIST` | yes | Comma separated list of host names or IP addresses to run tests against. | n/a | | `TEST_INTERVAL_MS` | no | How often to run iperf tests. | 600000ms (= 10 minutes) | | `OPTIONS` | no | Additional [iperf options](https://github.com/esnet/iperf/blob/master/docs/invoking.rst) (e.g. `--bitrate 1k`) | none | +| `PROTOCOL` | no | One of `tcp` or `udp` | `tcp` | ### `iperf` Server diff --git a/src/config.ts b/src/config.ts index 859ffae..09ac653 100644 --- a/src/config.ts +++ b/src/config.ts @@ -4,6 +4,7 @@ enum ConfigKey { TargetList = "TARGET_LIST", Options = "OPTIONS", TestIntervalMs = "TEST_INTERVAL_MS", + Protocol = "PROTOCOL", } const loadedConfig: Partial<{ [key in ConfigKey]: string }> = {}; diff --git a/src/server.ts b/src/server.ts index 14f161c..af0dc20 100644 --- a/src/server.ts +++ b/src/server.ts @@ -9,6 +9,7 @@ const exec = util.promisify(execRaw); // get config const targetList = getConfig(ConfigKey.TargetList); const options = getConfig(ConfigKey.Options); +const protocol = getConfig(ConfigKey.Protocol, "tcp"); const targets = targetList.split(",").map((t) => t.trim()); const testIntervalMs = parseInt(getConfig(ConfigKey.TestIntervalMs, "600000")); @@ -22,22 +23,43 @@ async function getMeasurements(): Promise { }; try { - const iperfCmd = await exec(`iperf3 -c ${target} --json ${options}`); + let udpOptionStr = ""; + if (protocol == "udp") { + udpOptionStr = "--udp"; + } + + const iperfCmd = await exec(`iperf3 -c ${target} ${udpOptionStr} --json ${options}`); const result = JSON.parse(iperfCmd.stdout); if (result["error"]) { throw result["error"]; } - - measurements.push(formatMeasurement("iperf_sent_bytes", tags, parseFloat(result["end"]["sum_sent"]["bytes"]))); - measurements.push( - formatMeasurement("iperf_sent_seconds", tags, parseFloat(result["end"]["sum_sent"]["seconds"])), - ); - measurements.push( - formatMeasurement("iperf_received_bytes", tags, parseFloat(result["end"]["sum_received"]["bytes"])), - ); - measurements.push( - formatMeasurement("iperf_received_seconds", tags, parseFloat(result["end"]["sum_received"]["seconds"])), - ); + switch(protocol) { + case "tcp": + measurements.push(formatMeasurement("iperf_sent_bytes", tags, parseFloat(result["end"]["sum_sent"]["bytes"]))); + measurements.push( + formatMeasurement("iperf_sent_seconds", tags, parseFloat(result["end"]["sum_sent"]["seconds"])), + ); + measurements.push( + formatMeasurement("iperf_received_bytes", tags, parseFloat(result["end"]["sum_received"]["bytes"])), + ); + measurements.push( + formatMeasurement("iperf_received_seconds", tags, parseFloat(result["end"]["sum_received"]["seconds"])), + ); + break; + case "udp": + measurements.push( + formatMeasurement("iperf_lost_packets", tags, parseFloat(result["end"]["sum"]["lost_packets"])), + ); + measurements.push( + formatMeasurement("iperf_received_bytes", tags, parseFloat(result["end"]["sum"]["bytes"])), + ); + measurements.push( + formatMeasurement("iperf_received_seconds", tags, parseFloat(result["end"]["sum"]["seconds"])), + ); + break; + default: + throw `unsupported protocol ${protocol}`; + } } catch (e) { log(`Could not get iperf metrics for ${target}`, e); continue;