@@ -6,63 +6,113 @@ import (
6
6
"log"
7
7
"time"
8
8
9
+ "smanalyzer/pkg/istio"
10
+ "smanalyzer/pkg/k8s"
11
+ "smanalyzer/pkg/output"
12
+
9
13
"github.com/spf13/cobra"
10
14
)
11
15
12
16
var monitorCmd = & cobra.Command {
13
17
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 .` ,
17
21
Run : runMonitor ,
18
22
}
19
23
20
24
var (
21
- monitorInterval time.Duration
22
- modelPath string
23
- outputFormat string
25
+ monitorInterval time.Duration
26
+ monitorNamespace string
27
+ outputFormat string
24
28
)
25
29
26
30
func init () {
27
31
rootCmd .AddCommand (monitorCmd )
28
-
32
+
29
33
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)" )
32
36
}
33
37
34
38
func runMonitor (cmd * cobra.Command , args []string ) {
35
39
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 )
38
43
fmt .Printf ("Interval: %v\n " , monitorInterval )
39
44
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
+
45
47
if err := performMonitoring (ctx ); err != nil {
46
48
log .Fatalf ("Monitoring failed: %v" , err )
47
49
}
48
50
}
49
51
50
52
func 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
+
54
72
ticker := time .NewTicker (monitorInterval )
55
73
defer ticker .Stop ()
56
-
74
+
57
75
for {
58
76
select {
59
77
case <- ctx .Done ():
60
78
fmt .Println ("Monitoring stopped" )
61
79
return nil
62
80
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
+ }
66
84
}
67
85
}
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