Skip to content

Commit

Permalink
remove extra fields from df parser. All size values are now normalize…
Browse files Browse the repository at this point in the history
…d to bytes. Size field is now an integer.
  • Loading branch information
kellyjonbrazil committed Feb 26, 2024
1 parent bce4eb5 commit 850917a
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 51 deletions.
8 changes: 5 additions & 3 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
jc changelog

20240226 v1.25.2
- Enhance `free` parser to add human readable output support
- Enhance `df` parser to include `_bytes` fields for `size`, `used`, and `available`.
This provides normalized output when human readable `df` output is used
- Enhance `df` parser to add human readable output support. Note: the `size`
field has been changed to an integer and all size values have been normalized
to bytes.
- Enhance `free` parser to add human readable output support. All size values
have been normalizd to bytes.
- Fix `iwconfig` parser to handle more special characters in the SSID name
- Documentation updates

Expand Down
18 changes: 4 additions & 14 deletions docs/parsers/df.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

jc - JSON Convert `df` command output parser

Values are normalized to bytes when using `df -h`.

Usage (cli):

$ df | jc --df
Expand All @@ -23,14 +25,11 @@ Schema:
[
{
"filesystem": string,
"size": string,
"size_bytes": integer, # [0]
"size": integer,
"1k_blocks": integer,
"512_blocks": integer,
"used": integer,
"used_bytes": integer, # [0]
"available": integer,
"available_bytes": integer, # [0]
"capacity_percent": integer,
"ifree": integer,
"iused": integer,
Expand All @@ -40,9 +39,6 @@ Schema:
}
]

[0] It is recommended to use these fields as they are normalized to bytes
and will work even with human-readable `df` output.

Examples:

$ df | jc --df -p
Expand All @@ -51,29 +47,23 @@ Examples:
"filesystem": "devtmpfs",
"1k_blocks": 1918820,
"used": 0,
"used_bytes": 0,
"available": 1918820,
"available_bytes": 1918820,
"use_percent": 0,
"mounted_on": "/dev"
},
{
"filesystem": "tmpfs",
"1k_blocks": 1930668,
"used": 0,
"used_bytes": 0,
"available": 1930668,
"available_bytes": 1930668,
"use_percent": 0,
"mounted_on": "/dev/shm"
},
{
"filesystem": "tmpfs",
"1k_blocks": 1930668,
"used": 11800,
"used_bytes": 11800,
"available": 1918868,
"available_bytes": 1918868,
"use_percent": 1,
"mounted_on": "/run"
},
Expand Down Expand Up @@ -134,4 +124,4 @@ Compatibility: linux, darwin, freebsd

Source: [`jc/parsers/df.py`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/df.py)

Version 1.12 by Kelly Brazil ([email protected])
Version 2.0 by Kelly Brazil ([email protected])
35 changes: 10 additions & 25 deletions jc/parsers/df.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""jc - JSON Convert `df` command output parser
Values are normalized to bytes when using `df -h`.
Usage (cli):
$ df | jc --df
Expand All @@ -18,14 +20,11 @@
[
{
"filesystem": string,
"size": string,
"size_bytes": integer, # [0]
"size": integer,
"1k_blocks": integer,
"512_blocks": integer,
"used": integer,
"used_bytes": integer, # [0]
"available": integer,
"available_bytes": integer, # [0]
"capacity_percent": integer,
"ifree": integer,
"iused": integer,
Expand All @@ -35,9 +34,6 @@
}
]
[0] It is recommended to use these fields as they are normalized to bytes
and will work even with human-readable `df` output.
Examples:
$ df | jc --df -p
Expand All @@ -46,29 +42,23 @@
"filesystem": "devtmpfs",
"1k_blocks": 1918820,
"used": 0,
"used_bytes": 0,
"available": 1918820,
"available_bytes": 1918820,
"use_percent": 0,
"mounted_on": "/dev"
},
{
"filesystem": "tmpfs",
"1k_blocks": 1930668,
"used": 0,
"used_bytes": 0,
"available": 1930668,
"available_bytes": 1930668,
"use_percent": 0,
"mounted_on": "/dev/shm"
},
{
"filesystem": "tmpfs",
"1k_blocks": 1930668,
"used": 11800,
"used_bytes": 11800,
"available": 1918868,
"available_bytes": 1918868,
"use_percent": 1,
"mounted_on": "/run"
},
Expand Down Expand Up @@ -111,7 +101,7 @@

class info():
"""Provides parser metadata (version, author, etc.)"""
version = '1.12'
version = '2.0'
description = '`df` command parser'
author = 'Kelly Brazil'
author_email = '[email protected]'
Expand All @@ -135,8 +125,8 @@ def _process(proc_data):
List of Dictionaries. Structured data to conform to the schema:
"""
int_list = {'used', 'available', 'use_percent', 'capacity_percent', 'ifree',
'iused', 'iused_percent'}
int_list = {'use_percent', 'capacity_percent', 'ifree', 'iused', 'iused_percent'}
size_list = {'size', 'used', 'available'}

for entry in proc_data:
if 'avail' in entry:
Expand Down Expand Up @@ -166,15 +156,10 @@ def _process(proc_data):
if 'iused_percent' in entry:
entry['iused_percent'] = entry['iused_percent'].rstrip('%')

# parse the size, used, and available fields and create a 'x_bytes' fields
if 'size' in entry:
entry['size_bytes'] = jc.utils.convert_size_to_int(entry['size'])

if 'used' in entry:
entry['used_bytes'] = jc.utils.convert_size_to_int(entry['used'])

if 'available' in entry:
entry['available_bytes'] = jc.utils.convert_size_to_int(entry['available'])
# parse the size, used, and available fields to bytes
for key in entry:
if key in size_list:
entry[key] = jc.utils.convert_size_to_int(entry[key])

# convert integers
for key in entry:
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/centos-7.7/df-h.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"filesystem":"devtmpfs","size":"1.9G","used":0,"mounted_on":"/dev","available":1,"use_percent":0,"size_bytes":1900000000,"used_bytes":0,"available_bytes":1900000000},{"filesystem":"tmpfs","size":"1.9G","used":0,"mounted_on":"/dev/shm","available":1,"use_percent":0,"size_bytes":1900000000,"used_bytes":0,"available_bytes":1900000000},{"filesystem":"tmpfs","size":"1.9G","used":12,"mounted_on":"/run","available":1,"use_percent":1,"size_bytes":1900000000,"used_bytes":12000000,"available_bytes":1900000000},{"filesystem":"tmpfs","size":"1.9G","used":0,"mounted_on":"/sys/fs/cgroup","available":1,"use_percent":0,"size_bytes":1900000000,"used_bytes":0,"available_bytes":1900000000},{"filesystem":"/dev/mapper/centos-root","size":"17G","used":1,"mounted_on":"/","available":16,"use_percent":11,"size_bytes":17000000000,"used_bytes":1800000000,"available_bytes":16000000000},{"filesystem":"/dev/sda1","size":"1014M","used":233,"mounted_on":"/boot","available":782,"use_percent":23,"size_bytes":1014000000,"used_bytes":233000000,"available_bytes":782000000},{"filesystem":"tmpfs","size":"378M","used":0,"mounted_on":"/run/user/1000","available":378,"use_percent":0,"size_bytes":378000000,"used_bytes":0,"available_bytes":378000000}]
[{"filesystem":"devtmpfs","size":1900000000,"used":0,"mounted_on":"/dev","available":1900000000,"use_percent":0},{"filesystem":"tmpfs","size":1900000000,"used":0,"mounted_on":"/dev/shm","available":1900000000,"use_percent":0},{"filesystem":"tmpfs","size":1900000000,"used":12000000,"mounted_on":"/run","available":1900000000,"use_percent":1},{"filesystem":"tmpfs","size":1900000000,"used":0,"mounted_on":"/sys/fs/cgroup","available":1900000000,"use_percent":0},{"filesystem":"/dev/mapper/centos-root","size":17000000000,"used":1800000000,"mounted_on":"/","available":16000000000,"use_percent":11},{"filesystem":"/dev/sda1","size":1014000000,"used":233000000,"mounted_on":"/boot","available":782000000,"use_percent":23},{"filesystem":"tmpfs","size":378000000,"used":0,"mounted_on":"/run/user/1000","available":378000000,"use_percent":0}]
2 changes: 1 addition & 1 deletion tests/fixtures/centos-7.7/df.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"filesystem":"devtmpfs","1k_blocks":1918816,"used":0,"available":1918816,"mounted_on":"/dev","use_percent":0,"used_bytes":0,"available_bytes":1918816},{"filesystem":"tmpfs","1k_blocks":1930664,"used":0,"available":1930664,"mounted_on":"/dev/shm","use_percent":0,"used_bytes":0,"available_bytes":1930664},{"filesystem":"tmpfs","1k_blocks":1930664,"used":11832,"available":1918832,"mounted_on":"/run","use_percent":1,"used_bytes":11832,"available_bytes":1918832},{"filesystem":"tmpfs","1k_blocks":1930664,"used":0,"available":1930664,"mounted_on":"/sys/fs/cgroup","use_percent":0,"used_bytes":0,"available_bytes":1930664},{"filesystem":"/dev/mapper/centos-root","1k_blocks":17811456,"used":1805580,"available":16005876,"mounted_on":"/","use_percent":11,"used_bytes":1805580,"available_bytes":16005876},{"filesystem":"/dev/sda1","1k_blocks":1038336,"used":237600,"available":800736,"mounted_on":"/boot","use_percent":23,"used_bytes":237600,"available_bytes":800736},{"filesystem":"tmpfs","1k_blocks":386136,"used":0,"available":386136,"mounted_on":"/run/user/1000","use_percent":0,"used_bytes":0,"available_bytes":386136}]
[{"filesystem":"devtmpfs","1k_blocks":1918816,"used":0,"available":1918816,"mounted_on":"/dev","use_percent":0},{"filesystem":"tmpfs","1k_blocks":1930664,"used":0,"available":1930664,"mounted_on":"/dev/shm","use_percent":0},{"filesystem":"tmpfs","1k_blocks":1930664,"used":11832,"available":1918832,"mounted_on":"/run","use_percent":1},{"filesystem":"tmpfs","1k_blocks":1930664,"used":0,"available":1930664,"mounted_on":"/sys/fs/cgroup","use_percent":0},{"filesystem":"/dev/mapper/centos-root","1k_blocks":17811456,"used":1805580,"available":16005876,"mounted_on":"/","use_percent":11},{"filesystem":"/dev/sda1","1k_blocks":1038336,"used":237600,"available":800736,"mounted_on":"/boot","use_percent":23},{"filesystem":"tmpfs","1k_blocks":386136,"used":0,"available":386136,"mounted_on":"/run/user/1000","use_percent":0}]
2 changes: 1 addition & 1 deletion tests/fixtures/generic/df-long-filesystem.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"filesystem":"/dev/mapper/VolGroup00-LogVol00","type":"ext3","1024_blocks":6030784,"used":1147932,"available":4571556,"mounted_on":"/","capacity_percent":21,"used_bytes":1147932,"available_bytes":4571556},{"filesystem":"proc","type":"proc","1024_blocks":0,"used":0,"available":0,"mounted_on":"/proc","capacity_percent":null,"used_bytes":0,"available_bytes":0},{"filesystem":"sysfs","type":"sysfs","1024_blocks":0,"used":0,"available":0,"mounted_on":"/sys","capacity_percent":null,"used_bytes":0,"available_bytes":0}]
[{"filesystem":"/dev/mapper/VolGroup00-LogVol00","type":"ext3","1024_blocks":6030784,"used":1147932,"available":4571556,"mounted_on":"/","capacity_percent":21},{"filesystem":"proc","type":"proc","1024_blocks":0,"used":0,"available":0,"mounted_on":"/proc","capacity_percent":null},{"filesystem":"sysfs","type":"sysfs","1024_blocks":0,"used":0,"available":0,"mounted_on":"/sys","capacity_percent":null}]
2 changes: 1 addition & 1 deletion tests/fixtures/osx-10.11.6/df-h.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"filesystem":"/dev/disk1s1","size":"466Gi","used":137,"iused":674413,"ifree":9223372036854101394,"mounted_on":"/","available":326,"capacity_percent":30,"iused_percent":0,"size_bytes":466000000000,"used_bytes":137000000000,"available_bytes":326000000000},{"filesystem":"devfs","size":"188Ki","used":188,"iused":650,"ifree":0,"mounted_on":"/dev","available":0,"capacity_percent":100,"iused_percent":100,"size_bytes":188000,"used_bytes":188000,"available_bytes":0},{"filesystem":"/dev/disk1s4","size":"466Gi","used":2,"iused":2,"ifree":9223372036854775805,"mounted_on":"/private/var/vm","available":326,"capacity_percent":1,"iused_percent":0,"size_bytes":466000000000,"used_bytes":2000000000,"available_bytes":326000000000},{"filesystem":"map -hosts","size":"0Bi","used":0,"iused":0,"ifree":0,"mounted_on":"/net","available":0,"capacity_percent":100,"iused_percent":100,"size_bytes":0,"used_bytes":0,"available_bytes":0},{"filesystem":"map auto_home","size":"0Bi","used":0,"iused":0,"ifree":0,"mounted_on":"/home","available":0,"capacity_percent":100,"iused_percent":100,"size_bytes":0,"used_bytes":0,"available_bytes":0},{"filesystem":"//brazil@MyCloudEX2Ultra._afpovertcp._tcp.local/brazil","size":"3.5Ti","used":1,"iused":301134832,"ifree":649465741,"mounted_on":"/Volumes/brazil","available":2,"capacity_percent":32,"iused_percent":32,"size_bytes":3500000000000,"used_bytes":1100000000000,"available_bytes":2400000000000}]
[{"filesystem":"/dev/disk1s1","size":466000000000,"used":137000000000,"iused":674413,"ifree":9223372036854101394,"mounted_on":"/","available":326000000000,"capacity_percent":30,"iused_percent":0},{"filesystem":"devfs","size":188000,"used":188000,"iused":650,"ifree":0,"mounted_on":"/dev","available":0,"capacity_percent":100,"iused_percent":100},{"filesystem":"/dev/disk1s4","size":466000000000,"used":2000000000,"iused":2,"ifree":9223372036854775805,"mounted_on":"/private/var/vm","available":326000000000,"capacity_percent":1,"iused_percent":0},{"filesystem":"map -hosts","size":0,"used":0,"iused":0,"ifree":0,"mounted_on":"/net","available":0,"capacity_percent":100,"iused_percent":100},{"filesystem":"map auto_home","size":0,"used":0,"iused":0,"ifree":0,"mounted_on":"/home","available":0,"capacity_percent":100,"iused_percent":100},{"filesystem":"//brazil@MyCloudEX2Ultra._afpovertcp._tcp.local/brazil","size":3500000000000,"used":1100000000000,"iused":301134832,"ifree":649465741,"mounted_on":"/Volumes/brazil","available":2400000000000,"capacity_percent":32,"iused_percent":32}]
Empty file modified tests/fixtures/osx-10.11.6/df-h.out
100755 → 100644
Empty file.
2 changes: 1 addition & 1 deletion tests/fixtures/osx-10.11.6/df.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"filesystem":"/dev/disk1s1","512_blocks":976490576,"used":286747768,"available":684150392,"iused":674410,"ifree":9223372036854101397,"mounted_on":"/","capacity_percent":30,"iused_percent":0,"used_bytes":286747768,"available_bytes":684150392},{"filesystem":"devfs","512_blocks":375,"used":375,"available":0,"iused":650,"ifree":0,"mounted_on":"/dev","capacity_percent":100,"iused_percent":100,"used_bytes":375,"available_bytes":0},{"filesystem":"/dev/disk1s4","512_blocks":976490576,"used":4194344,"available":684150392,"iused":2,"ifree":9223372036854775805,"mounted_on":"/private/var/vm","capacity_percent":1,"iused_percent":0,"used_bytes":4194344,"available_bytes":684150392},{"filesystem":"map -hosts","512_blocks":0,"used":0,"available":0,"iused":0,"ifree":0,"mounted_on":"/net","capacity_percent":100,"iused_percent":100,"used_bytes":0,"available_bytes":0},{"filesystem":"map auto_home","512_blocks":0,"used":0,"available":0,"iused":0,"ifree":0,"mounted_on":"/home","capacity_percent":100,"iused_percent":100,"used_bytes":0,"available_bytes":0},{"filesystem":"//brazil@MyCloudEX2Ultra._afpovertcp._tcp.local/brazil","512_blocks":7604804600,"used":2409078672,"available":5195725928,"iused":301134832,"ifree":649465741,"mounted_on":"/Volumes/brazil","capacity_percent":32,"iused_percent":32,"used_bytes":2409078672,"available_bytes":5195725928}]
[{"filesystem":"/dev/disk1s1","512_blocks":976490576,"used":286747768,"available":684150392,"iused":674410,"ifree":9223372036854101397,"mounted_on":"/","capacity_percent":30,"iused_percent":0},{"filesystem":"devfs","512_blocks":375,"used":375,"available":0,"iused":650,"ifree":0,"mounted_on":"/dev","capacity_percent":100,"iused_percent":100},{"filesystem":"/dev/disk1s4","512_blocks":976490576,"used":4194344,"available":684150392,"iused":2,"ifree":9223372036854775805,"mounted_on":"/private/var/vm","capacity_percent":1,"iused_percent":0},{"filesystem":"map -hosts","512_blocks":0,"used":0,"available":0,"iused":0,"ifree":0,"mounted_on":"/net","capacity_percent":100,"iused_percent":100},{"filesystem":"map auto_home","512_blocks":0,"used":0,"available":0,"iused":0,"ifree":0,"mounted_on":"/home","capacity_percent":100,"iused_percent":100},{"filesystem":"//brazil@MyCloudEX2Ultra._afpovertcp._tcp.local/brazil","512_blocks":7604804600,"used":2409078672,"available":5195725928,"iused":301134832,"ifree":649465741,"mounted_on":"/Volumes/brazil","capacity_percent":32,"iused_percent":32}]
Empty file modified tests/fixtures/osx-10.11.6/df.out
100755 → 100644
Empty file.
2 changes: 1 addition & 1 deletion tests/fixtures/osx-10.14.6/df-h.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"filesystem":"/dev/disk1s1","size":"466Gi","used":144,"iused":1507697,"ifree":9223372036853268110,"mounted_on":"/","available":315,"capacity_percent":32,"iused_percent":0,"size_bytes":466000000000,"used_bytes":144000000000,"available_bytes":315000000000},{"filesystem":"devfs","size":"334Ki","used":334,"iused":1154,"ifree":0,"mounted_on":"/dev","available":0,"capacity_percent":100,"iused_percent":100,"size_bytes":334000,"used_bytes":334000,"available_bytes":0},{"filesystem":"/dev/disk1s4","size":"466Gi","used":6,"iused":6,"ifree":9223372036854775801,"mounted_on":"/private/var/vm","available":315,"capacity_percent":2,"iused_percent":0,"size_bytes":466000000000,"used_bytes":6000000000,"available_bytes":315000000000},{"filesystem":"map -hosts","size":"0Bi","used":0,"iused":0,"ifree":0,"mounted_on":"/net","available":0,"capacity_percent":100,"iused_percent":100,"size_bytes":0,"used_bytes":0,"available_bytes":0},{"filesystem":"map auto_home","size":"0Bi","used":0,"iused":0,"ifree":0,"mounted_on":"/home","available":0,"capacity_percent":100,"iused_percent":100,"size_bytes":0,"used_bytes":0,"available_bytes":0},{"filesystem":"/dev/disk2s2","size":"6.9Gi","used":5,"iused":8,"ifree":4294967271,"mounted_on":"/Volumes/InstallESD","available":1,"capacity_percent":76,"iused_percent":0,"size_bytes":6900000000,"used_bytes":5200000000,"available_bytes":1700000000},{"filesystem":"com.apple.TimeMachine.2019-11-29-075900@/dev/disk1s1","size":"466Gi","used":132,"iused":1459164,"ifree":9223372036853316643,"mounted_on":"/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/kbrazil-mac/2019-11-29-075900/Macintosh HD","available":315,"capacity_percent":30,"iused_percent":0,"size_bytes":466000000000,"used_bytes":132000000000,"available_bytes":315000000000},{"filesystem":"//brazil@MyCloudEX2Ultra._afpovertcp._tcp.local/brazil","size":"3.5Ti","used":1,"iused":301134832,"ifree":649465741,"mounted_on":"/Volumes/brazil","available":2,"capacity_percent":32,"iused_percent":32,"size_bytes":3500000000000,"used_bytes":1100000000000,"available_bytes":2400000000000}]
[{"filesystem":"/dev/disk1s1","size":466000000000,"used":144000000000,"iused":1507697,"ifree":9223372036853268110,"mounted_on":"/","available":315000000000,"capacity_percent":32,"iused_percent":0},{"filesystem":"devfs","size":334000,"used":334000,"iused":1154,"ifree":0,"mounted_on":"/dev","available":0,"capacity_percent":100,"iused_percent":100},{"filesystem":"/dev/disk1s4","size":466000000000,"used":6000000000,"iused":6,"ifree":9223372036854775801,"mounted_on":"/private/var/vm","available":315000000000,"capacity_percent":2,"iused_percent":0},{"filesystem":"map -hosts","size":0,"used":0,"iused":0,"ifree":0,"mounted_on":"/net","available":0,"capacity_percent":100,"iused_percent":100},{"filesystem":"map auto_home","size":0,"used":0,"iused":0,"ifree":0,"mounted_on":"/home","available":0,"capacity_percent":100,"iused_percent":100},{"filesystem":"/dev/disk2s2","size":6900000000,"used":5200000000,"iused":8,"ifree":4294967271,"mounted_on":"/Volumes/InstallESD","available":1700000000,"capacity_percent":76,"iused_percent":0},{"filesystem":"com.apple.TimeMachine.2019-11-29-075900@/dev/disk1s1","size":466000000000,"used":132000000000,"iused":1459164,"ifree":9223372036853316643,"mounted_on":"/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/kbrazil-mac/2019-11-29-075900/Macintosh HD","available":315000000000,"capacity_percent":30,"iused_percent":0},{"filesystem":"//brazil@MyCloudEX2Ultra._afpovertcp._tcp.local/brazil","size":3500000000000,"used":1100000000000,"iused":301134832,"ifree":649465741,"mounted_on":"/Volumes/brazil","available":2400000000000,"capacity_percent":32,"iused_percent":32}]
Loading

0 comments on commit 850917a

Please sign in to comment.