2022-08-12 10:16:15 -07:00
|
|
|
[Home](https://kellyjonbrazil.github.io/jc/)
|
|
|
|
<a id="jc.parsers.syslog"></a>
|
|
|
|
|
|
|
|
# jc.parsers.syslog
|
|
|
|
|
|
|
|
jc - JSON Convert Syslog RFC 5424 string parser
|
|
|
|
|
2022-08-14 12:11:31 -07:00
|
|
|
This parser accepts a single syslog line string or multiple syslog lines
|
2022-08-15 15:39:20 -07:00
|
|
|
separated by newlines. A warning message to `STDERR` will be printed if an
|
2022-08-16 10:01:31 -07:00
|
|
|
unparsable line is found unless `--quiet` or `quiet=True` is used.
|
2022-08-14 12:11:31 -07:00
|
|
|
|
2022-08-14 12:03:16 -07:00
|
|
|
The `timestamp_epoch` calculated timestamp field is naive. (i.e. based on
|
|
|
|
the local time of the system the parser is run on)
|
|
|
|
|
|
|
|
The `timestamp_epoch_utc` calculated timestamp field is timezone-aware and
|
|
|
|
is only available if the timezone field is UTC.
|
2022-08-12 10:16:15 -07:00
|
|
|
|
|
|
|
Usage (cli):
|
|
|
|
|
2022-08-14 12:11:31 -07:00
|
|
|
$ echo <165>1 2003-08-24T05:14:15.000003-07:00 192.0.2... | jc --syslog
|
2022-08-12 10:16:15 -07:00
|
|
|
|
|
|
|
Usage (module):
|
|
|
|
|
|
|
|
import jc
|
2022-08-14 12:11:31 -07:00
|
|
|
result = jc.parse('syslog', syslog_string)
|
2022-08-12 10:16:15 -07:00
|
|
|
|
|
|
|
Schema:
|
2022-08-17 12:54:12 -07:00
|
|
|
|
|
|
|
Blank values converted to `null`/`None`.
|
2022-08-12 10:16:15 -07:00
|
|
|
|
|
|
|
[
|
|
|
|
{
|
2022-08-14 12:03:16 -07:00
|
|
|
"priority": integer,
|
|
|
|
"version": integer,
|
|
|
|
"timestamp": string,
|
|
|
|
"timestamp_epoch": integer, # [0]
|
|
|
|
"timestamp_epoch_utc": integer, # [1]
|
|
|
|
"hostname": string,
|
|
|
|
"appname": string,
|
|
|
|
"proc_id": integer,
|
|
|
|
"msg_id": string,
|
|
|
|
"structured_data": [
|
|
|
|
{
|
|
|
|
"identity": string,
|
2022-08-14 12:18:22 -07:00
|
|
|
"parameters": {
|
2022-08-14 12:03:16 -07:00
|
|
|
"<key>": string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
2022-08-15 15:39:20 -07:00
|
|
|
"message": string,
|
|
|
|
"unparsable": string # [2]
|
2022-08-12 10:16:15 -07:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2022-08-14 12:03:16 -07:00
|
|
|
[0] naive timestamp if "timestamp" field is parsable, else null
|
2022-11-16 10:01:58 +08:00
|
|
|
[1] timezone aware timestamp available for UTC, else null
|
2022-08-15 15:39:20 -07:00
|
|
|
[2] this field exists if the syslog line is not parsable. The value
|
|
|
|
is the original syslog line.
|
2022-08-14 12:03:16 -07:00
|
|
|
|
2022-08-12 10:16:15 -07:00
|
|
|
Examples:
|
|
|
|
|
2022-08-16 16:33:50 -07:00
|
|
|
$ cat syslog.txt | jc --syslog -p
|
2022-08-14 12:03:16 -07:00
|
|
|
[
|
|
|
|
{
|
|
|
|
"priority": 35,
|
|
|
|
"version": 1,
|
|
|
|
"timestamp": "2003-10-11T22:14:15.003Z",
|
|
|
|
"hostname": "mymachine.example.com",
|
|
|
|
"appname": "evntslog",
|
|
|
|
"proc_id": null,
|
|
|
|
"msg_id": "ID47",
|
|
|
|
"structured_data": [
|
|
|
|
{
|
|
|
|
"identity": "exampleSDID@32473",
|
2022-08-14 12:18:22 -07:00
|
|
|
"parameters": {
|
2022-08-14 12:03:16 -07:00
|
|
|
"iut": "3",
|
|
|
|
"eventSource": "Application",
|
|
|
|
"eventID": "1011"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"identity": "examplePriority@32473",
|
2022-08-14 12:18:22 -07:00
|
|
|
"parameters": {
|
2022-08-14 12:03:16 -07:00
|
|
|
"class": "high"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"message": "unauthorized attempt",
|
|
|
|
"timestamp_epoch": 1065935655,
|
|
|
|
"timestamp_epoch_utc": 1065910455
|
|
|
|
}
|
|
|
|
]
|
2022-08-12 10:16:15 -07:00
|
|
|
|
2022-08-16 16:33:50 -07:00
|
|
|
$ cat syslog.txt | jc --syslog -p -r
|
2022-08-14 12:03:16 -07:00
|
|
|
[
|
|
|
|
{
|
|
|
|
"priority": "35",
|
|
|
|
"version": "1",
|
|
|
|
"timestamp": "2003-10-11T22:14:15.003Z",
|
|
|
|
"hostname": "mymachine.example.com",
|
|
|
|
"appname": "evntslog",
|
|
|
|
"proc_id": null,
|
|
|
|
"msg_id": "ID47",
|
2022-08-14 12:11:31 -07:00
|
|
|
"structured_data": "[exampleSDID@32473 iut=\\"3\\" eventSource...",
|
2022-08-14 12:03:16 -07:00
|
|
|
"message": "unauthorized attempt"
|
|
|
|
}
|
|
|
|
]
|
2022-08-12 10:16:15 -07:00
|
|
|
|
|
|
|
<a id="jc.parsers.syslog.parse"></a>
|
|
|
|
|
|
|
|
### parse
|
|
|
|
|
|
|
|
```python
|
|
|
|
def parse(data: str, raw: bool = False, quiet: bool = False) -> List[Dict]
|
|
|
|
```
|
|
|
|
|
|
|
|
Main text parsing function
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
|
|
|
data: (string) text data to parse
|
|
|
|
raw: (boolean) unprocessed output if True
|
|
|
|
quiet: (boolean) suppress warning messages if True
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
List of Dictionaries. Raw or processed structured data.
|
|
|
|
|
|
|
|
### Parser Information
|
|
|
|
Compatibility: linux, darwin, cygwin, win32, aix, freebsd
|
|
|
|
|
|
|
|
Version 1.0 by Kelly Brazil (kellyjonbrazil@gmail.com)
|