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