summaryrefslogtreecommitdiffstats
path: root/tools/gfind_missing_files/gfind_missing_files.sh
blob: f42fe7b05af4d8496fa8fa18e479cd85504d332a (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
#!/bin/sh

##  Copyright (c) 2015 Red Hat, Inc. <http://www.redhat.com/>
##  This file is part of GlusterFS.
##
##  This file is licensed to you under your choice of the GNU Lesser
##  General Public License, version 3 or any later version (LGPLv3 or
##  later), or the GNU General Public License, version 2 (GPLv2), in all
##  cases as published by the Free Software Foundation.

BRICKPATH=    #Brick path of gluster volume
SLAVEHOST=    #Slave hostname
SLAVEVOL=     #Slave volume
SLAVEMNT=     #Slave gluster volume mount point
WORKERS=4     #Default number of worker threads

out()
{
    echo "$@";
}

fatal()
{
    out FATAL "$@";
    exit 1
}

ping_host ()
{
    ### Use bash internal socket support
    {
        exec 400<>/dev/tcp/$1/$2
        if [ $? -ne '0' ]; then
            return 1;
        else
            exec 400>&-
            return 0;
        fi
    } 1>&2 2>/dev/null
}

mount_slave()
{
    local i; # inode number
    SSH_PORT=22

    SLAVEMNT=`mktemp -d`
    [ "x$SLAVEMNT" = "x" ] && fatal "Could not mktemp directory";
    [ -d "$SLAVEMNT" ] || fatal "$SLAVEMNT not a directory";

    ping_host ${SLAVEHOST} $SSH_PORT
    if [ $? -ne 0 ]; then
        echo "$SLAVEHOST not reachable.";
        exit 1;
    fi;

    glusterfs --volfile-id=$SLAVEVOL --aux-gfid-mount --volfile-server=$SLAVEHOST $SLAVEMNT;
    i=$(stat -c '%i' $SLAVEMNT);
    [ "x$i" = "x1" ] || fatal "Could not mount volume $2 on $SLAVEMNT Please check host and volume exists";
}

parse_cli()
{
    if [[ $# -ne 4 ]]; then
        echo "Usage: gfind_missing_files <brick-path> <slave-host> <slave-vol> <OUTFILE>"
        exit 1
    else
        BRICKPATH=$1;
        SLAVEHOST=$2;
        SLAVEVOL=$3;
        OUTFILE=$4;

        mount_slave;
        echo "Slave volume is mounted at ${SLAVEMNT}"
        echo
    fi
}

main()
{
    parse_cli "$@";

    echo "Calling crawler...";
    path=$(readlink -e $0)
    $(dirname $path)/gcrawler ${BRICKPATH} ${SLAVEMNT} ${WORKERS} > ${OUTFILE}

    #Clean up the mount
    umount $SLAVEMNT;
    rmdir $SLAVEMNT;

    echo "Crawl Complete."
    num_files_missing=$(wc -l ${OUTFILE} | awk '{print $1}')
    if [ $num_files_missing -eq 0 ]
    then
        echo "Total Missing File Count : 0"
        exit 0;
    fi

    echo "gfids of skipped files are available in the file ${OUTFILE}"
    echo
    echo "Starting gfid to path conversion"

    #Call python script to convert gfids to full pathname
    INFILE=$(readlink -e ${OUTFILE})
    python $(dirname $path)/gfid_to_path.py ${BRICKPATH} ${INFILE} 1> ${OUTFILE}_pathnames 2> ${OUTFILE}_gfids
    echo "Path names of skipped files are available in the file ${OUTFILE}_pathnames"

    gfid_to_path_failures=$(wc -l ${OUTFILE}_gfids | awk '{print $1}')
    if [ $gfid_to_path_failures -gt 0 ]
    then
       echo "WARNING: Unable to convert some GFIDs to Paths, GFIDs logged to ${OUTFILE}_gfids"
       echo "Use $(dirname $path)/gfid_to_path.sh <brick-path> ${OUTFILE}_gfids to convert those GFIDs to Path"
    fi

    #Output
    echo "Total Missing File Count : $(wc -l ${OUTFILE} | awk '{print $1}')"
}

main "$@";