-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move tcp protocol generator to packetbeat (#3447)
The tcp procol generator is packetbeat specific. Similar to module and metricset generator it belongs inside the beat. * The generator was migrated from cookiecutter to a python script to not have additional dependency. * A makefile target was added to simplify the generation In the future collect should fetch all protocols and add them automatically to the import to have it the same as for metricbeat. In addition it should be possible based on the global generator to create a packetbeat "shell" to put in own protocols.
- Loading branch information
Showing
9 changed files
with
135 additions
and
50 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,87 @@ | ||
import os | ||
import argparse | ||
|
||
# Creates a tcp protocol | ||
|
||
protocol = "" | ||
plugin_type = "" | ||
plugin_var = "" | ||
|
||
def generate_protocol(): | ||
read_input() | ||
process_file() | ||
|
||
def read_input(): | ||
"""Requests input form the command line for empty variables if needed. | ||
""" | ||
global protocol, plugin_type, plugin_var | ||
|
||
if protocol == "": | ||
protocol = raw_input("Protocol Name [exampletcp]: ") or "exampletcp" | ||
|
||
protocol = protocol.lower() | ||
|
||
plugin_type = protocol + "Plugin" | ||
plugin_var = protocol[0] + "p" | ||
|
||
|
||
def process_file(): | ||
|
||
# Load path information | ||
generator_path = os.path.dirname(os.path.realpath(__file__)) | ||
go_path = os.environ['GOPATH'] | ||
|
||
for root, dirs, files in os.walk(generator_path + '/tcp-protocol/{protocol}'): | ||
|
||
for file in files: | ||
|
||
full_path = root + "/" + file | ||
|
||
## load file | ||
content = "" | ||
with open(full_path) as f: | ||
content = f.read() | ||
|
||
# process content | ||
content = replace_variables(content) | ||
|
||
# Write new path | ||
new_path = replace_variables(full_path).replace(".go.tmpl", ".go") | ||
|
||
# remove generator info from path | ||
file_path = new_path.replace(generator_path + "/tcp-protocol/", "") | ||
|
||
# New file path to write file content to | ||
write_file = "protos/" + file_path | ||
|
||
# Create parent directory if it does not exist yet | ||
dir = os.path.dirname(write_file) | ||
if not os.path.exists(dir): | ||
os.makedirs(dir) | ||
|
||
# Write file to new location | ||
with open(write_file, 'w') as f: | ||
f.write(content) | ||
|
||
def replace_variables(content): | ||
"""Replace all template variables with the actual values | ||
""" | ||
return content.replace("{protocol}", protocol) \ | ||
.replace("{plugin_var}", plugin_var) \ | ||
.replace("{plugin_type}", plugin_type) | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
parser = argparse.ArgumentParser(description="Creates a beat") | ||
parser.add_argument("--protocol", help="Protocol name") | ||
|
||
args = parser.parse_args() | ||
|
||
|
||
if args.protocol is not None: | ||
protocol = args.protocol | ||
|
||
generate_protocol() | ||
|
||
|
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
8 changes: 4 additions & 4 deletions
8
...rotocol/{{cookiecutter.module}}/config.go → ...ts/tcp-protocol/{protocol}/config.go.tmpl
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 |
---|---|---|
@@ -1,22 +1,22 @@ | ||
package {{ cookiecutter.module }} | ||
package {protocol} | ||
|
||
import ( | ||
"github.com/elastic/beats/packetbeat/config" | ||
"github.com/elastic/beats/packetbeat/protos" | ||
) | ||
|
||
type {{ cookiecutter.module }}Config struct { | ||
type {protocol}Config struct { | ||
config.ProtocolCommon `config:",inline"` | ||
} | ||
|
||
var ( | ||
defaultConfig = {{ cookiecutter.module }}Config{ | ||
defaultConfig = {protocol}Config{ | ||
ProtocolCommon: config.ProtocolCommon{ | ||
TransactionTimeout: protos.DefaultTransactionExpiration, | ||
}, | ||
} | ||
) | ||
|
||
func (c *{{ cookiecutter.module }}Config) Validate() error { | ||
func (c *{protocol}Config) Validate() error { | ||
return nil | ||
} |
2 changes: 1 addition & 1 deletion
2
...rotocol/{{cookiecutter.module}}/parser.go → ...ts/tcp-protocol/{protocol}/parser.go.tmpl
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package {{ cookiecutter.module }} | ||
package {protocol} | ||
|
||
import ( | ||
"errors" | ||
|
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
2 changes: 1 addition & 1 deletion
2
...protocol/{{cookiecutter.module}}/trans.go → ...pts/tcp-protocol/{protocol}/trans.go.tmpl
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package {{ cookiecutter.module }} | ||
package {protocol} | ||
|
||
import ( | ||
"time" | ||
|
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
868b622
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand correctly, this commit removes the dependency on cookiecutter.
I noticed that the docs located here reflect this change, but those located here do not. Hope this helps!
868b622
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rwaweber The reason for the difference is that the second link goes to
current
which is today 5.1 branch. This branch doc must be updated with a line to checkout the 5.1 branch (will happen this week). If you use this link instead which points to master, you should see the new docs. https://www.elastic.co/guide/en/beats/libbeat/master/newbeat-generate.html#newbeat-generate