]> git.proxmox.com Git - mirror_smartmontools-debian.git/blob - smartd_warning.sh.in
4f9142519641ee1197ca7fba1d1e11da350ced75
[mirror_smartmontools-debian.git] / smartd_warning.sh.in
1 #! /bin/sh
2 #
3 # smartd warning script
4 #
5 # Home page of code is: http://www.smartmontools.org
6 #
7 # Copyright (C) 2012-16 Christian Franke
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2, or (at your option)
12 # any later version.
13 #
14 # You should have received a copy of the GNU General Public License
15 # (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
16 #
17 # $Id: smartd_warning.sh.in 4351 2016-10-17 18:53:40Z chrfranke $
18 #
19
20 set -e
21
22 # Set by config.status
23 @ENABLE_SCRIPTPATH_TRUE@export PATH="@scriptpath@"
24 PACKAGE="@PACKAGE@"
25 VERSION="@VERSION@"
26 prefix="@prefix@"
27 sysconfdir="@sysconfdir@"
28 smartdscriptdir="@smartdscriptdir@"
29
30 # Default mailer
31 os_mailer="@os_mailer@"
32
33 # Plugin directory (disabled if empty)
34 plugindir="@smartdplugindir@"
35
36 # Parse options
37 dryrun=
38 case $1 in
39 --dryrun) dryrun=t; shift ;;
40 esac
41
42 if [ $# != 0 ]; then
43 cat <<EOF
44 smartd $VERSION warning message script
45
46 Usage:
47 export SMARTD_MAILER='Path to external script, empty for "$os_mailer"'
48 export SMARTD_ADDRESS='Space separated mail adresses, empty if none'
49 export SMARTD_MESSAGE='Error Message'
50 export SMARTD_FAILTYPE='Type of failure, "EMailTest" for tests'
51 export SMARTD_TFIRST='Date of first message sent, empty if none'
52 #export SMARTD_TFIRSTEPOCH='time_t format of above'
53 export SMARTD_PREVCNT='Number of previous messages, 0 if none'
54 export SMARTD_NEXTDAYS='Number of days until next message, empty if none'
55 export SMARTD_DEVICEINFO='Device identify information'
56 #export SMARTD_DEVICE='Device name'
57 #export SMARTD_DEVICESTRING='Annotated device name'
58 #export SMARTD_DEVICETYPE='Device type from -d directive, "auto" if none'
59 $0 [--dryrun]
60 EOF
61 exit 1
62 fi
63
64 if [ -z "${SMARTD_ADDRESS}${SMARTD_MAILER}" ]; then
65 echo "$0: SMARTD_ADDRESS or SMARTD_MAILER must be set" >&2
66 exit 1
67 fi
68
69 # Get host and domain names
70 for cmd in @os_hostname@ 'echo "[Unknown]"'; do
71 hostname=`eval $cmd 2>/dev/null` || continue
72 test -n "$hostname" || continue
73 break
74 done
75
76 dnsdomain=${hostname#*.}
77 if [ "$dnsdomain" != "$hostname" ]; then
78 # hostname command printed FQDN
79 hostname=${hostname%%.*}
80 else
81 for cmd in @os_dnsdomainname@ 'echo'; do
82 dnsdomain=`eval $cmd 2>/dev/null` || continue
83 break
84 done
85 test "$dnsdomain" != "(none)" || dnsdomain=
86 fi
87
88 for cmd in @os_nisdomainname@ 'echo'; do
89 nisdomain=`eval $cmd 2>/dev/null` || continue
90 break
91 done
92 test "$nisdomain" != "(none)" || nisdomain=
93
94 # Format subject
95 export SMARTD_SUBJECT="SMART error (${SMARTD_FAILTYPE-[SMARTD_FAILTYPE]}) detected on host: $hostname"
96
97 # Format message
98 fullmessage=`
99 echo "This message was generated by the smartd daemon running on:"
100 echo
101 echo " host name: $hostname"
102 echo " DNS domain: ${dnsdomain:-[Empty]}"
103 test -z "$nisdomain" ||
104 echo " NIS domain: $nisdomain"
105 @OS_WIN32_TRUE@test -z "$USERDOMAIN" ||
106 @OS_WIN32_TRUE@ echo " Win domain: $USERDOMAIN"
107 echo
108 echo "The following warning/error was logged by the smartd daemon:"
109 echo
110 echo "${SMARTD_MESSAGE-[SMARTD_MESSAGE]}"
111 echo
112 echo "Device info:"
113 echo "${SMARTD_DEVICEINFO-[SMARTD_DEVICEINFO]}"
114 echo
115 echo "For details see host's SYSLOG."
116 if [ "$SMARTD_FAILTYPE" != "EmailTest" ]; then
117 echo
118 echo "You can also use the smartctl utility for further investigation."
119 test "$SMARTD_PREVCNT" = "0" ||
120 echo "The original message about this issue was sent at ${SMARTD_TFIRST-[SMARTD_TFIRST]}"
121 case $SMARTD_NEXTDAYS in
122 '') echo "No additional messages about this problem will be sent." ;;
123 1) echo "Another message will be sent in 24 hours if the problem persists." ;;
124 *) echo "Another message will be sent in $SMARTD_NEXTDAYS days if the problem persists." ;;
125 esac
126 fi
127 `
128
129 # Export message with trailing newline
130 export SMARTD_FULLMESSAGE="$fullmessage
131 "
132
133 # Run plugin scripts if requested
134 if test -n "$plugindir"; then
135 case " $SMARTD_ADDRESS" in
136 *\ @*)
137 if [ -n "$dryrun" ]; then
138 echo "export SMARTD_SUBJECT='$SMARTD_SUBJECT'"
139 echo "export SMARTD_FULLMESSAGE='$SMARTD_FULLMESSAGE'"
140 fi
141
142 # Run ALL scripts if requested
143 case " $SMARTD_ADDRESS " in
144 *\ @ALL\ *)
145 for cmd in "$plugindir"/*; do
146 if [ -f "$cmd" ] && [ -x "$cmd" ]; then
147 if [ -n "$dryrun" ]; then
148 echo "$cmd </dev/null"
149 else
150 "$cmd" </dev/null
151 fi
152 fi
153 done
154 ;;
155 esac
156
157 # Run selected scripts
158 addrs=$SMARTD_ADDRESS
159 SMARTD_ADDRESS=
160 for ad in $addrs; do
161 case $ad in
162 @ALL)
163 ;;
164 @?*)
165 cmd="$plugindir/${ad#@}"
166 if [ -f "$cmd" ] && [ -x "$cmd" ]; then
167 if [ -n "$dryrun" ]; then
168 echo "$cmd </dev/null"
169 else
170 "$cmd" </dev/null
171 fi
172 elif [ ! -e "$cmd" ]; then
173 echo "$cmd: Not found" >&2
174 fi
175 ;;
176 *)
177 SMARTD_ADDRESS="${SMARTD_ADDRESS:+ }$ad"
178 ;;
179 esac
180 done
181
182 # Send email to remaining addresses
183 test -n "$SMARTD_ADDRESS" || exit 0
184 ;;
185 esac
186 fi
187
188 # Send mail or run command
189 if [ -n "$SMARTD_ADDRESS" ]; then
190
191 # Send mail, use platform mailer by default
192 test -n "$SMARTD_MAILER" || SMARTD_MAILER=$os_mailer
193 if [ -n "$dryrun" ]; then
194 echo "exec '$SMARTD_MAILER' -s '$SMARTD_SUBJECT' $SMARTD_ADDRESS <<EOF
195 $fullmessage
196 EOF"
197 else
198 exec "$SMARTD_MAILER" -s "$SMARTD_SUBJECT" $SMARTD_ADDRESS <<EOF
199 $fullmessage
200 EOF
201 fi
202
203 elif [ -n "$SMARTD_MAILER" ]; then
204
205 # Run command
206 if [ -n "$dryrun" ]; then
207 echo "export SMARTD_SUBJECT='$SMARTD_SUBJECT'"
208 echo "export SMARTD_FULLMESSAGE='$SMARTD_FULLMESSAGE'"
209 echo "exec '$SMARTD_MAILER' </dev/null"
210 else
211 unset SMARTD_ADDRESS
212 exec "$SMARTD_MAILER" </dev/null
213 fi
214
215 fi