Skip to content

Commit

Permalink
Monitor CLI added for check-connection and aggregate all monitor CLI (#…
Browse files Browse the repository at this point in the history
…754)


Signed-off-by: sayedppqq <[email protected]>
Signed-off-by: Arnob kumar saha <[email protected]>
Co-authored-by: Arnob kumar saha <[email protected]>
  • Loading branch information
sayedppqq and ArnobKumarSaha authored Jan 31, 2024
1 parent a67dadc commit 7aeaa86
Show file tree
Hide file tree
Showing 22 changed files with 676 additions and 287 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
kmodules.xyz/client-go v0.29.6
kmodules.xyz/custom-resources v0.29.1
kmodules.xyz/monitoring-agent-api v0.29.0
kubedb.dev/apimachinery v0.41.0-rc.1
kubedb.dev/apimachinery v0.41.0-rc.1.0.20240131123101-c711b3abb2a0
kubedb.dev/db-client-go v0.0.9
sigs.k8s.io/controller-runtime v0.17.0
sigs.k8s.io/yaml v1.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -819,8 +819,8 @@ kmodules.xyz/prober v0.29.0 h1:Ex7m4F9rH7uWNNJlLgP63ROOM+nUATJkC2L5OQ7nwMg=
kmodules.xyz/prober v0.29.0/go.mod h1:UtK+HKyI1lFLEKX+HFLyOCVju6TO93zv3kwGpzqmKOo=
kmodules.xyz/resource-metadata v0.18.2-0.20240105072614-e92a8a48d400 h1:bmd9/fvbhE55xtMF9hXVzjoUZFLGjMfEoHSHu9Hw6Gc=
kmodules.xyz/resource-metadata v0.18.2-0.20240105072614-e92a8a48d400/go.mod h1:XsCdEKjfoulX29tMGviDhjT/jLl158uvMvXlKOhK1as=
kubedb.dev/apimachinery v0.41.0-rc.1 h1:+aaSLuMXcBlQXuaJMhHRalqOIKrE1pReBwyV3YwdpSo=
kubedb.dev/apimachinery v0.41.0-rc.1/go.mod h1:Z6vywQE35f+j6Vh24OaY2q5jnwAirDlx70nuKmlrrN0=
kubedb.dev/apimachinery v0.41.0-rc.1.0.20240131123101-c711b3abb2a0 h1:pgtLsEzFPYzRXCNHKiuczfbwhYi5XyRrvT9ijDB2nWI=
kubedb.dev/apimachinery v0.41.0-rc.1.0.20240131123101-c711b3abb2a0/go.mod h1:Z6vywQE35f+j6Vh24OaY2q5jnwAirDlx70nuKmlrrN0=
kubedb.dev/db-client-go v0.0.9 h1:oYfNBjZRLtF5jij1u83NW0yCjz1Is4zE9RI0tG0h5AU=
kubedb.dev/db-client-go v0.0.9/go.mod h1:h37/SUuec3Jnxusxv7JTs3Vl5SX9C/rNPG7qB7hQq4M=
kubeops.dev/sidekick v0.0.5-0.20231225214445-a15c70833046 h1:X1ieV+IXqNesmFwSH6NEVF1J2wO0vplC4k6v3Vmq0d0=
Expand Down
59 changes: 0 additions & 59 deletions pkg/cmds/alert.go

This file was deleted.

65 changes: 0 additions & 65 deletions pkg/cmds/grafana_dashboard.go

This file was deleted.

199 changes: 199 additions & 0 deletions pkg/cmds/monitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
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/monitor"
"kubedb.dev/cli/pkg/monitor/alerts"
"kubedb.dev/cli/pkg/monitor/connection"
"kubedb.dev/cli/pkg/monitor/dashboard"

"github.com/spf13/cobra"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/i18n"
"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 [DATABASE] [DATABASE_NAME] -n [NAMESPACE]
# Check availability of grafana dashboard of a database
kubectl dba monitor dashboard [DATABASE] [DASHBOARD_NAME]
# Check connection status of target with prometheus server for a specific database
kubectl dba monitor check-connection [DATABASE] [DATABASE_NAME] -n [NAMESPACE]
# Common 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
`)

func NewCmdMonitor(f cmdutil.Factory) *cobra.Command {
cmd := &cobra.Command{
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))

return cmd
}

// alert
var alertLong = templates.LongDesc(`
Get the prometheus alerts for a specific database in just one command
`)

var alertExample = templates.Examples(`
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
* kafka
* mariadb
* mongodb
* mysql
* perconaxtradb
* postgres
* proxysql
* redis
`)

func AlertCMD(f cmdutil.Factory) *cobra.Command {
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,
}
return cmd
}

// dashboard
var dashboardLong = templates.LongDesc(`
Check availability of metrics in prometheus server used in a grafana dashboard.
`)

var dashboardExample = templates.Examples(`
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
* kafka
* mariadb
* mongodb
* mysql
* perconaxtradb
* postgres
* proxysql
* redis
`)

func DashboardCMD(f cmdutil.Factory) *cobra.Command {
var branch string
cmd := &cobra.Command{
Use: "dashboard",
Short: i18n.T("Check availability of a grafana dashboard"),
Long: dashboardLong,

Run: func(cmd *cobra.Command, args []string) {
dashboard.Run(f, args, branch, prom)
},
Example: dashboardExample,
DisableFlagsInUseLine: true,
DisableAutoGenTag: true,
}
cmd.Flags().StringVarP(&branch, "branch", "b", "master", "branch name of the github repo")
return cmd
}

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

var connectionExample = templates.Examples(`
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
* kafka
* mariadb
* mongodb
* mysql
* perconaxtradb
* postgres
* proxysql
* redis
`)

func ConnectionCMD(f cmdutil.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "check-connection",
Short: i18n.T("Check connection status of prometheus targets with server"),
Long: connectionLong,
Example: connectionExample,
Run: func(cmd *cobra.Command, args []string) {
connection.Run(f, args, prom)
},
DisableFlagsInUseLine: true,
DisableAutoGenTag: true,
}
return cmd
}
10 changes: 2 additions & 8 deletions pkg/cmds/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,6 @@ 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 All @@ -114,9 +108,9 @@ func NewKubeDBCommand(in io.Reader, out, err io.Writer) *cobra.Command {
},
},
{
Message: "Check availability of metrics",
Message: "Metric related CMDs",
Commands: []*cobra.Command{
NewCmdDashboard(f),
NewCmdMonitor(f),
},
},
}
Expand Down
Loading

0 comments on commit 7aeaa86

Please sign in to comment.