-
Notifications
You must be signed in to change notification settings - Fork 2
/
cleaner.go
87 lines (79 loc) · 2.15 KB
/
cleaner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/influxdata/influxdb/client/v2"
"github.com/spf13/viper"
)
// This handy script will DELETE ALL INFLUXDB SUBSCRIPTIONS!
// Workaround for https://github.com/influxdata/kapacitor/issues/870
// Configure using Environment variables:
// INFLUXDB_URL=http://myinflux:8086
// INFLUXDB_DRYRUN=true // for dry run
func checkIfSet(name string) {
if !viper.IsSet(name) {
fmt.Printf("You must set the environment variable $INFLUXDB_%s, exiting..\n", name)
os.Exit(1)
}
}
func clean() {
// Create a new HTTPClient
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: viper.GetString("url"),
})
if err != nil {
log.Fatal(err)
}
defer c.Close()
q := client.NewQuery("SHOW SUBSCRIPTIONS", "", "")
if response, err := c.Query(q); err == nil && response.Error() == nil {
for _, sub := range response.Results[0].Series {
fmt.Printf("Series Name: %s\n", sub.Name)
for _, v := range sub.Values {
retention := v[0]
dbname := sub.Name
name := v[1]
dropQuery := fmt.Sprintf("drop subscription \"%s\" on \"%s\".\"%s\"", name, dbname, retention)
if viper.IsSet("dryrun") {
fmt.Printf("we would run: %s\n", dropQuery)
} else {
dq := client.NewQuery(dropQuery, "", "")
fmt.Printf("running: %s\n", dropQuery)
if deleteResponse, err := c.Query(dq); err == nil && response.Error() == nil {
fmt.Println(deleteResponse)
} else {
fmt.Printf("error while deleting: %s", err)
}
}
}
}
}
}
func main() {
viper.SetEnvPrefix("influxdb")
viper.AutomaticEnv()
checkIfSet("url")
// If interval is set, re-run every interval hours.
if viper.IsSet("interval") {
fmt.Printf("Starting up and cleaning.")
clean()
fmt.Printf("Cleaning subscriptions every %d seconds\n", viper.GetInt("interval"))
ticker := time.NewTicker(time.Second * viper.GetDuration("interval"))
go func() {
for t := range ticker.C {
fmt.Println("Starting cleaning run ", t)
clean()
}
}()
// wait for exit
exitSignal := make(chan os.Signal)
signal.Notify(exitSignal, syscall.SIGINT, syscall.SIGTERM)
<-exitSignal
} else {
clean()
}
}