-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinux_check_disk_health.sh
executable file
·197 lines (171 loc) · 4.28 KB
/
linux_check_disk_health.sh
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/bin/bash
#
# Disk Drive Health Check
#
# Uses smartctl to check physical disk health.
#
# Supports:
# Debian-All
# RHEL-All
# Arch
#
# Category: Hardware
#
# @LICENSE AGPLv3
# @AUTHOR Charlie Powell <[email protected]>
# @TRMM-TIMEOUT 120
#
function usage() {
cat >&2 <<EOD
Usage: $0
Uses smartctl to check physical disk health.
EOD
exit 1
}
# Parse arguments
while [ "$#" -gt 0 ]; do
case "$1" in
-h|--help) usage;;
esac
done
##
# Check if the OS is "like" a certain type
#
# ie: "ubuntu" will be like "debian"
function os_like_debian() {
if [ -f '/etc/os-release' ]; then
ID="$(egrep '^ID=' /etc/os-release | sed 's:ID=::')"
LIKE="$(egrep '^ID_LIKE=' /etc/os-release | sed 's:ID_LIKE=::')"
if [[ "$LIKE" =~ 'debian' ]]; then echo 1; return; fi
if [[ "$LIKE" =~ 'ubuntu' ]]; then echo 1; return; fi
if [ "$ID" == 'debian' ]; then echo 1; return; fi
if [ "$ID" == 'ubuntu' ]; then echo 1; return; fi
fi
echo 0
}
##
# Check if the OS is "like" a certain type
#
# ie: "ubuntu" will be like "debian"
function os_like_ubuntu() {
if [ -f '/etc/os-release' ]; then
ID="$(egrep '^ID=' /etc/os-release | sed 's:ID=::')"
LIKE="$(egrep '^ID_LIKE=' /etc/os-release | sed 's:ID_LIKE=::')"
if [[ "$LIKE" =~ 'ubuntu' ]]; then echo 1; return; fi
if [ "$ID" == 'ubuntu' ]; then echo 1; return; fi
fi
echo 0
}
##
# Check if the OS is "like" a certain type
#
# ie: "ubuntu" will be like "debian"
function os_like_rhel() {
if [ -f '/etc/os-release' ]; then
ID="$(egrep '^ID=' /etc/os-release | sed 's:ID=::')"
LIKE="$(egrep '^ID_LIKE=' /etc/os-release | sed 's:ID_LIKE=::')"
if [[ "$LIKE" =~ 'rhel' ]]; then echo 1; return; fi
if [[ "$LIKE" =~ 'fedora' ]]; then echo 1; return; fi
if [[ "$LIKE" =~ 'centos' ]]; then echo 1; return; fi
if [ "$ID" == 'rhel' ]; then echo 1; return; fi
if [ "$ID" == 'fedora' ]; then echo 1; return; fi
if [ "$ID" == 'centos' ]; then echo 1; return; fi
fi
echo 0
}
##
# Check if the OS is "like" a certain type
#
# ie: "ubuntu" will be like "debian"
function os_like_suse() {
if [ -f '/etc/os-release' ]; then
ID="$(egrep '^ID=' /etc/os-release | sed 's:ID=::')"
LIKE="$(egrep '^ID_LIKE=' /etc/os-release | sed 's:ID_LIKE=::')"
if [[ "$LIKE" =~ 'suse' ]]; then echo 1; return; fi
if [ "$ID" == 'suse' ]; then echo 1; return; fi
fi
echo 0
}
##
# Check if the OS is "like" a certain type
#
# ie: "ubuntu" will be like "debian"
function os_like_arch() {
if [ -f '/etc/os-release' ]; then
ID="$(egrep '^ID=' /etc/os-release | sed 's:ID=::')"
LIKE="$(egrep '^ID_LIKE=' /etc/os-release | sed 's:ID_LIKE=::')"
if [[ "$LIKE" =~ 'arch' ]]; then echo 1; return; fi
if [ "$ID" == 'arch' ]; then echo 1; return; fi
fi
echo 0
}
##
# Check if the OS is "like" a certain type
#
# ie: "ubuntu" will be like "debian"
function os_like_bsd() {
if [ "$(uname -s)" == 'FreeBSD' ]; then
echo 1
else
echo 0
fi
}
##
# Check if the OS is "like" a certain type
#
# ie: "ubuntu" will be like "debian"
function os_like_macos() {
if [ "$(uname -s)" == 'Darwin' ]; then
echo 1
else
echo 0
fi
}
##
# Install a package with the system's package manager.
#
# Uses Redhat's yum, Debian's apt-get, and SuSE's zypper.
#
# Usage:
#
# ```syntax-shell
# package_install apache2 php7.0 mariadb-server
# ```
#
# @param $1..$N string
# Package, (or packages), to install. Accepts multiple packages at once.
#
function package_install (){
echo "package_install: Installing $*..."
TYPE_BSD="$(os_like_bsd)"
TYPE_DEBIAN="$(os_like_debian)"
TYPE_RHEL="$(os_like_rhel)"
TYPE_ARCH="$(os_like_arch)"
TYPE_SUSE="$(os_like_suse)"
if [ "$TYPE_BSD" == 1 ]; then
pkg install -y $*
elif [ "$TYPE_DEBIAN" == 1 ]; then
apt-get -o Dpkg::Options::="--force-confold" -o Dpkg::Options::="--force-confdef" install -y $*
elif [ "$TYPE_RHEL" == 1 ]; then
yum install -y $*
elif [ "$TYPE_ARCH" == 1 ]; then
pacman -Syu --noconfirm $*
elif [ "$TYPE_SUSE" == 1 ]; then
zypper install -y $*
else
echo 'package_install: Unsupported or unknown OS' >&2
echo 'Please report this at https://github.com/cdp1337/ScriptsCollection/issues' >&2
exit 1
fi
}
if [ -z "$(which smartmontools)" ]; then
package_install smartmontools
fi
EXIT=0
for DISK in $(sudo smartctl --scan | cut -d ' ' -f1); do
sudo smartctl -H $DISK
if [ $? -ne 0 ]; then
EXIT=1
fi
done
exit $EXIT