Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. #! /bin/sh
  2. #
  3. # Given the types and search patterns in the configuration file, determine
  4. # whether there are any processes or network connections that should
  5. # not be terminated.
  6. #
  7.  
  8. CONF_FILE="/etc/vdr/lifeguard.conf"
  9. DESC="vdr-lifeguard"
  10.  
  11. # without a configuration file, there's no reason not to shutdown
  12. [ -f $CONF_FILE ] || exit 0
  13.  
  14. # read the configuration file line by line
  15. # break on the first match found
  16. REASON=
  17. exec 9<&0 <$CONF_FILE
  18. while read TYPE PATTERN DESCRIPTION REST; do
  19. case "$TYPE" in
  20. ""|\#*)
  21. # empty lines or comments (i.e. lines starting with #)
  22. continue
  23. ;;
  24. usr)
  25. # users logged into the system
  26. [ ${PATTERN:=".*"} ]
  27. if [ $(users | grep -cE "\b$PATTERN\b") -gt 0 ]; then
  28. REASON=${DESCRIPTION:-"active user"}
  29. break
  30. fi
  31. continue
  32. ;;
  33. host)
  34. # specified host alive
  35. #
  36. # NOTE: This is a clutch for several reasons:
  37. # - it would not be necessary if I knew how to detect NFS clients reliably
  38. # - it uses language specific search patterns that will fail with localized versions of ping
  39. # - ping is slow if the specified host does not answer
  40. #
  41. [ ${PATTERN:="."} ]
  42. TEMP=mktemp
  43. ping -c 1 -q $PATTERN >& $TEMP
  44. if [ $(grep -c "unknown host" $TEMP) -eq 0 ] && [ $(grep -e "transmitted.*received" $TEMP | cut -d "," -f 2 | cut -d " " -f 2) -gt 0 ]; then
  45. REASON=${DESCRIPTION:-"host $PATTERN active"}
  46. rm -f $TEMP
  47. break
  48. fi
  49. rm -f $TEMP
  50. continue
  51. ;;
  52. cmd)
  53. # commands with a process ID of their own
  54. if [ "$(pidof $PATTERN)" ]; then
  55. REASON=${DESCRIPTION:-"$PATTERN"}
  56. break
  57. fi
  58. continue
  59. ;;
  60. tcp)
  61. # network connections
  62. # test only the local ports of tcp connections
  63. if [ $(netstat -t | grep -e "^tcp" | grep -ve "CLOSE_WAIT[ \t]*$" | sed -re "s/ +/ /g" | cut -d " " -f 4 | grep -cE ":$PATTERN\b") -gt 0 ]; then
  64. REASON=${DESCRIPTION:-"$PATTERN"}
  65. break
  66. fi
  67. continue
  68. ;;
  69. smb)
  70. # samba locks or shares
  71. case "$TYPE" in
  72. lock*)
  73. if [ $(smbstatus -L | wc -l) -gt 2 ]; then
  74. REASON=${DESCRIPTION:-"Samba locks"}
  75. break 2
  76. fi
  77. continue 2
  78. ;;
  79. share*)
  80. if [ $(smbstatus -S | wc -l) -gt 4 ]; then
  81. REASON=${DESCRIPTION:-"Samba shares"}
  82. break 2
  83. fi
  84. continue 2
  85. ;;
  86. *)
  87. if [ $(smbstatus -S | grep -cE "$PATTERN\b") -gt 0 ]; then
  88. REASON=${DESCRIPTION:-"$PATTERN"}
  89. break 2
  90. fi
  91. continue 2
  92. ;;
  93. esac
  94. continue
  95. ;;
  96. afp)
  97. # Apple file sharing
  98. if [ $(ps ancx -o user,command | grep "afpd$" | sed -e "s/^ \+//g" | grep -cve "^0") -gt 0 ]; then
  99. REASON=${DESCRIPTION:-"AppleShare"}
  100. break
  101. fi
  102. continue
  103. ;;
  104. sh)
  105. # shell scripts
  106. if [ "$(pidof -x $PATTERN)" ]; then
  107. REASON=${DESCRIPTION:-"$PATTERN"}
  108. break
  109. fi
  110. continue
  111. ;;
  112. oth*)
  113. # any other processes
  114. if [ $(ps ax | grep -v "grep" | grep -cE "$PATTERN\b") -gt 0 ]; then
  115. REASON=${DESCRIPTION:-"$PATTERN"}
  116. break
  117. fi
  118. continue
  119. ;;
  120. *)
  121. logger -t "$DESC" "unkown type '$TYPE'"
  122. continue
  123. ;;
  124. esac
  125. done
  126. exec 0<&9 9<&-
  127.  
  128. # if there is a reason not to shutdown, tell VDR about it
  129. if [ "$REASON" ]; then
  130. MESSAGE="$REASON"
  131. # add an explanation to syslog that includes the reason
  132. # logger -t "$DESC" "$MESSAGE"
  133. echo "ABORT_MESSAGE=\"$MESSAGE\""
  134. exit 1
  135. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement