Skip to content
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

LogOutputHook Feature #24

Merged
merged 1 commit into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,22 @@ outputs:
# nfdump -r /tmp/netflow/nfcapd.202207101030 -o extended
- log:
file: /tmp/flow.log

# hooks:
# - name: hostname addition
# command: /usr/bin/hook_command_example_hostname.sh
# - name: shell to resolve hostname
# shell: |
# #!/bin/sh
# echo `cat` | jq --arg hostname $(hostname) '. + {hostname: $hostname}'
# - name: shell to resolve ifname from ifindex
# shell: |
# #!/bin/sh
# IN=$(cat)
# I_IDX=$(echo $IN | jq .ingressIfindex -r)
# E_IDX=$(echo $IN | jq .egressIfindex -r )
# I_NAME=$(ip -n ns0 -j link | jq --argjson idx $I_IDX '.[] | select(.ifindex == $idx) | .ifname' -r)
# E_NAME=$(ip -n ns0 -j link | jq --argjson idx $E_IDX '.[] | select(.ifindex == $idx) | .ifname' -r)
# echo $IN | jq --arg i_name $I_NAME --arg e_name $E_NAME '. + {ingressIfname: $i_name, egressIfname: $e_name}'
templates:
- id: 1001
template:
Expand Down
23 changes: 23 additions & 0 deletions examples/agent_output_hook/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
maxIpfixMessageLen: 100
timerFinishedDrainSeconds: 1
timerForceDrainSeconds: 30
timerTemplateFlushSeconds: 60
outputs:
- log:
file: /tmp/flowlog.json
hooks:
- name: hostname addition
command: ./misc/hook_command_example_dummy.sh
- name: shell to resolve hostname
shell: |
#!/bin/sh
echo `cat` | jq --arg hostname $(hostname) '. + {hostname: $hostname}'
- name: shell to resolve ifname from ifindex
shell: |
#!/bin/sh
IN=$(cat)
I_IDX=$(echo $IN | jq .ingressIfindex -r)
E_IDX=$(echo $IN | jq .egressIfindex -r )
I_NAME=$(ip -n ns0 -j link | jq --argjson idx $I_IDX '.[] | select(.ifindex == $idx) | .ifname' -r)
E_NAME=$(ip -n ns0 -j link | jq --argjson idx $E_IDX '.[] | select(.ifindex == $idx) | .ifname' -r)
echo $IN | jq --arg i_name $I_NAME --arg e_name $E_NAME '. + {ingressIfname: $i_name, egressIfname: $e_name}'
46 changes: 46 additions & 0 deletions examples/agent_output_hook/flowlog.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"level": "info",
"ts": 1667266821.7434478,
"caller": "flowctl/ipfix.go:430",
"msg": "flowlog",
"bytes": 594,
"finished": 1,
"foo": "bar",
"pkts": 6,
"proto": 6,
"start": 12168400776634952,
"action": 0,
"ingressIfindex": 3,
"dport": 32816,
"egressIfindex": 2,
"hostname": "slankdev",
"src": "10.2.0.2",
"ingressIfname": "eth2",
"egressIfname": "eth1",
"dst": "10.1.0.2",
"sport": 8080,
"end": 12168400778056844
}
{
"level": "info",
"ts": 1667266821.9629853,
"caller": "flowctl/ipfix.go:430",
"msg": "flowlog",
"dport": 8080,
"dst": "10.2.0.2",
"proto": 6,
"src": "10.1.0.2",
"finished": 1,
"sport": 32816,
"start": 12168400776608754,
"ingressIfname": "eth1",
"egressIfindex": 3,
"foo": "bar",
"egressIfname": "eth2",
"pkts": 6,
"action": 0,
"bytes": 481,
"end": 12168400778037424,
"hostname": "slankdev",
"ingressIfindex": 2
}
18 changes: 18 additions & 0 deletions misc/hook_command_example_dummy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh
# IN:
# {
# "src": "10.1.0.1",
# "dst": "10.2.0.1",
# "pkts": 10,
# "bytes": 1000
# }
#
# OUT:
# {
# "src": "10.1.0.1",
# "dst": "10.2.0.1",
# "pkts": 10,
# "bytes": 1000,
# "foo": "bar"
# }
echo `cat` | jq '. + {foo: "bar"}'
18 changes: 18 additions & 0 deletions misc/hook_command_example_hostname.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh
# IN:
# {
# "src": "10.1.0.1",
# "dst": "10.2.0.1",
# "pkts": 10,
# "bytes": 1000
# }
#
# OUT:
# {
# "src": "10.1.0.1",
# "dst": "10.2.0.1",
# "pkts": 10,
# "bytes": 1000,
# "hostname": "machine1"
# }
echo `cat` | jq --arg hostname $(hostname) '. + {hostname: $hostname}'
24 changes: 24 additions & 0 deletions misc/hook_command_example_ifname.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh
# IN:
# {
# "ingressIfindex": 1,
# "egressIfindex": 2,
# "pkts": 10,
# "bytes": 1000
# }
#
# OUT:
# {
# "ingressIfindex": 1,
# "egressIfindex": 2,
# "ingressIfname": 1,
# "egressIfname": 2,
# "pkts": 10,
# "bytes": 1000
# }
IN=$(cat)
I_IDX=$(echo $IN | jq .ingressIfindex -r)
E_IDX=$(echo $IN | jq .egressIfindex -r )
I_NAME=$(ip -n ns0 -j link | jq --argjson idx $I_IDX '.[] | select(.ifindex == $idx) | .ifname' -r)
E_NAME=$(ip -n ns0 -j link | jq --argjson idx $E_IDX '.[] | select(.ifindex == $idx) | .ifname' -r)
echo $IN | jq --arg i_name $I_NAME --arg e_name $E_NAME '. + {ingressIfname: $i_name, egressIfname: $e_name}'
44 changes: 29 additions & 15 deletions pkg/ebpfmap/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,20 +308,34 @@ func ToIpfixFlowFile(ebflows []Flow) (*ipfix.FlowFile, error) {
return flowFile, nil
}

func (f Flow) ToZap() []interface{} {
return []interface{}{
"src", util.ConvertUint32ToIP(f.Key.Saddr).String(),
"dst", util.ConvertUint32ToIP(f.Key.Daddr).String(),
"proto", f.Key.Proto,
"sport", f.Key.Sport,
"dport", f.Key.Dport,
"ingressIfindex", f.Key.IngressIfindex,
"egressIfindex", f.Key.EgressIfindex,
"pkts", f.Val.FlowPkts,
"bytes", f.Val.FlowBytes,
"action", f.Key.Mark,
"start", f.Val.FlowStartMilliSecond,
"end", f.Val.FlowEndMilliSecond,
"finished", f.Val.Finished,
func (f Flow) ToZap(o ipfix.OutputLog) ([]interface{}, error) {
m := map[string]interface{}{
"src": util.ConvertUint32ToIP(f.Key.Saddr).String(),
"dst": util.ConvertUint32ToIP(f.Key.Daddr).String(),
"proto": f.Key.Proto,
"sport": f.Key.Sport,
"dport": f.Key.Dport,
"ingressIfindex": f.Key.IngressIfindex,
"egressIfindex": f.Key.EgressIfindex,
"pkts": f.Val.FlowPkts,
"bytes": f.Val.FlowBytes,
"action": f.Key.Mark,
"start": f.Val.FlowStartMilliSecond,
"end": f.Val.FlowEndMilliSecond,
"finished": f.Val.Finished,
}

for _, h := range o.Hooks {
var err error
m, err = h.Execute(m)
if err != nil {
return nil, err
}
}

ret := []interface{}{}
for key, val := range m {
ret = append(ret, key, val)
}
return ret, nil
}
14 changes: 9 additions & 5 deletions pkg/flowctl/ipfix.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func fnIpfixDump(cmd *cobra.Command, args []string) error {
return fmt.Errorf("invalid config")
}
if o.Log != nil {
if err := FlowOutputLog(ebpfFlows, o.Log.File); err != nil {
if err := FlowOutputLog(ebpfFlows, o.Log.File, *o.Log); err != nil {
return err
}
}
Expand Down Expand Up @@ -345,7 +345,7 @@ func flushCachesFinished(config ipfix.Config) error {
return fmt.Errorf("invalid config")
}
if o.Log != nil {
if err := FlowOutputLog(ebpfFlows, o.Log.File); err != nil {
if err := FlowOutputLog(ebpfFlows, o.Log.File, *o.Log); err != nil {
return err
}
}
Expand Down Expand Up @@ -385,7 +385,7 @@ func flushCaches(config ipfix.Config) error {
return fmt.Errorf("invalid config")
}
if o.Log != nil {
if err := FlowOutputLog(ebpfFlows, o.Log.File); err != nil {
if err := FlowOutputLog(ebpfFlows, o.Log.File, *o.Log); err != nil {
return err
}
}
Expand Down Expand Up @@ -415,7 +415,7 @@ func flushCaches(config ipfix.Config) error {
return nil
}

func FlowOutputLog(flows []ebpfmap.Flow, out string) error {
func FlowOutputLog(flows []ebpfmap.Flow, out string, o ipfix.OutputLog) error {
cfg := zap.NewProductionConfig()
cfg.OutputPaths = []string{
out,
Expand All @@ -427,7 +427,11 @@ func FlowOutputLog(flows []ebpfmap.Flow, out string) error {
log := zapr.NewLogger(zapLog)

for _, flow := range flows {
log.Info("flowlog", flow.ToZap()...)
args, err := flow.ToZap(o)
if err != nil {
return err
}
log.Info("flowlog", args...)
}
return nil
}
56 changes: 56 additions & 0 deletions pkg/hook/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2022 Hiroki Shirokura.
Copyright 2022 LINE Corporation.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package hook

import (
"bytes"
"encoding/json"
"fmt"
"os/exec"
)

type Command string

var _ Hook = (*Command)(nil)

func (c Command) Execute(in map[string]interface{}) (map[string]interface{}, error) {
// Prepare input/output
stdoutbuf := bytes.Buffer{}
stderrbuf := bytes.Buffer{}
stdinbytes, err := json.Marshal(in)
if err != nil {
return nil, err
}

// Execute child process
cmd := exec.Command(string(c))
cmd.Stdout = &stdoutbuf
cmd.Stderr = &stderrbuf
cmd.Stdin = bytes.NewBuffer(stdinbytes)
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("child process is failed: err=%v stderr=%s",
err, stderrbuf.String())
}

// Convert back to map data from json-bytes
out := map[string]interface{}{}
if err := json.Unmarshal(stdoutbuf.Bytes(), &out); err != nil {
return nil, err
}
return out, nil
}
22 changes: 22 additions & 0 deletions pkg/hook/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Copyright 2022 Hiroki Shirokura.
Copyright 2022 LINE Corporation.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package hook

type Hook interface {
Execute(in map[string]interface{}) (map[string]interface{}, error)
}
Loading