| 
									
										
										
										
											2021-04-19 03:41:13 +08:00
										 |  |  | // Copyright (c) 2015-2021 MinIO, Inc.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This file is part of MinIO Object Storage stack
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | package cmd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2022-07-06 05:45:49 +08:00
										 |  |  | 	"container/ring" | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2019-10-08 13:47:56 +08:00
										 |  |  | 	"sync" | 
					
						
							| 
									
										
										
										
											2022-11-11 02:20:21 +08:00
										 |  |  | 	"sync/atomic" | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-07 05:46:50 +08:00
										 |  |  | 	"github.com/minio/madmin-go/v2" | 
					
						
							| 
									
										
										
										
											2021-06-02 05:59:40 +08:00
										 |  |  | 	"github.com/minio/minio/internal/logger" | 
					
						
							|  |  |  | 	"github.com/minio/minio/internal/logger/message/log" | 
					
						
							|  |  |  | 	"github.com/minio/minio/internal/logger/target/console" | 
					
						
							| 
									
										
										
										
											2022-02-25 01:05:33 +08:00
										 |  |  | 	"github.com/minio/minio/internal/logger/target/types" | 
					
						
							| 
									
										
										
										
											2021-06-02 05:59:40 +08:00
										 |  |  | 	"github.com/minio/minio/internal/pubsub" | 
					
						
							| 
									
										
										
										
											2021-06-15 05:54:37 +08:00
										 |  |  | 	xnet "github.com/minio/pkg/net" | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // number of log messages to buffer
 | 
					
						
							|  |  |  | const defaultLogBufferCount = 10000 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-17 01:28:29 +08:00
										 |  |  | // HTTPConsoleLoggerSys holds global console logger state
 | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | type HTTPConsoleLoggerSys struct { | 
					
						
							| 
									
										
										
										
											2022-11-11 02:20:21 +08:00
										 |  |  | 	totalMessages  int64 | 
					
						
							|  |  |  | 	failedMessages int64 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-17 12:30:57 +08:00
										 |  |  | 	sync.RWMutex | 
					
						
							| 
									
										
										
										
											2022-10-29 01:55:42 +08:00
										 |  |  | 	pubsub   *pubsub.PubSub[log.Info, madmin.LogMask] | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 	console  *console.Target | 
					
						
							|  |  |  | 	nodeName string | 
					
						
							|  |  |  | 	logBuf   *ring.Ring | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-17 12:30:57 +08:00
										 |  |  | // NewConsoleLogger - creates new HTTPConsoleLoggerSys with all nodes subscribed to
 | 
					
						
							|  |  |  | // the console logging pub sub system
 | 
					
						
							|  |  |  | func NewConsoleLogger(ctx context.Context) *HTTPConsoleLoggerSys { | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 	return &HTTPConsoleLoggerSys{ | 
					
						
							| 
									
										
										
										
											2022-10-29 01:55:42 +08:00
										 |  |  | 		pubsub:  pubsub.New[log.Info, madmin.LogMask](8), | 
					
						
							| 
									
										
										
										
											2019-12-17 12:30:57 +08:00
										 |  |  | 		console: console.New(), | 
					
						
							|  |  |  | 		logBuf:  ring.New(defaultLogBufferCount), | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 00:03:26 +08:00
										 |  |  | // IsOnline always true in case of console logger
 | 
					
						
							|  |  |  | func (sys *HTTPConsoleLoggerSys) IsOnline() bool { | 
					
						
							|  |  |  | 	return true | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-17 12:30:57 +08:00
										 |  |  | // SetNodeName - sets the node name if any after distributed setup has initialized
 | 
					
						
							| 
									
										
										
										
											2021-03-27 02:37:58 +08:00
										 |  |  | func (sys *HTTPConsoleLoggerSys) SetNodeName(nodeName string) { | 
					
						
							|  |  |  | 	if !globalIsDistErasure { | 
					
						
							|  |  |  | 		sys.nodeName = "" | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	host, err := xnet.ParseHost(globalLocalNodeName) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logger.FatalIf(err, "Unable to start console logging subsystem") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sys.nodeName = host.Name | 
					
						
							| 
									
										
										
										
											2019-12-17 12:30:57 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | // HasLogListeners returns true if console log listeners are registered
 | 
					
						
							|  |  |  | // for this node or peers
 | 
					
						
							|  |  |  | func (sys *HTTPConsoleLoggerSys) HasLogListeners() bool { | 
					
						
							| 
									
										
										
										
											2022-10-29 01:55:42 +08:00
										 |  |  | 	return sys != nil && sys.pubsub.Subscribers() > 0 | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Subscribe starts console logging for this node.
 | 
					
						
							| 
									
										
										
										
											2022-10-29 01:55:42 +08:00
										 |  |  | func (sys *HTTPConsoleLoggerSys) Subscribe(subCh chan log.Info, doneCh <-chan struct{}, node string, last int, logKind madmin.LogMask, filter func(entry log.Info) bool) error { | 
					
						
							| 
									
										
										
										
											2019-12-17 12:30:57 +08:00
										 |  |  | 	// Enable console logging for remote client.
 | 
					
						
							|  |  |  | 	if !sys.HasLogListeners() { | 
					
						
							| 
									
										
										
										
											2022-02-25 01:05:33 +08:00
										 |  |  | 		logger.AddSystemTarget(sys) | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cnt := 0 | 
					
						
							|  |  |  | 	// by default send all console logs in the ring buffer unless node or limit query parameters
 | 
					
						
							|  |  |  | 	// are set.
 | 
					
						
							| 
									
										
										
										
											2020-03-21 06:13:41 +08:00
										 |  |  | 	var lastN []log.Info | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 	if last > defaultLogBufferCount || last <= 0 { | 
					
						
							|  |  |  | 		last = defaultLogBufferCount | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-21 06:13:41 +08:00
										 |  |  | 	lastN = make([]log.Info, last) | 
					
						
							| 
									
										
										
										
											2019-12-17 12:30:57 +08:00
										 |  |  | 	sys.RLock() | 
					
						
							| 
									
										
										
										
											2019-10-08 13:47:56 +08:00
										 |  |  | 	sys.logBuf.Do(func(p interface{}) { | 
					
						
							| 
									
										
										
										
											2020-03-21 06:13:41 +08:00
										 |  |  | 		if p != nil { | 
					
						
							|  |  |  | 			lg, ok := p.(log.Info) | 
					
						
							|  |  |  | 			if ok && lg.SendLog(node, logKind) { | 
					
						
							|  |  |  | 				lastN[cnt%last] = lg | 
					
						
							|  |  |  | 				cnt++ | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2019-12-17 12:30:57 +08:00
										 |  |  | 	sys.RUnlock() | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 	// send last n console log messages in order filtered by node
 | 
					
						
							|  |  |  | 	if cnt > 0 { | 
					
						
							|  |  |  | 		for i := 0; i < last; i++ { | 
					
						
							|  |  |  | 			entry := lastN[(cnt+i)%last] | 
					
						
							| 
									
										
										
										
											2020-03-21 06:13:41 +08:00
										 |  |  | 			if (entry == log.Info{}) { | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			select { | 
					
						
							|  |  |  | 			case subCh <- entry: | 
					
						
							|  |  |  | 			case <-doneCh: | 
					
						
							| 
									
										
										
										
											2022-06-06 05:29:12 +08:00
										 |  |  | 				return nil | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-10-29 01:55:42 +08:00
										 |  |  | 	return sys.pubsub.Subscribe(madmin.LogMaskAll, subCh, doneCh, filter) | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-14 00:39:13 +08:00
										 |  |  | // Init if HTTPConsoleLoggerSys is valid, always returns nil right now
 | 
					
						
							|  |  |  | func (sys *HTTPConsoleLoggerSys) Init() error { | 
					
						
							| 
									
										
										
										
											2020-08-17 01:25:00 +08:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-03 07:19:44 +08:00
										 |  |  | // Endpoint - dummy function for interface compatibility
 | 
					
						
							|  |  |  | func (sys *HTTPConsoleLoggerSys) Endpoint() string { | 
					
						
							|  |  |  | 	return sys.console.Endpoint() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // String - stringer function for interface compatibility
 | 
					
						
							|  |  |  | func (sys *HTTPConsoleLoggerSys) String() string { | 
					
						
							| 
									
										
										
										
											2022-02-18 09:50:10 +08:00
										 |  |  | 	return logger.ConsoleLoggerTgt | 
					
						
							| 
									
										
										
										
											2020-10-03 07:19:44 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-11 02:20:21 +08:00
										 |  |  | // Stats returns the target statistics.
 | 
					
						
							|  |  |  | func (sys *HTTPConsoleLoggerSys) Stats() types.TargetStats { | 
					
						
							|  |  |  | 	return types.TargetStats{ | 
					
						
							|  |  |  | 		TotalMessages:  atomic.LoadInt64(&sys.totalMessages), | 
					
						
							|  |  |  | 		FailedMessages: atomic.LoadInt64(&sys.failedMessages), | 
					
						
							|  |  |  | 		QueueLength:    0, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-16 09:02:54 +08:00
										 |  |  | // Content returns the console stdout log
 | 
					
						
							|  |  |  | func (sys *HTTPConsoleLoggerSys) Content() (logs []log.Entry) { | 
					
						
							|  |  |  | 	sys.RLock() | 
					
						
							|  |  |  | 	sys.logBuf.Do(func(p interface{}) { | 
					
						
							|  |  |  | 		if p != nil { | 
					
						
							|  |  |  | 			lg, ok := p.(log.Info) | 
					
						
							|  |  |  | 			if ok { | 
					
						
							|  |  |  | 				if (lg.Entry != log.Entry{}) { | 
					
						
							|  |  |  | 					logs = append(logs, lg.Entry) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	sys.RUnlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-18 03:11:15 +08:00
										 |  |  | // Cancel - cancels the target
 | 
					
						
							|  |  |  | func (sys *HTTPConsoleLoggerSys) Cancel() { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-25 01:05:33 +08:00
										 |  |  | // Type - returns type of the target
 | 
					
						
							|  |  |  | func (sys *HTTPConsoleLoggerSys) Type() types.TargetType { | 
					
						
							|  |  |  | 	return types.TargetConsole | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | // Send log message 'e' to console and publish to console
 | 
					
						
							|  |  |  | // log pubsub system
 | 
					
						
							| 
									
										
										
										
											2022-07-06 05:45:49 +08:00
										 |  |  | func (sys *HTTPConsoleLoggerSys) Send(entry interface{}) error { | 
					
						
							| 
									
										
										
										
											2020-03-21 06:00:44 +08:00
										 |  |  | 	var lg log.Info | 
					
						
							| 
									
										
										
										
											2022-07-06 05:45:49 +08:00
										 |  |  | 	switch e := entry.(type) { | 
					
						
							| 
									
										
										
										
											2019-09-22 16:24:32 +08:00
										 |  |  | 	case log.Entry: | 
					
						
							| 
									
										
										
										
											2020-03-21 06:00:44 +08:00
										 |  |  | 		lg = log.Info{Entry: e, NodeName: sys.nodeName} | 
					
						
							| 
									
										
										
										
											2019-09-22 16:24:32 +08:00
										 |  |  | 	case string: | 
					
						
							| 
									
										
										
										
											2020-03-21 06:00:44 +08:00
										 |  |  | 		lg = log.Info{ConsoleMsg: e, NodeName: sys.nodeName} | 
					
						
							| 
									
										
										
										
											2019-09-22 16:24:32 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-11-11 02:20:21 +08:00
										 |  |  | 	atomic.AddInt64(&sys.totalMessages, 1) | 
					
						
							| 
									
										
										
										
											2019-09-22 16:24:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 	sys.pubsub.Publish(lg) | 
					
						
							| 
									
										
										
										
											2019-12-17 12:30:57 +08:00
										 |  |  | 	sys.Lock() | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | 	// add log to ring buffer
 | 
					
						
							|  |  |  | 	sys.logBuf.Value = lg | 
					
						
							|  |  |  | 	sys.logBuf = sys.logBuf.Next() | 
					
						
							| 
									
										
										
										
											2019-12-17 12:30:57 +08:00
										 |  |  | 	sys.Unlock() | 
					
						
							| 
									
										
										
										
											2022-11-11 02:20:21 +08:00
										 |  |  | 	err := sys.console.Send(entry, string(logger.All)) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		atomic.AddInt64(&sys.failedMessages, 1) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return err | 
					
						
							| 
									
										
										
										
											2019-09-04 02:10:48 +08:00
										 |  |  | } |