wedged.sh.in
上传用户:weiyuanprp
上传日期:2020-05-20
资源大小:1169k
文件大小:6k
源码类别:

传真(Fax)编程

开发平台:

C/C++

  1. #! @SCRIPT_SH@
  2. # $Id: wedged.sh.in,v 1.5 2008/03/08 18:16:19 faxguy Exp $
  3. #
  4. # HylaFAX Facsimile Software
  5. #
  6. # Copyright (c) 1994-1996 Sam Leffler
  7. # Copyright (c) 1994-1996 Silicon Graphics, Inc.
  8. # HylaFAX is a trademark of Silicon Graphics
  9. # Permission to use, copy, modify, distribute, and sell this software and 
  10. # its documentation for any purpose is hereby granted without fee, provided
  11. # that (i) the above copyright notices and this permission notice appear in
  12. # all copies of the software and related documentation, and (ii) the names of
  13. # Sam Leffler and Silicon Graphics may not be used in any advertising or
  14. # publicity relating to the software without the specific, prior written
  15. # permission of Sam Leffler and Silicon Graphics.
  16. # THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  17. # EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  18. # WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  19. # IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  20. # ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  21. # OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22. # WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  23. # LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  24. # OF THIS SOFTWARE.
  25. #
  26. #
  27. # wedged deviceID device 
  28. #
  29. # Do something when a modem looks irretrievably wedged.
  30. #
  31. if [ $# != 2 ]; then
  32.     echo "Usage: $0 deviceID device"
  33.     exit 1
  34. fi
  35. test -f etc/setup.cache || {
  36.     SPOOL=`pwd`
  37.     cat<<EOF
  38. FATAL ERROR: $SPOOL/etc/setup.cache is missing!
  39. The file $SPOOL/etc/setup.cache is not present.  This
  40. probably means the machine has not been setup using the faxsetup(@MANNUM1_8@)
  41. command.  Read the documentation on setting up HylaFAX before you
  42. startup a server system.
  43. EOF
  44.     exit 1
  45. }
  46. CHARSET=UTF-8 # this really gets set in dictionary
  47. . etc/setup.cache
  48. . bin/common-functions
  49. #
  50. # Redirect errors to a tty, if possible, rather than
  51. # dev-nulling them or allowing them to creep into
  52. # the mail.
  53. #
  54. if $TTYCMD >/dev/null 2>&1; then
  55.     ERRORSTO=`$TTYCMD`
  56. else
  57.     ERRORSTO=/dev/null
  58. fi   
  59. #
  60. # Command line params
  61. #
  62. devID=$1
  63. device=$2
  64. #
  65. # Variables customizable through etc/FaxWedged
  66. #
  67. TOADDR=FaxMaster
  68. FROMADDR=fax
  69. WEDGED_EMAIL_INTERVAL=5 # minutes: at most 1 wedged email every X mins
  70. WEDGED_DISABLE_FAXGETTY= # if set, faxgetty disabled from inittab
  71. if [ -f etc/FaxWedged ]; then
  72.     . etc/FaxWedged
  73. fi
  74. #
  75. # Fetch language settings (after FaxDispatch for customization of $LANG).
  76. #
  77. . bin/dictionary
  78. if [ -f etc/FaxDictionary ]; then
  79.     . etc/FaxDictionary
  80. fi
  81. #
  82. # Internal variables
  83. #
  84. # unix secs since epoch for systems without GNU date +%s extension
  85. unixtime() {
  86.     TZ=GMT date "+%Y %j %H %M %S" | $SED 's/ 0*/ /g' | {
  87.     read year yday hour min sec
  88.     expr  $sec + $min * 60 + $hour * 3600 
  89. + (  ( $year - 1969 ) / 4 
  90.     - ( $year / 100 - 19 ) 
  91.     + ( $year / 400 - 4  ) 
  92.     + $yday - 1 
  93.    ) * 86400 
  94. + ( $year - 1970 ) * 31536000
  95.     }
  96. }
  97. tty=`basename $device`
  98. wedged_last_time=0 # seconds: unix timestamp
  99. wedged_interval=0 # minutes
  100. wedged_log_file="tmp/${devID}_last_wedged_email"
  101. wedged_current_time=`date +%s` # seconds: unix timestamp
  102. # If it's not a number (no GNU date), fallback to internal function
  103. wedged_current_time=`expr "$wedged_current_time" + 0 2>/dev/null || unixtime`
  104. #
  105. # Read last time 'wedged modem' email was sent
  106. #
  107. if [ -r $wedged_log_file ]; then
  108.     # read stored timestamp
  109.     read wedged_last_time < $wedged_log_file 2>/dev/null
  110.     # Set it to 0 if it's not a number
  111.     wedged_last_time=`expr "$wedged_last_time" + 0 2>/dev/null || echo 0`
  112.     # shouldn't happen, just in case...
  113.     if [ $wedged_last_time -gt $wedged_current_time ]; then
  114. wedged_last_time=0
  115.     fi
  116. fi
  117. #
  118. # Minutes since last 'wedged modem' email was sent
  119. #
  120. wedged_interval=`expr ( $wedged_current_time - $wedged_last_time ) / 60`
  121. #
  122. # Send 'wedged modem' email if either is true:
  123. # 1. there's no log file (email was never sent before or someone deleted it)
  124. # 2. email was sent longer than WEDGED_EMAIL_INTERVAL minutes ago
  125. # Cases like 'modem was wedged 1 year ago, and is now again wedged for the
  126. # first time', fall into #1 if someone deleted the log file, into #2 if
  127. # log file was left in place.
  128. #
  129. if [ ! -r $wedged_log_file 
  130. -o $wedged_interval -gt $WEDGED_EMAIL_INTERVAL ]; then
  131.     #
  132.     # Write current timestamp into logfile under tmp/
  133.     #
  134.     if [ $wedged_current_time -gt 0 ]; then
  135. $RM -f $wedged_log_file # symlink? :-)
  136. $CAT<<EOF > $wedged_log_file
  137. $wedged_current_time
  138. This is a temp file written and read by bin/wedged to rate-limit
  139. the emails it sends about the wedged status of device $device.
  140. The first line contains a timestamp for when the last email was sent.
  141. This file is never deleted automatically, there's no need to do it
  142. and it may be useful to know when/if a device had last a problem.
  143. However, you can safely delete it at any time if you wish, it will
  144. be recreated when needed.
  145. last modified: `date`
  146. EOF
  147.     fi
  148.     #
  149.     # Send 'modem is wedged' email
  150.     #
  151.     (echo "To: $TOADDR"
  152.     printf "From: "
  153.     printf "$DICTRECEIVEAGENT" | LANG=C $AWK -f bin/rfc2047-encode.awk -v charset="$CHARSET"
  154.     echo " <$FROMADDR>"
  155.     $CAT<<EOF
  156. Subject: modem on $device appears wedged
  157. The HylaFAX software thinks that there is a problem with the modem
  158. on device $device that needs attention; repeated attempts to
  159. initialize the modem have failed.
  160. Consult the server trace logs for more information on what is happening.
  161. You will be notified again after $WEDGED_EMAIL_INTERVAL minutes if the problem persists.
  162. EOF
  163.     if [ -x etc/resetmodem ]; then
  164. echo "An attempt to reset the modem has been made."
  165. etc/resetmodem $device
  166.     fi
  167.     #
  168.     # Disable faxgetty
  169.     # NB: this is for an System V-style system.
  170.     #
  171.     if [ -f /etc/inittab ] && [ -n "$WEDGED_DISABLE_FAXGETTY" ]; then
  172. ed - /etc/inittab<<EOF
  173. /^[^#].*:respawn:.*faxgetty .*$tty/s/respawn/off/
  174. w
  175. q
  176. EOF
  177. #
  178. # ed doesn't appear to have consistent exit
  179. # status under SysV-style systems (does under BSD);
  180. # this means checking if the above succeeded is
  181. # problematic.
  182. #
  183. if [ $? -ne 0 ] && /bin/kill -1 1; then
  184.     cat<<EOF
  185. The $tty entry in /etc/inittab that spawns faxgetty on $device has
  186. been disabled.  After you have figured out what is wrong you may
  187. want to restart this process.
  188. EOF
  189. fi
  190.     fi
  191.     ) 2>$ERRORSTO | $SENDMAIL -f$FROMADDR -oi $TOADDR
  192. fi
  193. exit 0