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
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" >> $CHATLOG
62
63 for H in $(getHooksFor chat $INSTANCE); do
64 $H $INSTANCE "$1"
65 done
66}
67
68handleRemoteCommand() {
69 local cmd="$1"
70 local name="$2"
71
72 echo "$(timestamp): Player \"$name\" executed \"$cmd\"" >> $COMMANDLOG
73
74 for H in $(getHooksFor remoteCommand $INSTANCE); do
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
85 for H in $(getHooksFor telnetCommand $INSTANCE); do
86 $H $INSTANCE "$cmd" "$ip"
87 done
88}
89
90
91if [ ! -d "$(getInstancePath $INSTANCE)/logs" ]; then
92 mkdir "$(getInstancePath $INSTANCE)/logs"
93fi
94
95setAllPlayersOffline
96
97rm $(getInstancePath $INSTANCE)/logs/current_output_log.txt
98rm $(getInstancePath $INSTANCE)/logs/current_chat.log
99rm $(getInstancePath $INSTANCE)/logs/current_commandExecution.log
100ln -s $LOG $(getInstancePath $INSTANCE)/logs/current_output_log.txt
101ln -s $CHATLOG $(getInstancePath $INSTANCE)/logs/current_chat.log
102ln -s $COMMANDLOG $(getInstancePath $INSTANCE)/logs/current_commandExecution.log
103
104sleep 5
105
106NOBUF="stdbuf -e0 -o0"
107
108$NOBUF tail -n 5000 -F $LOG |
109$NOBUF tr '\\' '/' |
110$NOBUF tr -d '\r' |
111$NOBUF grep -v "^(Filename: " |
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/' |
113while read line ; do
114 if [ -n "$line" ]; then
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.]*$
117 if [ -n "$(echo "$line" | grep '^Player connected,')" ]; then
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.]*\)$')
122 sleep 1
123 handleConnect "$entityId" "$playerName" "$steamId" "$ip"
124 unset entityId playerName steamId ip
125 else
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='.*'$")
130 handleDisconnect "$playerId" "$entityId"
131 unset playerId entityId
132 else
133 #GMSG: .*$
134 if [ -n "$(echo "$line" | grep -E '^GMSG: .+')" ]; then
135 msg=$(expr "$line" : 'GMSG: \(.*\)$')
136 handleChat "$msg"
137 unset msg
138 else
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 \(.*\)$")
143 handleRemoteCommand "$cmd" "$nick"
144 unset cmd nick
145 else
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 \(.*\)$")
150 handleTelnetCommand "$cmd" "$ip"
151 unset cmd ip
152 fi
153 fi
154 fi
155 fi
156 fi
157 fi
158done
Note: See TracBrowser for help on using the repository browser.