From 3979be96355f1bc694056c036928df9b71117870 Mon Sep 17 00:00:00 2001 From: kshithijiyer Date: Fri, 29 May 2020 19:26:39 +0530 Subject: [Tool] Add tool to fetch sosreports Adding tool to fetch sosreports from all servers and clients using glusto-tests config file. This tool is essentially just a tweeked version of getsos[1] tool which can take glusto-tests config file and is relicensed under GPLv3+. Reference: [1] https://github.com/kshithijiyer/getsos Change-Id: Ic1685163154ed4358064397d74d3965097448621 Signed-off-by: kshithijiyer --- tools/get_sosreports/README.md | 45 ++++++++ tools/get_sosreports/get_sosreports.py | 190 +++++++++++++++++++++++++++++++++ tools/get_sosreports/setup.py | 33 ++++++ 3 files changed, 268 insertions(+) create mode 100644 tools/get_sosreports/README.md create mode 100644 tools/get_sosreports/get_sosreports.py create mode 100644 tools/get_sosreports/setup.py (limited to 'tools/get_sosreports') diff --git a/tools/get_sosreports/README.md b/tools/get_sosreports/README.md new file mode 100644 index 000000000..57176f27f --- /dev/null +++ b/tools/get_sosreports/README.md @@ -0,0 +1,45 @@ +# get_sosreports +Tool to collect sosreports from all servers and clients. + +## Prerequisites +1. Python 3.x +2. Passwordless ssh should be setup. + +## Installation +1. Change directory to the project directory. + +``` +# cd tools/get_sosreports +``` + +2. Now run the installation script. + +``` +# python3 setup.py install +``` + +3. To check run: + +``` +# get_sosreports --help +``` + +## Usage +There are 2 ways of using the tool. +1. Passing IP addresses through command line seperated by comma(,): + +``` +# get_sosreports -m machine_1,machine_2,machine_3 +``` + +2. Passing a glusto-tests config file: + +``` +# get_sosreports -f config_file +``` + +**Note**: +The default destination directory is `.` (present dir) `-d` or `--dist-dir` option. + +## Licence +[GPLv3](https://github.com/gluster/glusto-tests/blob/master/LICENSE) diff --git a/tools/get_sosreports/get_sosreports.py b/tools/get_sosreports/get_sosreports.py new file mode 100644 index 000000000..962fa6d7f --- /dev/null +++ b/tools/get_sosreports/get_sosreports.py @@ -0,0 +1,190 @@ +# Copyright (C) 2020 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY :or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +# Imports needed by the script. +import argparse +import os +import sys +from yaml import safe_load + + +def read_config_file(config_file): + """ + A function to read the yaml file given to the script. + + Args: + config_file(str): A config file used to run glusto-tests. + + Return: + dict: A dictornary with all the details from config file. + """ + return safe_load(open(config_file, 'r')) + + +def remove_previous_sosreports(server): + """ + A function to remove old sosreports. + + Args: + server: hostname/IP server from which sosreport + has to be removed. + + Returns: + bool: True if successful else false. + """ + cmd = ("ssh root@{} \"rm -rf /var/tmp/sosreport-*\"" + .format(server)) + ret = os.system(cmd) + if ret: + return False + return True + + +def collect_new_sosreports(server): + """ + A function to generate sosreports. + + Args: + server: hostname/IP server from which sosreport + has to be collected. + + Returns: + bool: True if successful else false. + """ + cmd = ("ssh root@{} \"sosreport --batch --name=$HOSTNAME\"" + .format(server)) + ret = os.system(cmd) + if ret: + return False + return True + + +def copy_sosreports_to_dir(server, directory): + """ + A function to copy sosreports to local dir. + + Args: + server: hostname/IP of server for passwordless ssh + has to be configured. + directory: Directory to be used to store sosreports. + + Returns: + bool: True if successful else false. + """ + cmd = ("scp root@{}:/var/tmp/sosreport-* {}" + .format(server, directory)) + ret = os.system(cmd) + if ret: + return False + return True + + +def check_and_create_dir_if_not_present(directory): + """ + A function to check and create directory if not present. + + Args: + directory: Directory to be checked/created. + + Returns: + bool: True if successful else false. + """ + if not os.path.isdir(directory): + cmd = ("mkdir -p {}".format(directory)) + ret = os.system(cmd) + if ret: + return False + else: + print("[INFO]:The dir already exists.") + return True + + +def main(): + """ + Main function of the tool. + """ + # Setting up command line arguments. + parser = argparse.ArgumentParser( + description="Tool to collect sosreports from servers and clients." + ) + parser.add_argument("-f", + "--config_file", + type=str, + dest="config_file", + help="A glusto-tests configuration file.") + parser.add_argument("-m", "--servers", type=str, + dest="servers", + help=("A list of hostnames/ips of" + " servers seperated by comma(',').")) + parser.add_argument("-d", "--dist-dir", type=str, default=".", + dest="directory", + help=("Directory where reports are to be stored." + "(Default:.)")) + args = parser.parse_args() + + # Getting list of hostname/IP. + if args.servers: + servers = args.servers.split(',') + + # Reading the config file. + if args.config_file: + config = read_config_file(args.config_file) + servers = [] + servers += config.get('clients', []) + servers += config.get('servers', []) + + # Fetching other parameters from command line. + directory = args.directory + + # Checking and creating dir if not present. + ret = check_and_create_dir_if_not_present(directory) + if not ret: + sys.exit("[ERROR]:Unable to create dir for storing sosreports.") + + try: + for server in servers: + + # Removing old sosreports from the server. + ret = remove_previous_sosreports(server) + if not ret: + sys.exit("[ERROR]:Unable to remove old sosreports on {}!" + .format(server)) + print("[INFO]:Successfully removed old sosreports on {}." + .format(server)) + + # Collecting sosreport on the server. + ret = collect_new_sosreports(server) + if not ret: + sys.exit("[ERROR]:Unable to collect sosreport on {}!" + .format(server)) + print("[INFO]:Successfully collected sosreport on {}." + .format(server)) + + # Downloading sosreport to local machine. + ret = copy_sosreports_to_dir(server, directory) + if not ret: + sys.exit("[ERROR]:Unable download sosreport from {}." + .format(server)) + print("[INFO]:Successfully copied sosreports from {}." + .format(server)) + + # If servers aren't provided. + except UnboundLocalError: + sys.exit("[ERROR]:servers were not provided") + + +if __name__ == "__main__": + main() diff --git a/tools/get_sosreports/setup.py b/tools/get_sosreports/setup.py new file mode 100644 index 000000000..79392e7ec --- /dev/null +++ b/tools/get_sosreports/setup.py @@ -0,0 +1,33 @@ +# Copyright (C) 2020 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY :or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +from setuptools import setup + +setup( + name='get_sosreports', + version="1.0", + author='Red Hat, Inc.', + author_email='gluster-devel@gluster.org', + url='http://www.gluster.org', + licens="GPLv3+", + description=("Tool to collect sosreports" + " from all servers and clients"), + py_modules=['get_sosreports'], + entry_points=""" + [console_scripts] + get_sosreports = get_sosreports:main + """ +) -- cgit