1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00

add history and uptime parsers

This commit is contained in:
Kelly Brazil
2019-10-24 17:09:32 -07:00
parent f101d881a1
commit 41cd489c34
5 changed files with 126 additions and 0 deletions

View File

@ -63,6 +63,7 @@ jc [parser] [options]
- `--df` enables the `df` parser - `--df` enables the `df` parser
- `--env` enables the `env` parser - `--env` enables the `env` parser
- `--free` enables the `free` parser - `--free` enables the `free` parser
- `--history` enables the `history` parser
- `--ifconfig` enables the `ifconfig` parser - `--ifconfig` enables the `ifconfig` parser
- `--iptables` enables the `iptables` parser - `--iptables` enables the `iptables` parser
- `--jobs` enables the `jobs` parser - `--jobs` enables the `jobs` parser
@ -75,6 +76,7 @@ jc [parser] [options]
- `--ps` enables the `ps` parser - `--ps` enables the `ps` parser
- `--route` enables the `route` parser - `--route` enables the `route` parser
- `--uname` enables the `uname -a` parser - `--uname` enables the `uname -a` parser
- `--uptime` enables the `uptime` parser
- `--w` enables the `w` parser - `--w` enables the `w` parser
### Options ### Options
@ -135,6 +137,38 @@ $ env | jc --env -p
"_": "/usr/bin/env" "_": "/usr/bin/env"
} }
``` ```
### free
```
$ free | jc --free -p
[
{
"type": "Mem",
"total": "2017300",
"used": "213104",
"free": "1148452",
"shared": "1176",
"buff/cache": "655744",
"available": "1622204"
},
{
"type": "Swap",
"total": "2097148",
"used": "0",
"free": "2097148"
}
]
```
### history
```
$ history | jc --history -p
{
"118": "sleep 100",
"119": "ls /bin",
"120": "echo \"hello\"",
"121": "docker images",
...
}
```
### ifconfig ### ifconfig
``` ```
$ ifconfig | jc --ifconfig -p $ ifconfig | jc --ifconfig -p
@ -1043,6 +1077,18 @@ $ uname -a | jc --uname -p
"kernel_version": "#74-Ubuntu SMP Tue Sep 17 17:06:04 UTC 2019" "kernel_version": "#74-Ubuntu SMP Tue Sep 17 17:06:04 UTC 2019"
} }
``` ```
### uptime
```
$ uptime | jc --uptime -p
{
"time": "16:52",
"uptime": "3 days, 4:49",
"users": "5",
"load_1m": "1.85",
"load_5m": "1.90",
"load_15m": "1.91"
}
```
### w ### w
``` ```
$ w | jc --w -p $ w | jc --w -p

View File

@ -1,6 +1,9 @@
jc changelog jc changelog
2019xxxx v1.0.1 2019xxxx v1.0.1
- Add w parser
- Add uptime parser
- Add free parser
- Fix env parser - Fix env parser
20191023 v0.9.1 20191023 v0.9.1

View File

@ -9,6 +9,7 @@ import json
import jc.parsers.df import jc.parsers.df
import jc.parsers.env import jc.parsers.env
import jc.parsers.free import jc.parsers.free
import jc.parsers.history
import jc.parsers.ifconfig import jc.parsers.ifconfig
import jc.parsers.iptables import jc.parsers.iptables
import jc.parsers.jobs import jc.parsers.jobs
@ -21,6 +22,7 @@ import jc.parsers.netstat
import jc.parsers.ps import jc.parsers.ps
import jc.parsers.route import jc.parsers.route
import jc.parsers.uname import jc.parsers.uname
import jc.parsers.uptime
import jc.parsers.w import jc.parsers.w
@ -30,6 +32,7 @@ def helptext():
print(' --df df parser', file=sys.stderr) print(' --df df parser', file=sys.stderr)
print(' --env env parser', file=sys.stderr) print(' --env env parser', file=sys.stderr)
print(' --free free parser', file=sys.stderr) print(' --free free parser', file=sys.stderr)
print(' --history history parser', file=sys.stderr)
print(' --ifconfig iconfig parser', file=sys.stderr) print(' --ifconfig iconfig parser', file=sys.stderr)
print(' --iptables iptables parser', file=sys.stderr) print(' --iptables iptables parser', file=sys.stderr)
print(' --jobs jobs parser', file=sys.stderr) print(' --jobs jobs parser', file=sys.stderr)
@ -42,6 +45,7 @@ def helptext():
print(' --ps ps parser', file=sys.stderr) print(' --ps ps parser', file=sys.stderr)
print(' --route route parser', file=sys.stderr) print(' --route route parser', file=sys.stderr)
print(' --uname uname parser', file=sys.stderr) print(' --uname uname parser', file=sys.stderr)
print(' --uptime uptime parser', file=sys.stderr)
print(' --w w parser\n', file=sys.stderr) print(' --w w parser\n', file=sys.stderr)
print('Options:', file=sys.stderr) print('Options:', file=sys.stderr)
print(' -p pretty print output\n', file=sys.stderr) print(' -p pretty print output\n', file=sys.stderr)
@ -72,6 +76,9 @@ def main():
elif '--free' in sys.argv: elif '--free' in sys.argv:
result = jc.parsers.free.parse(data) result = jc.parsers.free.parse(data)
elif '--history' in sys.argv:
result = jc.parsers.history.parse(data)
elif '--ifconfig' in sys.argv: elif '--ifconfig' in sys.argv:
result = jc.parsers.ifconfig.parse(data) result = jc.parsers.ifconfig.parse(data)
@ -108,6 +115,9 @@ def main():
elif '--uname' in sys.argv: elif '--uname' in sys.argv:
result = jc.parsers.uname.parse(data) result = jc.parsers.uname.parse(data)
elif '--uptime' in sys.argv:
result = jc.parsers.uptime.parse(data)
elif '--w' in sys.argv: elif '--w' in sys.argv:
result = jc.parsers.w.parse(data) result = jc.parsers.w.parse(data)

32
jc/parsers/history.py Normal file
View File

@ -0,0 +1,32 @@
"""jc - JSON CLI output utility history Parser
Usage:
specify --history as the first argument if the piped input is coming from history
Example:
$ history | jc --history -p
{
"118": "sleep 100",
"119": "ls /bin",
"120": "echo \"hello\"",
"121": "docker images",
...
}
"""
def parse(data):
output = {}
linedata = data.splitlines()
# Clear any blank lines
cleandata = list(filter(None, linedata))
if cleandata:
for entry in cleandata:
parsed_line = entry.split(maxsplit=1)
output[parsed_line[0]] = parsed_line[1]
return output

35
jc/parsers/uptime.py Normal file
View File

@ -0,0 +1,35 @@
"""jc - JSON CLI output utility uptime Parser
Usage:
specify --uptime as the first argument if the piped input is coming from uptime
Example:
$ uptime | jc --uptime -p
{
"time": "16:52",
"uptime": "3 days, 4:49",
"users": "5",
"load_1m": "1.85",
"load_5m": "1.90",
"load_15m": "1.91"
}
"""
def parse(data):
output = {}
cleandata = data.splitlines()
if cleandata:
parsed_line = cleandata[0].split()
output['time'] = parsed_line[0]
output['uptime'] = ' '.join(parsed_line[2:5]).rstrip(',')
output['users'] = parsed_line[5]
output['load_1m'] = parsed_line[9]
output['load_5m'] = parsed_line[10]
output['load_15m'] = parsed_line[11]
return output