Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP | Feat: Add cmd parser prometheus-* #60

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 36 additions & 26 deletions omg/cmd/parser/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import os
import json
from tabulate import tabulate

from omg.common.config import Config
from .etcd_out import etcd_member_list
from .etcd_out import etcd_endpoint_health
from .etcd_out import etcd_endpoint_status
from .etcd_out import etcd_show_all
from .alerts_out import alerts_summary
from .alerts_out import alerts_firing
from .etcd_out import (
etcd_member_list,
etcd_endpoint_health,
etcd_endpoint_status,
etcd_show_all
)
from .alerts_out import (
alerts_summary, alerts_firing
)
from . import prometheus_out as prom_out


parser_map = {
Expand Down Expand Up @@ -53,6 +58,30 @@
"helper": "Parser alerts firing exported by must-gather monitoring/alerts.json",
"file_in": "monitoring/alerts.json",
"fn_out": alerts_firing
},
"prometheus-status-tsdb":
{
"command": "prometheus-status-tsdb",
"helper": "Parser TSDB status exported by must-gather monitoring/prometheus/status/tsdb.json",
"file_in": "",
"ignore_err": True,
"fn_out": prom_out.prom_status_tsdb
},
"prometheus-runtime-build-info":
{
"command": "prometheus-runtime-build-info",
"helper": "Parser Runtime and Build info exported by must-gather monitoring/prometheus/status/{runtime,buildinfo}.json",
"file_in": "",
"ignore_err": True,
"fn_out": prom_out.prom_status_runtime_buildinfo
},
"prometheus-status-config-diff":
{
"command": "prometheus-status-config-diff",
"helper": "Compare the configuration from both replicas exported by must-gather monitoring/prometheus/status/config.json",
"file_in": "",
"ignore_err": True,
"fn_out": prom_out.prom_status_config_diff
}
}

Expand All @@ -76,25 +105,6 @@ def help():
print(tabulate(output_res, tablefmt="plain"))


def file_reader(path):
"""
Read a file to be parsed and return raw buffer.
"""
try:
full_path = os.path.join(Config().path, path)
with open(full_path, 'r') as f:
return f.read(), False
except IsADirectoryError as e:
print("WANING: ignoring file reader; Is a directory")
return "", True
except FileNotFoundError as e:
print(f"ERROR: file [{path}] not found")
return "", True
except Exception as e:
print(f"ERROR: Unknow error opening file {path}")
return "", True


def print_table(data=None, headers=[], rows=[], fmt="psql"):
"""
Print a generic table. When headers and rows are defined, it will
Expand Down Expand Up @@ -136,7 +146,7 @@ def parser_main(command=None, show=None):

try:
cmd = command[0]
buffer, err = file_reader(parser_map[cmd]['file_in'])
buffer, err = load_file(parser_map[cmd]['file_in'])
if err:
if 'ignore_err' in parser_map[cmd]:
if not (parser_map[cmd]['ignore_err']):
Expand Down
28 changes: 9 additions & 19 deletions omg/cmd/parser/etcd_out.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import json


def _load_buffer_as_json(buffer):
"""
wrapper function to open a json from a given buffer.
Return json object and error
"""
try:
data = json.loads(buffer)
return data, False
except json.decoder.JSONDecodeError :
return "JSONDecodeError", True
except Exception as e:
return e, True
from omg.common.helper import (
load_file,
load_json_file
)


def etcd_member_list(buffer=None):
Expand All @@ -21,7 +11,7 @@ def etcd_member_list(buffer=None):
"""
from . import print_table

data, err = _load_buffer_as_json(buffer)
data, err = load_json_buffer(buffer)
h = data['header']

print(f"\nClusterID: {h['cluster_id']}, MemberID: {h['member_id']}, RaftTerm: {h['raft_term']}")
Expand All @@ -34,7 +24,7 @@ def etcd_endpoint_health(buffer=None):
"""
from . import print_table

data, err = _load_buffer_as_json(buffer)
data, err = load_json_buffer(buffer)
print_table(data=data)


Expand All @@ -51,7 +41,7 @@ def sizeof_fmt(num, suffix='B'):
num /= 1024.0
return ("%.1f %s%s" % (num, 'Yi', suffix))

data, err = _load_buffer_as_json(buffer)
data, err = load_json_buffer(buffer)
headers_map = [
"ENDPOINT", "ID", "VERSION", "DB SIZE", "IS LEADER",
"IS LEARNER", "RAFT TERM", "RAFT INDEX", "RAFT APPLIED INDEX",
Expand Down Expand Up @@ -93,7 +83,7 @@ def etcd_show_all(buffer=None):
Show all etcd commands.
"""
from . import (
parser_map, file_reader
parser_map
)

etcd_cmds = []
Expand All @@ -105,7 +95,7 @@ def etcd_show_all(buffer=None):
etcd_cmds.append(parser_map[cmd])

for cmd in etcd_cmds:
buffer, err = file_reader(cmd['file_in'])
buffer, err = load_file(cmd['file_in'])
parser_map[cmd['command']]["fn_out"](buffer)

return
Loading