plugin.go 3.52 KB
Newer Older
Lei Li's avatar
Lei Li committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package plugin

import (
	"agent/cmd/agent/global"
	"fmt"
	"linkfog.com/public/lib/common"
	"sync"
	"time"

	"agent/cmd/agent/option"
	"linkfog.com/pluginx/pluginmgr"
	"linkfog.com/pluginx/pluginrpc"
	"linkfog.com/public/lib/l"
)

var defaultPluginMgrConf string = `{
	"doberman": {
		"name": "doberman",
		"path": "/dosec/plugin/doberman",
		"enable": true,
		"mem": 629145600,
		"md5": "uninitialized"
	}
}`

var defaultPluginCliConf = map[string]string{
	DobermanPlugin: DobermanPluginSocket,
}

type Plugin struct {
Lei Li's avatar
Lei Li committed
31
	signal     chan *global.Message
Lei Li's avatar
Lei Li committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
	signalSize int
	isRunning  bool
	plgServer  *pluginServer
	plgConf    map[string]*pluginmgr.PluginProcessConf
	processMgr *pluginmgr.PluginProcessMgr
	grpcServer *pluginrpc.PluginGrpcServer
	grpcClient *pluginrpc.PluginGrpcClient
	sync.Mutex
}

type PluginOpt func(*Plugin)

func New(opts ...PluginOpt) *Plugin {
	p := Plugin{
Lei Li's avatar
Lei Li committed
46
		signalSize: 512,
Lei Li's avatar
Lei Li committed
47
48
49
50
	}
	for _, opt := range opts {
		opt(&p)
	}
Lei Li's avatar
Lei Li committed
51
	p.signal = make(chan *global.Message, p.signalSize)
Lei Li's avatar
Lei Li committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

	return &p
}

func (p *Plugin) Start() error {
	if p.IsRunning() {
		return nil
	}
	l.Info("init plugin module")

	//// 启动自身grpc服务
	//p.plgServer = newPluginServer()
	//var err error
	//p.grpcServer, err = pluginrpc.NewPluginGrpcServer(HadesPluginSocket, p.plgServer)
	//if err != nil {
	//	return fmt.Errorf("NewPluginGrpcServer err: %v", err)
	//}

	// 启动插件管理器,运行各插件进程
	pluginmgr.EnableValidatePluginMD5 = option.Opt.EnableValidatePluginMD5
	pluginmgr.InitDur = option.Opt.PluginStatusCheckDur
	plgCfgMap, err := pluginmgr.LoadPluginConfigWithData([]byte(defaultPluginMgrConf))
	if err != nil {
		return fmt.Errorf("LoadPluginConfigWithData err: %v", err)
	}
	for name, plg := range plgCfgMap {
		l.Info("plugin config:", name, plg)
	}
	p.plgConf = plgCfgMap
	p.processMgr = pluginmgr.NewProcessMgr(p.plgConf)
	p.processMgr.Start()

	// 建立与各插件进程的grpc连接
	p.grpcClient, err = pluginrpc.NewPluginGrpcClient(defaultPluginCliConf)
	if err != nil {
		return fmt.Errorf("NewGrpcClientHelper err: %v", err)
	}

	// 等待所有插件运行正常
	allIsRunning := false
	for i := 0; i < option.Opt.PluginStatusCheckTimes; i++ {
		if p.processMgr.AllPluginProcessIsRunning() {
			allIsRunning = true
			break
		}
		l.Info("all plugin process are init ...")
		time.Sleep(option.Opt.PluginStatusCheckDur)
	}
	if !allIsRunning {
		return fmt.Errorf("plugin processes init failed")
	}

	go func() {
Lei Li's avatar
Lei Li committed
105
		var sig *global.Message
Lei Li's avatar
Lei Li committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
		var ok bool
		for {
			select {
			case sig, ok = <-p.signal:
				if !ok {
					p.Stop()
					l.Info("module resources exit")
					return
				} else {
					p.dealWithSig(sig)
				}
			}
		}
	}()
	p.isRunning = true
	l.Info("init plugin module success")
	return nil
}

func (p *Plugin) Name() string {
	return global.PluginModuleName
}

func (p *Plugin) IsRunning() bool {
	return p.isRunning
}

func (p *Plugin) Stop() {
	if p.IsRunning() {
		if p.processMgr != nil {
			p.processMgr.Stop()
		}
		if p.grpcClient != nil {
			p.grpcClient.Close()
		}
		if p.grpcServer != nil {
			p.grpcServer.Close()
		}
		close(p.signal)
		p.isRunning = false
	}
}

Lei Li's avatar
Lei Li committed
149
func (p *Plugin) Receive(msg *global.Message) error {
Lei Li's avatar
Lei Li committed
150
	if len(p.signal) > (p.signalSize - 2) {
Lei Li's avatar
Lei Li committed
151
		return l.WrapError("plugin manager signal chan is full, drop msg:", msg.Key)
Lei Li's avatar
Lei Li committed
152
153
154
155
156
	}
	p.signal <- msg
	return nil
}

Lei Li's avatar
Lei Li committed
157
158
159
160
161
func (p *Plugin) dealWithSig(msg *global.Message) {
	defer common.TimeCost("deal sig finished, msgType:" + msg.Key)()

	switch msg.Key {
	case global.ConsumerTopicPluginUpgrade:
Lei Li's avatar
Lei Li committed
162

Lei Li's avatar
Lei Li committed
163
	case global.ConsumerTopicStartupPlugin:
Lei Li's avatar
Lei Li committed
164

Lei Li's avatar
Lei Li committed
165
166
	case global.ConsumerTopicStopPlugin:
	}
Lei Li's avatar
Lei Li committed
167
}