MetricsHub
MetricsHub Community Connectors 1.0.22
-
Home
- Connector Developer Guide Computes 24
awk (Compute)
When To Use
Use the awk compute for parsing jobs that simpler computes cannot express: reshaping free-form command output into semicolon-separated rows, correlating multi-line records, or computing derived values. The script receives the source's current result as its input — the raw text when available (e.g. commandLine output), otherwise the table serialized as semicolon-separated lines — and its printed output becomes the new table.
MetricsHub embeds JAWK, a Java implementation of the AWK language, extended with a few MetricsHub utility functions. Standard AWK constructs work; do not assume GNU awk (gawk) extensions are available.
This compute transforms an existing source's result. If the script itself is the source (computing over an explicit input), use the awk source[1] instead.
Syntax
sources:
volumes:
type: commandLine
commandLine: cmd /c ${file::diskpart.bat}
computes:
# The script prints: MSHW;ID;Label;Letter;VolumeType;FileSystem;Size;Status
- type: awk
script: ${file::diskPart.awk}
keep: ^MSHW;
separators: ;
selectColumns: "2,3,4,5,6,7,8"
script may also be written inline as a YAML block scalar (see LinuxService for a real example).
Properties
| Property | Required | Default | Description |
|---|---|---|---|
type |
Yes | None | awk. |
script |
Yes | None | The AWK script: an embedded-file reference ${file::script.awk} (a file next to the connector), or the script text itself, typically as a YAML block scalar (script: \|). |
keep |
No | None | Regular expression; only output lines of the script that match are kept. |
exclude |
No | None | Regular expression; matching output lines are discarded. |
separators |
No | None | Character(s) splitting each remaining output line into columns. Required for selectColumns to have any effect. |
selectColumns |
No | None | Comma-separated 1-based column numbers to keep after splitting with separators, e.g. "2,3,4". |
Table Transformation Example
Suppose diskPart.awk prints one MSHW;-prefixed line per volume; keep, separators, and selectColumns then clean up its output:
Input (raw command output)
| Raw line |
|---|
| Volume 0 C System NTFS Partition 237 GB Healthy |
| Volume 1 D Data NTFS Partition 931 GB Healthy |
Result
| ID | Label | Letter | VolumeType | FileSystem | Size | Status |
|---|---|---|---|---|---|---|
| 0 | System | C | Partition | NTFS | 254568116160 | Healthy |
| 1 | Data | D | Partition | NTFS | 999784378368 | Healthy |
Between the two, the script's serialized output looked like:
MSHW;0;System;C;Partition;NTFS;254568116160;Healthy
MSHW;1;Data;D;Partition;NTFS;999784378368;Healthy
The keep: ^MSHW; filter drops any stray script output, and selectColumns removes the MSHW marker column.
Recommended Pattern
- Prefix intentional output lines with a marker (
MSHW;by convention) and filter withkeep: ^MSHW;— this is the standard community pattern (DiskPart, lmsensors, LinuxNetwork). - Emit semicolon-separated values with a constant column count so mapping stays predictable.
- Keep long scripts in dedicated
.awkembedded files referenced with${file::...}; reserve inline block scalars for short scripts. - Put the
awkcompute first in the pipeline, then refine with cheaper computes (translate,keepOnlyMatchingLines…).
Common Mistakes
- Relying on GNU awk extensions (
gensub,asort, coprocesses…) that JAWK does not provide. - Setting
selectColumnswithoutseparators: the lines are never split, so the selection does nothing. - Expecting
keep/excludeto filter the script's input — they filter its output. Filter input inside the script itself. - A script that prints nothing empties the table, and every downstream compute and mapping silently gets no rows.
Community Examples
- DiskPart[2]
- lmsensors[3]
- LinuxService[4] (inline script)
From DiskPart, included directly from the connector source:
# Run Source(1) through an AWK script
# ID;Label;Letter;VolumeType;FileSystem;Size;Status;MoreInformation
- type: awk
script: ${file::diskPart.awk}
separators: ;
keep: ^MSHW;
selectColumns: 2,3,4,5,6,7,8,9
- [1] ../sources/awk.html
- [2] https://github.com/metricshub/community-connectors/blob/main/src/main/connector/hardware/DiskPart/DiskPart.yaml
- [3] https://github.com/metricshub/community-connectors/blob/main/src/main/connector/hardware/lmsensors/lmsensors.yaml
- [4] https://github.com/metricshub/community-connectors/blob/main/src/main/connector/system/LinuxService/LinuxService.yaml
