source: scripts/usr/local/lib/7dtd/monitor-log.sh@ 277

Last change on this file since 277 was 275, checked in by alloc, 9 years ago

v101: Fixed log-monitor for chat

  • Property svn:executable set to *
File size: 5.3 KB
Line 
1#!/bin/bash
2
3# Copyright 2016 Christian 'Alloc' Illy
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17
18. /usr/local/lib/7dtd/common.sh
19. /usr/local/lib/7dtd/playerlog.sh
20
21if [ $(isValidInstance $1) -eq 0 ]; then
22 echo "No instance given or not a valid instance!"
23 return
24fi
25
26INSTANCE=$1
27LOGTIMESTAMP=$2
28LOG=$(getInstancePath $INSTANCE)/logs/${LOGTIMESTAMP}_output_log.txt
29CHATLOG=$(getInstancePath $INSTANCE)/logs/${LOGTIMESTAMP}_chat.log
30COMMANDLOG=$(getInstancePath $INSTANCE)/logs/${LOGTIMESTAMP}_commandExecution.log
31
32timestamp() {
33 date '+%Y.%m.%d %H:%M:%S'
34}
35
36handleConnect() {
37 local entityId="$1"
38 local name="$2"
39 local steamId="$3"
40 local ip="$4"
41
42 logPlayerConnect $INSTANCE "$entityId" "$name" "$steamId" "$ip"
43
44 for H in $(getHooksFor playerConnect $INSTANCE); do
45 $H $INSTANCE "$entityId" "$name" "$steamId" "$ip"
46 done
47}
48
49handleDisconnect() {
50 local playerId="$1"
51 local entityId="$2"
52
53 logPlayerDisconnect $INSTANCE "$entityId"
54
55 for H in $(getHooksFor playerDisconnect $INSTANCE); do
56 $H $INSTANCE "$playerId" "$entityId" "$NICKNAME" "$STEAMID"
57 done
58}
59
60handleChat() {
61 echo "$(timestamp): $1: $2" >> $CHATLOG
62
63 for H in $(getHooksFor chat $INSTANCE); do
64 $H $INSTANCE "$1" "$2"
65 done
66}
67
68handleGmsg() {
69 echo "$(timestamp): GMSG: $1" >> $CHATLOG
70
71 for H in $(getHooksFor gmsg $INSTANCE); do
72 $H $INSTANCE "$1"
73 done
74}
75
76handleRemoteCommand() {
77 local cmd="$1"
78 local name="$2"
79
80 echo "$(timestamp): Player \"$name\" executed \"$cmd\"" >> $COMMANDLOG
81
82 for H in $(getHooksFor remoteCommand $INSTANCE); do
83 $H $INSTANCE "$cmd" "$name"
84 done
85}
86
87handleTelnetCommand() {
88 local cmd="$1"
89 local ip="$2"
90
91 echo "$(timestamp): Telnet from \"$ip\" executed \"$cmd\"" >> $COMMANDLOG
92
93 for H in $(getHooksFor telnetCommand $INSTANCE); do
94 $H $INSTANCE "$cmd" "$ip"
95 done
96}
97
98
99if [ ! -d "$(getInstancePath $INSTANCE)/logs" ]; then
100 mkdir "$(getInstancePath $INSTANCE)/logs"
101fi
102
103setAllPlayersOffline
104
105rm $(getInstancePath $INSTANCE)/logs/current_output_log.txt
106rm $(getInstancePath $INSTANCE)/logs/current_chat.log
107rm $(getInstancePath $INSTANCE)/logs/current_commandExecution.log
108ln -s $LOG $(getInstancePath $INSTANCE)/logs/current_output_log.txt
109ln -s $CHATLOG $(getInstancePath $INSTANCE)/logs/current_chat.log
110ln -s $COMMANDLOG $(getInstancePath $INSTANCE)/logs/current_commandExecution.log
111
112sleep 5
113
114NOBUF="stdbuf -e0 -o0"
115
116$NOBUF tail -n 5000 -F $LOG |
117$NOBUF tr '\\' '/' |
118$NOBUF tr -d '\r' |
119$NOBUF grep -v "^(Filename: " |
120$NOBUF sed -r 's/^[0-9]+-[0-9]+-[0-9]+T[0-9]+:[0-9]+:[0-9]+ [0-9]+[.,][0-9]+ [A-Z]+ (.*)$/\1/' |
121while read line ; do
122 if [ -n "$line" ]; then
123 #Player connected, entityid=1278, name=termo2, steamid=76561197997439820, ip=178.203.27.140
124 #Player connected, entityid=[0-9]*, name=.*, steamid=[0-9]*, ip=[0-9.]*$
125 if [ -n "$(echo "$line" | grep '^Player connected,')" ]; then
126 entityId=$(expr "$line" : 'Player connected, entityid=\([0-9]*\), name=.*, steamid=[0-9]*, ip=[0-9.]*$')
127 playerName=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=\(.*\), steamid=[0-9]*, ip=[0-9.]*$')
128 steamId=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=.*, steamid=\([0-9]*\), ip=[0-9.]*$')
129 ip=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=.*, steamid=[0-9]*, ip=\([0-9.]*\)$')
130 sleep 1
131 handleConnect "$entityId" "$playerName" "$steamId" "$ip"
132 unset entityId playerName steamId ip
133 else
134 #Player disconnected: EntityID=[0-9]*, PlayerID='[0-9]*', OwnerID='[0-9]*', PlayerName='.*'$
135 if [ -n "$(echo "$line" | grep '^Player disconnected: ')" ]; then
136 playerId=$(expr "$line" : "Player disconnected: EntityID=[0-9]*, PlayerID='\([0-9]*\)', OwnerID='[0-9]*', PlayerName='.*'$")
137 entityId=$(expr "$line" : "Player disconnected: EntityID=\([0-9]*\), PlayerID='[0-9]*', OwnerID='[0-9]*', PlayerName='.*'$")
138 handleDisconnect "$playerId" "$entityId"
139 unset playerId entityId
140 else
141 #GMSG: .*$
142 if [ -n "$(echo "$line" | grep -E '^GMSG: .+')" ]; then
143 msg=$(expr "$line" : 'GMSG: \(.*\)$')
144 handleGmsg "$msg"
145 unset msg
146 else
147 #Chat: 'name': .*$
148 if [ -n "$(echo "$line" | grep -E '^Chat: .+')" ]; then
149 name=$(expr "$line" : "Chat: '\(.*\)': .*$")
150 msg=$(expr "$line" : "Chat: '.*': \(.*\)$")
151 handleChat "$name" "$msg"
152 unset name msg
153 else
154 #Executing command ".*" from client ".*"$
155 if [ -n "$(echo "$line" | grep '^Executing command '.*' from client')" ]; then
156 cmd=$(expr "$line" : "Executing command '\(.*\)' from client .*$")
157 nick=$(expr "$line" : "Executing command '.*' from client \(.*\)$")
158 handleRemoteCommand "$cmd" "$nick"
159 unset cmd nick
160 else
161 #Executing command ".*" by Telnet from .*$
162 if [ -n "$(echo "$line" | grep '^Executing command '.*' by Telnet from ')" ]; then
163 cmd=$(expr "$line" : "Executing command '\(.*\)' by Telnet from .*$")
164 ip=$(expr "$line" : "Executing command '.*' by Telnet from \(.*\)$")
165 handleTelnetCommand "$cmd" "$ip"
166 unset cmd ip
167 fi
168 fi
169 fi
170 fi
171 fi
172 fi
173 fi
174done
Note: See TracBrowser for help on using the repository browser.