|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import click |
| 4 | +import sys |
| 5 | +import configparser |
| 6 | +import functools |
| 7 | +import os |
| 8 | +import glob |
| 9 | +from tabulate import tabulate |
| 10 | +import utilities_common.cli as clicommon |
| 11 | + |
| 12 | +SYSTEMD_BOOTCHART = "/lib/systemd/systemd-bootchart" |
| 13 | +BOOTCHART_CONF = "/etc/systemd/bootchart.conf" |
| 14 | +BOOTCHART_DEFAULT_OUTPUT_DIR = "/run/log/" |
| 15 | +BOOTCHART_DEFAULT_OUTPUT_GLOB = os.path.join(BOOTCHART_DEFAULT_OUTPUT_DIR, "bootchart-*.svg") |
| 16 | + |
| 17 | +class BootChartConfigParser(configparser.ConfigParser): |
| 18 | + """ Custom bootchart config parser. Changes the way ConfigParser passes options """ |
| 19 | + |
| 20 | + def optionxform(self, option): |
| 21 | + """ Pass options as is, without modifications """ |
| 22 | + return option |
| 23 | + |
| 24 | + |
| 25 | +def exit_cli(*args, **kwargs): |
| 26 | + """ Print a message and exit with rc 1. """ |
| 27 | + click.secho(*args, **kwargs) |
| 28 | + sys.exit(1) |
| 29 | + |
| 30 | + |
| 31 | +def root_privileges_required(func): |
| 32 | + """ Decorates a function, so that the function is invoked |
| 33 | + only if the user is root. """ |
| 34 | + @functools.wraps(func) |
| 35 | + def wrapped_function(*args, **kwargs): |
| 36 | + """ Wrapper around func. """ |
| 37 | + if os.geteuid() != 0: |
| 38 | + exit_cli("Root privileges required for this operation", fg="red") |
| 39 | + return func(*args, **kwargs) |
| 40 | + |
| 41 | + wrapped_function.__doc__ += "\n\n NOTE: This command requires elevated (root) privileges to run." |
| 42 | + return wrapped_function |
| 43 | + |
| 44 | + |
| 45 | +def check_bootchart_installed(): |
| 46 | + """ Fails imidiatelly if bootchart is not installed """ |
| 47 | + if not os.path.exists(SYSTEMD_BOOTCHART): |
| 48 | + exit_cli("systemd-bootchart is not installed", fg="red") |
| 49 | + |
| 50 | + |
| 51 | +def get_enabled_status(): |
| 52 | + """ Get systemd-bootchart status """ |
| 53 | + return clicommon.run_command("systemctl is-enabled systemd-bootchart", return_cmd=True) |
| 54 | + |
| 55 | +def get_active_status(): |
| 56 | + """ Get systemd-bootchart status """ |
| 57 | + return clicommon.run_command("systemctl is-active systemd-bootchart", return_cmd=True) |
| 58 | + |
| 59 | +def get_output_files(): |
| 60 | + bootchart_output_files = [] |
| 61 | + for bootchart_output_file in glob.glob(BOOTCHART_DEFAULT_OUTPUT_GLOB): |
| 62 | + bootchart_output_files.append(bootchart_output_file) |
| 63 | + return "\n".join(bootchart_output_files) |
| 64 | + |
| 65 | + |
| 66 | +@click.group() |
| 67 | +def cli(): |
| 68 | + """ Main CLI group """ |
| 69 | + check_bootchart_installed() |
| 70 | + |
| 71 | + |
| 72 | +@cli.command() |
| 73 | +@root_privileges_required |
| 74 | +def enable(): |
| 75 | + """ Enable bootchart """ |
| 76 | + clicommon.run_command("systemctl enable systemd-bootchart", display_cmd=True) |
| 77 | + |
| 78 | + |
| 79 | +@cli.command() |
| 80 | +@root_privileges_required |
| 81 | +def disable(): |
| 82 | + """ Disable bootchart """ |
| 83 | + clicommon.run_command("systemctl disable systemd-bootchart", display_cmd=True) |
| 84 | + |
| 85 | + |
| 86 | +@cli.command() |
| 87 | +@click.option('--time', type=click.IntRange(min=1), required=True) |
| 88 | +@click.option('--frequency', type=click.IntRange(min=1), required=True) |
| 89 | +@root_privileges_required |
| 90 | +def config(time, frequency): |
| 91 | + """ Configure bootchart """ |
| 92 | + samples = time * frequency |
| 93 | + |
| 94 | + config = { |
| 95 | + 'Samples': str(samples), |
| 96 | + 'Frequency': str(frequency), |
| 97 | + } |
| 98 | + bootchart_config = BootChartConfigParser() |
| 99 | + bootchart_config.read(BOOTCHART_CONF) |
| 100 | + bootchart_config['Bootchart'].update(config) |
| 101 | + with open(BOOTCHART_CONF, 'w') as config_file: |
| 102 | + bootchart_config.write(config_file, space_around_delimiters=False) |
| 103 | + |
| 104 | + |
| 105 | +@cli.command() |
| 106 | +def show(): |
| 107 | + """ Display bootchart configuration """ |
| 108 | + bootchart_config = BootChartConfigParser() |
| 109 | + bootchart_config.read(BOOTCHART_CONF) |
| 110 | + |
| 111 | + try: |
| 112 | + samples = int(bootchart_config["Bootchart"]["Samples"]) |
| 113 | + frequency = int(bootchart_config["Bootchart"]["Frequency"]) |
| 114 | + except KeyError as key: |
| 115 | + raise click.ClickException(f"Failed to parse bootchart config: {key} not found") |
| 116 | + except ValueError as err: |
| 117 | + raise click.ClickException(f"Failed to parse bootchart config: {err}") |
| 118 | + |
| 119 | + try: |
| 120 | + time = samples // frequency |
| 121 | + except ZeroDivisionError: |
| 122 | + raise click.ClickException(f"Invalid frequency value: {frequency}") |
| 123 | + |
| 124 | + field_values = { |
| 125 | + "Status": get_enabled_status(), |
| 126 | + "Operational Status": get_active_status(), |
| 127 | + "Frequency": frequency, |
| 128 | + "Time (sec)": time, |
| 129 | + "Output": get_output_files(), |
| 130 | + } |
| 131 | + |
| 132 | + click.echo(tabulate([field_values.values()], field_values.keys())) |
| 133 | + |
| 134 | + |
| 135 | +def main(): |
| 136 | + cli() |
| 137 | + |
| 138 | +if __name__ == "__main__": |
| 139 | + main() |
0 commit comments