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

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

Linux scripts 111

  • Property svn:executable set to *
File size: 8.0 KB
RevLine 
[17]1#!/bin/bash
2
[258]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
[17]18. /usr/local/lib/7dtd/common.sh
[67]19. /usr/local/lib/7dtd/playerlog.sh
[17]20
21if [ $(isValidInstance $1) -eq 0 ]; then
22 echo "No instance given or not a valid instance!"
23 return
24fi
25
26INSTANCE=$1
[240]27LOGTIMESTAMP=$2
28LOG=$(getInstancePath $INSTANCE)/logs/${LOGTIMESTAMP}_output_log.txt
29CHATLOG=$(getInstancePath $INSTANCE)/logs/${LOGTIMESTAMP}_chat.log
30COMMANDLOG=$(getInstancePath $INSTANCE)/logs/${LOGTIMESTAMP}_commandExecution.log
[17]31
32timestamp() {
33 date '+%Y.%m.%d %H:%M:%S'
34}
35
36handleConnect() {
[234]37 local entityId="$1"
38 local name="$2"
39 local steamId="$3"
40 local ip="$4"
[294]41 local ownerId="$5"
[17]42
[294]43 logPlayerConnect $INSTANCE "$entityId" "$name" "$steamId" "$ip" "$ownerId"
[17]44
[259]45 for H in $(getHooksFor playerConnect $INSTANCE); do
[294]46 $H $INSTANCE "$entityId" "$name" "$steamId" "$ip" "$ownerId"
[17]47 done
48}
49
50handleDisconnect() {
[234]51 local playerId="$1"
[87]52 local entityId="$2"
[17]53
[87]54 logPlayerDisconnect $INSTANCE "$entityId"
55
[259]56 for H in $(getHooksFor playerDisconnect $INSTANCE); do
[234]57 $H $INSTANCE "$playerId" "$entityId" "$NICKNAME" "$STEAMID"
[17]58 done
59}
60
[308]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
[62]74handleChat() {
[344]75 echo "$(timestamp): $1: $2 (SteamID $3, EntityID $4, Target $5)" >> $CHATLOG
[62]76
[259]77 for H in $(getHooksFor chat $INSTANCE); do
[344]78 $H $INSTANCE "$1" "$2" "$3" "$4" "$5"
[275]79 done
80}
81
82handleGmsg() {
83 echo "$(timestamp): GMSG: $1" >> $CHATLOG
84
85 for H in $(getHooksFor gmsg $INSTANCE); do
[63]86 $H $INSTANCE "$1"
[62]87 done
88}
89
[87]90handleRemoteCommand() {
91 local cmd="$1"
92 local name="$2"
93
94 echo "$(timestamp): Player \"$name\" executed \"$cmd\"" >> $COMMANDLOG
95
[259]96 for H in $(getHooksFor remoteCommand $INSTANCE); do
[87]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
[259]107 for H in $(getHooksFor telnetCommand $INSTANCE); do
[87]108 $H $INSTANCE "$cmd" "$ip"
109 done
110}
111
112
[67]113if [ ! -d "$(getInstancePath $INSTANCE)/logs" ]; then
114 mkdir "$(getInstancePath $INSTANCE)/logs"
115fi
116
117setAllPlayersOffline
118
[219]119rm $(getInstancePath $INSTANCE)/logs/current_output_log.txt
[87]120rm $(getInstancePath $INSTANCE)/logs/current_chat.log
121rm $(getInstancePath $INSTANCE)/logs/current_commandExecution.log
[219]122ln -s $LOG $(getInstancePath $INSTANCE)/logs/current_output_log.txt
[87]123ln -s $CHATLOG $(getInstancePath $INSTANCE)/logs/current_chat.log
124ln -s $COMMANDLOG $(getInstancePath $INSTANCE)/logs/current_commandExecution.log
125
[18]126sleep 5
127
[17]128NOBUF="stdbuf -e0 -o0"
129
[220]130$NOBUF tail -n 5000 -F $LOG |
[17]131$NOBUF tr '\\' '/' |
132$NOBUF tr -d '\r' |
133$NOBUF grep -v "^(Filename: " |
[219]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/' |
[17]135while read line ; do
136 if [ -n "$line" ]; then
[294]137 #Player connected, entityid=1278, name=termo2, steamid=76561197997439820, steamOwner=76561197997439820, ip=178.203.27.140
[298]138 #Player connected, entityid=[0-9]*, name=.*, steamid=[0-9]*, steamOwner=[0-9]*, ip=[a-fA-F:0-9.]*$
[87]139 if [ -n "$(echo "$line" | grep '^Player connected,')" ]; then
[298]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.]*\)$')
[17]145 sleep 1
[294]146 handleConnect "$entityId" "$playerName" "$steamId" "$ip" "$steamOwner"
147 unset entityId playerName steamId steamOwner ip
[234]148 #Player disconnected: EntityID=[0-9]*, PlayerID='[0-9]*', OwnerID='[0-9]*', PlayerName='.*'$
[308]149 elif [ -n "$(echo "$line" | grep '^Player disconnected: ')" ]; then
[234]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='.*'$")
[57]152 handleDisconnect "$playerId" "$entityId"
[17]153 unset playerId entityId
[308]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
[87]164 #GMSG: .*$
[308]165 elif [ -n "$(echo "$line" | grep -E '^GMSG: .+')" ]; then
[62]166 msg=$(expr "$line" : 'GMSG: \(.*\)$')
[275]167 handleGmsg "$msg"
[62]168 unset msg
[344]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
[234]178 #Executing command ".*" from client ".*"$
[308]179 elif [ -n "$(echo "$line" | grep '^Executing command '.*' from client')" ]; then
[234]180 cmd=$(expr "$line" : "Executing command '\(.*\)' from client .*$")
181 nick=$(expr "$line" : "Executing command '.*' from client \(.*\)$")
[87]182 handleRemoteCommand "$cmd" "$nick"
183 unset cmd nick
[234]184 #Executing command ".*" by Telnet from .*$
[308]185 elif [ -n "$(echo "$line" | grep '^Executing command '.*' by Telnet from ')" ]; then
[234]186 cmd=$(expr "$line" : "Executing command '\(.*\)' by Telnet from .*$")
187 ip=$(expr "$line" : "Executing command '.*' by Telnet from \(.*\)$")
[87]188 handleTelnetCommand "$cmd" "$ip"
189 unset cmd ip
[17]190 fi
191 fi
192done
Note: See TracBrowser for help on using the repository browser.