-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# README | ||
|
||
This is just a little guy that allows you to quickly mute and unmute hosts in Datadog. | ||
|
||
## Installation | ||
|
||
```bash | ||
go install github.com/hkrutzer/dd-mute-host@latest | ||
``` | ||
|
||
## Configuration | ||
|
||
The tool requires Datadog API credentials. Set the following environment variables: | ||
|
||
- `DD_API_KEY`: Your Datadog API key | ||
- `DD_APP_KEY`: Your Datadog application key | ||
|
||
## Usage | ||
|
||
### List Hosts | ||
To see all hosts and their mute status: | ||
```bash | ||
dd-mute-host list | ||
``` | ||
|
||
### Mute Hosts | ||
To mute one or more hosts for a specified duration: | ||
```bash | ||
dd-mute-host mute [--duration minutes] host1 [host2 ...] | ||
``` | ||
|
||
Options: | ||
- `--duration`: How many minutes to mute the host for (default: 60) | ||
|
||
Example: | ||
```bash | ||
# Mute a single host for 30 minutes | ||
dd-mute-host mute --duration 30 webserver1 | ||
|
||
# Mute multiple hosts for the default duration (60 minutes) | ||
dd-mute-host mute webserver1 webserver2 webserver3 | ||
``` | ||
|
||
### Unmute Hosts | ||
To unmute one or more hosts: | ||
```bash | ||
dd-mute-host unmute host1 [host2 ...] | ||
``` | ||
|
||
Example: | ||
```bash | ||
# Unmute a single host | ||
dd-mute-host unmute webserver1 | ||
|
||
# Unmute multiple hosts | ||
dd-mute-host unmute webserver1 webserver2 webserver3 | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module dd-host-mute | ||
|
||
go 1.23.4 | ||
|
||
require ( | ||
github.com/DataDog/datadog-api-client-go/v2 v2.34.0 | ||
github.com/alecthomas/kong v1.6.0 | ||
) | ||
|
||
require ( | ||
github.com/DataDog/zstd v1.5.2 // indirect | ||
github.com/goccy/go-json v0.10.2 // indirect | ||
github.com/golang/protobuf v1.5.3 // indirect | ||
golang.org/x/net v0.17.0 // indirect | ||
golang.org/x/oauth2 v0.10.0 // indirect | ||
google.golang.org/appengine v1.6.7 // indirect | ||
google.golang.org/protobuf v1.31.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
github.com/DataDog/datadog-api-client-go/v2 v2.34.0 h1:0VVmv8uZg8vdBuEpiF2nBGUezl2QITrxdEsLgh38j8M= | ||
github.com/DataDog/datadog-api-client-go/v2 v2.34.0/go.mod h1:d3tOEgUd2kfsr9uuHQdY+nXrWp4uikgTgVCPdKNK30U= | ||
github.com/DataDog/zstd v1.5.2 h1:vUG4lAyuPCXO0TLbXvPv7EB7cNK1QV/luu55UHLrrn8= | ||
github.com/DataDog/zstd v1.5.2/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw= | ||
github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8vS6K3D0= | ||
github.com/alecthomas/assert/v2 v2.11.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k= | ||
github.com/alecthomas/kong v1.6.0 h1:mwOzbdMR7uv2vul9J0FU3GYxE7ls/iX1ieMg5WIM6gE= | ||
github.com/alecthomas/kong v1.6.0/go.mod h1:p2vqieVMeTAnaC83txKtXe8FLke2X07aruPWXyMPQrU= | ||
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc= | ||
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4= | ||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= | ||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= | ||
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= | ||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= | ||
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= | ||
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= | ||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= | ||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= | ||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= | ||
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= | ||
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= | ||
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= | ||
golang.org/x/oauth2 v0.10.0 h1:zHCpF2Khkwy4mMB4bv0U37YtJdTGW8jI0glAApi0Kh8= | ||
golang.org/x/oauth2 v0.10.0/go.mod h1:kTpgurOux7LqtuxjuyZa4Gj2gdezIt/jQtGnNFfypQI= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= | ||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= | ||
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= | ||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= | ||
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= | ||
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= | ||
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"text/tabwriter" | ||
"time" | ||
|
||
"github.com/DataDog/datadog-api-client-go/v2/api/datadog" | ||
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV1" | ||
"github.com/alecthomas/kong" | ||
) | ||
|
||
var CLI struct { | ||
List struct{} `cmd:"" help:"List hosts."` | ||
Mute struct { | ||
Duration int `help:"How many minutes to mute the host for." default:"60"` | ||
|
||
Hosts []string `arg:"" name:"host" help:"Hosts to mute. Note that hosts that don't exist do not generate an error from Datadog."` | ||
} `cmd:"" help:"Mute hosts."` | ||
|
||
Unmute struct { | ||
Hosts []string `arg:"" name:"host" help:"Hosts to mute."` | ||
} `cmd:"" help:"Unmute hosts."` | ||
} | ||
|
||
func newDatadogApi() (context.Context, *datadogV1.HostsApi) { | ||
ctx := datadog.NewDefaultContext(context.Background()) | ||
configuration := datadog.NewConfiguration() | ||
apiClient := datadog.NewAPIClient(configuration) | ||
api := datadogV1.NewHostsApi(apiClient) | ||
return ctx, api | ||
} | ||
|
||
func main() { | ||
ctx := kong.Parse(&CLI, kong.Description("Quickly mute and unmute hosts in Datadog.")) | ||
switch ctx.Command() { | ||
case "list": | ||
ctx, api := newDatadogApi() | ||
resp, r, err := api.ListHosts(ctx, *datadogV1.NewListHostsOptionalParameters()) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error when calling `HostsApi.ListHosts`: %v\n", err) | ||
if r != nil { | ||
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r) | ||
} | ||
os.Exit(1) | ||
} | ||
|
||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) | ||
fmt.Fprintln(w, "Hostname\tMute Status\t") | ||
for _, host := range resp.HostList { | ||
muteStatus := "unmuted" | ||
if host.MuteTimeout.IsSet() && host.MuteTimeout.Get() != nil { | ||
muteTime := time.Unix(*host.MuteTimeout.Get(), 0) | ||
muteStatus = "until " + muteTime.Format(time.RFC3339) | ||
} | ||
|
||
fmt.Fprintf(w, "%s\t%s\t\n", *host.HostName, muteStatus) | ||
} | ||
w.Flush() | ||
|
||
case "mute <host>": | ||
ctx, api := newDatadogApi() | ||
|
||
body := datadogV1.HostMuteSettings{ | ||
End: datadog.PtrInt64(time.Now().Add(time.Duration(CLI.Mute.Duration) * time.Minute).Unix()), | ||
Message: datadog.PtrString("Host muted via dd-mute-host"), | ||
Override: datadog.PtrBool(false), | ||
} | ||
|
||
for _, host_name := range CLI.Mute.Hosts { | ||
resp, r, err := api.MuteHost(ctx, host_name, body) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Unable to mute host %v: %v %v\n", host_name, err, r.Body) | ||
continue | ||
} | ||
fmt.Fprintf(os.Stdout, "resp %v \n", resp) | ||
fmt.Fprintf(os.Stdout, "Host %v status: %v\n", host_name, resp.GetAction()) | ||
} | ||
|
||
case "unmute <host>": | ||
ctx, api := newDatadogApi() | ||
|
||
for _, host_name := range CLI.Unmute.Hosts { | ||
resp, r, err := api.UnmuteHost(ctx, host_name) | ||
|
||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Unable to unmute host %v: %v %v\n", host_name, err, r.Body) | ||
continue | ||
} | ||
|
||
fmt.Fprintf(os.Stdout, "Host %v status: %v\n", host_name, resp.GetAction()) | ||
} | ||
default: | ||
panic(ctx.Command()) | ||
} | ||
} |