Skip to content

Commit

Permalink
Add JSON output (psmode#7)
Browse files Browse the repository at this point in the history
* Adding JSON output

* Only use output_format if we're not using JSON output

* Update essstat.py

Reorder help text for alphabetic listing of switches

* Update essstat.py

Increment minor version for new feature (JSON support)

* Update README.md

Documentation of JSON support in optional arguments section

* Update CHANGELOG.md

v0.6.0 update entry for added JSON output support

* Update CHANGELOG.md

log entry for v0.6.0 added feature for JSON output support

Co-authored-by: Peter Smode <[email protected]>
  • Loading branch information
robrankin and psmode authored Jun 21, 2022
1 parent 273c326 commit c345b73
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.6.0] - 2022-06-21

### Added
- [essstat.py] Added support for JSON output by [robrankin](https://github.com/robrankin)


## [0.5.0] - 2021-04-25

### Addeded
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Python&nbsp;3.6 and uses the [Beautiful Soup](https://pypi.org/project/beautiful
-h, --help show this help message and exit
-1, --1line output in a single line
-d, --debug activate debugging output
-j, --json output in JSON format
-p TPpswd, --password TPpswd
password for switch access
-s, --statsonly output post statistics only
Expand Down
41 changes: 27 additions & 14 deletions essstat.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
__copyright__ = "Copyright 2021, Peter Smode"
__credits__ = "Peter Smode"
__license__ = "GPL 3.0"
__version__ = "0.5.0"
__version__ = "0.6.0"
__maintainer__ = "Peter Smode"
__email__ = "[email protected]"
__status__ = "Beta"
Expand All @@ -17,7 +17,7 @@
# In[2]:


import argparse, pprint, re, requests, sys
import argparse, pprint, re, requests, sys, json
from datetime import datetime
from bs4 import BeautifulSoup

Expand Down Expand Up @@ -53,6 +53,7 @@ def isnotebook():
parser.add_argument('target', metavar='TPhost', help='IP address or hostname of switch')
parser.add_argument('-1', '--1line', action='store_true', help='output in a single line')
parser.add_argument('-d', '--debug', action='store_true', help='activate debugging output')
parser.add_argument('-j', '--json', action='store_true', help='output in JSON format')
parser.add_argument('-p', '--password', metavar='TPpswd', required=True, help='password for switch access')
parser.add_argument('-s', '--statsonly', action='store_true', help='output post statistics only')
parser.add_argument('-u', '--username', metavar='TPuser', required=False, default='admin', help='username for switch access')
Expand All @@ -63,6 +64,7 @@ def isnotebook():
BASE_URL = "http://"+args['target']
TPLstatsonly = args['statsonly']
TPL1line = args['1line']
TPLjson = args['json']
TPLdebug = args['debug']


Expand Down Expand Up @@ -181,7 +183,7 @@ def isnotebook():
max_port_num = int(pattern.search(str(soup.head.find_all("script"))).group(2))
else:
max_port_num = int(pattern.search(str(soup.script)).group(2))
if not (TPLstatsonly or TPL1line):
if not (TPLstatsonly or TPL1line or TPLjson):
print(current_dt)
print("max_port_num={0:d}".format(max_port_num))

Expand Down Expand Up @@ -260,20 +262,21 @@ def isnotebook():

# In[25]:


if TPL1line:
print(current_dt+"," + str(max_port_num)+",", end="")
output_format = "{0:d},{1:s},{2:s},{3:s},{4:s},{5:s},{6:s}"
myend = ","
else:
output_format = "{0:d};{1:s};{2:s};{3:s},{4:s},{5:s},{6:s}"
myend = "\n"
if not TPLjson:
if TPL1line:
print(current_dt+"," + str(max_port_num)+",", end="")
output_format = "{0:d},{1:s},{2:s},{3:s},{4:s},{5:s},{6:s}"
myend = ","
else:
output_format = "{0:d};{1:s};{2:s};{3:s},{4:s},{5:s},{6:s}"
myend = "\n"

pdict = {}
jlist = []
for x in range(1, max_port_num+1):
#print(x, ((x-1)*4), ((x-1)*4)+1, ((x-1)*4)+2, ((x-1)*4)+3 )
pdict[x] = {}
if TPL1line:
if (TPL1line or TPLjson):
pdict[x]['state'] = e3[x-1]
pdict[x]['link_status'] = e4[x-1]
else:
Expand All @@ -286,14 +289,24 @@ def isnotebook():

if (x == max_port_num):
myend = "\n"
print(output_format.format(x,

if TPLjson:
z = { **{"port": x}, **pdict[x] }
jlist.append( z )
else:
print(output_format.format(x,
pdict[x]['state'],
pdict[x]['link_status'],
pdict[x]['TxGoodPkt'],
pdict[x]['TxBadPkt'],
pdict[x]['RxGoodPkt'],
pdict[x]['RxBadPkt']), end=myend)

if TPLjson:
for dict in jlist:
for key in dict:
dict[key] = int(dict[key])
json_object = json.dumps(jlist)
print(json_object)

# In[26]:

Expand Down

0 comments on commit c345b73

Please sign in to comment.