Skip to content

Commit

Permalink
Create coredump group of commands to handle corefile management
Browse files Browse the repository at this point in the history
Signed-off-by: Rajendra Dendukuri <rajendra.dendukuri@broadcom.com>
  • Loading branch information
rajendra-dendukuri committed Dec 2, 2020
1 parent 4e1755d commit d3459d7
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 96 deletions.
31 changes: 31 additions & 0 deletions config/coredump.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os
import click
import utilities_common.cli as clicommon
from swsssdk import ConfigDBConnector

@click.group(cls=clicommon.AbbreviationGroup, name="coredump")
def coredump():
""" Configure coredump """
if os.geteuid() != 0:
exit("Root privileges are required for this operation")
pass

@coredump.command()
@click.argument('disable', required=False)
def disable(disable):
"""Administratively Disable coredump generation"""
config_db = ConfigDBConnector()
config_db.connect()
table = "COREDUMP"
key = "config"
config_db.set_entry(table, key, {"enabled": "false"})

@coredump.command()
@click.argument('enable', required=False)
def enable(enable):
"""Administratively Enable coredump generation"""
config_db = ConfigDBConnector()
config_db.connect()
table = "COREDUMP"
key = "config"
config_db.set_entry(table, key, {"enabled": "true"})
29 changes: 2 additions & 27 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from . import aaa
from . import chassis_modules
from . import console
from . import coredump
from . import feature
from . import kube
from . import mlnx
Expand Down Expand Up @@ -877,6 +878,7 @@ def config(ctx):
config.add_command(aaa.tacacs)
config.add_command(chassis_modules.chassis_modules)
config.add_command(console.console)
config.add_command(coredump.coredump)
config.add_command(feature.feature)
config.add_command(kube.kubernetes)
config.add_command(muxcable.muxcable)
Expand Down Expand Up @@ -1901,33 +1903,6 @@ def shutdown():
"""Shut down BGP session(s)"""
pass

@config.group(cls=clicommon.AbbreviationGroup)
def core():
""" Configure coredump """
if os.geteuid() != 0:
exit("Root privileges are required for this operation")
pass

@core.command()
@click.argument('disable', required=False)
def disable(disable):
"""Administratively Disable coredump generation"""
config_db = ConfigDBConnector()
config_db.connect()
table = "COREDUMP"
key = "config"
config_db.set_entry(table, key, {"enabled": "false"})

@core.command()
@click.argument('enable', required=False)
def enable(enable):
"""Administratively Enable coredump generation"""
config_db = ConfigDBConnector()
config_db.connect()
table = "COREDUMP"
key = "config"
config_db.set_entry(table, key, {"enabled": "true"})

@config.group(cls=clicommon.AbbreviationGroup)
def kdump():
""" Configure kdump """
Expand Down
75 changes: 75 additions & 0 deletions show/coredump.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import os
import click
import utilities_common.cli as clicommon
from swsssdk import ConfigDBConnector

#
# 'coredumpctl' group ("show coredump")
#

@click.group(cls=clicommon.AliasedGroup, name="coredump")
def coredump():
"""Show core dump events encountered"""
pass

# 'config' subcommand ("show coredump config")
@coredump.command('config')
@click.option('--verbose', is_flag=True, help="Enable verbose output")
def core_config(verbose):
""" Show coredump configuration """
# Default admin mode
admin_mode = True
# Obtain config from Config DB
config_db = ConfigDBConnector()
if config_db is not None:
config_db.connect()
table_data = config_db.get_table('COREDUMP')
if table_data is not None:
config_data = table_data.get('config')
if config_data is not None:
admin_mode = config_data.get('enabled')
if admin_mode is not None and admin_mode.lower() == 'false':
admin_mode = False

# Core dump administrative mode
if admin_mode:
click.echo('Coredump : %s' % 'Enabled')
else:
click.echo('Coredump : %s' % 'Disabled')

# 'list' subcommand ("show coredump list")
@coredump.command('list')
@click.argument('pattern', required=False)
@click.option('--verbose', is_flag=True, help="Enable verbose output")
def core_list(verbose, pattern):
""" List available coredumps """

if not os.geteuid()==0:
click.echo("Note: To list all the core files please run the command with root privileges\n")

if os.path.exists("/usr/bin/coredumpctl"):
cmd = "coredumpctl list"
if pattern is not None:
cmd = cmd + " " + pattern
clicommon.run_command(cmd, display_cmd=verbose)
else:
exit("Note: Install systemd-coredump package to run this command")

# 'info' subcommand ("show coredump info")
@coredump.command('info')
@click.argument('pattern', required=False)
@click.option('--verbose', is_flag=True, help="Enable verbose output")
def core_info(verbose, pattern):
""" Show information about one or more coredumps """

if not os.geteuid()==0:
click.echo("Note: To view all the core files please run the command with root privileges\n")

if os.path.exists("/usr/bin/coredumpctl"):
cmd = "coredumpctl info"
if pattern is not None:
cmd = cmd + " " + pattern
clicommon.run_command(cmd, display_cmd=verbose)
else:
exit("Note: Install systemd-coredump package to run this command")

71 changes: 2 additions & 69 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from . import bgp_common
from . import chassis_modules
from . import coredump
from . import feature
from . import fgnhg
from . import interfaces
Expand Down Expand Up @@ -129,6 +130,7 @@ def cli(ctx):

# Add groups from other modules
cli.add_command(chassis_modules.chassis_modules)
cli.add_command(coredump.coredump)
cli.add_command(feature.feature)
cli.add_command(fgnhg.fgnhg)
cli.add_command(interfaces.interfaces)
Expand Down Expand Up @@ -1423,75 +1425,6 @@ def system_memory(verbose):
cmd = "free -m"
run_command(cmd, display_cmd=verbose)

#
# 'coredumpctl' group ("show cores")
#

@cli.group(cls=clicommon.AliasedGroup)
def cores():
"""Show core dump events encountered"""
pass

# 'config' subcommand ("show cores config")
@cores.command('config')
@click.option('--verbose', is_flag=True, help="Enable verbose output")
def core_config(verbose):
""" Show coredump configuration """
# Default admin mode
admin_mode = True
# Obtain config from Config DB
config_db = ConfigDBConnector()
if config_db is not None:
config_db.connect()
table_data = config_db.get_table('COREDUMP')
if table_data is not None:
config_data = table_data.get('config')
if config_data is not None:
admin_mode = config_data.get('enabled')
if admin_mode is not None and admin_mode.lower() == 'false':
admin_mode = False

# Core dump administrative mode
if admin_mode:
click.echo('Coredump : %s' % 'Enabled')
else:
click.echo('Coredump : %s' % 'Disabled')

# 'list' subcommand ("show cores list")
@cores.command('list')
@click.argument('pattern', required=False)
@click.option('--verbose', is_flag=True, help="Enable verbose output")
def core_list(verbose, pattern):
""" List available coredumps """

if not os.geteuid()==0:
click.echo("Note: To list all the core files please run the command with root privileges\n")

if os.path.exists("/usr/bin/coredumpctl"):
cmd = "coredumpctl list"
if pattern is not None:
cmd = cmd + " " + pattern
run_command(cmd, display_cmd=verbose)
else:
exit("Note: Install systemd-coredump package to run this command")

# 'info' subcommand ("show cores info")
@cores.command('info')
@click.argument('pattern', required=False)
@click.option('--verbose', is_flag=True, help="Enable verbose output")
def core_info(verbose, pattern):
""" Show information about one or more coredumps """

if not os.geteuid()==0:
click.echo("Note: To view all the core files please run the command with root privileges\n")

if os.path.exists("/usr/bin/coredumpctl"):
cmd = "coredumpctl info"
if pattern is not None:
cmd = cmd + " " + pattern
run_command(cmd, display_cmd=verbose)
else:
exit("Note: Install systemd-coredump package to run this command")

#
# 'kdump command ("show kdump ...")
Expand Down

0 comments on commit d3459d7

Please sign in to comment.