mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-10-25 15:25:01 +03:00
fix(monitoring): read LXC CPU, swap and uptime from the container (#6920)
In an LXC container with LXCFS the Overview showed the container's RAM beside the host's swap, the host's CPU topology and a CPU percentage from the host's view. Swap and uptime now come from LXCFS's virtualised files and CPU from the container's cgroup v2 counter, normalised by the visible vCPUs and any quota; container CPU and swap history is kept apart from the earlier host samples. A value the reader cannot measure (the first CPU sample, or a layout it cannot verify such as cgroup v1) is reported as unavailable instead of 0: the dashboard, the node list, node CPU history and the Telegram and Discord uptime show a dash, and nodes keep the flags in a new unavailable_metrics column.
This commit is contained in:
@@ -187,7 +187,12 @@ func (t *Tgbot) prepareServerUsageInfo() string {
|
||||
info += t.I18nBot("tgbot.messages.ipv6", "IPv6=="+ipv6)
|
||||
}
|
||||
|
||||
info += t.I18nBot("tgbot.messages.serverUpTime", "UpTime=="+strconv.FormatUint(t.lastStatus.Uptime/86400, 10), "Unit=="+t.I18nBot("tgbot.days"))
|
||||
uptime, uptimeUnit := "—", ""
|
||||
if t.lastStatus.MetricAvailable("uptime") {
|
||||
uptime = strconv.FormatUint(t.lastStatus.Uptime/86400, 10)
|
||||
uptimeUnit = t.I18nBot("tgbot.days")
|
||||
}
|
||||
info += t.I18nBot("tgbot.messages.serverUpTime", "UpTime=="+uptime, "Unit=="+uptimeUnit)
|
||||
info += t.I18nBot("tgbot.messages.serverLoad", "Load1=="+strconv.FormatFloat(t.lastStatus.Loads[0], 'f', 2, 64), "Load2=="+strconv.FormatFloat(t.lastStatus.Loads[1], 'f', 2, 64), "Load3=="+strconv.FormatFloat(t.lastStatus.Loads[2], 'f', 2, 64))
|
||||
info += t.I18nBot("tgbot.messages.serverMemory", "Current=="+common.FormatTraffic(int64(t.lastStatus.Mem.Current)), "Total=="+common.FormatTraffic(int64(t.lastStatus.Mem.Total)))
|
||||
info += t.I18nBot("tgbot.messages.onlinesCount", "Count=="+fmt.Sprint(len(onlines)))
|
||||
|
||||
@@ -14,11 +14,14 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/mymmrac/telego"
|
||||
"github.com/nicksnyder/go-i18n/v2/i18n"
|
||||
"golang.org/x/text/language"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/web/locale"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/web/service"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/xray"
|
||||
)
|
||||
@@ -284,3 +287,44 @@ func TestNotifyExhaustedLooksUpEachIDOnce(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerUsageInfoShowsUnavailableUptime(t *testing.T) {
|
||||
bundle := i18n.NewBundle(language.English)
|
||||
_ = bundle.AddMessages(language.English, &i18n.Message{ID: "tgbot.messages.serverUpTime", Other: "Uptime: {{.UpTime}} {{.Unit}}\n"}, &i18n.Message{ID: "tgbot.days", Other: "days"})
|
||||
oldLocale := locale.LocalizerBot
|
||||
locale.LocalizerBot = i18n.NewLocalizer(bundle, "en")
|
||||
t.Cleanup(func() { locale.LocalizerBot = oldLocale })
|
||||
statusCache.mutex.Lock()
|
||||
oldStatus, oldTime := statusCache.data, statusCache.timestamp
|
||||
statusCache.mutex.Unlock()
|
||||
serverStatsCache.mutex.Lock()
|
||||
oldStats, oldStatsTime := serverStatsCache.data, serverStatsCache.timestamp
|
||||
serverStatsCache.mutex.Unlock()
|
||||
t.Cleanup(func() {
|
||||
statusCache.mutex.Lock()
|
||||
statusCache.data, statusCache.timestamp = oldStatus, oldTime
|
||||
statusCache.mutex.Unlock()
|
||||
serverStatsCache.mutex.Lock()
|
||||
serverStatsCache.data, serverStatsCache.timestamp = oldStats, oldStatsTime
|
||||
serverStatsCache.mutex.Unlock()
|
||||
})
|
||||
for _, unavailable := range []bool{true, false} {
|
||||
t.Run(map[bool]string{true: "unavailable", false: "valid zero"}[unavailable], func(t *testing.T) {
|
||||
bot := &Tgbot{}
|
||||
status := &service.Status{Loads: []float64{0, 0, 0}}
|
||||
if unavailable {
|
||||
status.UnavailableMetrics = []string{"uptime"}
|
||||
}
|
||||
bot.setCachedStatus(status)
|
||||
bot.setCachedServerStats("")
|
||||
info := bot.prepareServerUsageInfo()
|
||||
want := "Uptime: 0 days"
|
||||
if unavailable {
|
||||
want = "Uptime: —"
|
||||
}
|
||||
if !strings.Contains(info, want) {
|
||||
t.Fatalf("report did not contain %q: %s", want, info)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user