Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
List Elasticsearch indices and delete those from filebeat
// Get list of indices from Elasticsearch and delete any with filebeat as prefix
// this is ugly and there should be more functions instead of all this crap in main but it's a one-off script
/*
Public Domain. Use this however you wish!
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
import (
"bufio"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
)
func main() {
// assume localhost but allow IP address or hostname parameter
// for now, port 9200 is not optional
host := "localhost"
if len(os.Args) > 1 {
host = os.Args[1]
}
url := fmt.Sprintf("http://%s:9200/_cat/indices", host)
fmt.Println("Searching for Elasticsearch indices....")
// get indices response
resp, err := http.Get(url)
if err != nil {
fmt.Fprintf(os.Stderr, "\nUnable to fetch indices: %v", err)
return
}
defer resp.Body.Close()
// check status code from response is 200 (http.StatusOK)
if resp.StatusCode == http.StatusOK {
// get data as bytes then convert to string
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "\nUnable to get response body as bytes: %v", err)
return
}
bodyString := string(bodyBytes)
// iterate over response line by line and print indices found to stdout
scanner := bufio.NewScanner(strings.NewReader(bodyString))
for scanner.Scan() {
line := strings.Split(scanner.Text(), " ")
index := strings.TrimSpace(line[2])
fmt.Printf("\nIndex found: %s", index)
// iterate over response line by line and print indices found to stdout
scanner := bufio.NewScanner(strings.NewReader(bodyString))
for scanner.Scan() {
line := strings.Split(scanner.Text(), " ")
index := strings.TrimSpace(line[2])
fmt.Printf("\nIndex found: %s", index)
// delete filebeat indices if found
if strings.HasPrefix(index, "filebeat") {
indexURL := fmt.Sprintf("http://%s:9200/%s", host, index)
client := &http.Client{}
req, err := http.NewRequest("DELETE", indexURL, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "\nError deleting %s: %v", index, err)
return
}
r, err := client.Do(req)
if err != nil {
fmt.Fprintf(os.Stderr, "Error deleting %s: %v", index, err)
return
}
defer r.Body.Close()
rbody, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "Error deleting %s: %v", index, err)
}
fmt.Printf(" ... DELETED? Status: %s\t%s", r.Status, string(rbody))
}
}
} else {
fmt.Fprintf(os.Stderr, "\nStatus code %v returned.", resp.StatusCode)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.