Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
Signed-off-by: sayedppqq <[email protected]>
  • Loading branch information
sayedppqq committed Jan 31, 2024
1 parent 94c2bc9 commit d523ee8
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 87 deletions.
84 changes: 50 additions & 34 deletions pkg/cmds/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,49 @@ import (
"k8s.io/kubectl/pkg/util/templates"
)

var prom monitor.PromSvc

var monitorLong = templates.LongDesc(`
All monitoring related commands from AppsCode.
`)

var monitorExample = templates.Examples(`
# Check triggered alerts for a specific database
kubectl dba monitor get-alerts mongodb -n demo sample-mongodb --prom-svc-name=prometheus-kube-prometheus-prometheus --prom-svc-namespace=monitoring
kubectl dba monitor get-alerts [DATABASE] [DATABASE_NAME] -n [NAMESPACE] \
--prom-svc=[PROM_SVC_NAME] --prom-svc-namespace=[PROM_SVC_NS] --prom-svc-port=[PROM_SVC_PORT]
# Check availability of grafana dashboard of a database
kubectl dba monitor dashboard [DATABASE] [DASHBOARD_NAME] \
--prom-svc=[PROM_SVC_NAME] --prom-svc-namespace=[PROM_SVC_NS] --prom-svc-port=[PROM_SVC_PORT]
# Check availability of mongodb-summary-dashboard grafana dashboard of mongodb
kubectl dba monitor dashboard mongodb mongodb-summary-dashboard
# Check connection status of target with prometheus server for a specific database
kubectl dba monitor check-connection [DATABASE] [DATABASE_NAME] -n [NAMESPACE] \
--prom-svc=[PROM_SVC_NAME] --prom-svc-namespace=[PROM_SVC_NS] --prom-svc-port=[PROM_SVC_PORT]
# Check connection status of target with prometheus server
kubectl dba monitor check-connection mongodb
# Flags
--prom-svc-name : name of the prometheus service
--prom-svc-namespace : namespace of the prometheus service
--prom-svc-port : port of the prometheus service
Valid sub command include:
* get-alerts
* dashboard
* check-connection
`)

func NewCmdMonitor(f cmdutil.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "monitor",
Short: i18n.T("get-alerts,grafana dashboard check and target connection check for monitoring"),
Long: monitorLong,
Example: monitorExample,
Run: func(cmd *cobra.Command, args []string) {},
Use: "monitor",
Short: i18n.T("Monitoring related commands for a database"),
Long: monitorLong,
Example: monitorExample,
Run: func(cmd *cobra.Command, args []string) {
},
DisableFlagsInUseLine: true,
DisableAutoGenTag: true,
}

cmd.PersistentFlags().StringVarP(&prom.Name, "prom-svc-name", "", "", "name of the prometheus service")
cmd.PersistentFlags().StringVarP(&prom.Namespace, "prom-svc-namespace", "", "", "namespace of the prometheus service")
cmd.PersistentFlags().IntVarP(&prom.Port, "prom-svc-port", "", 9090, "port of the prometheus service")

cmd.AddCommand(DashboardCMD(f))
cmd.AddCommand(AlertCMD(f))
cmd.AddCommand(ConnectionCMD(f))
Expand All @@ -73,7 +84,12 @@ var alertLong = templates.LongDesc(`
`)

var alertExample = templates.Examples(`
kubectl dba monitor get-alerts mongodb -n demo sample-mongodb --prom-svc-name=prometheus-kube-prometheus-prometheus --prom-svc-namespace=monitoring
kubectl dba monitor get-alerts [DATABASE] [DATABASE_NAME] -n [NAMESPACE] \
--prom-svc=[PROM_SVC_NAME] --prom-svc-namespace=[PROM_SVC_NS] --prom-svc-port=[PROM_SVC_PORT]
# Get triggered alert for a specific mongodb
kubectl dba monitor get-alerts mongodb sample-mongodb -n demo \
--prom-svc-name=prometheus-kube-prometheus-prometheus --prom-svc-namespace=monitoring --prom-svc-port=9090
Valid resource types include:
* elasticsearch
Expand All @@ -82,10 +98,12 @@ var alertExample = templates.Examples(`
* mysql
* postgres
* redis
* kafka
* perconaxtradb
* proxysql
`)

func AlertCMD(f cmdutil.Factory) *cobra.Command {
var prom monitor.PromSvc
cmd := &cobra.Command{
Use: "get-alerts",
Short: i18n.T("Alerts associated with a database"),
Expand All @@ -97,9 +115,6 @@ func AlertCMD(f cmdutil.Factory) *cobra.Command {
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
}

Expand All @@ -109,9 +124,12 @@ var dashboardLong = templates.LongDesc(`
`)

var dashboardExample = templates.Examples(`
# Check availability of mongodb-summary-dashboard grafana dashboard of postgres
kubectl-dba monitor dashboard [DATABASE] [DASHBOARD_NAME]
kubectl-dba dashboard postgres postgres_databases_dashboard --branch=metric --prom-svc-name=prometheus-kube-prometheus-prometheus --prom-svc-namespace=monitoring --prom-svc-port=9090
kubectl dba monitor dashboard [DATABASE] [DASHBOARD_NAME] \
--prom-svc=[PROM_SVC_NAME] --prom-svc-namespace=[PROM_SVC_NS] --prom-svc-port=[PROM_SVC_PORT]
# Check availability of a postgres grafana dashboard
kubectl-dba monitor dashboard postgres postgres_databases_dashboard \
--prom-svc-name=prometheus-kube-prometheus-prometheus --prom-svc-namespace=monitoring --prom-svc-port=9090
Valid dashboards include:
* elasticsearch
Expand All @@ -120,11 +138,13 @@ var dashboardExample = templates.Examples(`
* mysql
* postgres
* redis
* kafka
* perconaxtradb
* proxysql
`)

func DashboardCMD(f cmdutil.Factory) *cobra.Command {
var branch string
var prom monitor.PromSvc
cmd := &cobra.Command{
Use: "dashboard",
Short: i18n.T("Check availability of a grafana dashboard"),
Expand All @@ -138,21 +158,21 @@ func DashboardCMD(f cmdutil.Factory) *cobra.Command {
DisableAutoGenTag: true,
}
cmd.Flags().StringVarP(&branch, "branch", "b", "master", "branch name of the github repo")
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
}

// check-connection
var connectionLong = templates.LongDesc(`
Check connection status for different targets of prometheus server for specific DB.
Check connection status for different targets with prometheus server for specific DB.
`)

var connectionExample = templates.Examples(`
Check connection status for different targets of prometheus server of a mongodb.
kubectl-dba monitor check-connection [DATABASE] [DATABASENAME] -n [NAMESPACE]
kubectl dba monitor check-connection mongodb -n demo sample_mg
kubectl dba monitor check-connection [DATABASE] [DATABASE_NAME] -n [NAMESPACE] \
--prom-svc=[PROM_SVC_NAME] --prom-svc-namespace=[PROM_SVC_NS] --prom-svc-port=[PROM_SVC_PORT]
# Check connection status for different targets with prometheus server for a specific postgres database
kubectl dba monitor check-connection mongodb sample_mg -n demo \
--prom-svc-name=prometheus-kube-prometheus-prometheus --prom-svc-namespace=monitoring --prom-svc-port=9090
Valid resource types include:
* elasticsearch
Expand All @@ -167,7 +187,6 @@ var connectionExample = templates.Examples(`
`)

func ConnectionCMD(f cmdutil.Factory) *cobra.Command {
var prom monitor.PromSvc
cmd := &cobra.Command{
Use: "check-connection",
Short: i18n.T("Check connection status of prometheus targets with server"),
Expand All @@ -179,8 +198,5 @@ func ConnectionCMD(f cmdutil.Factory) *cobra.Command {
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
}
25 changes: 1 addition & 24 deletions pkg/monitor/alerts/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"fmt"
"log"
"strings"
"time"

"kubedb.dev/cli/pkg/monitor"
Expand Down Expand Up @@ -62,7 +61,7 @@ func Run(f cmdutil.Factory, args []string, prom monitor.PromSvc) {
_ = fmt.Errorf("failed to get current namespace")
}

opts, err := newDBOpts(f, dbName, namespace, convertedResource(resource))
opts, err := newDBOpts(f, dbName, namespace, monitor.ConvertedResource(resource))
if err != nil {
log.Fatalln(err)
}
Expand All @@ -76,28 +75,6 @@ func Run(f cmdutil.Factory, args []string, prom monitor.PromSvc) {
}
}

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 {
Expand Down
1 change: 0 additions & 1 deletion pkg/monitor/connection/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func Run(f cmdutil.Factory, args []string, prom monitor.PromSvc) {
if err != nil {
log.Fatal(err)
}
// Port forwarding cluster prometheus service for that grafana dashboard's prom datasource.

promClient, tunnel := monitor.GetPromClientAndTunnel(config, prom)
defer tunnel.Close()
Expand Down
20 changes: 11 additions & 9 deletions pkg/monitor/connection/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package connection
import (
"fmt"
"log"

api "kubedb.dev/apimachinery/apis/kubedb/v1alpha2"
)

const (
Expand All @@ -44,55 +46,55 @@ func getDBMetrics(database, name string, queries map[string]*metrics) map[string
label := "service"
labelValue := fmt.Sprintf("%s-stats", name)
switch database {
case "mongodb":
case api.ResourcePluralMongoDB:
queries[database] = &metrics{
metric: "mongodb_up",
label: label,
labelValue: labelValue,
}
case "postgres":
case api.ResourcePluralPostgres:
queries[database] = &metrics{
metric: "pg_up",
label: label,
labelValue: labelValue,
}
case "mysql":
case api.ResourcePluralMySQL:
queries[database] = &metrics{
metric: "mysql_up",
label: label,
labelValue: labelValue,
}
case "redis":
case api.ResourcePluralRedis:
queries[database] = &metrics{
metric: "redis_up",
label: label,
labelValue: labelValue,
}
case "mariadb":
case api.ResourcePluralMariaDB:
queries[database] = &metrics{
metric: "mysql_up",
label: label,
labelValue: labelValue,
}
case "proxysql":
case api.ResourcePluralProxySQL:
queries[database] = &metrics{
metric: "proxysql_uptime_seconds_total",
label: label,
labelValue: labelValue,
}
case "elasticsearch":
case api.ResourcePluralElasticsearch:
queries[database] = &metrics{
metric: "elasticsearch_clusterinfo_up",
label: label,
labelValue: labelValue,
}
case "perconaxtradb":
case api.ResourcePluralPerconaXtraDB:
queries[database] = &metrics{
metric: "mysql_up",
label: label,
labelValue: labelValue,
}
case "kafka":
case api.ResourcePluralKafka:
queries[database] = &metrics{
metric: "kafka_controller_kafkacontroller_activebrokercount",
label: label,
Expand Down
2 changes: 1 addition & 1 deletion pkg/monitor/dashboard/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Run(f cmdutil.Factory, args []string, branch string, prom monitor.PromSvc)
log.Fatal("Enter database and grafana dashboard name as argument")
}

database := args[0]
database := monitor.ConvertedResource(args[0])
dashboard := args[1]

url := getURL(branch, database, dashboard)
Expand Down
38 changes: 20 additions & 18 deletions pkg/monitor/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,32 @@ package monitor
import (
"log"
"strings"

api "kubedb.dev/apimachinery/apis/kubedb/v1alpha2"
)

func ConvertedResource(resource string) string {
// standardizing the resource name
res := strings.ToLower(resource)
switch res {
case "es", "elasticsearch", "elasticsearches":
res = "elasticsearch"
case "kf", "kafka", "kafkas":
res = "kafka"
case "md", "mariadb", "mariadbs":
res = "mariadb"
case "mg", "mongodb", "mongodbs":
res = "mongodb"
case "my", "mysql", "mysqls":
res = "mysqls"
case "px", "perconaxtradb", "perconaxtradbs":
res = "perconaxtradb"
case "pg", "postgres", "postgreses":
res = "postgres"
case "prx", "proxysql", "proxysqls":
res = "proxysql"
case "rd", "redis", "redises":
res = "redis"
case api.ResourceCodeElasticsearch, api.ResourcePluralElasticsearch, api.ResourceSingularElasticsearch:
res = api.ResourcePluralElasticsearch
case api.ResourceCodeMongoDB, api.ResourcePluralMongoDB, api.ResourceSingularMongoDB:
res = api.ResourcePluralMongoDB
case api.ResourceCodePostgres, api.ResourcePluralPostgres, api.ResourceSingularPostgres:
res = api.ResourcePluralPostgres
case api.ResourceCodeMySQL, api.ResourcePluralMySQL, api.ResourceSingularMySQL:
res = api.ResourcePluralMySQL
case api.ResourceCodeKafka, api.ResourcePluralKafka, api.ResourceSingularKafka:
res = api.ResourcePluralKafka
case api.ResourceCodeProxySQL, api.ResourcePluralProxySQL, api.ResourceSingularProxySQL:
res = api.ResourcePluralProxySQL
case api.ResourceCodePerconaXtraDB, api.ResourcePluralPerconaXtraDB, api.ResourceSingularPerconaXtraDB:
res = api.ResourcePluralPerconaXtraDB
case api.ResourceCodeRedis, api.ResourcePluralRedis, api.ResourceSingularRedis:
res = api.ResourcePluralRedis
case api.ResourceCodeMariaDB, api.ResourcePluralMariaDB, api.ResourceSingularMariaDB:
res = api.ResourcePluralMariaDB
default:
log.Fatalf("%s is not a valid resource type \n", resource)
}
Expand Down

0 comments on commit d523ee8

Please sign in to comment.