Last active
September 15, 2024 22:00
-
-
Save vadirajks/8c8a53a4f2be2afa4750d50eb64c402e to your computer and use it in GitHub Desktop.
aerospike disk and memory usage in csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
output_csv="aerospike_cluster_stats.csv" | |
# Check if the output CSV exists and if not, write the header | |
if [[ ! -f "$output_csv" ]]; then | |
echo "Node,Namespace,Device Total Bytes,Device Total (MB/GB/TB),Device Used Bytes,Device Used (MB/GB/TB),Device Free %,Device Available %,High Water Disk %,Memory Total Bytes,Memory Total (MB/GB/TB),Memory Used Bytes,Memory Used (MB/GB/TB),Memory Free %,High Water Memory %,Stop Writes %" > "$output_csv" | |
else | |
mv -f "$output_csv" "${output_csv}-$(date +%s)" | |
echo "Node,Namespace,Device Total Bytes,Device Total (MB/GB/TB),Device Used Bytes,Device Used (MB/GB/TB),Device Free %,Device Available %,High Water Disk %,Memory Total Bytes,Memory Total (MB/GB/TB),Memory Used Bytes,Memory Used (MB/GB/TB),Memory Free %,High Water Memory %,Stop Writes %" > "$output_csv" | |
fi | |
# Function to convert bytes to human-readable format | |
convert_bytes() { | |
local bytes=$1 | |
local size | |
if ((bytes >= 1099511627776)); then | |
size=$(echo "scale=2; $bytes / 1099511627776" | bc) # TB | |
echo "${size} TB" | |
elif ((bytes >= 1073741824)); then | |
size=$(echo "scale=2; $bytes / 1073741824" | bc) # GB | |
echo "${size} GB" | |
elif ((bytes >= 1048576)); then | |
size=$(echo "scale=2; $bytes / 1048576" | bc) # MB | |
echo "${size} MB" | |
else | |
size=$(echo "scale=2; $bytes / 1024" | bc) # KB | |
echo "${size} KB" | |
fi | |
} | |
# Loop through each namespace | |
for ns in $(asinfo -lv namespaces); do | |
# Define output CSV file and temporary sorted file | |
temp_csv="aerospike_cluster_stats_sorted_${ns}.csv" | |
rm -f "$temp_csv" | |
ns_filename="/tmp/${ns}.txt" | |
asadm --enable -e "asinfo -v 'namespace/${ns}'" -o "$ns_filename" | |
# Temporary file to store new results before sorting | |
temp_results_file=$(mktemp) | |
# Variables to accumulate sums and track minimums | |
total_device_total_bytes=0 | |
total_device_used_bytes=0 | |
total_memory_total_bytes=0 | |
total_memory_used_bytes=0 | |
total_device_free_pct=0 | |
total_device_available_pct=0 | |
total_memory_free_pct=0 | |
min_high_water_disk_pct=100 | |
min_high_water_memory_pct=100 | |
min_stop_writes_pct=100 | |
num_records=0 | |
# Read the input data file and process each node block | |
while read -r line; do | |
if [[ $line == *"returned:"* ]]; then | |
# Extract node information (e.g., node name and IP) | |
node_info=$(echo "$line" | cut -d ':' -f 1) | |
node=$(echo "$node_info" | cut -d '(' -f 1) | |
fi | |
# Extract and process key metrics | |
device_total_bytes=$(echo "$line" | grep -oP 'device_total_bytes=\K\d+') | |
device_used_bytes=$(echo "$line" | grep -oP 'device_used_bytes=\K\d+') | |
device_free_pct=$(echo "$line" | grep -oP 'device_free_pct=\K\d+') | |
device_available_pct=$(echo "$line" | grep -oP 'device_available_pct=\K\d+') | |
high_water_disk_pct=$(echo "$line" | grep -oP 'high-water-disk-pct=\K\d+') | |
memory_total_bytes=$(echo "$line" | grep -oP 'memory-size=\K\d+') | |
memory_used_bytes=$(echo "$line" | grep -oP 'memory_used_bytes=\K\d+') | |
memory_free_pct=$(echo "$line" | grep -oP 'memory_free_pct=\K\d+') | |
high_water_memory_pct=$(echo "$line" | grep -oP 'high-water-memory-pct=\K\d+') | |
stop_writes_pct=$(echo "$line" | grep -oP 'stop-writes-pct=\K\d+') | |
# Convert bytes to the largest appropriate unit (MB, GB, TB) | |
device_total_readable=$(convert_bytes $device_total_bytes) | |
device_used_readable=$(convert_bytes $device_used_bytes) | |
memory_total_readable=$(convert_bytes $memory_total_bytes) | |
memory_used_readable=$(convert_bytes $memory_used_bytes) | |
# When all fields are non-empty, write to the temporary results file | |
if [[ -n $device_total_bytes && -n $memory_used_bytes ]]; then | |
echo "$node,$ns,$device_total_bytes,$device_total_readable,$device_used_bytes,$device_used_readable,$device_free_pct,$device_available_pct,$high_water_disk_pct,$memory_total_bytes,$memory_total_readable,$memory_used_bytes,$memory_used_readable,$memory_free_pct,$high_water_memory_pct,$stop_writes_pct" >> "$temp_results_file" | |
# Accumulate sums and track minimums | |
total_device_total_bytes=$((total_device_total_bytes + device_total_bytes)) | |
total_device_used_bytes=$((total_device_used_bytes + device_used_bytes)) | |
total_memory_total_bytes=$((total_memory_total_bytes + memory_total_bytes)) | |
total_memory_used_bytes=$((total_memory_used_bytes + memory_used_bytes)) | |
total_device_free_pct=$(echo "$total_device_free_pct + $device_free_pct" | bc) | |
total_device_available_pct=$(echo "$total_device_available_pct + $device_available_pct" | bc) | |
total_memory_free_pct=$(echo "$total_memory_free_pct + $memory_free_pct" | bc) | |
# Update minimum values | |
if (( $(echo "$high_water_disk_pct < $min_high_water_disk_pct" | bc) )); then | |
min_high_water_disk_pct=$high_water_disk_pct | |
fi | |
if (( $(echo "$high_water_memory_pct < $min_high_water_memory_pct" | bc) )); then | |
min_high_water_memory_pct=$high_water_memory_pct | |
fi | |
if (( $(echo "$stop_writes_pct < $min_stop_writes_pct" | bc) )); then | |
min_stop_writes_pct=$stop_writes_pct | |
fi | |
num_records=$((num_records + 1)) | |
fi | |
done < "$ns_filename" | |
# Sort the temporary results based on the "Device Used Bytes" column (5th column) numerically in descending order | |
sort -t',' -k5,5nr "$temp_results_file" > "$temp_csv" | |
# Append the sorted data to the main CSV | |
cat "$temp_csv" >> "$output_csv" | |
# Calculate averages and final row for the namespace | |
if ((num_records > 0)); then | |
device_used_readable=$(convert_bytes $total_device_used_bytes) | |
memory_used_readable=$(convert_bytes $total_memory_used_bytes) | |
device_free_pct=$(echo "scale=2; (100 * ($total_device_total_bytes - $total_device_used_bytes)) / $total_device_total_bytes" | bc) | |
device_available_pct=$(echo "scale=2; $total_device_available_pct / $num_records" | bc) | |
memory_free_pct=$(echo "scale=2; (100 * ($total_memory_total_bytes - $total_memory_used_bytes)) / $total_memory_total_bytes" | bc) | |
high_water_disk_pct=$(echo "$min_high_water_disk_pct" | bc) | |
high_water_memory_pct=$(echo "$min_high_water_memory_pct" | bc) | |
# Append the final aggregated row to the CSV | |
echo ",$ns,$total_device_total_bytes,$(convert_bytes $total_device_total_bytes),$total_device_used_bytes,$device_used_readable,$device_free_pct,$device_available_pct,$high_water_disk_pct,$total_memory_total_bytes,$(convert_bytes $total_memory_total_bytes),$total_memory_used_bytes,$memory_used_readable,$memory_free_pct,$high_water_memory_pct,$min_stop_writes_pct" >> "$output_csv" | |
fi | |
echo >> "$output_csv" | |
# Clean up temporary files | |
rm "$temp_results_file" "$temp_csv" | |
done | |
echo "Data successfully written and sorted to $output_csv" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment