Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ logs
build
.idea
.DS_Store
dist
47 changes: 47 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -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 <lavishrjain1997@gmail.com>
formats:
- deb
- rpm
- exe
3 changes: 2 additions & 1 deletion beater/execbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions beater/execevent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 13 additions & 8 deletions beater/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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, " ")
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package config

// Defaults for config variables which are not set
const (
DefaultSchedule string = "@every 1m"
DefaultSchedule string = ""
DefaultDocumentType string = "execbeat"
)

Expand All @@ -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 {
Expand Down
13 changes: 11 additions & 2 deletions execbeat.full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ======================================

Expand Down
10 changes: 7 additions & 3 deletions execbeat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

const appVersion = "3.3.0"
const appVersion = "3.3.2"