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

Last change on this file since 370 was 344, checked in by alloc, 6 years ago

Linux scripts 111

  • Property svn:executable set to *
File size: 8.0 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 local ownerId="$5"
42
43 logPlayerConnect $INSTANCE "$entityId" "$name" "$steamId" "$ip" "$ownerId"
44
45 for H in $(getHooksFor playerConnect $INSTANCE); do
46 $H $INSTANCE "$entityId" "$name" "$steamId" "$ip" "$ownerId"
47 done
48}
49
50handleDisconnect() {
51 local playerId="$1"
52 local entityId="$2"
53
54 logPlayerDisconnect $INSTANCE "$entityId"
55
56 for H in $(getHooksFor playerDisconnect $INSTANCE); do
57 $H $INSTANCE "$playerId" "$entityId" "$NICKNAME" "$STEAMID"
58 done
59}
60
61handlePlayerSpawnedInWorld() {
62 local entityId="$1"
63 local playerId="$2"
64 local ownerId="$3"
65 local playerName="$4"
66 local reason="$5"
67 local position="$6"
68
69 for H in $(getHooksFor playerSpawned $INSTANCE); do
70 $H $INSTANCE "$entityId" "$playerId" "$ownerId" "$playerName" "$reason" "$position"
71 done
72}
73
74handleChat() {
75 echo "$(timestamp): $1: $2 (SteamID $3, EntityID $4, Target $5)" >> $CHATLOG
76
77 for H in $(getHooksFor chat $INSTANCE); do
78 $H $INSTANCE "$1" "$2" "$3" "$4" "$5"
79 done
80}
81
82handleGmsg() {
83 echo "$(timestamp): GMSG: $1" >> $CHATLOG
84
85 for H in $(getHooksFor gmsg $INSTANCE); do
86 $H $INSTANCE "$1"
87 done
88}
89
90handleRemoteCommand() {
91 local cmd="$1"
92 local name="$2"
93
94 echo "$(timestamp): Player \"$name\" executed \"$cmd\"" >> $COMMANDLOG
95
96 for H in $(getHooksFor remoteCommand $INSTANCE); do
97 $H $INSTANCE "$cmd" "$name"
98 done
99}
100
101handleTelnetCommand() {
102 local cmd="$1"
103 local ip="$2"
104
105 echo "$(timestamp): Telnet from \"$ip\" executed \"$cmd\"" >> $COMMANDLOG
106
107 for H in $(getHooksFor telnetCommand $INSTANCE); do
108 $H $INSTANCE "$cmd" "$ip"
109 done
110}
111
112
113if [ ! -d "$(getInstancePath $INSTANCE)/logs" ]; then
114 mkdir "$(getInstancePath $INSTANCE)/logs"
115fi
116
117setAllPlayersOffline
118
119rm $(getInstancePath $INSTANCE)/logs/current_output_log.txt
120rm $(getInstancePath $INSTANCE)/logs/current_chat.log
121rm $(getInstancePath $INSTANCE)/logs/current_commandExecution.log
122ln -s $LOG $(getInstancePath $INSTANCE)/logs/current_output_log.txt
123ln -s $CHATLOG $(getInstancePath $INSTANCE)/logs/current_chat.log
124ln -s $COMMANDLOG $(getInstancePath $INSTANCE)/logs/current_commandExecution.log
125
126sleep 5
127
128NOBUF="stdbuf -e0 -o0"
129
130$NOBUF tail -n 5000 -F $LOG |
131$NOBUF tr '\\' '/' |
132$NOBUF tr -d '\r' |
133$NOBUF grep -v "^(Filename: " |
134$NOBUF sed -r 's/^[0-9]+-[0-9]+-[0-9]+T[0-9]+:[0-9]+:[0-9]+ [0-9]+[.,][0-9]+ [A-Z]+ (.*)$/\1/' |
135while read line ; do
136 if [ -n "$line" ]; then
137 #Player connected, entityid=1278, name=termo2, steamid=76561197997439820, steamOwner=76561197997439820, ip=178.203.27.140
138 #Player connected, entityid=[0-9]*, name=.*, steamid=[0-9]*, steamOwner=[0-9]*, ip=[a-fA-F:0-9.]*$
139 if [ -n "$(echo "$line" | grep '^Player connected,')" ]; then
140 entityId=$(expr "$line" : 'Player connected, entityid=\([0-9]*\), name=.*, steamid=[0-9]*, steamOwner=[0-9]*, ip=[a-fA-F:0-9.]*$')
141 playerName=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=\(.*\), steamid=[0-9]*, steamOwner=[0-9]*, ip=[a-fA-F:0-9.]*$')
142 steamId=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=.*, steamid=\([0-9]*\), steamOwner=[0-9]*, ip=[a-fA-F:0-9.]*$')
143 steamOwner=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=.*, steamid=[0-9]*, steamOwner=\([0-9]*\), ip=[a-fA-F:0-9.]*$')
144 ip=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=.*, steamid=[0-9]*, steamOwner=[0-9]*, ip=\([a-fA-F:0-9.]*\)$')
145 sleep 1
146 handleConnect "$entityId" "$playerName" "$steamId" "$ip" "$steamOwner"
147 unset entityId playerName steamId steamOwner ip
148 #Player disconnected: EntityID=[0-9]*, PlayerID='[0-9]*', OwnerID='[0-9]*', PlayerName='.*'$
149 elif [ -n "$(echo "$line" | grep '^Player disconnected: ')" ]; then
150 playerId=$(expr "$line" : "Player disconnected: EntityID=[0-9]*, PlayerID='\([0-9]*\)', OwnerID='[0-9]*', PlayerName='.*'$")
151 entityId=$(expr "$line" : "Player disconnected: EntityID=\([0-9]*\), PlayerID='[0-9]*', OwnerID='[0-9]*', PlayerName='.*'$")
152 handleDisconnect "$playerId" "$entityId"
153 unset playerId entityId
154 #PlayerSpawnedInWorld (reason: .+, position: [0-9]+, [0-9]+, [0-9]+): EntityID=[0-9]+, PlayerID='[0-9]+', OwnerID='[0-9]+', PlayerName='.*'
155 elif [ -n "$(echo "$line" | grep '^PlayerSpawnedInWorld ')" ]; then
156 reason=$(expr "$line" : "PlayerSpawnedInWorld (reason: \(.+\), position: [0-9]+, [0-9]+, [0-9]+): EntityID=[0-9]+, PlayerID='[0-9]+', OwnerID='[0-9]+', PlayerName='.*'$")
157 position=$(expr "$line" : "PlayerSpawnedInWorld (reason: .+, position: \([0-9]+, [0-9]+, [0-9]+\)): EntityID=[0-9]+, PlayerID='[0-9]+', OwnerID='[0-9]+', PlayerName='.*'$")
158 entityId=$(expr "$line" : "PlayerSpawnedInWorld (reason: .+, position: [0-9]+, [0-9]+, [0-9]+): EntityID=\([0-9]+\), PlayerID='[0-9]+', OwnerID='[0-9]+', PlayerName='.*'$")
159 playerId=$(expr "$line" : "PlayerSpawnedInWorld (reason: .+, position: [0-9]+, [0-9]+, [0-9]+): EntityID=[0-9]+, PlayerID='\([0-9]+\)', OwnerID='[0-9]+', PlayerName='.*'$")
160 ownerId=$(expr "$line" : "PlayerSpawnedInWorld (reason: .+, position: [0-9]+, [0-9]+, [0-9]+): EntityID=[0-9]+, PlayerID='[0-9]+', OwnerID='\([0-9]+\)', PlayerName='.*'$")
161 playerName=$(expr "$line" : "PlayerSpawnedInWorld (reason: .+, position: [0-9]+, [0-9]+, [0-9]+): EntityID=[0-9]+, PlayerID='[0-9]+', OwnerID='[0-9]+', PlayerName='\(.*\)'$")
162 handlePlayerSpawnedInWorld "$entityId" "$playerId" "$ownerId" "$playerName" "$reason" "$position"
163 unset reason position entityId playerId ownerId playerName
164 #GMSG: .*$
165 elif [ -n "$(echo "$line" | grep -E '^GMSG: .+')" ]; then
166 msg=$(expr "$line" : 'GMSG: \(.*\)$')
167 handleGmsg "$msg"
168 unset msg
169 #Chat (from '<steamid>', entity id '<entityid>', to '<targettype>'): '<senderchatname>': <msg>
170 elif [ -n "$(echo "$line" | grep -E '^Chat .+')" ]; then
171 steamId=$(expr "$line" : "Chat (from '\(.+\)', entity id '[0-9]+', to '[a-fA-F:0-9.]+'): '.*': .*$")
172 entityId=$(expr "$line" : "Chat (from '.+', entity id '\([0-9]+\)', to '[a-fA-F:0-9.]+'): '.*': .*$")
173 targetType=$(expr "$line" : "Chat (from '.+', entity id '[0-9]+', to '\([a-fA-F:0-9.]+\)'): '.*': .*$")
174 name=$(expr "$line" : "Chat (from '.+', entity id '[0-9]+', to '[a-fA-F:0-9.]+'): '\(.*\)': .*$")
175 msg=$(expr "$line" : "Chat (from '.+', entity id '[0-9]+', to '[a-fA-F:0-9.]+'): '.*': \(.*\)$")
176 handleChat "$name" "$msg" "$steamId" "$entityId" "$targetType"
177 unset name msg steamId entityId targetType
178 #Executing command ".*" from client ".*"$
179 elif [ -n "$(echo "$line" | grep '^Executing command '.*' from client')" ]; then
180 cmd=$(expr "$line" : "Executing command '\(.*\)' from client .*$")
181 nick=$(expr "$line" : "Executing command '.*' from client \(.*\)$")
182 handleRemoteCommand "$cmd" "$nick"
183 unset cmd nick
184 #Executing command ".*" by Telnet from .*$
185 elif [ -n "$(echo "$line" | grep '^Executing command '.*' by Telnet from ')" ]; then
186 cmd=$(expr "$line" : "Executing command '\(.*\)' by Telnet from .*$")
187 ip=$(expr "$line" : "Executing command '.*' by Telnet from \(.*\)$")
188 handleTelnetCommand "$cmd" "$ip"
189 unset cmd ip
190 fi
191 fi
192done
Note: See TracBrowser for help on using the repository browser.