1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-07-13 01:20:24 +02:00

add stat parser

This commit is contained in:
Kelly Brazil
2019-11-14 16:31:52 -08:00
parent edb9a7c76e
commit c2a67e1b70
2 changed files with 212 additions and 15 deletions

View File

@ -83,6 +83,7 @@ jc PARSER [OPTIONS]
- `--ps` enables the `ps` parser - `--ps` enables the `ps` parser
- `--route` enables the `route` parser - `--route` enables the `route` parser
- `--ss` enables the `ss` parser - `--ss` enables the `ss` parser
- `--stat` enables the `stat` parser
- `--uname` enables the `uname -a` parser - `--uname` enables the `uname -a` parser
- `--uptime` enables the `uptime` parser - `--uptime` enables the `uptime` parser
- `--w` enables the `w` parser - `--w` enables the `w` parser
@ -1142,6 +1143,53 @@ $ sudo ss -a | jc --ss -p
} }
] ]
``` ```
### stat
```
$ stat /bin/* | jc --stat -p
[
{
"file": "/bin/bash",
"size": 1113504,
"blocks": 2176,
"io_blocks": 4096,
"type": "regular file",
"device": "802h/2050d",
"inode": 131099,
"links": 1,
"access": "0755",
"flags": "-rwxr-xr-x",
"uid": 0,
"user": "root",
"gid": 0,
"group": "root",
"access_time": "2019-11-14 08:18:03.509681766 +0000",
"modify_time": "2019-06-06 22:28:15.000000000 +0000",
"change_time": "2019-08-12 17:21:29.521945390 +0000",
"birth_time": "-"
},
{
"file": "/bin/btrfs",
"size": 716464,
"blocks": 1400,
"io_blocks": 4096,
"type": "regular file",
"device": "802h/2050d",
"inode": 131100,
"links": 1,
"access": "0755",
"flags": "-rwxr-xr-x",
"uid": 0,
"user": "root",
"gid": 0,
"group": "root",
"access_time": "2019-11-14 08:18:28.990834276 +0000",
"modify_time": "2018-03-12 23:04:27.000000000 +0000",
"change_time": "2019-08-12 17:21:29.545944399 +0000",
"birth_time": "-"
},
...
]
```
### uname -a ### uname -a
``` ```
$ uname -a | jc --uname -p $ uname -a | jc --uname -p

View File

@ -5,11 +5,95 @@ Usage:
Examples: Examples:
$ stats | jc --stats -p $ stat /bin/* | jc --stat -p
[] [
{
"file": "/bin/bash",
"size": 1113504,
"blocks": 2176,
"io_blocks": 4096,
"type": "regular file",
"device": "802h/2050d",
"inode": 131099,
"links": 1,
"access": "0755",
"flags": "-rwxr-xr-x",
"uid": 0,
"user": "root",
"gid": 0,
"group": "root",
"access_time": "2019-11-14 08:18:03.509681766 +0000",
"modify_time": "2019-06-06 22:28:15.000000000 +0000",
"change_time": "2019-08-12 17:21:29.521945390 +0000",
"birth_time": "-"
},
{
"file": "/bin/btrfs",
"size": 716464,
"blocks": 1400,
"io_blocks": 4096,
"type": "regular file",
"device": "802h/2050d",
"inode": 131100,
"links": 1,
"access": "0755",
"flags": "-rwxr-xr-x",
"uid": 0,
"user": "root",
"gid": 0,
"group": "root",
"access_time": "2019-11-14 08:18:28.990834276 +0000",
"modify_time": "2018-03-12 23:04:27.000000000 +0000",
"change_time": "2019-08-12 17:21:29.545944399 +0000",
"birth_time": "-"
},
...
]
$ stats | jc --stats -p -r $ stat /bin/* | jc --stat -p -r
[] [
{
"file": "/bin/bash",
"size": "1113504",
"blocks": "2176",
"io_blocks": "4096",
"type": "regular file",
"device": "802h/2050d",
"inode": "131099",
"links": "1",
"access": "0755",
"flags": "-rwxr-xr-x",
"uid": "0",
"user": "root",
"gid": "0",
"group": "root",
"access_time": "2019-11-14 08:18:03.509681766 +0000",
"modify_time": "2019-06-06 22:28:15.000000000 +0000",
"change_time": "2019-08-12 17:21:29.521945390 +0000",
"birth_time": "-"
},
{
"file": "/bin/btrfs",
"size": "716464",
"blocks": "1400",
"io_blocks": "4096",
"type": "regular file",
"device": "802h/2050d",
"inode": "131100",
"links": "1",
"access": "0755",
"flags": "-rwxr-xr-x",
"uid": "0",
"user": "root",
"gid": "0",
"group": "root",
"access_time": "2019-11-14 08:18:28.990834276 +0000",
"modify_time": "2018-03-12 23:04:27.000000000 +0000",
"change_time": "2019-08-12 17:21:29.545944399 +0000",
"birth_time": "-"
},
..
]
""" """
import jc.utils import jc.utils
@ -28,14 +112,37 @@ def process(proc_data):
[ [
{ {
"stats": string, "file": string,
"bar": boolean, "size": integer,
"baz": integer "blocks": integer,
"io_blocks": integer,
"type": string,
"device": string,
"inode": integer,
"links": integer,
"access": string,
"flags": string,
"uid": integer,
"user": string,
"gid": integer,
"group": string,
"access_time": string,
"modify_time": string,
"change_time": string,
"birth_time": string
} }
] ]
""" """
for entry in proc_data:
int_list = ['size', 'blocks', 'io_blocks', 'inode', 'links', 'uid', 'gid']
for key in int_list:
if key in entry:
try:
key_int = int(entry[key])
entry[key] = key_int
except (ValueError):
entry[key] = None
# rebuild output for added semantic information
return proc_data return proc_data
@ -67,23 +174,65 @@ def parse(data, raw=False, quiet=False):
cleandata = list(filter(None, cleandata)) cleandata = list(filter(None, cleandata))
if cleandata: if cleandata:
output_line = {}
# stats output contains 8 lines # stats output contains 8 lines
for line in cleandata: for line in cleandata:
# line #1 # line #1
if line.find('File:') == 1: if line.find('File:') == 2:
output_line = {} output_line = {}
line_list = line.split(maxsplit=1) line_list = line.split(maxsplit=1)
output_line['file'] = line_list[1] output_line['file'] = line_list[1]
continue
# line #2 # line #2
if line.find('Size:') == 1: if line.find('Size:') == 2:
line_list = line.split(maxsplit=5) line_list = line.split(maxsplit=7)
output_line['size'] = line_list[1] output_line['size'] = line_list[1]
output_line['Blocks'] = line_list[3] output_line['blocks'] = line_list[3]
output_line['io_blocks'] = line_list[5] output_line['io_blocks'] = line_list[6]
output_line['type'] = line_list[6] output_line['type'] = line_list[7]
continue
# line #3
if line.find('Device:') == 0:
line_list = line.split()
output_line['device'] = line_list[1]
output_line['inode'] = line_list[3]
output_line['links'] = line_list[5]
continue
# line #4
if line.find('Access: (') == 0:
line = line.replace('(', ' ').replace(')', ' ').replace('/', ' ')
line_list = line.split()
output_line['access'] = line_list[1]
output_line['flags'] = line_list[2]
output_line['uid'] = line_list[4]
output_line['user'] = line_list[5]
output_line['gid'] = line_list[7]
output_line['group'] = line_list[8]
continue
# line #5
if line.find('Access: 2') == 0:
line_list = line.split(maxsplit=1)
output_line['access_time'] = line_list[1]
# line #6
if line.find('Modify:') == 0:
line_list = line.split(maxsplit=1)
output_line['modify_time'] = line_list[1]
# line #7
if line.find('Change:') == 0:
line_list = line.split(maxsplit=1)
output_line['change_time'] = line_list[1]
# line #8
if line.find('Birth:') == 1:
line_list = line.split(maxsplit=1)
output_line['birth_time'] = line_list[1]
raw_output.append(output_line)
if raw: if raw:
return raw_output return raw_output