Commit 175d0ee6 authored by Lei Li's avatar Lei Li
Browse files

feat: 增加调试版本

parent ba515042
...@@ -9,3 +9,7 @@ ...@@ -9,3 +9,7 @@
build: .pre-build build: .pre-build
go build -gcflags='all=-N -l' -ldflags "-X agent/cmd/agent/config.Version=`./branch_info.sh get_version` -X agent/cmd/agent/config.BuildCommit=`git rev-list -1 HEAD`" -o bin/linkfog_agent ./cmd/agent go build -gcflags='all=-N -l' -ldflags "-X agent/cmd/agent/config.Version=`./branch_info.sh get_version` -X agent/cmd/agent/config.BuildCommit=`git rev-list -1 HEAD`" -o bin/linkfog_agent ./cmd/agent
bin/linkfog_agent -v | tee version bin/linkfog_agent -v | tee version
build-dev: .pre-build
go build -tags dev -gcflags='all=-N -l' -ldflags "-X agent/cmd/agent/config.Version=`./branch_info.sh get_version` -X agent/cmd/agent/config.BuildCommit=`git rev-list -1 HEAD`" -o bin/linkfog_agent ./cmd/agent
bin/linkfog_agent -v | tee version
\ No newline at end of file
...@@ -5,6 +5,8 @@ import "time" ...@@ -5,6 +5,8 @@ import "time"
var ( var (
WorkDir = "/linkfog/agent/" WorkDir = "/linkfog/agent/"
BinDir = "bin"
PluginDir = "plugin"
LogDir = "log" LogDir = "log"
LogFileName = "agent.log" LogFileName = "agent.log"
DaemonLogFileName = "agent_daemon.log" DaemonLogFileName = "agent_daemon.log"
...@@ -12,12 +14,9 @@ var ( ...@@ -12,12 +14,9 @@ var (
MountPrefix = "/" MountPrefix = "/"
MountProc = MountPrefix + "/proc/" MountProc = MountPrefix + "/proc/"
PluginDir = WorkDir + "/plugin"
Version = "" Version = ""
BuildCommit = "" BuildCommit = ""
Edition = ""
MonitorLogUnModDuration = 600 * time.Second MonitorLogUnModDuration = 600 * time.Second
BinDir = "bin"
) )
//go:build dev
package config
func init() {
Edition = "dev"
}
...@@ -147,6 +147,7 @@ func initSvcCfg() error { ...@@ -147,6 +147,7 @@ func initSvcCfg() error {
} else { } else {
} }
global.SubscribePrefixInfo = global.DeviceSerialNumber + "/publish/"
// 时间修正 // 时间修正
if !iTime.SynchronizeSystemTime() { if !iTime.SynchronizeSystemTime() {
......
...@@ -29,14 +29,14 @@ func (a *Agent) chatMsgProcess(msg *global.Message) { ...@@ -29,14 +29,14 @@ func (a *Agent) chatMsgProcess(msg *global.Message) {
switch msg.Key { switch msg.Key {
case global.ConsumerTopicAgentSelfUpgrade: case global.ConsumerTopicAgentSelfUpgrade:
case global.ConsumerTopicPluginUpgrade: case global.ConsumerTopicPluginUpgrade:
fallthrough
case global.ConsumerTopicStartupPlugin:
fallthrough
case global.ConsumerTopicStopPlugin:
err := module.SendMsgToMod(global.PluginModuleName, msg) err := module.SendMsgToMod(global.PluginModuleName, msg)
if err != nil { if err != nil {
l.Error(err) l.Error(err)
return return
} }
fallthrough
case global.ConsumerTopicStartupPlugin:
fallthrough
case global.ConsumerTopicStopPlugin:
} }
} }
...@@ -93,6 +93,12 @@ func SetWorkDir() error { ...@@ -93,6 +93,12 @@ func SetWorkDir() error {
return err return err
} }
config.PluginDir = path.Join(config.WorkDir, config.PluginDir)
err = os.MkdirAll(config.BinDir, 0644)
if err != nil {
return err
}
return nil return nil
} }
......
...@@ -14,6 +14,8 @@ const ( ...@@ -14,6 +14,8 @@ const (
ConsumerTopicStopPlugin = "stop-plugin" ConsumerTopicStopPlugin = "stop-plugin"
) )
var SubscribePrefixInfo = ""
type MQTTConnInfo struct { type MQTTConnInfo struct {
Status int Status int
ReconnectTimes int ReconnectTimes int
......
...@@ -7,6 +7,8 @@ import ( ...@@ -7,6 +7,8 @@ import (
"path/filepath" "path/filepath"
"time" "time"
"agent/cmd/agent/config"
"github.com/olekukonko/tablewriter" "github.com/olekukonko/tablewriter"
) )
...@@ -36,6 +38,10 @@ type options struct { ...@@ -36,6 +38,10 @@ type options struct {
MonitorDefaultBackupFileName string MonitorDefaultBackupFileName string
SerialNumber string SerialNumber string
// 调试信息
MQTTBrokerURL string
MQTTBrokerPort int
} }
const ( const (
...@@ -64,6 +70,10 @@ const ( ...@@ -64,6 +70,10 @@ const (
) )
func flagSet() { func flagSet() {
if config.Edition == "dev" {
flag.StringVar(&Opt.MQTTBrokerURL, "mqtt-url", "tcp://broker.hivemq.com", "MQTT broker url")
flag.IntVar(&Opt.MQTTBrokerPort, "mqtt-port", 1883, "MQTT broker port")
}
flag.BoolVar(&Opt.Usage, ArgUsage, false, "custom format usage") flag.BoolVar(&Opt.Usage, ArgUsage, false, "custom format usage")
flag.BoolVar(&Opt.PrintVersion, ArgVersion, false, "print version and exit") flag.BoolVar(&Opt.PrintVersion, ArgVersion, false, "print version and exit")
flag.BoolVar(&Opt.Debug, ArgDebug, false, "debug mode") flag.BoolVar(&Opt.Debug, ArgDebug, false, "debug mode")
......
...@@ -5,7 +5,9 @@ import ( ...@@ -5,7 +5,9 @@ import (
"strings" "strings"
"time" "time"
"agent/cmd/agent/config"
"agent/cmd/agent/global" "agent/cmd/agent/global"
"agent/cmd/agent/option"
"linkfog.com/public/lib/l" "linkfog.com/public/lib/l"
MQTT "github.com/eclipse/paho.mqtt.golang" MQTT "github.com/eclipse/paho.mqtt.golang"
...@@ -35,6 +37,10 @@ func New(opts ...BackendOpt) *Backend { ...@@ -35,6 +37,10 @@ func New(opts ...BackendOpt) *Backend {
for _, b := range global.HostInfoTail.MQTTInfo.BrokerInfo { for _, b := range global.HostInfoTail.MQTTInfo.BrokerInfo {
clientOpts.AddBroker(fmt.Sprintf("ssl://%s", b.BrokerIP)) clientOpts.AddBroker(fmt.Sprintf("ssl://%s", b.BrokerIP))
} }
if config.Edition == "dev" {
clientOpts.AddBroker(fmt.Sprintf("%s:%d", option.Opt.MQTTBrokerURL, option.Opt.MQTTBrokerPort))
}
clientOpts.SetClientID(global.DeviceSerialNumber) clientOpts.SetClientID(global.DeviceSerialNumber)
clientOpts.SetUsername(global.MQTTUsername) clientOpts.SetUsername(global.MQTTUsername)
clientOpts.SetPassword(global.MQTTPassword) clientOpts.SetPassword(global.MQTTPassword)
...@@ -51,7 +57,7 @@ func New(opts ...BackendOpt) *Backend { ...@@ -51,7 +57,7 @@ func New(opts ...BackendOpt) *Backend {
clientOpts.SetOnConnectHandler(connectCallback) clientOpts.SetOnConnectHandler(connectCallback)
clientOpts.SetConnectionLostHandler(connectInterruptionCallback) clientOpts.SetConnectionLostHandler(connectInterruptionCallback)
clientOpts.SetReconnectingHandler(reconnectingCallback) clientOpts.SetReconnectingHandler(reconnectingCallback)
clientOpts.SetDefaultPublishHandler(messagePubHandler) clientOpts.SetDefaultPublishHandler(handleSubscribeMsgCallback)
// 初始化客户端 // 初始化客户端
b.client = MQTT.NewClient(clientOpts) b.client = MQTT.NewClient(clientOpts)
...@@ -65,7 +71,7 @@ func (b *Backend) Start() error { ...@@ -65,7 +71,7 @@ func (b *Backend) Start() error {
for { for {
if token := b.client.Connect(); token.Wait() && token.Error() != nil { if token := b.client.Connect(); token.Wait() && token.Error() != nil {
l.Infof("MQTT init connecting err :%v", token.Error()) l.Warnf("MQTT init connecting err :%v", token.Error())
b.Status = global.MQTTFailedToConnect b.Status = global.MQTTFailedToConnect
b.ReconnectTimes++ b.ReconnectTimes++
} else { } else {
...@@ -131,12 +137,13 @@ func reconnectingCallback(client MQTT.Client, opts *MQTT.ClientOptions) { ...@@ -131,12 +137,13 @@ func reconnectingCallback(client MQTT.Client, opts *MQTT.ClientOptions) {
backendMod.ReconnectTimes++ backendMod.ReconnectTimes++
} }
func messagePubHandler(client MQTT.Client, msg MQTT.Message) { func handleSubscribeMsgCallback(client MQTT.Client, msg MQTT.Message) {
l.Infof("MQTT Received message from topic %s, %s", msg.Topic(), string(msg.Payload())) l.Infof("MQTT Received message from topic %s, %s", msg.Topic(), string(msg.Payload()))
if strings.HasPrefix(msg.Topic(), global.DeviceSerialNumber+"/publish/") { if strings.HasPrefix(msg.Topic(), global.SubscribePrefixInfo) {
key := msg.Topic()[strings.Index(msg.Topic(), global.SubscribePrefixInfo)+len(global.SubscribePrefixInfo):]
global.HostInfoTail.MQTTInfo.ConsumeChan <- &global.Message{ global.HostInfoTail.MQTTInfo.ConsumeChan <- &global.Message{
Payload: string(msg.Payload()), Payload: string(msg.Payload()),
Key: msg.Topic(), Key: key,
} }
} }
//} else if msg.Topic() == "device/sleep" { //} else if msg.Topic() == "device/sleep" {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment