0

So, I'm trying to count the time a user has been online on a Teamspeak 3 server via log. The log format is something like

2015-10-14 23:30:29.676932|INFO |VirtualServerBase| 1| client connected 'clientX'(id:XY) from IPx.IPx.IPx.IPx:PortX 2015-10-15 00:12:45.870381|INFO |VirtualServerBase| 1| client disconnected 'clientX'(id:XY) reason 'reasonmsg=leave'.

I can of course use grep "client connected \|client disconnected" to filter out the other entries and add | grep "(id:XY)" to only see user XY. If i use | cut -c 1-16 to only see the time and date and filter out everything unnecessary.

The resulting command is cat *.log | grep "client connected\|client disconnected" | cut -c 1-16. Afterwards, the output looks like this:

2015-10-02 14:12 2015-10-02 14:17 2015-10-06 14:18 2015-10-06 15:27

The question is: how do I count the times between connects and disconnects and add them up inside a shell-script?

TestTab
  • 1
  • 1
  • More complex than you think, you have to deal with differents users If you want to get a connection time... – Amessihel Oct 15 '15 at 13:44

1 Answers1

0

In my opinion, the best way is to get an output like :

CLIENTID1 YYYY-MM-DD connect
CLIENTID2 YYYY-MM-DD connect
CLIENTID3 YYYY-MM-DD connect
CLIENTID1 YYYY-MM-DD disconnect

Here is a first command line to get theses lines (assuming that (id:XY) is an uniq client id) :

sed 's/^\(.*:[0-9][0-9]\).* \(dis\)\?connected.*(id:\([^)]*\)).*$/\3 \1 \2connected/'

Then for each connect line you have to find the disconnect one using CLIENTID. Retrieving the dates (trivial) you can get their diff (already discussed on SO, you can use date).

Amessihel
  • 5,891
  • 3
  • 16
  • 40