diff --git a/pkg/cmds/monitor.go b/pkg/cmds/monitor.go index 264734eb3..6a10e2510 100644 --- a/pkg/cmds/monitor.go +++ b/pkg/cmds/monitor.go @@ -28,6 +28,8 @@ import ( "k8s.io/kubectl/pkg/util/templates" ) +var prom monitor.PromSvc + var monitorLong = templates.LongDesc(` All monitoring related commands from AppsCode. `) @@ -35,31 +37,40 @@ var monitorLong = templates.LongDesc(` 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)) @@ -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 @@ -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"), @@ -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 } @@ -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 @@ -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"), @@ -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 @@ -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"), @@ -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 } diff --git a/pkg/monitor/alerts/db.go b/pkg/monitor/alerts/db.go index 05baa96be..dd53226fb 100644 --- a/pkg/monitor/alerts/db.go +++ b/pkg/monitor/alerts/db.go @@ -20,7 +20,6 @@ import ( "context" "fmt" "log" - "strings" "time" "kubedb.dev/cli/pkg/monitor" @@ -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) } @@ -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 { diff --git a/pkg/monitor/connection/db.go b/pkg/monitor/connection/db.go index ed41cfa05..7190ef2d2 100644 --- a/pkg/monitor/connection/db.go +++ b/pkg/monitor/connection/db.go @@ -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() diff --git a/pkg/monitor/connection/helper.go b/pkg/monitor/connection/helper.go index 19ef165c9..21b39bd23 100644 --- a/pkg/monitor/connection/helper.go +++ b/pkg/monitor/connection/helper.go @@ -19,6 +19,8 @@ package connection import ( "fmt" "log" + + api "kubedb.dev/apimachinery/apis/kubedb/v1alpha2" ) const ( @@ -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, diff --git a/pkg/monitor/dashboard/db.go b/pkg/monitor/dashboard/db.go index e4841a5f3..8d950304d 100644 --- a/pkg/monitor/dashboard/db.go +++ b/pkg/monitor/dashboard/db.go @@ -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) diff --git a/pkg/monitor/util.go b/pkg/monitor/util.go index ddefaf652..a0ced3ff4 100644 --- a/pkg/monitor/util.go +++ b/pkg/monitor/util.go @@ -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) }