diff --git a/.gitignore b/.gitignore index 0de3e097..d4a337d2 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ logs build .idea .DS_Store +dist \ No newline at end of file diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 00000000..2f398179 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,47 @@ +# This is an example goreleaser.yaml file with some sane defaults. +# Make sure to check the documentation at http://goreleaser.com +project_name: execbeat +before: + hooks: + # You may remove this if you don't use go modules. + # - go mod download + # you may remove this if you don't need go generate + # - go generate ./... +builds: + + - env: + - CGO_ENABLED=0 + goos: + - linux + - windows + - darwin + goarch: + - amd64 + - arm + - arm64 + - 386 + goarm: + - 6 + - 7 +archives: + - format: zip +checksum: + name_template: 'checksums.txt' +snapshot: + name_template: "{{ .Tag }}-next" +changelog: + sort: asc + filters: + exclude: + - '^docs:' + - '^test:' + +nfpms: + - + package_name: execbeat + file_name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}" + maintainer: Lavish Jain + formats: + - deb + - rpm + - exe diff --git a/beater/execbeat.go b/beater/execbeat.go index 15f8e8a7..7a2ab16c 100644 --- a/beater/execbeat.go +++ b/beater/execbeat.go @@ -2,12 +2,13 @@ package beat import ( "fmt" - "github.com/christiangalsterer/execbeat/config" + "github.com/elastic/beats/libbeat/beat" "github.com/elastic/beats/libbeat/cfgfile" "github.com/elastic/beats/libbeat/common" "github.com/elastic/beats/libbeat/logp" "github.com/elastic/beats/libbeat/publisher" + "github.com/sonnylaskar/execbeat/config" ) type Execbeat struct { diff --git a/beater/execevent.go b/beater/execevent.go index 701efcc6..ab3522cf 100644 --- a/beater/execevent.go +++ b/beater/execevent.go @@ -17,6 +17,8 @@ type Exec struct { StdOut string `json:"stdout"` StdErr string `json:"stderr,omitempty"` ExitCode int `json:"exitCode"` + Args string `json:"args,omitempty"` + Name string `json:"name"` } func (h *ExecEvent) ToMapStr() common.MapStr { diff --git a/beater/executor.go b/beater/executor.go index 97802ae2..2b5c1043 100644 --- a/beater/executor.go +++ b/beater/executor.go @@ -2,13 +2,14 @@ package beat import ( "bytes" - "github.com/christiangalsterer/execbeat/config" - "github.com/elastic/beats/libbeat/logp" - "github.com/robfig/cron" "os/exec" "strings" "syscall" "time" + + "github.com/elastic/beats/libbeat/logp" + "github.com/robfig/cron" + "github.com/sonnylaskar/execbeat/config" ) type Executor struct { @@ -42,11 +43,13 @@ func (e *Executor) Run() { if e.config.Schedule != "" { logp.Debug("Execbeat", "Use schedule: [%w]", e.config.Schedule) e.schedule = e.config.Schedule + cron := cron.New() + cron.AddFunc(e.schedule, func() { e.runOneTime() }) + cron.Start() + } else { + e.runOneTime() } - cron := cron.New() - cron.AddFunc(e.schedule, func() { e.runOneTime() }) - cron.Start() } func (e *Executor) runOneTime() error { @@ -59,7 +62,7 @@ func (e *Executor) runOneTime() error { var exitCode int = 0 cmdName := strings.TrimSpace(e.config.Command) - + name := strings.TrimSpace(e.config.Name) args := strings.TrimSpace(e.config.Args) if len(args) > 0 { cmdArgs = strings.Split(args, " ") @@ -97,6 +100,8 @@ func (e *Executor) runOneTime() error { commandEvent := Exec{ Command: cmdName, + Args: args, + Name: name, StdOut: stdout.String(), StdErr: stderr.String(), ExitCode: exitCode, @@ -108,8 +113,8 @@ func (e *Executor) runOneTime() error { Fields: e.config.Fields, Exec: commandEvent, } - e.execbeat.client.PublishEvent(event.ToMapStr()) + // fmt.Println(event.ToMapStr()) return nil } diff --git a/config/config.go b/config/config.go index 651b39e4..ad678332 100644 --- a/config/config.go +++ b/config/config.go @@ -2,7 +2,7 @@ package config // Defaults for config variables which are not set const ( - DefaultSchedule string = "@every 1m" + DefaultSchedule string = "" DefaultDocumentType string = "execbeat" ) @@ -16,6 +16,7 @@ type ExecConfig struct { Args string DocumentType string `config:"document_type"` Fields map[string]string `config:"fields"` + Name string `config:"name"` } type ConfigSettings struct { diff --git a/execbeat.full.yml b/execbeat.full.yml index 9328399e..306b7aad 100644 --- a/execbeat.full.yml +++ b/execbeat.full.yml @@ -7,19 +7,28 @@ execbeat: # Each - Commands to execute. - # Optional cron expression, defines when to execute the command. - # Default is every 1 minute. + # If schedule not mentioned, command will run only once #schedule: # The command to execute by Execbeat command: echo # Optional arguments to be passed to the command to execute - args: "Hello World" + args: "Will Run only once" + + # Name given to command to distinguish between two similar commands for different contexts + name: "echoOnce" # Type to be published in the 'type' field. For Elasticsearch output, # the type defines the document type these entries should be stored # in. Default: execbeat document_type: execbeat + - + command: echo + schedule: "* * * * * *" + args: "Will run every second" + name: "echoPeriod" + document_type: execbeat #================================ General ====================================== diff --git a/execbeat.yml b/execbeat.yml index 1fb1151c..97ae4ff7 100644 --- a/execbeat.yml +++ b/execbeat.yml @@ -7,11 +7,15 @@ execbeat: # Each - Commands to execute. - # Optional cron expression, defines when to execute the command. - # Default is every 1 minute. - #schedule: + schedule: "* * * * * *" # The command to execute by Execbeat - command: date + command: echo + args: "Will run every second" + - + command: echo + args: "Will run only once" + # Optional arguments to be passed to the command to execute #args: diff --git a/main.go b/main.go index 96281dc3..383a73e3 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,13 @@ package main import ( - execbeat "github.com/christiangalsterer/execbeat/beater" - "github.com/elastic/beats/libbeat/beat" "os" + + "github.com/elastic/beats/libbeat/beat" + execbeat "github.com/sonnylaskar/execbeat/beater" ) -var version = "3.3.0" +var version = "3.3.2" var name = "execbeat" func main() { diff --git a/version.go b/version.go index d044a326..b71aa43f 100644 --- a/version.go +++ b/version.go @@ -1,3 +1,3 @@ package main -const appVersion = "3.3.0" +const appVersion = "3.3.2"