diff --git a/script.py b/script.py index 62654d30ffc1b8fd237709554f00f28ecc112e8f..c7ca559a816534f5daa4728d77193de6b64235a3 100644 --- a/script.py +++ b/script.py @@ -42,21 +42,31 @@ def load_api_token(): def get_disk_space(): # Run the df command to get disk space information - result = subprocess.run(['df', '-h'], capture_output=True, text=True) + result = subprocess.run(['df', '-h'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) + # Extract the output and parse the relevant information output_lines = result.stdout.strip().split('\n') - header_line, *data_lines = output_lines + + # Find the line containing '/data' + data_line = None + for line in output_lines: + if '/data' in line: + data_line = line + break + + if data_line is None: + print("Error: /data not found in the 'df' output.") + return None + + + data_info = data_line.split() - # Assuming the output format of 'df -h' is as follows: - # Filesystem Size Used Avail Use% Mounted on - # /dev/sda1 100G 50G 50G 50% / - # ... - # Extracting the available space (Avail) from the first line of data - disk_space_info = header_line.split() - free_space_on_disk = disk_space_info[3] + # Extract the available space (Avail) and percentage (Use%) for /data + free_space_on_data = data_info[3] + data_percentage = data_info[4] - return free_space_on_disk + return f"{free_space_on_data} ({data_percentage}%)" def get_total_commits_last_month(gl): # Calculate the date range for the last month @@ -88,7 +98,7 @@ def get_projects_with_no_activity(gl): def get_gitlab_version(): # Run 'gitlab-ctl' command to get GitLab version - result = subprocess.run(['gitlab-ctl', 'version'], capture_output=True, text=True) + result = subprocess.run(['grep', 'gitlab-ee', '/opt/gitlab/version-manifest.txt'],stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) # Extract GitLab version from the output gitlab_version = result.stdout.strip() @@ -97,33 +107,45 @@ def get_gitlab_version(): def get_gitlab_info(api_token): # Replace 'YOUR_GITLAB_URL' and 'YOUR_PRIVATE_TOKEN' with your GitLab URL and private token - + print("Auth against "+gitlab_url) gl = gitlab.Gitlab(gitlab_url, api_token) gl.auth() + print("Auth successful") # Get the number of projects projects = gl.projects.list() num_projects = len(projects) + print(f"Number of projects: {num_projects}") # Get the number of users users = gl.users.list(all=True) num_users = len(users) + print(f"Number of users: {num_users}") + # Get a list of admin users - admin_users = [user for user in users if user.is_admin] + admin_users = [user for user in users if hasattr(user, 'is_admin') and user.is_admin] admin_users_list = [user.name for user in admin_users] + print(f"Number of Admin-Users: {len(admin_users_list)}") + # Get free space on disk free_space_on_disk = get_disk_space() + print(f"Free space on disk: {free_space_on_disk}") # Get the total commits last month total_commits_last_month = get_total_commits_last_month(gl) + print(f"Total commits last month: {total_commits_last_month}") # Get projects with no activity projects_with_no_activity = get_projects_with_no_activity(gl) + print(f"Projects with no activity: {len(projects_with_no_activity)}") + # Get GitLab version gitlab_version = get_gitlab_version() + print(f"GitLab version: {gitlab_version}") + return num_projects, num_users, admin_users_list, free_space_on_disk, total_commits_last_month, projects_with_no_activity, gitlab_version @@ -222,7 +244,7 @@ api_token, confluence_token = load_api_token() subject = 'Monthly GitLab Report - {month} {year}'.format(month=datetime.datetime.now().strftime('%B'), year=datetime.datetime.now().strftime('%Y')) # Get GitLab information -num_projects, num_users, admin_users_list, free_space_on_disk = get_gitlab_info(api_token) +num_projects, num_users, admin_users_list, free_space_on_disk, total_commits_last_month, projects_with_no_activity, gitlab_version = get_gitlab_info(api_token) # Compose the email body as a dictionary email_body = {