Guest User

Untitled

a guest
Nov 23rd, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.49 KB | None | 0 0
  1. #!/bin/bash
  2. # Filename : autoshutdown.sh
  3.  
  4. ######## VARIABLE DEFINITION ########
  5. RESULT=0 # declare reusable RESULT variable to check function return values
  6. ACTIVEIPS="" # declare empty list of active IPs
  7.  
  8. ######## CONSTANT DEFINITION ########
  9. SCANRG="true" # control the rescanning of the defined IP range
  10. # (default="true" is needed for preparation scan on boot up)
  11. OPTSTRING="C:R:bdf:hl:t:r:s:v" # option string for function 'getopt'
  12.  
  13. ######## DEFAULT CONFIGURATION DEFINITION ########
  14. CLASS=192.168.2 # Set the default IP base CLASS.
  15. DEBUG="false" # default status of debug log outputs (default="false")
  16.  
  17. PORT="445" # Set the Ports to watch for connection, separated with space
  18. # default "22 80 445" # SSH, Webinterface and SMB/CIFS-Transfer
  19. # SSH = 22
  20. # HTTP = 80 (Webinterface)
  21. # SMB/CIFS = 445
  22. # If any local IP in $CLASS is connected, FreeNAS is not shut down
  23.  
  24. FREENASIP=192.168.2.15 # Set the IP of Freenas, used for checking connection in with netstat
  25.  
  26. #### Auto-Unrar Check ####
  27. # If you don't know what this is, leave it by default
  28. AUTOUNRARCHECK=false # Check, if auto-unrar is extracting something
  29. # If an extraction-Process is running, the NAS is not shut down
  30. # default="false"
  31. UNRARLOGDIR="/mnt/Data_on_CF/logs" # Set the log-Dir of status.info
  32. UNRARLOG="status.info" # Set the filename of "status.info"
  33.  
  34.  
  35.  
  36. FLAG=5 # Set the number of total failures before shutdown. (default=10)
  37. RESCAN=3 # Number of cycles with full scan activity, before changing to background IP scan (default=10)
  38. BGSCAN=5 # Number of cycles after which one background IP scan is triggered (default=15, ~30 min)
  39. SLEEP=120 # Numbers of seconds between each check/loop. (default=120, 2 min)
  40.  
  41. BOOTUP="false" # activate a boot up phase: no shutdown will be triggered
  42. # until at least one active IP has been found before (default="false)
  43. # WARNING: This may lead to a NAS that will stay on, if no other host
  44. # will show active in the network !
  45.  
  46. RANGE="0" # define a range of IPs which should be scanned, (default="2..254")
  47. # via XXX.XXX.XXX.xxx last triple of IP address in a list
  48. # The following scheme is mandatory
  49. # "v..v+m,w,x..x+m,y+o..y,z"
  50. # define an ip range : <start>..<end> -> the two dots are mandatory
  51. # define a single ip : <ip>
  52. # all list entries seperated by comma ','
  53. # Please make sure to leave 1 and 255 out of the list !
  54.  
  55. LOADPROCNAMES="proftpd,smbd,nfsd,transmission-daemon,mt-daapd"
  56. # command names of processes with load dependent children
  57. # (default="proftpd smbd nfsd transmission-daemon mt-daapd")
  58. TEMPPROCNAMES="in.tftpd"
  59. # command names of processes only started when active
  60. # (default="in.tftpd")
  61.  
  62. LPREPEAT=50 # number of test cycles for finding and active L-Process (default=50)
  63. TPREPEAT=10 # number of test cycles for finding and active T-Process (default=10)
  64.  
  65. CTOPPARAM="-d 1" # define common parameters for the top command line (default="-d 1")
  66. STOPPARAM="-I $CTOPPARAM" # add specific parameters for the top command line (default="-I $CTOPPARAM")
  67.  
  68. SYSLOG="true" # activate write to syslog (default="true")
  69. VERBOSE="true" # use the verbose mode, with additional info on the command line (default="true")
  70. LOGGER="/usr/bin/logger" # path and name of logger (default="/usr/bin/logger")
  71. FACILITY="local6" # facility to log to -> see syslog.conf and add the line (default="local6")
  72. # "local6.* %/var/log/autoshutdown.log"
  73.  
  74. ######## FUNCTION DECLARATION ########
  75.  
  76.  
  77. ################################################################
  78. #
  79. # name : _script_help
  80. # parameter : none
  81. # return : none
  82. #
  83. _script_help()
  84. {
  85. echo "Command: $(basename $0) [-bdhv] [-C nnn.nnn.nnn] [-e mmm,mmm,mmm] [-S sss] [-E eee] [-c UP|DOWN] [-l lproc1,lproc2,lproc3,...] [-t tproc1,tproc2,tproc3,...] [-f nfc] [-r nrc] [-s sleeptime(sec)]
  86. -----------------------------------------------
  87. Command line option details:
  88.  
  89. -b : activate boot up phase (no shutdown until at least one host [IP] has been found active before)
  90. NOTE: If it is a normal state of your network, to have no active hosts [IPs], but the NAS started
  91. this will keep your NAS awake. In this case deactive '-b', as script scans
  92. 'nfc' times for active hosts in any case (see option '-r')
  93. [default: deactivated]
  94.  
  95. -d : activate additional debugging output in log file
  96. [default: deactivated]
  97.  
  98. -h : display this information
  99.  
  100. -v : activate verbose logging, all log informations are also visible
  101. in the terminal
  102.  
  103. -C nnn.nnn.nnn : define CLASS of network (first three triples of IPV4 address) for scanning
  104. with ping, e.g. 192.168.1
  105. [default: 192.168.1]
  106.  
  107. -R s1..s1+n,i1,s2+m..s2,i2,s3..s3+o : define a list of last triples of IPV4 addresses to scan for active hosts; single IPs
  108. or IP ranges can be defined, seperated by commas:
  109.  
  110. IP range : <s[ss]>..<e[ee]>
  111. -> numbers with 1..3 digits are allowed; two dots as range indicator are mandatory
  112.  
  113. single IP: <i[ii]>
  114. -> numbers with 1..3 digits are allowed
  115.  
  116. valid parameter lists:
  117.  
  118. e.g. -R 1..2,3,4..10,11,254..12
  119. e.g. -R 1..254
  120. e.g. -R 10..20,50..100,150..200
  121.  
  122. in valid parameter lists:
  123.  
  124. e.g. -R 1...2,3, 4.1011 254..12 (missing commas will lead to wrong ip ranges)
  125.  
  126. IP ranges are usually scanned from <s[ss]> UP to <e[ee]>, starting at <s[ss]>.
  127. <s[ss]> can be chosen bigger than <e[ee]>, this will lead to a DOWN search, starting at <s[ss]>
  128. [default: 2..254]
  129.  
  130. -l lproc1,lproc2,... : define one or multiple commands of processes to be supervised on activity
  131. all of these commands spawn active 'clones' in 'top' on activity, e.g. smbd
  132. [default: proftpd,smbd,nfsd,transmission-daemon,mt-daapd]
  133.  
  134. -t tproc1,tproc2,... : define one or multiple commands of processes to be supervised on activity
  135. all of these commands are only shown in 'top' on activity, e.g. sshd
  136. [default: sshd,in.tftpd]
  137.  
  138. -f nfc : define number of execution cycles without activity, before shutdown is issued
  139. [default: 10]
  140.  
  141. -r nrc : define number of IP scan cycles without any active host [IP] found, before entering
  142. background scan mode without an active host [IP] present, the script scans
  143. 'nfc' times the bus ('burst scan'). Then scanning on every execution cycle is deactivated.
  144. NOTE: Background IP scanning every x execution cycles is always active.
  145. [default: 10]
  146.  
  147. -s slt (sec) : define how long the script will sleep [seconds], before checking again
  148. [default: 120]
  149. " 1>&2
  150. exit 0;
  151. }
  152.  
  153. ################################################################
  154. #
  155. # name : _log
  156. # parameter : $LOGMESSAGE : logmessage in format "PRIORITY: MESSAGE"
  157. # return : none
  158. #
  159. _log()
  160. {(
  161. [[ "$*" =~ ^([A-Za-z]*):(.*) ]] &&
  162. {
  163. PRIORITY=${BASH_REMATCH[1]}
  164. LOGMESSAGE=${BASH_REMATCH[2]}
  165. [[ "$(basename "$0")" =~ ^(.*)\. ]] && LOGMESSAGE="${BASH_REMATCH[1]}[$$]: $PRIORITY: $LOGMESSAGE";
  166. }
  167.  
  168. if $VERBOSE ; then
  169. # next line only with implementation where logger does not support option '-s'
  170. # echo "$(date '+%b %e %H:%M:%S'):$LOGMESSAGE"
  171.  
  172. [ $SYSLOG ] && $LOGGER -s -t "$(date '+%b %e %H:%M:%S'): $USER" -p $FACILITY.$PRIORITY $LOGMESSAGE
  173.  
  174. else
  175. [ $SYSLOG ] && $LOGGER -p $FACILITY.$PRIORITY $LOGMESSAGE
  176.  
  177. fi # > if [ "$VERBOSE" = "NO" ]; then
  178.  
  179. )}
  180.  
  181. ################################################################
  182. #
  183. # name : _ping_range
  184. # parameter : none
  185. # global return : ACTIVEIPS : list of all active hosts in given IP range, separated by blank
  186. # global return : SCANRG : 0 = at least one active IP found
  187. # return value : CNT : number of active IP hosts within given IP range
  188.  
  189. _ping_range()
  190. {
  191. CNT=0
  192. ACTIVEIPS=""
  193. IPING=
  194.  
  195. # separate the IP end number from the loop counter, to give the user a chance to configure the search "algorithm"
  196. # COUNTUP = 1 means starting at the lowest IP address; COUNTUP=0 will start at the upper end of the given range
  197. if $DEBUG ; then _log "DEBUG: _ping_range(): RANGE: '$RANGE'" ; fi
  198.  
  199. for RG in ${RANGE//,/ } ; do
  200.  
  201. if [[ ! "$RG" =~ \.{2} ]]; then
  202.  
  203. FINIT="J=$RG"
  204. FORCHECK="J<=$RG"
  205. STEP="J++"
  206.  
  207. if $DEBUG ; then _log "DEBUG: _ping_range(): Single Number, RG: '$RG', FINIT: '$FINIT', FORCHECK: '$FORCHECK', STEP: '$STEP'" ; fi
  208.  
  209. elif [[ "$RG" =~ ^([0-9]{1,3})\.{2}([0-9]{1,3}$) ]]; then
  210.  
  211. if $DEBUG ; then _log "DEBUG: _ping_range(): IP Range, RG: '$RG', BASH_REMATCH: '$BASH_REMATCH', BASH_REMATCH[1]: '${BASH_REMATCH[1]}', BASH_REMATCH[2]: '${BASH_REMATCH[2]}'" ; fi
  212.  
  213. if [ ${BASH_REMATCH[2]} -gt ${BASH_REMATCH[1]} ]; then
  214. FINIT="J=${BASH_REMATCH[1]}"
  215. FORCHECK="J<=${BASH_REMATCH[2]}"
  216. STEP="J++"
  217.  
  218. if $DEBUG ; then _log "DEBUG: _ping_range(): START < END, RG: '$RG', FINIT: '$FINIT', FORCHECK: '$FORCHECK', STEP: '$STEP'" ; fi
  219.  
  220. else
  221. FINIT="J=${BASH_REMATCH[1]}"
  222. FORCHECK="J>=${BASH_REMATCH[2]}"
  223. STEP="J--";
  224.  
  225. if $DEBUG ; then _log "DEBUG: _ping_range(): END > START, RG: '$RG', FINIT: '$FINIT', FORCHECK: '$FORCHECK', STEP: '$STEP'" ; fi
  226.  
  227. fi # > if [ ${BASH_REMATCH[2]} -gt ${BASH_REMATCH[1]} ]; then
  228.  
  229. fi # > iif [[ "$RG" =~ [0-9]{1,3} ]]; then
  230.  
  231. for (( $FINIT;$FORCHECK;$STEP )); do
  232.  
  233. # Ping each IP address in turn. if we find one -> CNT ++
  234. # added the redirect to /dev/null, as we are only interested in the return value (no output on console)
  235.  
  236. # Log the IP-adresses to ping only in DEBUG-Mode
  237. if $DEBUG ; then _log "DEBUG: Scan IP $CLASS.$J for active host."; fi
  238.  
  239. ping -c 1 -s 8 -t 1 $CLASS.$J > /dev/null &&
  240. {
  241. # Found one!!
  242. _log "INFO: Found IP $CLASS.$J as active host."
  243. ACTIVEIPS="$ACTIVEIPS$CLASS.$J "
  244. SCANRG="false"
  245. let CNT++;
  246. }
  247.  
  248. done # > for (( J=$iSTART;$FORCHECK;$STEP )); do
  249.  
  250. done # > for RG in ${RANGE//,/ } ; do
  251.  
  252. if $DEBUG ; then _log "DEBUG: _ping_range(): ACTIVEIPS: '$ACTIVEIPS', SCANRG: '$SCANRG', CNT: '$CNT'" ; fi
  253.  
  254.  
  255. return ${CNT};
  256. }
  257.  
  258. ################################################################
  259. #
  260. # name : _ping_active_list
  261. # parameter : ACTIVEIPS : list of all active IPs found
  262. # global return : SCANRG : 0 = no scan needed; 1 = scan of ACTIVEIPS to be done
  263. # return : 1 : if no active host has been found
  264. # : 0 : if at least one active host has been found
  265. #
  266. _ping_active_list()
  267. {
  268. CNT=0
  269. IPING=${iSTART}
  270.  
  271. if $DEBUG ; then _log "DEBUG: _ping_active_list(): Scan IPs ${1} for activity." ; fi
  272.  
  273. for IPADDRESS in "$1"; do
  274.  
  275. # Ping each IP address in parameter list. if we find one -> CNT != 0 we'll
  276. # stop as there's really no point continuing to looking for more.
  277. # added the redirect to /dev/null, as we are only interested in the return value (no output on console)
  278. if [ "$IPADDRESS" != "" ];then
  279.  
  280. if $DEBUG ; then _log "DEBUG: _ping_active_list(): Scan IP ${IPADDRESS} for activity." ; fi
  281.  
  282. ping -c 1 -s 8 -t 1 ${IPADDRESS} > /dev/null
  283. if [ $? -eq 0 ]; then
  284. # Found one!!
  285. _log "INFO: Found IP ${IPADDRESS} still active."
  286. let CNT++
  287. SCANRG="false"
  288. break
  289. else
  290. # mark the list as invalid, as there is at least one obsolete IP in it
  291. _log "INFO: Found IP ${IPADDRESS} inactive; triggered scan of network in next execution"
  292. SCANRG="true"
  293.  
  294. fi # > if [ $? -eq 0 ]; then
  295.  
  296. else
  297. # mark the list as invalid, as there is at least one obsolete IP in it
  298. _log "INFO: Found no valid IP; triggered scan of network in next execution cycle."
  299. SCANRG="true"
  300.  
  301. fi # > if [ "$IPADDRESS" != "" ];then
  302.  
  303. # As 'ping' takes nearly a second, this loop could be very time consuming
  304. # Do a check for some active processes, maybe, one never knows...
  305. # If there is at least one active, we leave this function with a 'bogus find'
  306. _check_processes &&
  307. {
  308. let CNT++
  309. break;
  310. }
  311.  
  312.  
  313. done # > for IPADDRESS in "$1"; do
  314.  
  315. return ${CNT};
  316. }
  317.  
  318. ################################################################
  319. #
  320. # name : _shutdown
  321. # parameter : none
  322. # return : none, script exit point
  323. #
  324. _shutdown()
  325. {
  326. # Goodbye and thanks for all the fish!!
  327. # We've had no responses for the required number of consecutive scans
  328. # defined in FLAG shutdown & power off.
  329. _log "INFO: Shutdown issued: shutdown -p now"
  330. shutdown -p now
  331. exit 0;
  332. }
  333.  
  334. ################################################################
  335. #
  336. # name : _ident_num_proc
  337. # parameter : $1...$(n-1) : parameter for command 'top'
  338. # : $n : search pattern for command 'grep'
  339. # return : none
  340. #
  341. _ident_num_proc()
  342. {
  343. (
  344. # retrieve all function parameters for the top command; as we know the last command line parameter
  345. # is the pattern for the grep command, we can stop one parameter before the end
  346. [[ "$*" =~ (.*)\ (.*)$ ]] &&
  347. {
  348. TPPARAM=${BASH_REMATCH[1]}
  349. GRPPATTERN=${BASH_REMATCH[2]};
  350. }
  351. # _log "DEBUG: _ident_num_proc(): top cmd line: $TPPARAM, grep cmd line: $GRPPATTERN"
  352.  
  353. # call top once, pipe the result to grep and count the number of patterns found
  354. # the number of found processes is returned to the callee
  355. RVALUE=$(top ${TPPARAM} | grep -c ${GRPPATTERN})
  356.  
  357. return $RVALUE
  358. )
  359. }
  360.  
  361. ################################################################
  362. #
  363. # name : _check_processes
  364. # parameter : none
  365. # global return : NUMPROC: number of active processes, defined in LOADPROCNAMES and TEMPPROCNAMES
  366. # return : 1 : if no active process has been found
  367. # : 0 : if at least one active process has been found
  368. #
  369. _check_processes()
  370. {
  371. RVALUE=1
  372. NUMPROC=0
  373. CHECK=0
  374.  
  375. # check for each given command name in LOADPROCNAMES if it is currently stated active in "top"
  376. # i found, that for smbd, proftpd, nsfd, ... there are processes always present in "ps" or "top" output
  377. # this could be due to the "daemon" mechanism... Only chance to identify there is something happening with these
  378. # processes, is to check if "top" states them active -> "-I" parameter on the command line
  379. for LPROCESS in ${LOADPROCNAMES//,/ } ; do
  380. LP=0
  381. IPROC=0
  382. for ((N=0;N < ${LPREPEAT};N++ )) ; do
  383. _ident_num_proc ${STOPPARAM} ${LPROCESS}
  384. RESULT=$?
  385. LP=$(($LP|$RESULT))
  386. [ $RESULT -gt 0 ] && let IPROC++
  387. done
  388.  
  389. let NUMPROC=$NUMPROC+$LP
  390.  
  391. if ! $DEBUG ; then { [ $LP -gt 0 ] && _log "INFO: _check_processes(): Found active process $LPROCESS"; }; fi
  392.  
  393. if $DEBUG ; then _log "DEBUG: _check_processes(): > $LPROCESS: found $IPROC of $LPREPEAT cycles active"; fi
  394.  
  395. done # > LPROCESS in ${LOADPROCNAMES//,/ } ; do
  396.  
  397. if ! $DEBUG ; then { [ $NUMPROC -gt 0 ] && _log "INFO: Found $NUMPROC active processes in $LOADPROCNAMES" ; }; fi
  398.  
  399.  
  400. # check for each given command name in TEMPPROCNAMES if it is currently stated present in "top"
  401. # i found, that for sshd, ... there are processes only present in "ps" or "top" output when is is used.
  402. # it can not be guaranteed, that top states these services active, as they usually wait for user input
  403. # no "-I" parameter on the command line, but shear presence is enough
  404. for TPROCESS in ${TEMPPROCNAMES//,/ } ; do
  405. TP=0
  406. IPROC=0
  407. for ((N=0;N < ${TPREPEAT};N++ )) ; do
  408. _ident_num_proc ${CTOPPARAM} ${TPROCESS}
  409. RESULT=$?
  410. TP=$(($TP|$RESULT))
  411. [ $RESULT -gt 0 ] && let IPROC++
  412. done
  413.  
  414. let CHECK=$CHECK+$TP
  415.  
  416. if ! $DEBUG ; then { [ $TP -gt 0 ] && _log "INFO: _check_processes(): Found active process $TPROCESS"; }; fi
  417.  
  418. if $DEBUG ; then _log "DEBUG: _check_processes(): > $TPROCESS: found $IPROC of $TPREPEAT cycles active"; fi
  419.  
  420. done # > for TPROCESS in ${TEMPPROCNAMES//,/ } ; do
  421.  
  422. if ! $DEBUG ; then { [ $CHECK -gt 0 ] &&_log "INFO: Found $CHECK active processes in $TEMPPROCNAMES" ; }; fi
  423.  
  424.  
  425. # Check if there are any connection on $PORT
  426.  
  427. for CHECKPORT in $(echo $PORT); do
  428.  
  429. # Check how many IPs are connected
  430. NROFCON=$(netstat -an | grep $CLASS | grep "ESTABLISHED" | grep $FREENASIP.$CHECKPORT | wc -l)
  431.  
  432. # Check which IP is connected on the specified Port
  433. CONIP=$(netstat -an | grep $CLASS | grep "ESTABLISHED" | grep $FREENASIP.$CHECKPORT | awk '{print $5}' | sed 's/\.[0-9]*$//g' | uniq)
  434.  
  435. if [ $NROFCON -gt 0 ]; then {
  436. let CHECK++
  437.  
  438. # Set PORTPROTOCOLL
  439. case $CHECKPORT in
  440. 80)
  441. PORTPROTOCOL="HTTP" ;;
  442. 22)
  443. PORTPROTOCOL="SSH" ;;
  444. 21)
  445. PORTPROTOCOL="FTP" ;;
  446. 8080)
  447. PORTPROTOCOL="HTTP" ;;
  448. 445)
  449. PORTPROTOCOL="SMB/CIFS" ;;
  450. *)
  451. PORTPROTOCOL="unknown" ;;
  452. esac
  453.  
  454. _log "INFO: Found active Connection on Port $CHECKPORT ($PORTPROTOCOL) from $CONIP"
  455.  
  456. if $DEBUG ; then {
  457. _log "DEBUG: check Port: $NROFCON Connections active.";
  458. _log "DEBUG: NROFCON: $NROFCON ";
  459. _log "DEBUG: CONIP: $CONIP ";
  460. }
  461. fi
  462. }
  463. fi
  464. done
  465.  
  466. if $DEBUG ; then _log "DEBUG: cat $UNRARLOGDIR/$UNRARLOG = $(cat $UNRARLOGDIR/$UNRARLOG)"; fi
  467.  
  468. UNRARCHECK=0
  469. if [ $AUTOUNRARCHECK = "true" ]; then
  470. if $DEBUG ; then _log "DEBUG: AUTOUNRARCHECK is running now" ; fi
  471. if [ -f $UNRARLOGDIR/$UNRARLOG ]; then
  472. if [ $(cat "$UNRARLOGDIR/$UNRARLOG") = "processing job" ]; then
  473. _log "INFO: unrar-script running - no shutdown."
  474. UNRARCHECK=1
  475. fi
  476. fi
  477. fi
  478.  
  479.  
  480.  
  481. let NUMPROC=$NUMPROC+$CHECK
  482.  
  483. if $DEBUG ; then
  484. _log "DEBUG: _check_processes(): $NUMPROC process(es) active."
  485. _log "DEBUG: UNRARCHECK: $UNRARCHECK"
  486. fi
  487.  
  488. [[ $NUMPROC -gt 0 ]] || [[ $UNRARCHECK -gt 0 ]] && RVALUE=0
  489.  
  490. # only return we found a process or unrar running
  491. return $RVALUE
  492.  
  493. }
  494.  
  495.  
  496. ######## START OF BODY FUNCTION SCRIPT AUTOSHUTDOWN.SH ########
  497.  
  498. # check command line options, on hold as getopt will be available in upcoming release
  499. #
  500. ARGS=`getopt "$OPTSTRING" "$*"`
  501. if [ $? -ne 0 ] ; then
  502. echo "Command: $(basename $0) [-bdhv] [-C nnn.nnn.nnn] [-R s1..s1+n,i1,s2+m..s2,i2,s3..s3+o] [-f nfc] [-l lproc1,lproc2,lproc3,...] [-t tproc1,tproc2,tproc3,...] [-r nrc] [-s slt (sec)] " 1>&2
  503. exit 2
  504. fi
  505.  
  506. # decoding of command line parameters
  507. #
  508. set -- $ARGS
  509.  
  510. for I do
  511. case "$1" in
  512. -C) shift; CLASS="$1"; [[ "$CLASS" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] || { echo " -C $1 : Invalid parameter format; [-C nnn.nnn.nnn]" 1>&2; exit 1; };;
  513. -R) shift; RANGE="$1"; [[ "$RANGE" =~ ^[[0-9]{1,3}|[0-9]{1,3}\.{2}[0-9]{1,3}\,]$ ]] || { echo " -R $1 : Invalid parameter list format; [-R v..v+n,w,x+m..x,y,z..z+o]" 1>&2; exit 1; };;
  514. -b) BOOTUP="true";;
  515. -d) DEBUG="true";;
  516. -f) shift; FLAG="$1"; [[ "$FLAG" =~ ^[0-9]{1,3}$ ]] || { echo " -f $1 : Invalid parameter format; [-f nfc]" 1>&2; exit 1; };;
  517. -l) shift; LOADPROCNAMES="$1"; [[ "$LOADPROCNAMES" =~ ^[[A-Za-z0-9_]\,]$ ]] || { echo " -l $1 : Invalid parameter list format; [-l lproc1,lproc2,lproc3,...]" 1>&2; exit 1; };;
  518. -r) shift; RESCAN="$1"; [[ "$RESCAN" =~ ^[0-9]{1,3}$ ]] || { echo " -r $1 : Invalid parameter format; [-r nrc]" 1>&2; exit 1; };;
  519. -s) shift; SLEEP="$1"; [[ "$SLEEP" =~ ^[0-9]{1,3}$ ]] || { echo " -s $1 : Invalid parameter format; [-s slt (sec)]" 1>&2; exit 1; };;
  520. -t) shift; TEMPPROCNAMES="$1"; [[ "$TEMPPROCNAMES" =~ ^[[A-Za-z0-9_]\,]$ ]] || { echo " -t $1 : Invalid parameter list format; [-t tproc1,tproc2,tproc3,...]" 1>&2; exit 1; };;
  521. -v) VERBOSE="true";;
  522. -h) _script_help;;
  523.  
  524. --) shift; break ;;
  525.  
  526. esac # > case "$1" in
  527. shift
  528.  
  529. done # > for I do
  530.  
  531. if $DEBUG ; then
  532. _log "DEBUG: ARGS: $ARGS "
  533. _log "DEBUG: CLASS: $CLASS "
  534. _log "DEBUG: RANGE: $RANGE "
  535. _log "DEBUG: BOOTUP: $BOOTUP "
  536. _log "DEBUG: FLAG: $FLAG "
  537. _log "DEBUG: LOADPROCNAMES: $LOADPROCNAMES "
  538. _log "DEBUG: RESCAN: $RESCAN "
  539. _log "DEBUG: SLEEP: $SLEEP "
  540. _log "DEBUG: TEMPPROCNAMES: $TEMPPROCNAMES "
  541. _log "DEBUG: VERBOSE: $VERBOSE "
  542. _log "DEBUG: PORT: $PORT "
  543. _log "DEBUG: AUTOUNRARCHECK: $AUTOUNRARCHECK";
  544. _log "DEBUG: UNRARLOG: $UNRARLOG";
  545. _log "DEBUG: UNRARLOGDIR: $UNRARLOGDIR";
  546. #exit 42;
  547. fi # > if $DEBUG ;then
  548.  
  549. # Init the counters
  550. FCNT=$FLAG
  551. SCNCNT=$RESCAN
  552. BGSCNCNT=$BGSCAN
  553.  
  554. # functional start of script
  555. _log "INFO:-----------------------------script started--------------------------------------"
  556. _log "INFO: ${FLAG} test cycles until shutdown is issued."
  557. _log "INFO: network range is given within \"$RANGE\"."
  558. _log "INFO: Boot up phase is $(if $BOOTUP ; then echo "ACTIVE"; else echo "DISABLED"; fi)"
  559.  
  560.  
  561. while : ; do
  562. _log "INFO:------------------------------------------------------"
  563. _log "INFO: new supervision cycle started."
  564. # Main loop, just keep pinging and checking for processes, to decide whether we can shutdown or not...
  565.  
  566. # Counter to implement maximum $RESCAN number of execution cycles with active IP scanning; then only the
  567. # IP scan in background once every $BGSCAN cycles is active to reduce network traffic
  568. #
  569. if [ $SCNCNT -gt 0 ] ; then
  570.  
  571. if $DEBUG ; then _log "DEBUG: main: SCNCNT is > 0, SCNCNT: '$SCNCNT', SCANRG: '$SCANRG'" ; fi
  572.  
  573. # decrease counter
  574. if $SCANRG ; then if $BOOTUP ;then : ; else let SCNCNT--; fi ; fi
  575.  
  576. else
  577. SCANRG="false" # suppress scanning always
  578. if $DEBUG ; then _log "DEBUG: main: SCNCNT is 0, SCANRG: '$SCANRG'" ; fi
  579.  
  580.  
  581. fi # > if [ $SCNCNT -gt 0 ] ; then
  582.  
  583. # IP scan in background once every $BGSCAN cycles; the background scan also allows to identify
  584. # new hosts [IPs] and add them to the $ACTIVEIPS list of active IPs
  585. #
  586. if [ $BGSCNCNT -gt 0 ] ; then
  587.  
  588. if $DEBUG ; then _log "DEBUG: main: BGSCNCNT is > 0, BGSCNCNT: '$BGSCNCNT', SCANRG: '$SCANRG'" ; fi
  589.  
  590. let BGSCNCNT--
  591.  
  592. else
  593.  
  594. BGSCNCNT=$BGSCAN # reset the counter, to start again
  595. SCANRG="true" # scan once
  596.  
  597. if $DEBUG ; then _log "DEBUG: main: BGSCNCNT has been 0, BGSCNCNT: '$BGSCNCNT', SCANRG: '$SCANRG'" ; fi
  598.  
  599. fi # > if [ $BGSCNCNT -gt 0 ] ; then
  600.  
  601. _log "INFO: Current IP scan mode: $(if ( $BOOTUP || $SCANRG ); then echo "FULL"; else echo "BACKGROUND"; fi)"
  602.  
  603.  
  604. if $SCANRG -o $BOOTUP ; then
  605. # retrieve all currently active IPs in your network
  606. # if there has none active found try again after $SLEEP seconds in next execution cycle
  607.  
  608. _log "INFO: retrieve list of active IPs..."
  609. if ! $DEBUG ; then _log "INFO: IPs not shown, use \"autoshutdown.sh -d\" for more Info"; fi
  610.  
  611. # if there have been active IPs found, there is no need to scan in next execution cycle
  612. _ping_range || { BOOTUP="false"; SCNCNT=$RESCAN; }
  613. RESULT=$?
  614.  
  615. if $DEBUG ; then
  616.  
  617. for IP in $ACTIVEIPS ; do
  618. _log "DEBUG: > IP ${IP} currently active."
  619. done
  620.  
  621. _log "DEBUG: Variable SCANRG: '$SCANRG', BOOTUP: '$BOOTUP', SCNCNT: '$SCNCNT'"
  622.  
  623. SCANRG="false"
  624.  
  625. fi # > if $DEBUG ; then
  626.  
  627. fi # > if $SCANRG -o $BOOTUP ; then
  628.  
  629. # search strategy; 1st ping host adresses
  630. # check for processes in between each 'ping'
  631.  
  632. _log "INFO: check number of active hosts in configured network range..."
  633.  
  634. if _ping_active_list $ACTIVEIPS ; then
  635. # check if we are still in the boot up phase (no shutdown until we have found our first active IP)
  636. if $BOOTUP ; then
  637. _log "INFO: Boot up phase is still ACTIVE"
  638. else
  639. if $DEBUG ; then _log "DEBUG: Boot up phase is DISABLED" ; fi
  640.  
  641. # Nothing found so sub one from the count and check if we can shutdown yet.
  642. let FCNT--
  643.  
  644. _log "INFO: No active processes, unraring or hosts within network range, ${FCNT} cycles until shutdown..."
  645.  
  646. if [ $FCNT -eq 0 ]; then
  647. _shutdown;
  648. fi # > if [ $FCNT -eq 0 ]; then
  649. fi # > if $BOOTUP ; then : ; else
  650. else
  651. # Live IP found so reset count
  652. FCNT=${FLAG};
  653. fi # > if { _ping_active_list $ACTIVEIPS }, then
  654.  
  655. # Wait for the required time before checking again.
  656. _log "INFO: sleep for ${SLEEP}s."
  657. sleep $SLEEP;
  658.  
  659. done # > while : ; do
  660.  
  661. #EOF####### END OF SCRIPT AUTOSHUTDOWN.SH ########
Add Comment
Please, Sign In to add comment