summaryrefslogtreecommitdiff
path: root/etc/common
blob: 4a634929c8d78435c74f687eff9736de69195987 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Little common functions

# push a mirror attached to us.
# Arguments:
# $1 - Name for the mirror, also basename for the logfile
# $2 - Hostname to push to
# $3 - Username there
# $4 - Protocol version, either 1 or 2.
# $5 - the ssh private key file to use for this push
# $6 - any other option ssh accepts, passed blindly, be careful
#
# This function assumes that the variable LOG is set to a directory where
# logfiles can be written to.
# Pushes will be done in background.
signal () {
	if [ $# -lt 5 ]; then
		echo "Called with only $# parameters, expect at least 5"
		return 2
	fi

	# Defaults we always want, no matter what
	SSH_OPTIONS="-o BatchMode=yes -o SetupTimeOut=45 -o ConnectTimeout=45 -o PasswordAuthentication=no"

	if [ $# -eq 6 ]; then
		# The sixth sense^Wparameter, add it
		SSH_OPTIONS="$SSH_OPTIONS $6"
	fi

	# Finally call ssh
	date -u >> ${LOGDIR}/$1.log
	ssh $SSH_OPTIONS -i "$5" -o"user $3" -$4 "$2" sleep 1 >>${LOGDIR}/$1.log 2>&1 &
}

# callback, used by ftpsync
callback () {
	# Defaults we always want, no matter what
	SSH_OPTIONS="-o BatchMode=yes -o SetupTimeOut=45 -o ConnectTimeout=45 -o PasswordAuthentication=no"
	ssh $SSH_OPTIONS -i "$3" -o"user $1" "$2" callback:${HOSTNAME}
}

# log something (basically echo it together with a timestamp)
#
# Set $PROGRAM to a string to have it added to the output.
log () {
	if [ -z ${PROGRAM} ]; then
		echo "$(date +"%b %d %H:%M:%S") $(hostname -s) [$$] $@"
	else
		echo "$(date +"%b %d %H:%M:%S") $(hostname -s) ${PROGRAM}[$$]: $@"
	fi
}

# log the message using log() but then also send a mail
# to the address configured in MAILTO (if non-empty)
error () {
	log "$@"
	if [ -z ${MAILTO} ]; then
		echo "$@" | mail -e -s "$PROGRAM ERROR ($(hostname -s)) [$$]" ${MAILTO}
	fi
}