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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Elktail major versions follow ElasticSearch versions. Here's the table indicatin

| Elktail | ElasticSearch |
| ------------- | ------------- |
| v6.x.x | >= 6.x.x |
| v5.x.x | >= 5.x.x |
| v1.x.x | 1.x.x, 2.x.x |

Expand Down
64 changes: 32 additions & 32 deletions configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,41 @@
*/

package main

import (
"runtime"
"os"
"encoding/json"
"github.com/urfave/cli"
"io/ioutil"
"github.com/codegangsta/cli"
"os"
"runtime"
)

type SearchTarget struct {
Url string
TunnelUrl string `json:"-"`
TunnelUrl string `json:"-"`
IndexPattern string
}

type QueryDefinition struct {
Terms []string
Format string
TimestampField string
AfterDateTime string `json:"-"`
BeforeDateTime string `json:"-"`
AfterDateTime string `json:"-"`
BeforeDateTime string `json:"-"`
}

type Configuration struct {
SearchTarget SearchTarget
QueryDefinition QueryDefinition
InitialEntries int
ListOnly bool `json:"-"`
ListOnly bool `json:"-"`
User string
Password string `json:"-"`
Verbose bool `json:"-"`
MoreVerbose bool `json:"-"`
TraceRequests bool `json:"-"`
Password string `json:"-"`
Verbose bool `json:"-"`
MoreVerbose bool `json:"-"`
TraceRequests bool `json:"-"`
SSHTunnelParams string
SaveQuery bool `json:"-"`
SaveQuery bool `json:"-"`
}

var confDir = ".elktail"
Expand All @@ -47,8 +48,6 @@ var defaultConfFile = "default.json"
//When changing this array, make sure to also make appropriate changes in CopyConfigRelevantSettingsTo
var configRelevantFlags = []string{"url", "f", "i", "t", "u", "ssh"}



func userHomeDir() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
Expand Down Expand Up @@ -95,58 +94,60 @@ func (c *Configuration) CopyNonConfigRelevantSettingsTo(dest *Configuration) {
dest.TraceRequests = c.TraceRequests
}



func (c *Configuration) SaveDefault() {
confDirPath := userHomeDir() + string(os.PathSeparator) + confDir;
confDirPath := userHomeDir() + string(os.PathSeparator) + confDir
if _, err := os.Stat(confDirPath); os.IsNotExist(err) {
//conf directory doesn't exist, let's create it
err := os.Mkdir(confDirPath, 0700)
if (err != nil) {
if err != nil {
Error.Printf("Failed to create configuration directory %s, %s\n", confDirPath, err)
return
}
}
confJson, err := json.MarshalIndent(c, "", " ")
if (err != nil) {
if err != nil {
Error.Printf("Failed to marshall configuration to json: %s.\n", err)
return
}
confFile := confDirPath + string(os.PathSeparator) + defaultConfFile;
confFile := confDirPath + string(os.PathSeparator) + defaultConfFile
err = ioutil.WriteFile(confFile, confJson, 0700)
if (err != nil) {
if err != nil {
Error.Printf("Failed to save configuration to file %s, %s\n", confFile, err)
return
}
}

func LoadDefault() (conf *Configuration, err error) {
confDirPath := userHomeDir() + string(os.PathSeparator) + confDir;
func LoadDefault() (conf *Configuration, err error) {
confDirPath := userHomeDir() + string(os.PathSeparator) + confDir
if _, err := os.Stat(confDirPath); os.IsNotExist(err) {
//conf directory doesn't exist, let's create it
err := os.Mkdir(confDirPath, 0700)
if (err != nil) {
if err != nil {
return nil, err
}
}
confFile := confDirPath + string(os.PathSeparator) + defaultConfFile;
confFile := confDirPath + string(os.PathSeparator) + defaultConfFile
var config *Configuration
confBytes, err := ioutil.ReadFile(confFile)
if (err != nil) {
if err != nil {
return nil, err
}
err = json.Unmarshal(confBytes, &config)
if (err != nil) {
if err != nil {
return nil, err
}
return config, nil
}


func (config *Configuration) Flags() []cli.Flag {
cli.VersionFlag.Usage = "Print the version"
cli.HelpFlag.Usage = "Show help"
return []cli.Flag {
cli.VersionFlag = cli.BoolFlag{
Name: "print-version, V",
Usage: "Print version only",
}
cli.HelpFlag = cli.BoolFlag{
Name: "Show help",
}
return []cli.Flag{
cli.StringFlag{
Name: "url",
Value: "http://127.0.0.1:9200",
Expand Down Expand Up @@ -248,4 +249,3 @@ func IsConfigRelevantFlagSet(c *cli.Context) bool {
}
return false
}

12 changes: 7 additions & 5 deletions elktail.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ package main
import (
"encoding/json"
"fmt"
"github.com/codegangsta/cli"
"github.com/urfave/cli"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/net/context"
"gopkg.in/olivere/elastic.v5"
"gopkg.in/olivere/elastic.v6"
"io/ioutil"
"net/url"
"os"
Expand Down Expand Up @@ -111,10 +111,14 @@ func NewTail(configuration *Configuration) *Tail {
// Selects appropriate indices in EL based on configuration. This basically means that if query is date filtered,
// then it attempts to select indices in the filtered date range, otherwise it selects the last index.
func (tail *Tail) selectIndices(configuration *Configuration) {
indices, err := tail.client.IndexNames()
result, err := tail.client.CatIndices().Do(context.TODO())
if err != nil {
Error.Fatalln("Could not fetch available indices.", err)
}
indices := make([]string, len(result))
for i, response := range result {
indices[i] = response.Index
}

if configuration.QueryDefinition.IsDateTimeFiltered() {
startDate := configuration.QueryDefinition.AfterDateTime
Expand Down Expand Up @@ -267,7 +271,6 @@ func (tail *Tail) printResult(entry map[string]interface{}) {
value, _ := EvaluateExpression(entry, f[1:])
result = strings.Replace(result, f, value, -1)
}
fmt.Println(result)
}

func (tail *Tail) buildSearchQuery() elastic.Query {
Expand Down Expand Up @@ -469,7 +472,6 @@ func main() {
tail := NewTail(config)
//If we don't exit here we can save the defaults
configToSave.SaveDefault()

tail.Start(!config.IsListOnly(), config.InitialEntries)
}

Expand Down