@@ -6,63 +6,113 @@ import (
66 "log"
77 "time"
88
9+ "smanalyzer/pkg/istio"
10+ "smanalyzer/pkg/k8s"
11+ "smanalyzer/pkg/output"
12+
913 "github.com/spf13/cobra"
1014)
1115
1216var monitorCmd = & cobra.Command {
1317 Use : "monitor" ,
14- Short : "Continuously monitor service mesh for anomalies " ,
15- Long : `Runs continuous monitoring of the service mesh, detecting anomalies in real-time
16- and reporting them as they occur. Requires a baseline model to be learned first .` ,
18+ Short : "Continuously monitor service mesh metrics " ,
19+ Long : `Runs continuous monitoring of the service mesh, displaying real-time metrics
20+ for all services including request counts, error rates, response times, and more .` ,
1721 Run : runMonitor ,
1822}
1923
2024var (
21- monitorInterval time.Duration
22- modelPath string
23- outputFormat string
25+ monitorInterval time.Duration
26+ monitorNamespace string
27+ outputFormat string
2428)
2529
2630func init () {
2731 rootCmd .AddCommand (monitorCmd )
28-
32+
2933 monitorCmd .Flags ().DurationVarP (& monitorInterval , "interval" , "i" , 30 * time .Second , "Monitoring interval (e.g., 30s, 1m)" )
30- monitorCmd .Flags ().StringVarP (& modelPath , "model " , "m " , "" , "Path to learned baseline model " )
31- monitorCmd .Flags ().StringVarP (& outputFormat , "format" , "f" , "text " , "Output format (text, table, json)" )
34+ monitorCmd .Flags ().StringVarP (& monitorNamespace , "namespace " , "n " , "default " , "Kubernetes namespace to monitor " )
35+ monitorCmd .Flags ().StringVarP (& outputFormat , "format" , "f" , "table " , "Output format (text, table, json)" )
3236}
3337
3438func runMonitor (cmd * cobra.Command , args []string ) {
3539 ctx := context .Background ()
36-
37- fmt .Printf ("Starting continuous service mesh monitoring...\n " )
40+
41+ fmt .Printf ("Starting service mesh monitoring...\n " )
42+ fmt .Printf ("Namespace: %s\n " , monitorNamespace )
3843 fmt .Printf ("Interval: %v\n " , monitorInterval )
3944 fmt .Printf ("Output format: %s\n " , outputFormat )
40-
41- if modelPath != "" {
42- fmt .Printf ("Using model: %s\n " , modelPath )
43- }
44-
45+ fmt .Println ()
46+
4547 if err := performMonitoring (ctx ); err != nil {
4648 log .Fatalf ("Monitoring failed: %v" , err )
4749 }
4850}
4951
5052func performMonitoring (ctx context.Context ) error {
51- fmt .Println ("Loading baseline model..." )
52- fmt .Println ("Starting monitoring loop..." )
53-
53+ // Connect to Kubernetes cluster
54+ k8sClient , err := k8s .NewClient ()
55+ if err != nil {
56+ return fmt .Errorf ("failed to create Kubernetes client: %w" , err )
57+ }
58+
59+ if err := k8sClient .CheckConnection (ctx ); err != nil {
60+ return fmt .Errorf ("failed to connect to cluster: %w" , err )
61+ }
62+
63+ // Initialize service discovery
64+ serviceDiscovery := istio .NewServiceDiscovery (k8sClient .Clientset )
65+
66+ // Initialize output formatter
67+ formatter := output .NewFormatter (outputFormat )
68+
69+ fmt .Println ("Connected to cluster, starting monitoring loop..." )
70+ fmt .Println ()
71+
5472 ticker := time .NewTicker (monitorInterval )
5573 defer ticker .Stop ()
56-
74+
5775 for {
5876 select {
5977 case <- ctx .Done ():
6078 fmt .Println ("Monitoring stopped" )
6179 return nil
6280 case <- ticker .C :
63- fmt . Printf ( "[%s] Checking for anomalies... \n " , time . Now (). Format ( "15:04:05" ))
64-
65- time . Sleep ( 1 * time . Second )
81+ if err := collectAndDisplayMetrics ( ctx , serviceDiscovery , formatter ); err != nil {
82+ fmt . Printf ( "Error collecting metrics: %v \n " , err )
83+ }
6684 }
6785 }
68- }
86+ }
87+
88+ func collectAndDisplayMetrics (ctx context.Context , sd * istio.ServiceDiscovery , formatter * output.Formatter ) error {
89+ // Discover services
90+ services , err := sd .DiscoverServices (ctx , monitorNamespace )
91+ if err != nil {
92+ return fmt .Errorf ("failed to discover services: %w" , err )
93+ }
94+
95+ fmt .Println ("Please Note: This is a continuous monitoring loop. Press CTRL + C to leave" )
96+ fmt .Println ()
97+
98+ for _ , s := range services {
99+ if len (s ) == 0 {
100+ fmt .Printf ("[%s] No services found in namespace %s\n " , time .Now ().Format ("15:04:05" ), monitorNamespace )
101+ break
102+ }
103+ }
104+
105+ // Collect metrics for each service
106+ var allMetrics []* istio.ServiceMeshMetrics
107+ for _ , serviceName := range services {
108+ metrics , err := sd .CollectMetrics (ctx , monitorNamespace , serviceName )
109+ if err != nil {
110+ fmt .Printf ("Warning: failed to collect metrics for %s: %v\n " , serviceName , err )
111+ continue
112+ }
113+ allMetrics = append (allMetrics , metrics )
114+ }
115+
116+ // Display metrics
117+ return formatter .DisplayMetrics (allMetrics )
118+ }
0 commit comments