-
-
Notifications
You must be signed in to change notification settings - Fork 14.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
nixos/packetbeat: Add basic module for packetbeat #94862
Changes from 1 commit
77293ba
8401161
e68e048
5a1a7d3
d2d24c9
de1c054
4c82b0c
d38821a
1c47df2
de5d2d6
54eacc7
9fb67a2
8bc01e4
cc4f18e
9a1f609
4a4e642
1965a24
3db58a2
9d0d3a0
f623e0f
713b162
5bc4467
1a68e21
c66477b
d8d4fc6
34c2ad1
895f0ce
f68e684
e0baa46
018f086
ae5bd28
de45bbe
2c152ee
84d0f3f
f1a29ad
b268e89
8511890
128e0ec
765d037
f333296
df8eed0
745a867
910ac86
f20daaa
c5dcb96
ee0d559
27b0c4b
4034145
18348c7
31008a8
32f5e5d
8d3123d
09c383c
259aaf6
a38a959
3d3eef2
ab525fa
e5bb747
f9bf64f
b2679e8
f145223
dff0034
8739e42
7f9acb7
df58a63
a47d059
9c6f11f
3e687bf
3678223
43afce4
8e6f466
304de03
3a9b91b
e1d6f7c
634d404
008f45f
0d820de
eef5656
e98facb
063692b
4f61203
ce47cdb
4e94600
f1b9dcd
a2842c7
1b235b4
e14d5a2
135a6f8
d6c7a19
8761381
55e8cbd
2eacb60
4adac63
f7100e6
d46e488
742c734
02a2649
7b73713
96bc644
aadbc19
412a28d
bf007a2
e30287c
a213818
d217a00
766958b
31772af
596de92
d43db8a
f806b3c
f928b91
6ff5c40
53bce4d
9fd106a
eda3730
31cb1dc
970bfc0
556b29d
6592980
c643d58
7ff50a7
b9326ec
c25a7cd
baf51e4
d1ffe81
30f07d1
987f1d5
7992188
423fc3f
7a4e3e5
40a3b21
bd3583f
ac96859
1272d20
9c49998
0865dc9
e5aeed5
94c2122
8cf4ec8
96ea00a
17d334e
e807447
e0aea88
653d925
80f1cc1
59f5cbd
6b043b1
3a6cea9
328e886
f6c94e7
1c55613
cc0ad3f
d86a966
8272eff
4e7728e
e5ec35b
4030b3f
982b8ff
ea5d0dc
c941a63
0fdc832
8f43f32
9ac15bd
04e0c46
0fd664e
41c29f2
af189fb
48b902d
915ee2f
565d04a
584315b
1dcf64b
476216e
219b7f7
6d41460
14b70e8
01ed700
417d98f
678e918
e8306e3
781b32a
81fcdbb
c7e7a3c
3d29275
b346c10
23ebed8
fd45756
3a3077a
25edae3
762434f
4906881
f3bf0f1
4333138
8003bb3
fd457ec
e385f98
cadc152
c7e8da6
5829744
a1b634b
1a3ebe5
740b2f8
adcb00e
f9f84cf
ee7e23f
2b8a771
5079e1c
16592e5
8a28f7f
b468834
58e585c
c2d50fb
9f5aed0
49b2bc6
4c15e6d
1cc8a03
8646c47
2a13231
e883800
bbdb8b0
b8a0cfd
a24cac6
bd3afd6
4bec5d5
d1e19e7
ea14b3a
3e65293
49f820d
17994f1
1664a17
3e49a3a
6779594
f5e5b57
93346d7
61d7945
8c7ed97
28d7bc1
9dc1d6a
84e2a0d
c9745a5
d9c33b2
f2e98e8
951efe4
b23f144
d32c437
712f366
e3e39aa
06e8f13
9c1e96f
c5646ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
{ config, lib, pkgs, ... }: | ||
|
||
with lib; | ||
|
||
let | ||
cfg = config.services.packetbeat; | ||
|
||
packetbeatYml = pkgs.writeText "packetbeat.yml" '' | ||
name: ${cfg.name} | ||
tags: ${builtins.toJSON cfg.tags} | ||
|
||
${cfg.configFlows} | ||
${cfg.configProtocols} | ||
${cfg.extraConfig} | ||
''; | ||
|
||
in | ||
{ | ||
options = { | ||
|
||
services.packetbeat = { | ||
|
||
enable = mkEnableOption "packetbeat"; | ||
|
||
package = mkOption { | ||
type = types.package; | ||
default = pkgs.packetbeat; | ||
defaultText = "pkgs.packetbeat"; | ||
example = literalExample "pkgs.packetbeat7"; | ||
description = '' | ||
The packetbeat package to use | ||
''; | ||
}; | ||
|
||
name = mkOption { | ||
type = types.str; | ||
default = "packetbeat"; | ||
description = "Name of the beat"; | ||
}; | ||
|
||
tags = mkOption { | ||
type = types.listOf types.str; | ||
default = []; | ||
description = "Tags to place on the shipped log messages"; | ||
}; | ||
|
||
stateDir = mkOption { | ||
lejonet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type = types.str; | ||
default = "packetbeat"; | ||
description = '' | ||
Directory below <literal>/var/lib/</literal> to store packetbeat's | ||
own logs and other data. This directory will be created automatically | ||
using systemd's StateDirectory mechanism. | ||
''; | ||
}; | ||
|
||
configFlows = mkOption { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is just yaml, this was a quick way of getting the module usable. My plan was to first get something usable, then refactor it to be smart too. |
||
type = types.lines; | ||
default = '' | ||
packebeat.flows: | ||
timeout: 30s | ||
period: 10s | ||
''; | ||
description = '' | ||
Configuration of how packetbeat should handle flows. See | ||
<link xlink:href='https://www.elastic.co/guide/en/beats/packetbeat/current/configuration-flows.html'/> | ||
for all available configuration options. | ||
''; | ||
}; | ||
|
||
configProtocols = mkOption { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this benefit from a structured type? Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not impossible, its purely yaml it needs to generate, so a structured type would probably fit. As said in other comments, this was a quick way to getting it usable. |
||
type = types.lines; | ||
default = '' | ||
packetbeat.protocols: | ||
- type: icmp | ||
enabled: true | ||
- type: amqp | ||
ports: [5672] | ||
- type: cassandra | ||
ports: [9042] | ||
- type: dhcpv4 | ||
ports: [67, 68] | ||
- type: dns | ||
ports: [53] | ||
- type: http | ||
ports: [80, 8080, 8000, 5000, 8002] | ||
- type: memcache | ||
ports: [11211] | ||
- type: mysql | ||
ports: [3306,3307] | ||
- type: pgsql | ||
ports: [5432] | ||
- type: redis | ||
ports: [6379] | ||
- type: thrift | ||
ports: [9090] | ||
- type: mongodb | ||
ports: [27017] | ||
- type: nfs | ||
ports: [2049] | ||
- type: tls | ||
ports: | ||
- 443 # HTTPS | ||
- 993 # IMAPS | ||
- 995 # POP3S | ||
- 5223 # XMPP over SSL | ||
- 8443 | ||
- 8883 # Secure MQTT | ||
- 9243 # Elasticsearch | ||
|
||
''; | ||
description = '' | ||
Configuration of what protocols packetbeat should gather info about. | ||
See <link xlink:href='https://www.elastic.co/guide/en/beats/packetbeat/current/configuration-protocols.html'/> | ||
for the configuration options available. | ||
''; | ||
}; | ||
|
||
extraConfig = mkOption { | ||
type = types.lines; | ||
default = '' | ||
packetbeat.interfaces.device: any | ||
|
||
setup.template.settings: | ||
index.number_of_shards: 1 | ||
|
||
setup.kibana: | ||
host: "localhost:5601" | ||
|
||
output.elasticsearch: | ||
hosts: ["localhost:9200"] | ||
|
||
processors: | ||
- # Add forwarded to tags when processing data from a network tap or mirror. | ||
if.contains.tags: forwarded | ||
then: | ||
- drop_fields: | ||
fields: [host] | ||
else: | ||
- add_host_metadata: ~ | ||
- add_cloud_metadata: ~ | ||
- add_docker_metadata: ~ | ||
''; | ||
description = "Any other configuration options you want to add"; | ||
}; | ||
|
||
}; | ||
}; | ||
|
||
config = mkIf cfg.enable { | ||
|
||
assertions = [ | ||
{ | ||
assertion = !hasPrefix "/" cfg.stateDir; | ||
message = | ||
"The option services.packetbeat.stateDir shouldn't be an absolute directory." + | ||
" It should be a directory relative to /var/lib/."; | ||
} | ||
{ | ||
assertion = cfg.configProtocols != "" || cfg.configFlows != ""; | ||
message = | ||
"The options services.packetbeat.configProtocols and/or services.packetbeat.configFlows should" + | ||
" be set or else packetbeat won't do anything useful and error out."; | ||
} | ||
]; | ||
|
||
systemd.services.packetbeat = { | ||
description = "Packetbeat log shipper"; | ||
wantedBy = [ "multi-user.target" ]; | ||
preStart = '' | ||
mkdir -p ${cfg.stateDir}/data | ||
mkdir -p ${cfg.stateDir}/logs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to log to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is an option to log to stderr (-e) that could be used in ExecStart There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is an option to log to stderr that should make it possible for journald to pick it up (-e). |
||
''; | ||
serviceConfig = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do not run as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is completely copied from the official unit, packetbeat needs to run as root because it captures packets from interfaces. I dunno if CAP_NET_ADMIN might be enough instead of having root, but this is how the official systemd unit does it. |
||
StateDirectory = cfg.stateDir; | ||
ExecStart = '' | ||
${cfg.package}/bin/packetbeat \ | ||
-c ${packetbeatYml} \ | ||
-path.data /var/lib/${cfg.stateDir}/data \ | ||
-path.logs /var/lib/${cfg.stateDir}/logs''; | ||
Restart = "always"; | ||
}; | ||
}; | ||
}; | ||
} |
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.
See comment below about a
settings
option which could replace this.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.
I will look into that RFC, I've added the configFile option, that short-circuits the config options in the module, and lets the user handle the configuration file as they please.