summaryrefslogtreecommitdiffstats
path: root/scripts/gluster.sh
blob: a3ff1867d67659782d90a372d0e02ab7d4ee067a (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#! /bin/bash

. opts.sh

[ $? -ne 0 -o -z "$gpod" ] && { echo "No storage pods are running..." ; exit 1 ; }
[ -z "$1" ] && { echo "Usage: $0 <info|heal|migrate> <vol|all> [src] [dst]" ; exit 1 ; }
action=$1
shift


function info {
    vol=$1

    status=$(gluster volume info "$vol" | grep -P 'Status' | awk '{ print $2 }' | tr -d '\r\n')
    bricks=$(gluster volume info "$vol"  | grep -P 'Number of Bricks' | awk '{ print $NF }' | tr -d '\r\n')
    avail=$(gluster volume status "$vol" detail | grep Brick | wc -l)
    online=$(gluster volume status "$vol" detail | grep Online | grep Y | wc -l)
    
    echo "Volume $vol: $status (Bricks: $bricks, Available: $avail, Online: $online)"
}

function heal {
    vol=$1

    distributed=0
    gluster volume info "$vol" | grep "Type:" | grep -i "Distribute" &> /dev/null
    [ $? -eq 0 ] && distributed=1

    echo "Healing volume $vol"
    echo "-------------------"
    gluster volume heal "$vol" full
    gluster volume heal "$vol" info
    
    if [ $distributed -eq 1 ]; then
        echo "Rebalancing distributed volume $vol"
        gluster volume rebalance "$vol" fix-layout start
    fi
    
    
    gluster volume status "$vol"
}

function migrate {
    vol=$1
    src=$2
    dst=$3
    
    [ -z "$src" -o -z "$dst" ] && { echo "Source and destination servers are required" ; exit 1 ; }

    src_brick=$(gluster volume info $vol | grep -P '^Brick\d+:' | awk '{ print $2 }' | grep -P "^$src" | tr -d '\r\n' )
    dst_brick=${src_brick/$src/$dst}

    [ -z "$src_brick" -o -z "$dst_brick" ] && return 0

    echo "Volume $vol: migrating failed brick"
    echo "             from $src_brick"
    echo "               to $dst_brick"
    echo "Press enter to continue"
    read
    [ $? -ne 0 ] && exit

    gluster volume replace-brick $vol "$src_brick" "$dst_brick"  commit force
    heal $vol
}
    

function transport {
    vol=$1
    transport=${2:-tcp,rdma}
    echo "Changing $vol to transport $transport"
    gluster volume stop "$vol"
    gluster volume set "$vol" config.transport "$transport"
    gluster volume start "$vol"
}



function restart {
    vol=$1

    echo $vol
    bricks=$(gluster volume info "$vol"  | grep -P 'Number of Bricks' | awk '{ print $NF }' | tr -d '\r\n')
    online=$(gluster volume status "$vol" detail | grep Online | grep Y | wc -l)

    if [ "$bricks" -ne "$online" ]; then
        echo "Restarting $vol ($online bricks of $bricks are/is online)"
        gluster --mode=script volume stop "$vol" 
        gluster --mode=script volume start "$vol"
    fi
}    

function delete_failed {
    vol=$1

    bricks=$(gluster volume info "$vol"  | grep -P 'Number of Bricks' | awk '{ print $NF }' | tr -d '\r\n')
    online=$(gluster volume status "$vol" detail | grep Online | grep Y | wc -l)

    if [ "$online" == "0" ]; then
        echo "Deleting $vol ($online bricks of $bricks are/is online)"
#        gluster --mode=script volume stop "$vol" 
        gluster --mode=script volume delete "$vol" 
    fi
}


function lvm_clean {
    used_bricks=`gluster volume info | grep "/brick_" | sed -r  -e 's/.*brick_(.*)\/brick/\1/'`
    
    for ip in $(seq 1 3); do
        echo "Node $ip"
        echo "========"
        lvm_bricks=`node $ip lvscan | grep brick  | sed -r -e 's/.*brick_([0-9a-z]*)[^0-9a-z].*/\1/'`
    
        diff=$(echo $used_bricks $lvm_bricks | tr -d '\r' | tr ' ' '\n' | sort | uniq -u)
        remove=$(echo "$diff $lvm_bricks" | tr -d '\r' | tr ' ' '\n' | sort | uniq -d)

        for id in $remove; do
            echo "Removing ---------------------------------------------"
            node $ip lvs -o name,time,size -S "'name =~ $id'"
            echo "Removing ---------------------------------------------"
            node $ip lvremove -y -S "'name =~ $id'"
        done
    done    
    
    
}

function lvm_remove_today {
    for ip in $(seq 1 3); do
        node $ip hostname
#lvdisplay -o name,time -S 'time since "2018-03-16"'
    done
}


function heketi_cmd {
    heketi "$@"
}

function heketi_clean {
    heketi_vols=`heketi topology info | grep "Name: vol_" | sed -r  -e 's/.*(vol_[0-9a-z]+)\s*$/\1/'`
    gluster_vols=`gluster volume info | grep "Name: vol_" | sed -r  -e 's/.*(vol_[0-9a-z]+)\s*$/\1/'`
    echo $heketi_vols
    
    diff=$(echo $gluster_vols $heketi_vols | tr -d '\r' | tr ' ' '\n' | sort | uniq -u)
    remove=$(echo "$diff $gluster_vols" | tr -d '\r' | tr ' ' '\n' | sort | uniq -d)

    for vol in $remove; do
        echo "Stopping and deleting volume $vol"
        echo "---------------------------------"
        gluster --mode=script volume stop "$vol" 
        gluster --mode=script volume delete "$vol"
    done
}

if [[ "$action" =~ ^heketi ]]; then
    eval "$action" "$@"
elif [[ "$action" =~ ^lvm ]]; then
    eval "$action" "$@"
elif [[ -z "$1" || "$1" =~ ^all ]]; then
    all=0
    [ "$1" == "all_heketi" ] && all=1
    [[ "$1" =~ ^all ]] && shift

    vols=$(gluster volume info | grep -P '^Volume Name' | awk '{ print $NF }' | tr '\r\n' '  ')
    for vol in $vols; do
        if [ $all -eq 0 ]; then
            [[ "$vol" =~ [0-9] ]] && continue
            [[ "$vol" =~ ^vol_ ]] && continue
            [[ "$vol" =~ ^heketi ]] && continue
        fi

        eval "$action" "$vol" "$@"
    done
else
    eval "$action" "$@"
fi