summaryrefslogtreecommitdiffstats
path: root/core/modules/slxlog/data/opt/openslx/bin/slxlog
blob: 41504a7452dd6a5f3a264de5b8b1a505282b57f6 (plain) (blame)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/ash

##################
# Remote logging #
##################
#
# Usage: slxlog [-e | --echo] "logtype" "Human readable string" ["file name which's contents should be sent too"]
# -e or --echo will echo message to stdout too
#

export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/openslx/sbin:/opt/openslx/bin"

[ $# -eq 0 ] && exit 0

. /opt/openslx/config
[ -z "$SLX_REMOTE_LOG" ] && exit 3

USER=$(whoami)
LOGCHECK="/tmp/remote_log_check-$USER"
NOW=$(date +%s)
ECHO=
DELFILE=
SYNC=

while [ $# -gt 0 ]; do
	case "$1" in
		-e|--echo)
			ECHO=yes
			;;
		-d|--delete)
			DELFILE=yes
			;;
		-s|--sync)
			SYNC=yes
			;;
		*)
			break
			;;
	esac
	shift
done

if [ "${ECHO}" == "yes" ]; then
	echo "$@"
fi

TYPE="$1"

# Simple spamcheck. Not very tamper-proof, but if you'd want to spam the server
# you could do it anyways. This is to protect from accidental loops calling this.
if [ -r "$LOGCHECK" ]; then
	# Allow max 150 messages in total
	LINES=$( < "$LOGCHECK"  wc -l)
	[ "$LINES" -gt "150" ] && exit 1
	# Allow max 5 of same type messages in 30 seconds
	LINES=$(grep -c "$TYPE" "$LOGCHECK")
	if [ "$LINES" -ge "5" ]; then
		LAST=$(grep "$TYPE" "$LOGCHECK" | tail -n 5 | head -n 1 | awk '{print $1}')
		if [ -n "$LAST" ]; then
			DIFF="$(( NOW - LAST ))"
			[ "$DIFF" -lt "30" ] && exit 2
		fi
	fi
fi
echo "$NOW $TYPE" >> "$LOGCHECK"
chmod 0600 "$LOGCHECK" 2>/dev/null

if [ $# -lt 2 ]; then
	MSG="Missing text for $*"
else
	MSG="$2"
fi
MSG="[$USER] $MSG"

if [ $# -gt 2 ]; then
	EXTRA="$3"
fi

. /opt/openslx/bin/slx-tools
if is_debug; then
	CURLLOG="/tmp/slxlog.$USER"
else
	CURLLOG="/dev/null"
fi

UUID=
if [ -s /etc/system-uuid ]; then
	UUID=$(cat /etc/system-uuid)
fi

submitlog () {
	if [ -n "$EXTRA" ] && [ -r "$EXTRA" ] && [ -s "$EXTRA" ] && [ "$(stat -c %s "$EXTRA")" -lt "10000" ]; then # valid file attachment
		curl --data-urlencode "uuid=$UUID" --data-urlencode "type=$TYPE" --data-urlencode "description=$MSG" --data-urlencode "longdesc@$EXTRA" "$SLX_REMOTE_LOG" >> "$CURLLOG" 2>&1
	elif [ -z "$EXTRA" ]; then # no attachment
		curl --data-urlencode "uuid=$UUID" --data-urlencode "type=$TYPE" --data-urlencode "description=$MSG" "$SLX_REMOTE_LOG" >> "$CURLLOG" 2>&1
	elif [ -s "$EXTRA" ]; then # attachment file to big (more than 10k)
		curl --data-urlencode "uuid=$UUID" --data-urlencode "type=$TYPE" --data-urlencode "description=$MSG" --data-urlencode "longdesc=Attachment too large: $EXTRA" "$SLX_REMOTE_LOG" >> "$CURLLOG" 2>&1
	else # empty attachment file (or missing)
		curl --data-urlencode "uuid=$UUID" --data-urlencode "type=$TYPE" --data-urlencode "description=$MSG" --data-urlencode "longdesc=Attachment missing/empty: $EXTRA" "$SLX_REMOTE_LOG" >> "$CURLLOG" 2>&1
	fi
	[ -n "$DELFILE" ] && [ -n "$EXTRA" ] && rm -f -- "$EXTRA"
}

if [ -z "$SYNC" ]; then
	submitlog &
	exit 0
fi

submitlog