disk.tree 7.64 KB
Newer Older
1
#!/bin/bash
2
# Copyright (C) 2009-2012 Centro de Computacao Cientifica e Software Livre
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
#
# This file is part of collect-agent
#
# collect-agent is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.

22 23
# Check if df(1) exists before verifying the disks
if ! which df &> /dev/null; then
24 25 26 27 28 29 30
  exit 2
fi

massStorage=""
number=0
disks=""
raid=false
31
ucaClassmate=$(grep "Metasys ClassmatePC" /etc/issue 2> /dev/null)
32

33 34 35 36 37
# This variable is used to grep(1) the model of the storage device. By default,
# it will hold the value "ata". If there is no hard disk but an USB used as root
# mount point, then it will hold the string "usb"
diskType="ata"

38
# If this is a Classmate notebook, find the USB used to mount the root and save
39
# it to storage variable. Also update the value of $diskType
40
if test -n "${ucaClassmate}"; then
41
    label=$(grep "\/ " /etc/fstab | cut -d"=" -f2 | cut -d" " -f1)
42 43
    storage=$(ls -l --time-style="+%F %R" /dev/disk/by-label |
              grep "$label" | cut -d"/" -f3 | sed 's/[0-9]//g')
44
    diskType="usb"
45 46 47
elif test -f "/etc/cmpc-release"; then
    storage=$(ls -l --time-style="+%F %R" /dev/disk/by-label |
              grep "Mdv_Root" | cut -d"/" -f3 | sed 's/[0-9]//g')
48
    diskType="usb"
49 50 51
fi

# If this is a ProInfo Projector, find the USB used to mount the system and
52
# save it to storage variable. Also update the value of $diskType
53 54 55
if test -f "/usr/sbin/projetor"; then
    storage=$(ls -l --time-style="+%F %R" /dev/disk/by-label | grep "conf" |
              cut -d"/" -f3 | sed 's/[0-9]//g')
56
    diskType="usb"
57
fi
58

59
# If the directory "/dev/disk/by-id/" exists, obtain the disks information
60
if test -d "/dev/disk/by-id/"; then
61 62

    # Find every USB connected to the computer
63
    for line in $(ls -l --time-style="+%F %R" /dev/disk/by-id/ | grep "usb" |
64
            awk '{print $10}' | grep "[a-z]$"; ls -l --time-style="+%F %R"\
65
            /dev/cdrom* 2> /dev/null | awk '{print $10}' | grep "[a-z]$"); do
66 67 68 69
        # If it's not the USB used as root by Classmate notebook, nor the USB
        # used by the Projector, then add it to the list to avoid counting it as
        # a disk
        if test "$(basename $line)" != "${storage}"; then
70
            test -n "${massStorage}" && massStorage="${massStorage}|"
71 72
            massStorage="${massStorage}$(basename $line)"
        fi
73
    done
74 75 76

    # If massStorage list isn't empty, add "-v" to grep parameters, to exclude
    # every USB listed here from verification
77
    test -n "${massStorage}" && ParamGrep="-v"
78 79 80 81 82 83 84 85 86 87 88 89 90

    # If there is only USB and no hard disk, remove the first USB found from the
    # exclusion list and consider it a valid disk (for computers which use USB
    # as root disk)
    if test -z "$(ls -l --time-style="+%F %R" /dev/disk/by-id/ |
            egrep ${ParamGrep} "$massStorage" | cut -d"/" -f3 |
            grep "[a-z]$" | sort -u)" -a -z "${storage}"; then
        if test -n "$(echo ${massStorage} | grep '|')"; then
            massStorage="$(echo ${massStorage} | cut -d'|' -f2-)"
        else
            massStorage=""
            ParamGrep=""
        fi
91
        diskType="usb"
92 93 94
    fi

    # If "/proc/mdstat" exists, than check for RAID disks
95 96
    test -f "/proc/mdstat" && raid=true

97 98 99
    # Add scsi to accept machines with only scsi as disk type
    diskType="${diskType}\|scsi"

100 101
    # Verify every disk in "/dev/disk/by-id/", excluding all USB's, unless it's
    # the USB used as root by Classmate notebook
102 103 104 105
    for line in $(ls -l --time-style="+%F %R" /dev/disk/by-id/ |
            egrep ${ParamGrep} "$massStorage" | cut -d"/" -f3 |
            grep "[a-z]$" | sort -u); do

106
        # Enumerate the disks and save the model of each one
107 108
        disks="${disks}<hdd${number}>"
        disks="${disks}<model value=\"$(ls -l --time-style="+%F %R"\
109
        /dev/disk/by-id/ | grep "$line$" | awk '{print $8}' | grep "$diskType" |
110
            head -n 1 | cut -d"-" -f2-)\" type=\"string\"/>"
111

112
        # Multiply the number of blocks by their standard size
113 114
        diskSize=$(($(cat /sys/block/$line/size)*512))

115
        # Size of disk in giga bytes
116 117 118 119
        disks="${disks}<size value=\"$((${diskSize}/1073741824
            ))\" type=\"int\"/>"
        diskUsed=0

120
        # RAID check
121
        if ${raid} && test -n "$(grep "raid" /proc/mdstat)"; then
122 123

            # Check every disk in the RAID
124
            for dev in $(grep "${line}" /proc/mdstat | cut -d" " -f1); do
125 126

                # Obtain the number of disks in the RAID array
127 128
                numDisksArray=$(grep -T1 "${dev}" /proc/mdstat | grep "blocks" |
                    cut -d"[" -f2 | cut -d"/" -f1)
129 130

                # Obtain the RAID type
131
                type=$(grep  "${dev}" /proc/mdstat | cut -d" " -f4)
132 133 134

                # If the disk is being used, obtain the amount used according to
                # the RAID type
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
                used=$(df -kPx swap | grep "$dev" | awk '{print $3}')
                if test -n "${used}"; then
                    case ${type} in
                        raid0)
                            (( used = used/2 ))
                            ;;
                        raid5)
                            (( used = used/${numDisksArray} + ${diskSize}/
                                ${numDisksArray} ))
                            ;;
                        raid6)
                            (( used = used/${numDisksArray} + 2*${diskSize}/
                                ${numDisksArray} ))
                    esac
                    (( diskUsed=${diskUsed}+${used} ))
                fi

            done
153 154

        # There is no RAID
155
        else
156 157
            # If it's a Metasys Classmate notebook, $label will have the USB
            # used as the system root
158
            if test -n "${ucaClassmate}"; then
159 160 161 162
                for used in $(df -kPx swap | grep "${label}" |
                              awk '{print $3}'); do
                    (( diskUsed=${diskUsed}+${used} ))
                done
163 164 165 166 167 168 169 170 171 172

            # If it's a Mandriva Classmate notebook, there is no label, so it
            # makes a grep for the root mount point
            elif test -f "/etc/cmpc-release"; then
                for used in $(df -kPx swap | grep "/" | head -n 1 |
                              awk '{print $3}'); do
                    (( diskUsed=${diskUsed}+${used} ))
                done

            # Else, $line will have the disk name
173 174 175 176 177 178
            else
                for used in $(df -kPx swap | grep "${line}" |
                              awk '{print $3}'); do
                    (( diskUsed=${diskUsed}+${used} ))
                done
            fi
179 180
        fi

181 182
        # Disk used value in GiB scale
        disks="${disks}<used value=\"$((${diskUsed}/1048576))\" type=\"int\"/>"
183 184 185
        disks="${disks}</hdd${number}>"
        (( number=${number}+1 ))
    done
186 187 188 189

    # Print the disk quantity and the details of every disk found (except USB's,
    # unless it's a Classmate notebook or there is no disk - i.e. the system is
    # mounted upon an USB)
190
    printf "<hdd quantity=\"$number\">${disks}</hdd>\n"
191 192

# There is no disk in the local system
193 194 195
else
    printf "<hdd quantity=\"0\"/>\n"
fi