a19101da9396f0c47b424725221c901eebe07f77
[mirror/dsa-puppet.git] / modules / bacula / templates / bacula-idle-restart.erb
1 #! /bin/sh -e
2
3 # Written by Tollef Fog Heen <tfheen@err.no>
4 # Based on code by Peter Palfrader
5 #
6 # Restart the given bacula director when it's idle.
7 #
8 # XXX: Add locking
9
10 set -e
11
12 if [ "$1" = "fd" ];then
13     PORT=<%= bacula_client_port %>
14     DIR="bacula-fd"
15 elif [ "$1" = "sd" ]; then
16     PORT=<%= bacula_storage_port %>
17     DIR="bacula-sd"
18 else
19     # Usage
20     echo "$0 [fd|sd]"
21     echo
22     echo "Restart bacula fd or sd when idle"
23     exit 1
24 fi
25
26 # chk_net <port>
27 # returns true (0) if there are connections to that port.
28 chk_net() {
29   local port="$1"; shift
30   local con="$(ss -nt "sport = :$port" | wc -l)"
31   if [ "$con" -gt 1 ]; then
32     return 0
33   else
34     return 1
35   fi
36 }
37
38 while chk_net "$PORT"; do
39     sleep 5
40 done
41
42 service $DIR restart