How to write a query that can identify user activity on VPN?

0

I have a Log Group that contains VPN logs. I am trying to create a graphical dashboard (if possible), but I can't work out how to output the textual lines into meaningful numbers. I've tried a number of queries to find the unique values and the closest I have found is very simple, but doesn't show the username, and as you can see, the usernames are in slightly different places or are encapsulated differently too.

Here are some example lines: Logged In: Mon Sep 25 14:58:00 2023 123.123.123.123 [username] Peer Connection Initiated with [AF_INET]123.123.123.123 Mon Sep 25 14:58:00 2023 123.123.123.123 [username] Peer Connection Initiated with [AF_INET]123.123.123.123

Exits: Mon Sep 25 12:42:03 2023 username/122.123.230.123:1234 SIGTERM[soft,remote-exit] received, client-instance exiting

Restarts: Sun Sep 24 23:14:03 2023 username/122.123.123.110:45321 [username] Inactivity timeout (--ping-restart), restarting

I'm looking to show the number of Clients for the following states: Logged In Restarted Exits

Any help would be really appreciated. Phil

3 Answers
1

Hi Phil,

Thank you for the clarifications. I think I understand what you are willing to achieve.

I tried to create some sample queries to get a similar output as you expect. You can use these to modify them or add more details, as per your requirements.


DISCONNECTED

fields @timestamp, @message
| filter @message like /SIGTERM/
| parse @message "* SIGTERM[*] *" as Details, ExitDetails 
| stats count(*) as Count by "Disconnected" as Type, Details

Output: Output for Disconnected

CONNECTED

fields @timestamp, @message
| filter @message like /Peer Connection Initiated/
| parse @message "* with *" as Details, OtherDetails
| stats count(*) as Count by "Connected" as Type, Details

Output: Output for Connected

RESTARTED

fields @timestamp, @message
| filter @message like /Inactivity/
| parse @message "* (--ping-restart), restarting" as Details
| stats count(*) as Count by "Restarted" as Type, Details

Output: Output for Restarted


Please let me know if this helps.

Regards,

Atul

profile picture
answered 7 months ago
  • Hey Atul, These are wonderful, thankyou so much. I had got 75% of the way, but this has helped so much.

    Is there a way that I can merge these altogether? So for example I can see when Phil connected....then phil might have restarted and then finally after say 2 hours, Phil disconnected?

    I was trying to work out if it was a case of just adding another couple 'Stats' lines, but I don't think it works that way.

    All you help is really appreciated so much. Many thanks Phil

  • Hi Phil,

    Glad to be of help. For merging all these together, you can try filtering all these logs together with 'OR' conditions, then parse 'Username' from each log event, and then group by Username. You can play around and try all the possibilities available by referring to this doc: https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax.html Hope it helps!

    And, if the answers helped you out, please feel free to Upvote them, and click on "Accept Answer", to help other people facing similar issues.

    Thanks, Atul

  • If this resolves your issue, please feel free to Upvote and click on "Accept Answer", to help others facing a similar problem. Thanks!

1

Hi Phil,

You can write a custom query for your logs, which seems to be in a predefined format.

For reference, to count unique logged in users, write a query like this:

fields @timestamp, @message
| parse @message "Logged In: * * * * * * * Peer Connection Initiated with *" as loggedInDay, loggedInMon, loggedInDate, loggedInTime, loggedInYear, loggedInIp, loggedInUser, inetIP
| stats count_distinct(loggedInUser) as LoggedInClients

This gives output like:

#  |  LoggedInClients
1  |  3

Similarly, you can add parse and stats statements for Exited and Restarted Clients.

You can refer to this documentation to write regular expression to extract specific fields: https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax-examples.html#CWL_QuerySyntax-examples-parse

[Query Syntax Reference] https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax.html

Hope this answers what you were looking for.

If you need further assistance, please let me know.

Thanks, Atul

profile picture
answered 7 months ago
  • Thanks Atul,

    I had to post an answer to your reply as this section only allows 600 characters!

0

Morning Atul,

Many thanks for you help on this so far. I think I may need to elaborate a little bit more, and I hope this makes sense.

Firstly, I have the following discovered fields in my Log: @log @logStream @message @timestamp

Secondly, each line of data I am try to count looks like this and each one has a specific string I am looking for: Exit lines look like this: Mon Sep 25 22:43:55 2023 username/123.123.123.123:53823 SIGTERM[soft,remote-exit] received, client-instance exiting

Connected lines look like this: Tue Sep 26 06:33:58 2023 123.123.123.123:56703 [username] Peer Connection Initiated with [AF_INET]123.123.123.123:56703

Restart lines look like this: Tue Sep 26 09:18:15 2023 username/123.123.123.123:56703 [username] Inactivity timeout (--ping-restart), restarting

I am using the following query, which I can adapt to look for specific parts of the @message lines, but I can't seem to piece them together: fields @timestamp, @message | filter @message like /.SIGTERM./ | parse @message "SIGTERM" as connectionType1 | stats count() as VPN_Disconnected by connectionType1

Ideally what I'd like to get is an output that looks similar to this (if possible): Details Connected Restarted Disconnected Tue Sep 26 06:33:58 2023 123.123.123.123:56703 [username] Peer Connection Initiated 1 Tue Sep 26 07:18:15 2023 username/123.123.123.123:56703 [username] Inactivity timeout 1 Tue Sep 26 09:43:55 2023 username/123.123.123.123:53823 SIGTERM 1

Apologies if this doesn't make sense...I'm pretty new to all this....I've just submitted this and the formatting is all a bit messed up. Many thanks Phil

Phil
answered 7 months ago
  • Thanks for sharing the details.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions