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

Last change on this file since 304 was 298, checked in by alloc, 8 years ago

Fixed IP detection in log monitor script

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