Skip to content

Commit

Permalink
Implement cli for getting promethus alerts (#730)
Browse files Browse the repository at this point in the history
Signed-off-by: Arnob kumar saha <[email protected]>
  • Loading branch information
ArnobKumarSaha authored Nov 2, 2023
1 parent 78ff050 commit fe9cd7c
Show file tree
Hide file tree
Showing 8 changed files with 1,611 additions and 2 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ require (
github.com/cert-manager/cert-manager v1.9.1
github.com/fatih/camelcase v1.0.0
github.com/go-sql-driver/mysql v1.6.0
github.com/prometheus/client_golang v1.13.0
github.com/prometheus/common v0.37.0
github.com/spf13/cobra v1.7.0
golang.org/x/text v0.13.0
gomodules.xyz/go-sh v0.1.0
Expand Down Expand Up @@ -99,9 +101,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.59.1 // indirect
github.com/prometheus-operator/prometheus-operator/pkg/client v0.59.1 // indirect
github.com/prometheus/client_golang v1.13.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/redis/go-redis/v9 v9.0.5 // indirect
github.com/russross/blackfriday v1.5.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22
github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand Down Expand Up @@ -522,6 +523,7 @@ github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8m
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
Expand Down
187 changes: 187 additions & 0 deletions pkg/alerts/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/*
Copyright AppsCode Inc. and Contributors
Licensed under the AppsCode Community License 1.0.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package alerts

import (
"context"
"fmt"
"log"
"strings"
"time"

promapi "github.com/prometheus/client_golang/api"
promv1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"kmodules.xyz/client-go/tools/portforward"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type PromSvc struct {
Name string
Namespace string
Port int
}

type dbOpts struct {
db client.Object
config *rest.Config
kubeClient *kubernetes.Clientset
resource string
}

const (
kubeDBGroup = "kubedb.com"
kubeDBVersion = "v1alpha2"
)

func Run(f cmdutil.Factory, args []string, prom PromSvc) {
if len(args) < 2 {
log.Fatal("Enter db object's name as an argument")
}
resource := args[0]
dbName := args[1]

namespace, _, err := f.ToRawKubeConfigLoader().Namespace()
if err != nil {
_ = fmt.Errorf("failed to get current namespace")
}

opts, err := newDBOpts(f, dbName, namespace, convertedResource(resource))
if err != nil {
log.Fatalln(err)
}

p, err := opts.ForwardPort("services", prom)
if err != nil {
log.Fatalln(err)
}
err = opts.work(p)
if err != nil {
log.Fatalln(err)
}
}

func convertedResource(resource string) string {
// standardizing the resource name
res := strings.ToLower(resource)
switch res {
case "es", "elasticsearch", "elasticsearches":
res = "elasticsearches"
case "md", "mariadb", "mariadbs":
res = "mariadbs"
case "mg", "mongodb", "mongodbs":
res = "mongodbs"
case "my", "mysql", "mysqls":
res = "mysqls"
case "pg", "postgres", "postgreses":
res = "postgreses"
case "rd", "redis", "redises":
res = "redises"
default:
fmt.Printf("%s is not a valid resource type \n", resource)
}
return res
}

func newDBOpts(f cmdutil.Factory, dbName, namespace, resource string) (*dbOpts, error) {
config, err := f.ToRESTConfig()
if err != nil {
return nil, err
}

dc, err := dynamic.NewForConfig(config)
if err != nil {
return nil, err
}

dbRes := schema.GroupVersionResource{Group: kubeDBGroup, Version: kubeDBVersion, Resource: resource}
db, err := dc.Resource(dbRes).Namespace(namespace).Get(context.TODO(), dbName, metav1.GetOptions{})
if err != nil {
return nil, err
}

kubeClient, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}

opts := &dbOpts{
db: db,
config: config,
kubeClient: kubeClient,
resource: resource,
}
return opts, nil
}

func (opts *dbOpts) ForwardPort(resource string, prom PromSvc) (*portforward.Tunnel, error) {
tunnel := portforward.NewTunnel(portforward.TunnelOptions{
Client: opts.kubeClient.CoreV1().RESTClient(),
Config: opts.config,
Resource: resource,
Namespace: prom.Namespace,
Name: prom.Name,
Remote: prom.Port,
})

if err := tunnel.ForwardPort(); err != nil {
return nil, err
}
return tunnel, nil
}

func (opts *dbOpts) work(p *portforward.Tunnel) error {
pc, err := promapi.NewClient(promapi.Config{
Address: fmt.Sprintf("http://localhost:%d", p.Local),
})
if err != nil {
return err
}

promAPI := promv1.NewAPI(pc)
alertQuery := fmt.Sprintf("ALERTS{alertstate=\"firing\",k8s_group=\"kubedb.com\",k8s_resource=\"%s\",app=\"%s\",app_namespace=\"%s\"}",
opts.resource, opts.db.GetName(), opts.db.GetNamespace())
result, warnings, err := promAPI.QueryRange(context.TODO(), alertQuery, promv1.Range{
Start: time.Now().Add(-time.Hour),
End: time.Now(),
Step: time.Minute * 2,
})
if err != nil {
return err
}
if len(warnings) > 0 {
fmt.Println("Warnings:", warnings)
}

// Access the elements of the matrix using indexing.
matrix := result.(model.Matrix)
fmt.Printf("The number of firing alerts for %s %s/%s is %d \n", opts.resource, opts.db.GetNamespace(), opts.db.GetName(), matrix.Len())
for _, sample := range matrix {
alertName := sample.Metric["alertname"]
alertValue := sample.Values[0].Value
labels := sample.Metric
fmt.Printf("Alert Name: %s, Value: %s, Labels: %s\n", alertName, alertValue, labels)
}
return nil
}
59 changes: 59 additions & 0 deletions pkg/cmds/alert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright AppsCode Inc. and Contributors
Licensed under the AppsCode Community License 1.0.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmds

import (
"kubedb.dev/cli/pkg/alerts"

"github.com/spf13/cobra"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
)

var (
alertLong = templates.LongDesc(`
Get the prometheus alerts for a specific database in just one command
`)
alertExample = templates.Examples(`
kubectl dba get-alerts mongodb -n demo sample-mongodb --prom-svc-name=prometheus-kube-prometheus-prometheus --prom-svc-namespace=monitoring
Valid resource types include:
* elasticsearch
* mongodb
* mariadb
* mysql
* postgres
* redis
`)
)

func NewCmdAlert(f cmdutil.Factory) *cobra.Command {
var prom alerts.PromSvc
cmd := &cobra.Command{
Use: "get-alerts",
Short: i18n.T("Alerts associated with a database"),
Long: alertLong,
Example: alertExample,
Run: func(cmd *cobra.Command, args []string) {
alerts.Run(f, args, prom)
},
DisableFlagsInUseLine: true,
DisableAutoGenTag: true,
}
cmd.Flags().StringVarP(&prom.Name, "prom-svc-name", "", "", "name of the prometheus service")
cmd.Flags().StringVarP(&prom.Namespace, "prom-svc-namespace", "", "", "namespace of the prometheus service")
cmd.Flags().IntVarP(&prom.Port, "prom-svc-port", "", 9090, "port of the prometheus service")
return cmd
}
6 changes: 6 additions & 0 deletions pkg/cmds/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ func NewKubeDBCommand(in io.Reader, out, err io.Writer) *cobra.Command {
NewCmdExec(f),
},
},
{
Message: "Get Alerts for specific database",
Commands: []*cobra.Command{
NewCmdAlert(f),
},
},
{
Message: "Insert and Verify data in Database",
Commands: []*cobra.Command{
Expand Down
Loading

0 comments on commit fe9cd7c

Please sign in to comment.