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

Last change on this file since 261 was 259, checked in by alloc, 9 years ago

v96

  • Property svn:executable set to *
File size: 5.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"
[17]41
[87]42 logPlayerConnect $INSTANCE "$entityId" "$name" "$steamId" "$ip"
[17]43
[259]44 for H in $(getHooksFor playerConnect $INSTANCE); do
[234]45 $H $INSTANCE "$entityId" "$name" "$steamId" "$ip"
[17]46 done
47}
48
49handleDisconnect() {
[234]50 local playerId="$1"
[87]51 local entityId="$2"
[17]52
[87]53 logPlayerDisconnect $INSTANCE "$entityId"
54
[259]55 for H in $(getHooksFor playerDisconnect $INSTANCE); do
[234]56 $H $INSTANCE "$playerId" "$entityId" "$NICKNAME" "$STEAMID"
[17]57 done
58}
59
[62]60handleChat() {
61 echo "$(timestamp): $1" >> $CHATLOG
62
[259]63 for H in $(getHooksFor chat $INSTANCE); do
[63]64 $H $INSTANCE "$1"
[62]65 done
66}
67
[87]68handleRemoteCommand() {
69 local cmd="$1"
70 local name="$2"
71
72 echo "$(timestamp): Player \"$name\" executed \"$cmd\"" >> $COMMANDLOG
73
[259]74 for H in $(getHooksFor remoteCommand $INSTANCE); do
[87]75 $H $INSTANCE "$cmd" "$name"
76 done
77}
78
79handleTelnetCommand() {
80 local cmd="$1"
81 local ip="$2"
82
83 echo "$(timestamp): Telnet from \"$ip\" executed \"$cmd\"" >> $COMMANDLOG
84
[259]85 for H in $(getHooksFor telnetCommand $INSTANCE); do
[87]86 $H $INSTANCE "$cmd" "$ip"
87 done
88}
89
90
[67]91if [ ! -d "$(getInstancePath $INSTANCE)/logs" ]; then
92 mkdir "$(getInstancePath $INSTANCE)/logs"
93fi
94
95setAllPlayersOffline
96
[219]97rm $(getInstancePath $INSTANCE)/logs/current_output_log.txt
[87]98rm $(getInstancePath $INSTANCE)/logs/current_chat.log
99rm $(getInstancePath $INSTANCE)/logs/current_commandExecution.log
[219]100ln -s $LOG $(getInstancePath $INSTANCE)/logs/current_output_log.txt
[87]101ln -s $CHATLOG $(getInstancePath $INSTANCE)/logs/current_chat.log
102ln -s $COMMANDLOG $(getInstancePath $INSTANCE)/logs/current_commandExecution.log
103
[18]104sleep 5
105
[17]106NOBUF="stdbuf -e0 -o0"
107
[220]108$NOBUF tail -n 5000 -F $LOG |
[17]109$NOBUF tr '\\' '/' |
110$NOBUF tr -d '\r' |
111$NOBUF grep -v "^(Filename: " |
[219]112$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]113while read line ; do
114 if [ -n "$line" ]; then
[234]115 #Player connected, entityid=1278, name=termo2, steamid=76561197997439820, ip=178.203.27.140
116 #Player connected, entityid=[0-9]*, name=.*, steamid=[0-9]*, ip=[0-9.]*$
[87]117 if [ -n "$(echo "$line" | grep '^Player connected,')" ]; then
[234]118 entityId=$(expr "$line" : 'Player connected, entityid=\([0-9]*\), name=.*, steamid=[0-9]*, ip=[0-9.]*$')
119 playerName=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=\(.*\), steamid=[0-9]*, ip=[0-9.]*$')
120 steamId=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=.*, steamid=\([0-9]*\), ip=[0-9.]*$')
121 ip=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=.*, steamid=[0-9]*, ip=\([0-9.]*\)$')
[17]122 sleep 1
[234]123 handleConnect "$entityId" "$playerName" "$steamId" "$ip"
124 unset entityId playerName steamId ip
[62]125 else
[234]126 #Player disconnected: EntityID=[0-9]*, PlayerID='[0-9]*', OwnerID='[0-9]*', PlayerName='.*'$
127 if [ -n "$(echo "$line" | grep '^Player disconnected: ')" ]; then
128 playerId=$(expr "$line" : "Player disconnected: EntityID=[0-9]*, PlayerID='\([0-9]*\)', OwnerID='[0-9]*', PlayerName='.*'$")
129 entityId=$(expr "$line" : "Player disconnected: EntityID=\([0-9]*\), PlayerID='[0-9]*', OwnerID='[0-9]*', PlayerName='.*'$")
[57]130 handleDisconnect "$playerId" "$entityId"
[17]131 unset playerId entityId
[62]132 else
[87]133 #GMSG: .*$
[62]134 if [ -n "$(echo "$line" | grep -E '^GMSG: .+')" ]; then
135 msg=$(expr "$line" : 'GMSG: \(.*\)$')
136 handleChat "$msg"
137 unset msg
[87]138 else
[234]139 #Executing command ".*" from client ".*"$
140 if [ -n "$(echo "$line" | grep '^Executing command '.*' from client')" ]; then
141 cmd=$(expr "$line" : "Executing command '\(.*\)' from client .*$")
142 nick=$(expr "$line" : "Executing command '.*' from client \(.*\)$")
[87]143 handleRemoteCommand "$cmd" "$nick"
144 unset cmd nick
145 else
[234]146 #Executing command ".*" by Telnet from .*$
147 if [ -n "$(echo "$line" | grep '^Executing command '.*' by Telnet from ')" ]; then
148 cmd=$(expr "$line" : "Executing command '\(.*\)' by Telnet from .*$")
149 ip=$(expr "$line" : "Executing command '.*' by Telnet from \(.*\)$")
[87]150 handleTelnetCommand "$cmd" "$ip"
151 unset cmd ip
[17]152 fi
[62]153 fi
154 fi
[87]155 fi
156 fi
[17]157 fi
158done
Note: See TracBrowser for help on using the repository browser.