Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Lei Li
agent
Commits
175d0ee6
Commit
175d0ee6
authored
Oct 16, 2024
by
Lei Li
Browse files
feat: 增加调试版本
parent
ba515042
Changes
9
Show whitespace changes
Inline
Side-by-side
Makefile
View file @
175d0ee6
...
@@ -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
cmd/agent/config/config.go
View file @
175d0ee6
...
@@ -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"
)
)
cmd/agent/config/edition_dev.go
0 → 100644
View file @
175d0ee6
//go:build dev
package
config
func
init
()
{
Edition
=
"dev"
}
cmd/agent/core/agent.go
View file @
175d0ee6
...
@@ -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
()
{
...
...
cmd/agent/core/chat_msg.go
View file @
175d0ee6
...
@@ -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
:
}
}
}
}
cmd/agent/global/global.go
View file @
175d0ee6
...
@@ -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
}
}
...
...
cmd/agent/global/mqtt.go
View file @
175d0ee6
...
@@ -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
...
...
cmd/agent/option/option.go
View file @
175d0ee6
...
@@ -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"
)
...
...
module/backend/backend.go
View file @
175d0ee6
...
@@ -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
.
Info
f
(
"MQTT init connecting err :%v"
,
token
.
Error
())
l
.
Warn
f
(
"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" {
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment