Skip to content

Commit

Permalink
initial init
Browse files Browse the repository at this point in the history
  • Loading branch information
Jmainguy committed Feb 15, 2020
1 parent 641da59 commit eac5396
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kibanaRefreshFields
45 changes: 45 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
env:
- GO111MODULE=on
before:
hooks:
# you may remove this if you don't use vgo
- go mod tidy
# you may remove this if you don't need go generate
#- go generate ./...
builds:
- env:
- CGO_ENABLED=0
goos:
- linux
- darwin
- windows
goarch:
- amd64
archives:
- name_template: '{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}'
replacements:
darwin: Darwin
linux: Linux
windows: Windows
amd64: x86_64
format_overrides:
- goos: windows
format: zip
nfpms:
- name_template: '{{ .ProjectName }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}'
homepage: https://soh.re
description: Update kibana index fields
maintainer: Jonathan Seth Mainguy <[email protected]>
license: GPLv2
vendor: Jmainguy
formats:
- deb
- rpm
checksum:
name_template: 'checksums.txt'
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
21 changes: 21 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
language: go
addons:
apt:
packages:
- rpm
env:
matrix:
- GO111MODULE="on"
global:
secure: 34hYh5WUaKifpYGE4FBDS7BYdymeEEbdQkbfwQCCchw6dLSRjEsYDSaKXN9PyzdzJEBYroKcNOpPANGWC0rW28f0h7QHV/xj/fIvPbow2aavWs356NTUY+/ZFamFi9trvwPw6+T3tqR+kgMefYKqBkS5jLNotLGAQqbvUDLcpYFqMuiDW05EIK++awvVdI/wROXa0hzrWGYNzqhtNbnu8BvO3L/7sby3GGVk0i4FF9MbP7+/OCECYRLwlS8mhjhYN6V36xrop20RJDwrFjiQp4/jcSyId4UVOIVrZgljzL2CzXOxIgAkbd9b0AOUEUcSBfLGmclE5bSkwquRwPuln69dR9BxOatMyUffLNVI6oMAMCNciquk2l9EyrmrMTz/ZOtid1C1vE6lhyR8hmd7wXM06MPekLufHCs3ytoryJLa1hHq01OSSWyUUgehDeh2NlZTnGJF/Nboh2AHLniknda8uhfxlI/3/pTvGaTYGaJAI+0EdG67TKIHKd9E2l4qEjU1wPUZZGuW0mpmFKhHB+Lxg4CHLiny/b0s7E0da8RTUkK7NJrfw12LYnepcKANUD8NGeu/FvHKGkO2saxephxbzpBUjheurZm3cqHQJAblYc99bxGR/IBMz2kuYpoW6oJqhvUk+ZOKPPERXwil6rRLUHxh3Odc4SuKHdBwqBM=
install:
after_success:
deploy:
- provider: script
skip_cleanup: true
script: curl -sL https://git.io/goreleaser | bash
on:
tags: true
condition: "$TRAVIS_OS_NAME = linux"
after_deploy:
- "./after_deploy.sh"
35 changes: 35 additions & 0 deletions after_deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash
# Set REPO To update
export METAREPO="slmingol/opsMarina"
export DIRNAME=$(echo $METAREPO | awk -F'/' '{print $2}')
# Set this repo name, to gather changelog from here, to add to meta repo.
export THISREPO="jmainguy/kibanaRefreshFields"

git clone "https://$GITHUB_TOKEN@github.com/$METAREPO.git"
cd $DIRNAME

# Only Curl once
CURL=$(curl --silent "https://$GITHUB_TOKEN@api.github.com/repos/$METAREPO/releases/latest")
# Set release tag
LASTRELEASE=$(echo $CURL | jq -r .tag_name)
echo "LASTRELEASE: $LASTRELEASE"
TODAY=$(date +"%Y-%m-%d")
echo "TODAY: $TODAY"
LASTRELEASEDAY=$(echo $LASTRELEASE | awk -F"-" '{print $1"-"$2"-"$3}')
if [[ $TODAY != $LASTRELEASEDAY ]]; then
export TRAVIS_TAG=$(echo "$TODAY-1")
else
N=$(echo $LASTRELEASE | awk -F'-' '{print $NF}')
((N++))
export TRAVIS_TAG=$(echo "$TODAY-$N")
fi
echo "TRAVIS_TAG: $TRAVIS_TAG"

# Tag and Push
CURL=$(curl --silent "https://$GITHUB_TOKEN@api.github.com/repos/$THISREPO/releases/latest")
COMMENT=$(echo $CURL | jq -r .body)
THISRELEASE=$(echo $CURL | jq -r .tag_name)
git tag -a $TRAVIS_TAG -m "Release generated by $THISREPO - Release - $THISRELEASE.
$COMMENT"
git push origin $TRAVIS_TAG
7 changes: 7 additions & 0 deletions examplePayload
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"attributes": {
"title": "jmainguy-*",
"timeFieldName": "@timestamp",
"fields": "[{\"name\":\"flow.bytes\",\"type\":\"number\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true}]",
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/jmainguy/kibanaRefreshFields

go 1.13
27 changes: 27 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"fmt"
"os"
)

func check(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func main() {

username := os.Getenv("KIBANA_USERNAME")
password := os.Getenv("KIBANA_PASSWORD")
kibanaUrl := os.Getenv("KIBANA_URL")
kibanaIndex := os.Getenv("KIBANA_INDEX")

fieldList := getFields(username, password, kibanaUrl, kibanaIndex)

body := formatPayload(fieldList)

submitPayload(username, password, kibanaUrl, kibanaIndex, body)
}
97 changes: 97 additions & 0 deletions payload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
)

func formatPayload(fieldList *FieldList) (body io.Reader) {
kibanaFilter := os.Getenv("KIBANA_FILTER")
payload := Payload{}
payload.Attributes.Title = "filebeat-*"
payload.Attributes.TimeFieldName = "@timestamp"

payload.Attributes.Fields = "["
if kibanaFilter != "" {
fmt.Printf("Filtering out %s\n", kibanaFilter)
}
for _, v := range fieldList.Fields {
// If you want to filter out some fields
if kibanaFilter != "" {
if !strings.Contains(v.Name, kibanaFilter) {
fields, err := json.Marshal(v)
check(err)
stringFields := string(fields)
payload.Attributes.Fields = fmt.Sprintf("%s%s,", payload.Attributes.Fields, stringFields)
}
} else {
fields, err := json.Marshal(v)
check(err)
stringFields := string(fields)
payload.Attributes.Fields = fmt.Sprintf("%s%s,", payload.Attributes.Fields, stringFields)
}
}
payload.Attributes.Fields = strings.TrimRight(payload.Attributes.Fields, ",")
payload.Attributes.Fields = fmt.Sprintf("%s]", payload.Attributes.Fields)

jsonNewData, err := json.Marshal(payload)
if err != nil {
fmt.Println(err)
}

body = bytes.NewReader(jsonNewData)

return body
}

func getFields(username, password, kibanaUrl, kibanaIndex string) (fieldList *FieldList) {

url := fmt.Sprintf("https://%s/api/index_patterns/_fields_for_wildcard?pattern=%s&meta_fields=_source&meta_fields=_id&meta_fields=_type&meta_fields=_index&meta_fields=_score", kibanaUrl, kibanaIndex)

fieldList = &FieldList{}

req, err := http.NewRequest("GET", url, nil)
check(err)
req.SetBasicAuth(username, password)
resp, err := http.DefaultClient.Do(req)
check(err)
defer resp.Body.Close()

bodyBytes, err := ioutil.ReadAll(resp.Body)
check(err)
json.Unmarshal(bodyBytes, fieldList)

return fieldList
}

func submitPayload(username, password, kibanaUrl, kibanaIndex string, body io.Reader) {

url := fmt.Sprintf("https://%s/api/saved_objects/index-pattern/%s", kibanaUrl, kibanaIndex)

req, err := http.NewRequest("PUT", url, body)
check(err)
req.SetBasicAuth(username, password)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Kbn-Xsrf", "true")

resp, err := http.DefaultClient.Do(req)
check(err)
defer resp.Body.Close()

bodyBytes, err := ioutil.ReadAll(resp.Body)
check(err)
bodyString := string(bodyBytes)

fmt.Println(resp.StatusCode)

if resp.StatusCode != 200 {
fmt.Println(bodyString)
}

}
26 changes: 26 additions & 0 deletions structs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

type Payload struct {
Attributes Attributes `json:"attributes"`
}

type Fields []struct {
Aggregatable bool `json:"aggregatable,omitempty"`
EsTypes []string `json:"esTypes,omitempty"`
Name string `json:"name"`
Parent string `json:"parent,omitempty"`
ReadFromDocValues bool `json:"readFromDocValues"`
Searchable bool `json:"searchable"`
SubType string `json:"subType,omitempty"`
Type string `json:"type"`
}

type Attributes struct {
Title string `json:"title"`
TimeFieldName string `json:"timeFieldName"`
Fields string `json:"fields"`
}

type FieldList struct {
Fields Fields `json:"fields"`
}

0 comments on commit eac5396

Please sign in to comment.