Rainbond/api/handler/eventLog.go

194 lines
4.8 KiB
Go
Raw Normal View History

2018-03-14 14:12:26 +08:00
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
2017-11-07 11:40:44 +08:00
// RAINBOND, Application Management Platform
2018-03-14 14:33:31 +08:00
2017-11-07 11:40:44 +08:00
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
2018-03-14 14:33:31 +08:00
2017-11-07 11:40:44 +08:00
// 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 General Public License for more details.
2018-03-14 14:33:31 +08:00
2017-11-07 11:40:44 +08:00
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package handler
import (
"bytes"
"compress/zlib"
"context"
"fmt"
"io"
"io/ioutil"
"os"
2020-03-08 01:41:44 +08:00
"path"
2017-11-07 11:40:44 +08:00
2020-03-08 01:41:44 +08:00
"github.com/coreos/etcd/clientv3"
"github.com/goodrain/rainbond/api/model"
api_model "github.com/goodrain/rainbond/api/model"
2020-03-08 01:41:44 +08:00
"github.com/goodrain/rainbond/db"
2020-02-03 13:47:13 +08:00
dbmodel "github.com/goodrain/rainbond/db/model"
eventdb "github.com/goodrain/rainbond/eventlog/db"
2020-03-08 01:41:44 +08:00
"github.com/goodrain/rainbond/util/constants"
2017-11-07 11:40:44 +08:00
)
//LogAction log action struct
type LogAction struct {
2020-02-03 13:47:13 +08:00
EtcdCli *clientv3.Client
eventdb *eventdb.EventFilePlugin
2017-11-07 11:40:44 +08:00
}
//CreateLogManager get log manager
2020-02-03 13:47:13 +08:00
func CreateLogManager(cli *clientv3.Client) *LogAction {
2017-11-07 11:40:44 +08:00
return &LogAction{
2020-02-03 13:47:13 +08:00
EtcdCli: cli,
2018-08-31 12:19:27 +08:00
eventdb: &eventdb.EventFilePlugin{
HomePath: "/grdata/logs/",
},
2018-03-04 22:48:50 +08:00
}
2017-11-07 11:40:44 +08:00
}
2019-08-23 20:33:38 +08:00
// GetEvents get target logs
func (l *LogAction) GetEvents(target, targetID string, page, size int) ([]*dbmodel.ServiceEvent, int, error) {
if target == "tenant" {
return db.GetManager().ServiceEventDao().GetEventsByTenantID(targetID, (page-1)*size, size)
}
return db.GetManager().ServiceEventDao().GetEventsByTarget(target, targetID, (page-1)*size, size)
2019-08-22 16:07:26 +08:00
}
2017-11-07 11:40:44 +08:00
//GetLogList get log list
2020-03-08 01:41:44 +08:00
func (l *LogAction) GetLogList(serviceAlias string) ([]*model.HistoryLogFile, error) {
logDIR := path.Join(constants.GrdataLogPath, serviceAlias)
2017-11-07 11:40:44 +08:00
_, err := os.Stat(logDIR)
if os.IsNotExist(err) {
return nil, err
}
fileList, err := ioutil.ReadDir(logDIR)
if err != nil {
return nil, err
}
2020-03-08 01:41:44 +08:00
var logFiles []*model.HistoryLogFile
2017-11-07 11:40:44 +08:00
for _, file := range fileList {
2020-03-08 01:41:44 +08:00
logfile := &model.HistoryLogFile{
Filename: file.Name(),
RelativePath: path.Join("logs", serviceAlias, file.Name()),
}
logFiles = append(logFiles, logfile)
2017-11-07 11:40:44 +08:00
}
2020-03-08 01:41:44 +08:00
return logFiles, nil
2017-11-07 11:40:44 +08:00
}
//GetLogFile GetLogFile
func (l *LogAction) GetLogFile(serviceAlias, fileName string) (string, string, error) {
2020-03-08 01:41:44 +08:00
logPath := path.Join(constants.GrdataLogPath, serviceAlias)
fullPath := path.Join(logPath, fileName)
2017-11-07 11:40:44 +08:00
_, err := os.Stat(fullPath)
if os.IsNotExist(err) {
return "", "", err
}
return logPath, fullPath, err
}
//GetLogInstance get log web socket instance
func (l *LogAction) GetLogInstance(serviceID string) (string, error) {
2020-02-03 13:47:13 +08:00
value, err := l.EtcdCli.Get(context.Background(), fmt.Sprintf("/event/dockerloginstacne/%s", serviceID))
if err != nil {
return "", err
}
2020-02-03 13:47:13 +08:00
if len(value.Kvs) > 0 {
2020-02-03 13:54:30 +08:00
return string(value.Kvs[0].Value), nil
2017-11-07 11:40:44 +08:00
}
2020-02-03 13:47:13 +08:00
2020-02-04 14:49:48 +08:00
return "", nil
2017-11-07 11:40:44 +08:00
}
//GetLevelLog get event log
2017-11-07 11:40:44 +08:00
func (l *LogAction) GetLevelLog(eventID string, level string) (*api_model.DataLog, error) {
re, err := l.eventdb.GetMessages(eventID, level, 0)
2017-11-07 11:40:44 +08:00
if err != nil {
return nil, err
}
if re != nil {
messageList, ok := re.(eventdb.MessageDataList)
if ok {
return &api_model.DataLog{
Status: "success",
Data: messageList,
}, nil
}
}
2017-11-07 11:40:44 +08:00
return &api_model.DataLog{
Status: "success",
Data: nil,
2017-11-07 11:40:44 +08:00
}, nil
}
//Decompress zlib解码
func decompress(zb []byte) ([]byte, error) {
b := bytes.NewReader(zb)
var out bytes.Buffer
r, err := zlib.NewReader(b)
if err != nil {
return nil, err
}
if _, err := io.Copy(&out, r); err != nil {
return nil, err
}
return out.Bytes(), nil
}
func checkLevel(level, info string) bool {
switch level {
case "error":
if info == "error" {
return true
}
return false
case "info":
if info == "info" || info == "error" {
return true
}
return false
case "debug":
if info == "info" || info == "error" || info == "debug" {
return true
}
return false
default:
if info == "info" || info == "error" {
return true
}
return false
}
}
func uncompress(source []byte) (re []byte, err error) {
r, err := zlib.NewReader(bytes.NewReader(source))
if err != nil {
return nil, err
}
var buffer bytes.Buffer
io.Copy(&buffer, r)
r.Close()
return buffer.Bytes(), nil
}
func bubSort(d []api_model.MessageData) []api_model.MessageData {
for i := 0; i < len(d); i++ {
for j := i + 1; j < len(d); j++ {
if d[i].Unixtime > d[j].Unixtime {
temp := d[i]
d[i] = d[j]
d[j] = temp
}
}
}
return d
}