From cab2232abaf78bf579359f5717ffc89544308360 Mon Sep 17 00:00:00 2001 From: Sagar Dhawan Date: Fri, 15 Nov 2019 13:25:40 -0800 Subject: [PATCH] Fix System Stats script (#6985) automerge --- .../grafana-provisioning/dashboards/testnet-monitor.json | 2 +- scripts/system-stats.sh | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) mode change 100644 => 100755 scripts/system-stats.sh diff --git a/metrics/scripts/grafana-provisioning/dashboards/testnet-monitor.json b/metrics/scripts/grafana-provisioning/dashboards/testnet-monitor.json index c7fc10b43..873f7945b 100644 --- a/metrics/scripts/grafana-provisioning/dashboards/testnet-monitor.json +++ b/metrics/scripts/grafana-provisioning/dashboards/testnet-monitor.json @@ -9707,7 +9707,7 @@ ], "orderByTime": "ASC", "policy": "default", - "query": "SELECT max(\"ram_usage\") as \"ram_usage\" FROM \"$testnet\".\"autogen\".\"system-stats\" WHERE hostname =~ /$hostid/ AND $timeFilter GROUP BY time(1s) fill(null)\n", + "query": "SELECT max(\"ram_usage\") as \"ram_usage\" FROM \"$testnet\".\"autogen\".\"system-stats\" WHERE hostname =~ /$hostid/ AND $timeFilter GROUP BY time(5s) fill(null)\n", "rawQuery": true, "refId": "B", "resultFormat": "time_series", diff --git a/scripts/system-stats.sh b/scripts/system-stats.sh old mode 100644 new mode 100755 index 2410ade72..bd77e6043 --- a/scripts/system-stats.sh +++ b/scripts/system-stats.sh @@ -11,10 +11,12 @@ cd "$(dirname "$0")/.." source scripts/configure-metrics.sh while true; do + # collect top twice because the first time is inaccurate + top_ouput="$(top -bn2 -d1)" # collect the total cpu usage by subtracting idle usage from 100% - cpu_usage=$(top -bn1 | grep '%Cpu(s):' | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}') + cpu_usage=$(echo "${top_ouput}" | grep '%Cpu(s):' | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | tail -1 | awk '{print 100 - $1}') # collect the total ram usage by dividing used memory / total memory - ram_total_and_usage=$(top -bn1 | grep 'MiB Mem'| sed "s/.*: *\([0-9.]*\)%* total.*, *\([0-9.]*\)%* used.*/\1 \2/") + ram_total_and_usage=$(echo "${top_ouput}" | grep '.*B Mem'| tail -1 | sed "s/.*: *\([0-9.]*\)%* total.*, *\([0-9.]*\)%* used.*/\1 \2/") read -r total used <<< "$ram_total_and_usage" ram_usage=$(awk "BEGIN {print $used / $total * 100}")