Home > database >  Select only part of text with regular expression in zabbix
Select only part of text with regular expression in zabbix

Time:12-15

I'm trying to monitor an eventlog in zabbix, I managed to pull the information, but I just want a snippet of the log using regex.

this is the complete log:

An account failed to log on.

Subject:
    Security ID:        NULL SID
    Account Name:       -
    Account Domain:     -
    Logon ID:       0x0

Logon Type:         3

Account For Which Logon Failed:
    Security ID:        NULL SID
    Account Name:       xxxxxxx
    Account Domain:     xxxxxxx

Failure Information:
    Failure Reason:     Unknown user name or bad password.
    Status:         0xC000006D
    Sub Status:     0xC000006A

Process Information:
    Caller Process ID:  0x0
    Caller Process Name:    -

Network Information:
    Workstation Name:   SSAPL1
    Source Network Address: 0.0.0.0
    Source Port:        40410

Detailed Authentication Information:
    Logon Process:      NtLmSsp 
    Authentication Package: NTLM
    Transited Services: -
    Package Name (NTLM only):   -
    Key Length:     0

This event is generated when a logon request fails. It is generated on the computer where access was attempted.

The Subject fields indicate the account on the local system which requested the logon. This is most commonly a service such as the Server service, or a local process such as Winlogon.exe or Services.exe.

The Logon Type field indicates the kind of logon that was requested. The most common types are 2 (interactive) and 3 (network).

The Process Information fields indicate which account and process on the system requested the logon.

The Network Information fields indicate where a remote logon request originated. Workstation name is not always available and may be left blank in some cases.

The authentication information fields provide detailed information about this specific logon request.
    - Transited services indicate which intermediate services have participated in this logon request.
    - Package name indicates which sub-protocol was used among the NTLM protocols.
    - Key length indicates the length of the generated session key. This will be 0 if no session key was requested.

this is the part that has the information I need:

Subject:
    Security ID:        NULL SID
    Account Name:       -
    Account Domain:     -
    Logon ID:       0x0

Logon Type:         3

Account For Which Logon Failed:
    Security ID:        NULL SID
    Account Name:       xxxxxxx
    Account Domain:     xxxxxxx

Failure Information:
    Failure Reason:     Unknown user name or bad password.
    Status:         0xC000006D
    Sub Status:     0xC000006A

Process Information:
    Caller Process ID:  0x0
    Caller Process Name:    -

Network Information:
    Workstation Name:   SSAPL1
    Source Network Address: 0.0.0.0
    Source Port:        40410

Detailed Authentication Information:
    Logon Process:      NtLmSsp 
    Authentication Package: NTLM
    Transited Services: -
    Package Name (NTLM only):   -
    Key Length:     0

I tried like this but without success

)
^
  (?:
    \s\w\w\w
    (?:
      \w\w
      (?:
        \w
        (?:
          \w\s\w\w\w\w
          (?:
            \w\w:\s\s
            (?:
              \w\w\w\w\w\w\w\s\w\w\w\w\s\w\w\w\w\s\w\w\s\w\w\w\s\w\w\w\w\w\w\w\w
              \.
              |
              \-
            )
            |
            :\s\s
            \-
          )
          |
          \s\w\w\w\w\w\w\w\s\w\w\w\w\w\w\w:\s\w\w\.\w\.\w\.\w\w
          |
          \w
          (?:
            \w\w\w\w\w\w\w\s\w\w\w\w\w\w\w
            :\s
            |
            \s\w\w\w\w\w\w
            :\s\s\w\w\w\w
            \.
          )\w\w\w\w
          |
          \w\s\w\w\w\w:\s\s\w\w\w\w\w\w\w\w\w\w\w\w\w
          |

      :\s\s\s\w
    )
  )
$

CodePudding user response:

Guess I'll make it an answer with a example code.

1st step

import re
result = '\r\n'.join(re.findall(r'. :.*', s))   #<------- s variable is your complete log
print(result)
Output:
Subject:
    Security ID:        NULL SID
    Account Name:       -
    Account Domain:     -
    Logon ID:       0x0
Logon Type:         3
Account For Which Logon Failed:
    Security ID:        NULL SID
    Account Name:       xxxxxxx
    Account Domain:     xxxxxxx
Failure Information:
    Failure Reason:     Unknown user name or bad password.
    Status:         0xC000006D
    Sub Status:     0xC000006A
Process Information:
    Caller Process ID:  0x0
    Caller Process Name:    -
Network Information:
    Workstation Name:   SSAPL1
    Source Network Address: 0.0.0.0
    Source Port:        40410
Detailed Authentication Information:
    Logon Process:      NtLmSsp 
    Authentication Package: NTLM
    Transited Services: -
    Package Name (NTLM only):   -
    Key Length:     0

Basically your desired output but missing an extra newline between your first level items.

2nd step

Substitution:

result = re.sub(r'^\s\s ', '- ', result, flags=re.MULTILINE)
result = re.sub(r'   ', ' ', result)
result = re.sub(r' -', ' NULL', result)
print(result)
Output:
Subject:
- Security ID: NULL SID
- Account Name: NULL
- Account Domain: NULL
- Logon ID: 0x0
Logon Type: 3
Account For Which Logon Failed:
- Security ID: NULL SID
- Account Name: xxxxxxx
- Account Domain: xxxxxxx
Failure Information:
- Failure Reason: Unknown user name or bad password.
- Status: 0xC000006D
- Sub Status: 0xC000006A
Process Information:
- Caller Process ID: 0x0
- Caller Process Name: NULL
Network Information:
- Workstation Name: SSAPL1
- Source Network Address: 0.0.0.0
- Source Port: 40410
Detailed Authentication Information:
- Logon Process: NtLmSsp 
- Authentication Package: NTLM
- Transited Services: NULL
- Package Name (NTLM only): NULL
- Key Length: 0

Here I prefix 2nd level items with - , remove all the double spaces and replace - at the end of the lines with NULL as - is a yaml syntax. Now this string is yaml friendly.

3rd step

import yaml
yaml.safe_load(result)
Output:
{'Subject': [{'Security ID': 'NULL SID'},
  {'Account Name': None},
  {'Account Domain': None},
  {'Logon ID': 0}],
 'Logon Type': 3,
 'Account For Which Logon Failed': [{'Security ID': 'NULL SID'},
  {'Account Name': 'xxxxxxx'},
  {'Account Domain': 'xxxxxxx'}],
 'Failure Information': [{'Failure Reason': 'Unknown user name or bad password.'},
  {'Status': 3221225581},
  {'Sub Status': 3221225578}],
 'Process Information': [{'Caller Process ID': 0},
  {'Caller Process Name': None}],
 'Network Information': [{'Workstation Name': 'SSAPL1'},
  {'Source Network Address': '0.0.0.0'},
  {'Source Port': 40410}],
 'Detailed Authentication Information': [{'Logon Process': 'NtLmSsp'},
  {'Authentication Package': 'NTLM'},
  {'Transited Services': None},
  {'Package Name (NTLM only)': None},
  {'Key Length': 0}]}

Now the output is a dictionary and can be accessed with keys, saved as json and etc.

CodePudding user response:

You might use:

^(?:[A-Z][^\n:]*:.*(?:\n[^\S\n]{2,}[A-Z].*)*\n*) 

The pattern matches:

  • ^ Start of string
  • (?: Non capture group to repeat as a whole part
    • [A-Z][^\n:]*:.* Match a char A-Z, then match till : and the rest of the line
    • (?:\n[^\S\n]{2,}[A-Z].*)* Match optional lines that start with 2 or more spaces followed by A-Z
    • \n* Match trailing newlines
  • ) Close the non capture group and repeat 1 times

Regex demo

Or using \w to match a word character, \R to match unicode newlines and \h to match horizontal whitespace chars, as zabbix supports PCRE

^(?:\w[^\n\r:]*:.*(?:\R\h{2,}\w.*)*\R*) 

Regex demo

  • Related