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