-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Adrian Moreno <[email protected]>
- Loading branch information
Showing
5 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Python bindings | ||
|
||
Besides the basic post-processing commands provided by `retis` (e.g: `sort`), | ||
python bindings exist to enable writing custom post-processing scripts. | ||
|
||
These bindings can be used in two different ways: the built-in python shell and | ||
the external python library. | ||
|
||
## Overview | ||
|
||
Python bindings currently provide 3 basic python classes that allow inspecting | ||
retis events: | ||
|
||
- **Event**: Python representation of a retis event. It provides helpers to | ||
access the event's sections and data within those sections. | ||
- **EventSeries**: Python representation of a series of sorted events resulting | ||
from the execution of `retis sort -o`. It implements the iterator protocol to | ||
access the events. | ||
- **EventReader**: Class capable of reading a file created by retis and iterate | ||
over the events or series it contains. It implements the iterator protocol to | ||
access the events. | ||
|
||
More details can be found in the `retis_events` crate documentation. | ||
|
||
## Builtin shell | ||
|
||
The builtin shell, although basic, it enables quick event inspection. | ||
Once you drop into the shell, a global variable called `events` is available. | ||
It is of type `EventReader`. | ||
|
||
``` python | ||
$ retis python | ||
>>> for event in events: | ||
>>> if "skb" in event and "tcp" in event["skb"].raw(): | ||
>>> print("TCP event with dport: {}".format( | ||
>>> event["skb"].raw()["tcp"]["dport"]) | ||
``` | ||
|
||
## Python library | ||
|
||
For more sophisticated programs, a python library is available in | ||
[pypi](https://pypi.org/retis). Unlike the builtin command, in this case the | ||
`EventReader` has to be created manually. | ||
|
||
```python | ||
from retis import EventReader | ||
|
||
import statistics | ||
|
||
reader = EventReader("sorted_events.json") | ||
|
||
events_per_series = [len(s) for s in reader] | ||
|
||
print("Number of series: {}".format(len(events_per_series))) | ||
print("Average events per series: {}".format(statistics.mean(events_per_series))) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters