@@ -21,6 +21,7 @@ import (
2121 "context"
2222 "time"
2323
24+ "go.uber.org/zap/zapcore"
2425 "github.com/GoogleCloudPlatform/sapagent/shared/log"
2526 "github.com/GoogleCloudPlatform/sapagent/shared/recovery"
2627 "github.com/GoogleCloudPlatform/workloadagent/internal/commondiscovery"
@@ -32,9 +33,11 @@ import (
3233
3334// Service implements the interfaces for MySQL workload agent service.
3435type Service struct {
35- Config * cpb.Configuration
36- CloudProps * cpb.CloudProperties
37- CommonCh chan commondiscovery.Result
36+ Config * cpb.Configuration
37+ CloudProps * cpb.CloudProperties
38+ CommonCh chan commondiscovery.Result
39+ processes commondiscovery.Result
40+ mySQLProcesses []commondiscovery.ProcessWrapper
3841 // ... MySQL-specific attributes ...
3942}
4043
@@ -48,8 +51,16 @@ type runMetricCollectionArgs struct {
4851
4952// Start initiates the MySQL workload agent service
5053func (s * Service ) Start (ctx context.Context , a any ) {
51- if ! s .Config .GetMysqlConfiguration ().GetEnabled () {
52- log .CtxLogger (ctx ).Info ("MySQL workload agent service is disabled" )
54+ if s .Config .GetMysqlConfiguration () == nil || s .Config .GetMysqlConfiguration ().Enabled == nil {
55+ // If MySQL workload agent service is not explicitly enabled in the configuration,
56+ // then check if the workload is present on the host.
57+ log .CtxLogger (ctx ).Info ("MySQL workload agent service is not explicitly enabled in the configuration" )
58+ if ! s .checkCommonDiscovery (ctx ) {
59+ return
60+ }
61+ } else if ! s .Config .GetMysqlConfiguration ().GetEnabled () {
62+ // If MySQL workload agent service is explicitly disabled in the configuration, then return.
63+ log .CtxLogger (ctx ).Info ("MySQL workload agent service is disabled in the configuration" )
5364 return
5465 }
5566
@@ -78,6 +89,10 @@ func (s *Service) Start(ctx context.Context, a any) {
7889 case <- ctx .Done ():
7990 log .CtxLogger (ctx ).Info ("MySQL workload agent service cancellation requested" )
8091 return
92+ case s .processes = <- s .CommonCh :
93+ log .CtxLogger (ctx ).Debugw ("MySQL workload agent service received common discovery result" , "result" , s .processes )
94+ s .identifyMySQLProcesses (ctx )
95+ return
8196 }
8297}
8398
@@ -126,6 +141,52 @@ func runMetricCollection(ctx context.Context, a any) {
126141 }
127142}
128143
144+ // checkCommonDiscovery checks for common discovery results.
145+ // Returns true if MySQL workload is present on the host.
146+ // Otherwise, returns false to indicate that the context is done.
147+ func (s * Service ) checkCommonDiscovery (ctx context.Context ) bool {
148+ for {
149+ select {
150+ case <- ctx .Done ():
151+ log .CtxLogger (ctx ).Info ("MySQL workload agent service cancellation requested" )
152+ return false
153+ case s .processes = <- s .CommonCh :
154+ log .CtxLogger (ctx ).Debugw ("MySQL workload agent service received common discovery result" , "NumProcesses" , len (s .processes .Processes ))
155+ s .identifyMySQLProcesses (ctx )
156+ if s .isWorkloadPresent () {
157+ log .CtxLogger (ctx ).Info ("MySQL workload is present, starting discovery and metric collection" )
158+ return true
159+ }
160+ log .CtxLogger (ctx ).Debug ("MySQL workload is not present" )
161+ }
162+ }
163+ }
164+
165+ func (s * Service ) identifyMySQLProcesses (ctx context.Context ) {
166+ for _ , process := range s .processes .Processes {
167+ name , err := process .Name ()
168+ if err == nil && name == "mysqld" {
169+ s .mySQLProcesses = append (s .mySQLProcesses , process )
170+ }
171+ }
172+ s .logMySQLProcesses (ctx , zapcore .DebugLevel )
173+ }
174+
175+ func (s * Service ) isWorkloadPresent () bool {
176+ return len (s .mySQLProcesses ) > 0
177+ }
178+
179+ func (s * Service ) logMySQLProcesses (ctx context.Context , loglevel zapcore.Level ) {
180+ log .CtxLogger (ctx ).Logf (loglevel , "Number of MySQL processes found: %v" , len (s .mySQLProcesses ))
181+ for _ , process := range s .mySQLProcesses {
182+ name , _ := process .Name ()
183+ username , _ := process .Username ()
184+ cmdline , _ := process .CmdlineSlice ()
185+ env , _ := process .Environ ()
186+ log .CtxLogger (ctx ).Logw (loglevel , "MySQL process" , "name" , name , "username" , username , "cmdline" , cmdline , "env" , env , "pid" , process .Pid ())
187+ }
188+ }
189+
129190// String returns the name of the MySQL service.
130191func (s * Service ) String () string {
131192 return "MySQL Service"
0 commit comments