04 - Parsing with whitespace
The df command is a very common command used to check disk usage. It provides a table like structure of data that doesn't automatically parse.
However, we can use the inbuilt parsing capabilities to handle these in more then one way, either via detecting whitespace, or with regex.
Whitespace parsing may not work for all cases where the output is as straight forward, so utilize regex parsing for more complex scenarios.
Command output
Example command output from df -k
ubuntu@host:~$ df -k
Filesystem 1K-blocks Used Available Use% Mounted on
tmpfs 98440 1560 96880 2% /run
/dev/sda1 47145992 5042744 42086864 11% /
tmpfs 492196 0 492196 0% /dev/shm
tmpfs 5120 0 5120 0% /run/lock
/dev/sda15 99791 6418 93374 7% /boot/efi
tmpfs 98436 4 98432 1% /run/user/1001
Review configuration
We have set the run command to df-k.
The parse options have explictily defined the header keys with split_whitespace enabled.
- Place the configuration into your
configsdirectory - Stop/Start the binary
./airpipe --api-key enter-your-api-key --config-dir configs/
tutorial-df-whitespace-to-api.yml
name: tutorial-df-whitespace-to-api
interfaces:
tutorial/parse-df-whitespace:
output: http
actions:
- name: DiskDf
command:
run: df -k
parse:
keys: [fs, 1K-blocks, used, available, usePerc, mountedOn]
split_whitespace: true
HTTP Response
{
"data": {
"DiskDf": {
"data": [
{
"1K-blocks": 4106912,
"available": 4105284,
"fs": "tmpfs",
"mountedOn": "/run",
"usePerc": "1%",
"used": 1628
},
{
"1K-blocks": 203770680,
"available": 5789456,
"fs": "/dev/mapper/ubuntu--vg-ubuntu--lv",
"mountedOn": "/",
"usePerc": "98%",
"used": 188632988
},
{
"1K-blocks": 20534548,
"available": 20534520,
"fs": "tmpfs",
"mountedOn": "/dev/shm",
"usePerc": "1%",
"used": 28
},
{
"1K-blocks": 5120,
"available": 5116,
"fs": "tmpfs",
"mountedOn": "/run/lock",
"usePerc": "1%",
"used": 4
},
{
"1K-blocks": 1992552,
"available": 1515172,
"fs": "/dev/sda2",
"mountedOn": "/boot",
"usePerc": "20%",
"used": 356140
},
{
"1K-blocks": 4106908,
"available": 4106832,
"fs": "tmpfs",
"mountedOn": "/run/user/1000",
"usePerc": "1%",
"used": 76
}
],
"time.ms": 20
}
}
}