Skip to content

Commit

Permalink
Add support for sensor temperature in host state (#359)
Browse files Browse the repository at this point in the history
* feat: Add support for sensor temperature in host state

* chore: Update file permissions for saving config data
  • Loading branch information
funnyzak authored Jun 1, 2024
1 parent 3782c0d commit e0e2b8c
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 80 deletions.
4 changes: 2 additions & 2 deletions model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (c *AgentConfig) Save() error {
if err != nil {
return err
}
return os.WriteFile(c.v.ConfigFileUsed(), data, os.ModePerm)
return os.WriteFile(c.v.ConfigFileUsed(), data, 0600)
}

// Config 站点配置
Expand Down Expand Up @@ -205,5 +205,5 @@ func (c *Config) Save() error {
if err != nil {
return err
}
return os.WriteFile(c.v.ConfigFileUsed(), data, os.ModePerm)
return os.WriteFile(c.v.ConfigFileUsed(), data, 0600)
}
24 changes: 24 additions & 0 deletions model/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const (
MTReportHostState
)

type SensorTemperature struct {
Name string
Temperature float64
}

type HostState struct {
CPU float64
MemUsed uint64
Expand All @@ -26,9 +31,18 @@ type HostState struct {
TcpConnCount uint64
UdpConnCount uint64
ProcessCount uint64
Temperatures []SensorTemperature
}

func (s *HostState) PB() *pb.State {
var ts []*pb.State_SensorTemperature
for _, t := range s.Temperatures {
ts = append(ts, &pb.State_SensorTemperature{
Name: t.Name,
Temperature: t.Temperature,
})
}

return &pb.State{
Cpu: s.CPU,
MemUsed: s.MemUsed,
Expand All @@ -45,10 +59,19 @@ func (s *HostState) PB() *pb.State {
TcpConnCount: s.TcpConnCount,
UdpConnCount: s.UdpConnCount,
ProcessCount: s.ProcessCount,
Temperatures: ts,
}
}

func PB2State(s *pb.State) HostState {
var ts []SensorTemperature
for _, t := range s.GetTemperatures() {
ts = append(ts, SensorTemperature{
Name: t.GetName(),
Temperature: t.GetTemperature(),
})
}

return HostState{
CPU: s.GetCpu(),
MemUsed: s.GetMemUsed(),
Expand All @@ -65,6 +88,7 @@ func PB2State(s *pb.State) HostState {
TcpConnCount: s.GetTcpConnCount(),
UdpConnCount: s.GetUdpConnCount(),
ProcessCount: s.GetProcessCount(),
Temperatures: ts,
}
}

Expand Down
241 changes: 164 additions & 77 deletions proto/nezha.pb.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions proto/nezha.proto
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ message State {
uint64 tcp_conn_count = 14;
uint64 udp_conn_count = 15;
uint64 process_count = 16;
repeated State_SensorTemperature temperatures = 17;
}

message State_SensorTemperature {
string name = 1;
double temperature = 2;
}

message Task {
Expand Down
3 changes: 3 additions & 0 deletions resource/l10n/en-US.toml
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,9 @@ other = "Template"
[Stat]
other = "Asset"

[Temperature]
other = "Temperature"

[DisableSwitchTemplateInFrontend]
other = "Disable Switch Template in Frontend"

Expand Down
3 changes: 3 additions & 0 deletions resource/l10n/es-ES.toml
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,9 @@ other = "Plantilla"
[Stat]
other = "Stat"

[Temperature]
other = "Temperatura"

[DisableSwitchTemplateInFrontend]
other = "Deshabilitar Cambio de Plantilla en Frontend"

Expand Down
3 changes: 3 additions & 0 deletions resource/l10n/zh-CN.toml
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,9 @@ other = "主题"
[Stat]
other = "信息"

[Temperature]
other = "温度"

[DisableSwitchTemplateInFrontend]
other = "禁止前台切换模板"

Expand Down
3 changes: 3 additions & 0 deletions resource/l10n/zh-TW.toml
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,9 @@ other = "主題"
[Stat]
other = "信息"

[Temperature]
other = "溫度"

[DisableSwitchTemplateInFrontend]
other = "禁止前台切換主題"

Expand Down
7 changes: 6 additions & 1 deletion resource/template/theme-default/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@
{{tr "ConnCount"}}: TCP @# server.State.TcpConnCount #@ / UDP @# server.State.UdpConnCount #@<br />
{{tr "BootTime"}}: @# formatTimestamp(server.Host.BootTime) #@<br />
{{tr "LastActive"}}: @# new Date(server.LastActive).toLocaleString() #@<br />
{{tr "Version"}}: @#server.Host.Version#@
{{tr "Version"}}: @#server.Host.Version#@ <br/>
{{tr "Temperature"}}:
<span v-for="temp in server.State.Temperatures">
@#temp.Name#@: @#temp.Temperature#@°C &nbsp;
</span>

<div class="chartbox" :key="server.ID" :ref="`chart${server.ID}`" style="width: 100%; height: auto; margin-bottom: 2px;"></div>
</div>
<div class="ui divider" style="margin-bottom: 5px"></div>
Expand Down

0 comments on commit e0e2b8c

Please sign in to comment.