Initial commit

This commit is contained in:
Marc Mance
2024-04-30 15:25:47 -04:00
commit 8ae4a2e656
3 changed files with 444 additions and 0 deletions

92
README.md Normal file
View File

@@ -0,0 +1,92 @@
# collect.zpool.iostat
## Getting started
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
## Add your files
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
```
cd existing_repo
git remote add origin https://gitlab.komputernerds.com/ixsystems/collect.zpool.iostat.git
git branch -M main
git push -uf origin main
```
## Integrate with your tools
- [ ] [Set up project integrations](https://gitlab.komputernerds.com/ixsystems/collect.zpool.iostat/-/settings/integrations)
## Collaborate with your team
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
## Test and Deploy
Use the built-in continuous integration in GitLab.
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.

22
collect.sh Executable file
View File

@@ -0,0 +1,22 @@
#!/bin/bash
greeting="Running perfcollect for $1 minutes"
echo "$greeting"
echo "Will auto upload to iXsystem when completed."
echo "Airgapped Systems will need to exfiltrate /root/*.csv"
python3 ./collect.zpool.iostat.py --minutes $1
start_time=$(date +%s) # Get start time in seconds since epoch
duration=$(($1*60)) # Set duration in seconds (here: 5 minutes)
gstat -C -s -d -o -p -I 60s > /root/gstat.csv & # Run the command in the background
while [[ $(($(date +%s) - $start_time)) -lt $duration ]]; do
sleep 1
done
cp /root/zpool_iostat.csv /root/gstat.csv /var/log
DEBUGFILE=`midclt call system.debug_generate -job` && export DEBUGFILE
curl --user customer:ixcustomer -T $DEBUGFILE ftp.ixsystems.com/debug-`hostname`-`date +%Y%m%d%H%M`.tgz

330
collect.zpool.iostat.py Executable file
View File

@@ -0,0 +1,330 @@
import subprocess
import csv
import os
import time
import re
import argparse
# Configuration parameters
MAX_FILE_SIZE = 1 * 1024 * 1024 * 1024 # 1 GiB in bytes
INTERVAL = 5 # Interval in seconds between zpool iostat commands
COUNT = 1 # Number of samples in each cycle
UPDATE_INTERVAL = 60 # Interval in seconds between updates
OUTPUT_FILE = "/root/zpool_iostat.csv"
# Placeholder for your other functions (load_existing_data, update_json_file, rotate_file_if_needed)
def update_csv_file(output_file, data):
"""
Writes data to a CSV file and rotates the file if necessary.
Parameters:
output_file (str): Path to the CSV file where data will be written.
data (list): The data to be written to the file.
"""
try:
print(f"Attempting to write data to {output_file}...")
with open(output_file, "w", newline="") as csv_file:
fieldnames = data[0].keys() if data else []
csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
csv_writer.writeheader()
for row in data:
csv_writer.writerow(row)
print(f"Data successfully written to {output_file}")
rotate_file_if_needed(output_file)
except Exception as e:
print(f"Error writing to file: {e}")
def rotate_file_if_needed(file_path):
"""
Rotates the file if it exceeds a specified size. The existing file is renamed to '.old'.
Parameters:
file_path (str): Path to the file that should be rotated.
"""
try:
file_size = os.path.getsize(file_path)
if file_size > MAX_FILE_SIZE:
old_file = file_path + ".old"
if os.path.exists(old_file):
print(f"Removing old file: {old_file}")
os.remove(old_file)
print(f"Rotating file: {file_path} to {old_file}")
os.rename(file_path, old_file)
except OSError as e:
print(f"Error during file rotation: {e}")
def load_existing_data(file_path):
"""
Loads existing data from a CSV file.
Parameters:
file_path (str): Path to the CSV file.
Returns:
list: The data loaded from the file, or an empty list if the file doesn't exist or an error occurs.
"""
data = []
try:
if os.path.exists(file_path):
with open(file_path, "r") as file:
print(f"Loading existing data from {file_path}.")
csv_reader = csv.DictReader(file)
for row in csv_reader:
data.append(row)
else:
print(f"No existing file found at {file_path}. Starting fresh.")
except Exception as e:
print(f"Error reading CSV file: {e}. Starting with empty data.")
return data
def remove_k(value_with_unit):
"""
Removes the 'K' unit from a value, if present, from the IOPS section.
Parameters:
value_with_unit (str): The string containing the value and possibly a 'K' unit.
Returns:
float: The numerical value, with 'K' removed if it was present.
"""
if value_with_unit.endswith("K"):
return float(value_with_unit[:-1])
else:
return float(value_with_unit)
def convert_to_mib(value_with_unit):
"""
Converts a value with a unit (K, M, G, T) to MiB.
Parameters:
value_with_unit (str): The string containing the value and unit.
Returns:
float: The value converted to MiB, or 0 if the input is invalid.
"""
# Check if the string is valid
if not value_with_unit or not value_with_unit[-1].isalpha():
return 0.0
# Extract the unit and value
unit = value_with_unit[-1].upper() # Ensure unit is uppercase for consistency
try:
value = float(value_with_unit[:-1])
except ValueError:
return 0.0
# Convert based on the unit
if unit == "K":
return round(value / 1024.0, 1) # Convert KiB to MiB
elif unit == "M":
return round(value, 1) # Already in MiB
elif unit == "G":
return round(value * 1024.0, 1) # Convert GiB to MiB
elif unit == "T":
return round(value * 1024.0 * 1024.0, 1) # Convert TiB to MiB
else:
print(f"Warning: Unrecognized unit '{unit}' in value '{value_with_unit}'.")
return 0.0
def convert_time_to_ms(value_with_unit):
"""
Converts a time value with a unit (ms, us, ns) to milliseconds (ms), rounded to three decimal places.
Returns 0 if the input is '-', indicating no data.
Parameters:
value_with_unit (str): The string containing the value and unit.
Returns:
float: The value converted to milliseconds (ms), rounded to three decimal places, or 0 if the input is invalid or '-'.
"""
if value_with_unit == "-":
# Handle no data case
return 0
try:
# Strip and lower case to normalize
value_with_unit = value_with_unit.strip().lower()
# Check for nanoseconds
if value_with_unit.endswith("ns"):
value = float(value_with_unit[:-2])
return round(value / 1e6, 1) # Convert ns to ms and round
# Check for microseconds
elif value_with_unit.endswith("us"):
value = float(value_with_unit[:-2])
return round(value / 1000.0, 1) # Convert us to ms and round
# Check for milliseconds
elif value_with_unit.endswith("ms"):
value = float(value_with_unit[:-2])
return round(value, 3) # Already in ms and round
# If no known unit, assume it's in milliseconds and round
else:
return round(float(value_with_unit), 1)
except ValueError:
# Log the error if conversion fails
print(f"Warning: Unrecognized or malformed time value '{value_with_unit}'.")
return 0.0
def parse_iostat_output(output):
"""
Parses the output of the `zpool iostat` command to include pool, vdev, disk level data,
and does not specifically skip the 'logs' line.
Parameters:
output (str): The output string from the `zpool iostat` command.
Returns:
list: A list of dictionaries, each representing an entity (pool, vdev, or disk) and its metrics.
"""
parsed_data = []
lines = output.strip().split("\n")
data_started = False
for line in lines:
if "capacity" in line and "operations" in line and "bandwidth" in line:
# This line indicates the end of the headers and the start of relevant data.
data_started = True
continue # Skip the header line itself
if data_started and "-----" in line or line.startswith("pool"):
# Skip separator lines and the column headers repeated in the output.
continue
if data_started:
# Process lines containing pool, vdev, and disk level data.
fields = line.split()
if len(fields) >= 7:
try:
# Check for numeric data in expected positions to avoid conversion errors.
if any(
not field.replace(".", "", 1).isdigit()
and not field.endswith("K")
for field in fields[3:6]
):
# Skip lines that do not contain numeric data where expected.
continue
data_entry = {
"time": time.strftime("%Y-%m-%dT%H:%M:%SZ"),
"name": fields[0],
"alloc": fields[1],
"free": fields[2],
"ops_read": remove_k(fields[3]),
"ops_write": remove_k(fields[4]),
"bandwidth_read": convert_to_mib(fields[5]),
"bandwidth_write": convert_to_mib(fields[6]),
"total_wait_read_ms": convert_time_to_ms(fields[7])
if len(fields) > 7
else 0,
"total_wait_write_ms": convert_time_to_ms(fields[8])
if len(fields) > 8
else 0,
"disk_wait_read_ms": convert_time_to_ms(fields[9])
if len(fields) > 9
else 0,
"disk_wait_write_ms": convert_time_to_ms(fields[10])
if len(fields) > 10
else 0,
"syncq_wait_read_ms": convert_time_to_ms(fields[11])
if len(fields) > 11
else 0,
"syncq_wait_write_ms": convert_time_to_ms(fields[12])
if len(fields) > 12
else 0,
"asyncq_wait_read_ms": convert_time_to_ms(fields[13])
if len(fields) > 13
else 0,
"asyncq_wait_write_ms": convert_time_to_ms(fields[14])
if len(fields) > 14
else 0,
"scrub_wait_ms": convert_time_to_ms(fields[15])
if len(fields) > 15 and fields[15] != "-"
else 0,
"trim_wait_ms": convert_time_to_ms(fields[16])
if len(fields) > 16 and fields[16] != "-"
else 0,
"rebuild_wait_ms": convert_time_to_ms(fields[17])
if len(fields) > 17 and fields[17] != "-"
else 0,
}
parsed_data.append(data_entry)
except Exception as e:
print(f"Error processing line: '{line}' - {e}")
return parsed_data
def run_zpool_iostat(interval, count, output_file, update_interval, TIMETORUN):
"""
Main function to run the zpool iostat command at regular intervals, parse its output, and update the CSV file.
Parameters:
interval (int): Interval in seconds between each command execution.
count (int): Number of samples to collect in each cycle.
output_file (str): Path to the output CSV file.
update_interval (int): Interval in seconds between updates to the CSV file.
"""
while not TIMETORUN == 0:
all_data = load_existing_data(output_file)
for _ in range(count):
command = "zpool iostat -vvyl 15 1"
try:
print("Running zpool iostat command...")
result = subprocess.run(
command, shell=True, capture_output=True, text=True
)
if result.returncode != 0:
print(f"Error running zpool iostat: {result.stderr}")
break
else:
parsed_data = parse_iostat_output(result.stdout)
if parsed_data:
all_data.extend(parsed_data)
else:
print("Error in parsing data. Skipping this sample.")
print("zpool iostat command executed successfully.")
except Exception as e:
print(f"Subprocess error occurred: {e}")
break
print(f"Completed sample {_+1}. Waiting for the next sample...")
time.sleep(interval)
update_csv_file(output_file, all_data)
print(
f"Completed data collection cycle. Waiting for the next update in {update_interval} seconds."
)
time.sleep(update_interval)
TIMETORUN = TIMETORUN - 1
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Collect for X minutes.")
parser.add_argument(
"--minutes", type=int, help="an integer for collected minutes", required=True
)
args = parser.parse_args()
TIMETORUN = args.minutes
print("Script started.")
try:
run_zpool_iostat(INTERVAL, COUNT, OUTPUT_FILE, UPDATE_INTERVAL, TIMETORUN)
except Exception as e:
print(f"An error occurred: {e}")
print("Script ended.")