-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcephfs_client_info.sh
More file actions
135 lines (113 loc) Β· 4.25 KB
/
cephfs_client_info.sh
File metadata and controls
135 lines (113 loc) Β· 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash
#Matthew Hutchinson <mhutchinson@45drives.com>
# Generate timestamp (YYYY-MM-DD_HH:MM)
timestamp=$(date +"%Y-%m-%d_%H:%M")
# Output file with timestamp
output_file="cephfs_client_report_${timestamp}.csv"
mount_point="/mnt/cephroot"
ceph_root=":/"
echo "π Preparing environment..."
# Step 1: Ensure /mnt/cephroot exists
if [[ ! -d "$mount_point" ]]; then
echo "π $mount_point does not exist. Creating it..."
mkdir -p "$mount_point"
echo "β
Created $mount_point"
fi
# Step 2: Check if CephFS is mounted
if ! mountpoint -q "$mount_point"; then
echo "π $mount_point is not mounted. Attempting to mount CephFS..."
mount -t ceph "$ceph_root" "$mount_point" -o name=admin
if [[ $? -ne 0 ]]; then
echo "β Failed to mount CephFS at $mount_point"
exit 1
fi
echo "β
Successfully mounted CephFS at $mount_point"
else
echo "β
CephFS is already mounted at $mount_point"
fi
# Step 3: Initialize CSV header
echo '"No","Mount Point Name","Pool Name","Protocol Type","Size","Used Space","Free Space","Client Name","CephFS Path"' > "$output_file"
i=1
declare -A seen_clients # Track unique hostname:path combinations
all_clients=""
echo "π‘ Gathering CephFS client info from all active MDSs..."
# Get all active MDS daemon names
mds_names=$(ceph fs status --format json | jq -r '.mdsmap[] | select(.state=="active") | .name')
if [[ -z "$mds_names" ]]; then
echo "β No active MDS found!"
exit 1
fi
# Collect all client entries from all MDS daemons
for mds in $mds_names; do
echo "πΈ Querying mds.$mds ..."
client_json=$(ceph tell mds."$mds" client ls --format json 2>/dev/null)
if [[ -n "$client_json" && "$client_json" != "null" && "$client_json" != "[]" ]]; then
all_clients+=$(echo "$client_json" | jq -c '.[]')$'\n'
else
echo "β οΈ No valid client data returned from mds.$mds (skipping)."
fi
done
# Human-readable size formatter
human_readable() {
bytes=$1
awk -v b="$bytes" 'function human(x) {
s="B KB MB GB TB PB"
n=split(s,arr)
for (i=n; i>1; i--) {
if (x >= 1024^(i-1)) {
printf "%.1f %s", x/(1024^(i-1)), arr[i]
return
}
}
printf "%d B", x
}
BEGIN {human(b)}'
}
# Deduplicate and write to CSV, skipping entries without client_metadata
echo "$all_clients" | sort | uniq | jq -c 'select(.client_metadata != null)' | while read -r entry; do
hostname=$(echo "$entry" | jq -r '.client_metadata.hostname // "unknown"')
cephfs_path=$(echo "$entry" | jq -r '.client_metadata.root // "/"')
# Build unique key for hostname + path
key="${hostname}:${cephfs_path}"
if [[ -n "${seen_clients["$key"]}" ]]; then
# Already seen, skip
continue
fi
seen_clients["$key"]=1
# Map to local path
local_path="${mount_point}${cephfs_path}"
[[ "$cephfs_path" == "/" ]] && local_path="$mount_point"
# Get real pool name
pool=$(getfattr -n ceph.dir.layout.pool "$local_path" --only-values 2>/dev/null)
if [[ -z "$pool" ]]; then
pool="cephfs"
fi
# Get size details
if [[ -d "$local_path" ]]; then
max_bytes=$(getfattr -n ceph.quota.max_bytes "$local_path" --only-values 2>/dev/null)
rbytes=$(getfattr -n ceph.dir.rbytes "$local_path" --only-values 2>/dev/null)
if [[ -n "$max_bytes" && "$max_bytes" -ne 0 ]]; then
size="$max_bytes"
used="${rbytes:-0}"
free=$((max_bytes - used))
else
df_info=$(df -B1 "$local_path" 2>/dev/null | tail -1)
size=$(echo "$df_info" | awk '{print $2}')
used=$(echo "$df_info" | awk '{print $3}')
free=$(echo "$df_info" | awk '{print $4}')
fi
size_h=$(human_readable "$size")
used_h=$(human_readable "$used")
free_h=$(human_readable "$free")
else
size_h="N/A"
used_h="N/A"
free_h="N/A"
fi
# Mount Point Name: just the last part
mp_name=$(basename "$local_path")
[[ "$mp_name" == "" || "$mp_name" == "/" ]] && mp_name="/"
echo "\"$i\",\"$mp_name\",\"$pool\",\"cephfs\",\"$size_h\",\"$used_h\",\"$free_h\",\"$hostname\",\"$cephfs_path\"" >> "$output_file"
((i++))
done
echo "β
Report generated: $output_file"