2021-04-18 15:41:13 -04: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-03 14:10:48 -04:00
|
|
|
|
|
|
|
package madmin
|
|
|
|
|
|
|
|
import (
|
2020-03-20 18:00:44 -04:00
|
|
|
"context"
|
2019-09-03 14:10:48 -04:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LogInfo holds console log messages
|
|
|
|
type LogInfo struct {
|
2020-03-20 18:00:44 -04:00
|
|
|
logEntry
|
2019-09-22 04:24:32 -04:00
|
|
|
ConsoleMsg string
|
|
|
|
NodeName string `json:"node"`
|
|
|
|
Err error `json:"-"`
|
2019-09-03 14:10:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetLogs - listen on console log messages.
|
2020-03-20 18:00:44 -04:00
|
|
|
func (adm AdminClient) GetLogs(ctx context.Context, node string, lineCnt int, logKind string) <-chan LogInfo {
|
2019-09-03 14:10:48 -04:00
|
|
|
logCh := make(chan LogInfo, 1)
|
|
|
|
|
|
|
|
// Only success, start a routine to start reading line by line.
|
|
|
|
go func(logCh chan<- LogInfo) {
|
|
|
|
defer close(logCh)
|
|
|
|
urlValues := make(url.Values)
|
|
|
|
urlValues.Set("node", node)
|
|
|
|
urlValues.Set("limit", strconv.Itoa(lineCnt))
|
2019-10-11 21:50:54 -04:00
|
|
|
urlValues.Set("logType", logKind)
|
2019-09-03 14:10:48 -04:00
|
|
|
for {
|
|
|
|
reqData := requestData{
|
2019-10-23 00:01:14 -04:00
|
|
|
relPath: adminAPIPrefix + "/log",
|
2019-09-03 14:10:48 -04:00
|
|
|
queryValues: urlValues,
|
|
|
|
}
|
|
|
|
// Execute GET to call log handler
|
2020-03-20 18:00:44 -04:00
|
|
|
resp, err := adm.executeMethod(ctx, http.MethodGet, reqData)
|
2019-09-03 14:10:48 -04:00
|
|
|
if err != nil {
|
|
|
|
closeResponse(resp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
logCh <- LogInfo{Err: httpRespToErrorResponse(resp)}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dec := json.NewDecoder(resp.Body)
|
|
|
|
for {
|
|
|
|
var info LogInfo
|
|
|
|
if err = dec.Decode(&info); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
select {
|
2020-03-20 18:00:44 -04:00
|
|
|
case <-ctx.Done():
|
2019-09-03 14:10:48 -04:00
|
|
|
return
|
|
|
|
case logCh <- info:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}(logCh)
|
|
|
|
|
|
|
|
// Returns the log info channel, for caller to start reading from.
|
|
|
|
return logCh
|
|
|
|
}
|