From 1f511c0b8f7c7d00c320307409506f55d3806c7f Mon Sep 17 00:00:00 2001 From: Ehco1996 Date: Mon, 9 Sep 2024 15:53:30 +0800 Subject: [PATCH] chore: fix some issues --- go.mod | 2 +- hack/get-ehco.sh | 47 ++++++++++++++++++++++------- internal/web/templates/_navbar.html | 2 +- pkg/metric_reader/node.go | 22 +++++++++++--- 4 files changed, 56 insertions(+), 17 deletions(-) diff --git a/go.mod b/go.mod index 2ff9d23c9..23f008918 100644 --- a/go.mod +++ b/go.mod @@ -27,7 +27,6 @@ require ( golang.org/x/sync v0.8.0 golang.org/x/time v0.6.0 google.golang.org/grpc v1.65.0 - gopkg.in/yaml.v3 v3.0.1 modernc.org/sqlite v1.32.0 ) @@ -130,6 +129,7 @@ require ( google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 // indirect google.golang.org/protobuf v1.34.2 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect gvisor.dev/gvisor v0.0.0-20231202080848-1f7806d17489 // indirect howett.net/plist v1.0.1 // indirect lukechampine.com/blake3 v1.3.0 // indirect diff --git a/hack/get-ehco.sh b/hack/get-ehco.sh index c96dc058d..61c71fbc7 100755 --- a/hack/get-ehco.sh +++ b/hack/get-ehco.sh @@ -61,21 +61,46 @@ WantedBy=multi-user.target EOF } -function _check_install_required() { - if [[ -z "$API_OR_CONFIG_PATH" ]]; then - _print_error_msg "Flag for config is required. please use --config to specify the configuration file path or api endpoint." - exit 1 +function _detect_package_manager() { + if command -v apt-get &>/dev/null; then + echo "apt-get" + elif command -v yum &>/dev/null; then + echo "yum" + elif command -v dnf &>/dev/null; then + echo "dnf" + else + echo "未知" fi +} - # check jq and curl - if ! command -v jq &>/dev/null; then - _print_error_msg "jq is required to parse JSON data. please use apt/yum to install jq." +function _install_dependencies() { + local pkg_manager=$(_detect_package_manager) + + case $pkg_manager in + apt-get) + sudo apt-get update + sudo apt-get install -y jq curl + ;; + yum|dnf) + sudo $pkg_manager install -y jq curl + ;; + *) + _print_error_msg "无法检测到支持的包管理器。请手动安装 jq 和 curl。" + exit 1 + ;; + esac +} + +function _check_install_required() { + if [[ -z "$API_OR_CONFIG_PATH" ]]; then + _print_error_msg "需要配置标志。请使用 --config 指定配置文件路径或 API 端点。" exit 1 fi - if ! command -v curl &>/dev/null; then - _print_error_msg "curl is required to download files. please use apt/yum to install curl." - exit 1 + # 检查并安装 jq 和 curl + if ! command -v jq &>/dev/null || ! command -v curl &>/dev/null; then + _print_warning_msg "正在安装必要的依赖项 (jq 和 curl)..." + _install_dependencies fi } @@ -248,7 +273,7 @@ function perform_install() { _download_bin _install_systemd_service - _print_warning_msg "Ehco has been installed." + _print_warning_msg "Ehco 已安装完成。" } function perform_remove() { diff --git a/internal/web/templates/_navbar.html b/internal/web/templates/_navbar.html index 4aa2aa573..693edbfef 100644 --- a/internal/web/templates/_navbar.html +++ b/internal/web/templates/_navbar.html @@ -20,7 +20,7 @@

Ehco Relay

- Metrics + Monitor diff --git a/pkg/metric_reader/node.go b/pkg/metric_reader/node.go index 4730bbd54..4a4e0d7b6 100644 --- a/pkg/metric_reader/node.go +++ b/pkg/metric_reader/node.go @@ -66,7 +66,6 @@ func (b *readerImpl) ParseNodeMetrics(metricMap map[string]*dto.MetricFamily, nm b.processLoadMetrics(metricMap, nm) b.calculateFinalMetrics(nm, cpu) - return nil } @@ -95,9 +94,24 @@ func (b *readerImpl) processMemoryMetrics(metricMap map[string]*dto.MetricFamily } func (b *readerImpl) processDiskMetrics(metricMap map[string]*dto.MetricFamily, nm *NodeMetrics) { - nm.DiskTotalBytes = sumInt64Metric(metricMap, metricFilesystemSizeBytes) - availableDisk := sumInt64Metric(metricMap, metricFilesystemAvailBytes) - nm.DiskUsageBytes = nm.DiskTotalBytes - availableDisk + if metric, ok := metricMap[metricFilesystemSizeBytes]; ok { + for _, m := range metric.Metric { + if getLabel(m, "mountpoint") == "/" { + nm.DiskTotalBytes = int64(getMetricValue(m, metric.GetType())) + break + } + } + } + + if metric, ok := metricMap[metricFilesystemAvailBytes]; ok { + for _, m := range metric.Metric { + if getLabel(m, "mountpoint") == "/" { + availableDisk := int64(getMetricValue(m, metric.GetType())) + nm.DiskUsageBytes = nm.DiskTotalBytes - availableDisk + break + } + } + } } func (b *readerImpl) processNetworkMetrics(metricMap map[string]*dto.MetricFamily, nm *NodeMetrics) {