-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-system-stats
executable file
·126 lines (98 loc) · 4.4 KB
/
get-system-stats
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/bash
set -euo pipefail
#system requirements: linux 3.14+, bash 4+, top, date
if [ "$(uname)" != "Linux" ]; then
echo "$0 only supported in linux"
fi
proc="${1-"/proc"}"
cycle_s="${2-"-1"}"
function get_ms {
date +%s%3N
}
function log_value {
name="$1"
value="$2"
if [ -z "$3" ]; then
ms="$(get_ms)"
else
ms="$3"
fi
echo "__BASH_METRIC_1;$name:$value:$ms" #__BASH_METRIC_x where x is the format version
}
function ltrim {
read temp
echo "${temp##" "}"
}
function log_stats {
# num_cores="$(grep 'core id' "$proc/cpuinfo" |sort -u |wc -l)"
# num_virtual_cores=$(grep 'core id' "$proc/cpuinfo" |wc -l)
ms="$(get_ms)"
cpu_agg_levels="$(top -d 1 -b -n 2 | grep -E '^%Cpu' |tail -n1)"
cpu_agg_idle_string="$([[ $cpu_agg_levels =~ ([0-9]+\.[0-9] id) ]] && echo "$BASH_REMATCH")"
cpu_agg_idle_float="$(echo "$cpu_agg_idle_string" | cut -d' ' -f1)"
cpu_agg_idle_int="$(printf "%.0f" "$cpu_agg_idle_float")"
cpu_agg_used="$((100 - cpu_agg_idle_int))"
log_value cpu_aggregate_normalized_p "$cpu_agg_used" "$ms"
ms="$(get_ms)"
cpu_stat="$(cat "$proc/stat")"
cpu_idle_userhz="$(echo "$cpu_stat" | grep -E '^cpu ' | tr -s ' ' |cut -d' ' -f5)"
cpu_iowait_userhz="$(echo "$cpu_stat" | grep -E '^cpu ' | tr -s ' ' |cut -d' ' -f6)"
cpu_processthread_count="$(echo "$cpu_stat" | grep -E '^processes' |cut -d' ' -f2)"
log_value cpu_idle_userhz "$cpu_idle_userhz" "$ms"
log_value cpu_iowait_userhz "$cpu_iowait_userhz" "$ms"
log_value cpu_processthread_count "$cpu_processthread_count" "$ms"
ms="$(get_ms)"
w_head="$(w |head -n1)"
load_averages="${w_head##*load average: }"
load_average_1="$(echo "$load_averages" |cut -d, -f1)"
load_average_5="$(echo "$load_averages" |cut -d' ' -f2 |cut -d, -f1)"
load_average_15="$(echo "$load_averages" |cut -d' ' -f3)"
log_value load_average_1 "$load_average_1" "$ms"
log_value load_average_5 "$load_average_5" "$ms"
log_value load_average_15 "$load_average_15" "$ms"
ms="$(get_ms)"
memory_stats="$(cat /proc/meminfo)"
memory_available="$(echo "$memory_stats" | grep MemAvail |tr -s ' ' | cut -d' ' -f2)"
memory_total="$(echo "$memory_stats" | grep MemTotal |tr -s ' ' | cut -d' ' -f2)"
memory_used="$(($memory_total - $memory_available))"
log_value memory_used_KiB "$memory_used" "$ms"
log_value memory_available_KiB "$memory_available" "$ms"
ms="$(get_ms)"
vmstat_pageouts="$(grep -E '^pgpgout' "$proc/vmstat" |cut -d' ' -f2)"
log_value vmstat_pageouts_count "$vmstat_pageouts" "$ms"
ms="$(get_ms)"
root_disk_info="$(df /)"
root_disk_used="$(echo "$root_disk_info" | tail -n1 |tr -s ' ' |cut -d' ' -f3)"
root_disk_free="$(echo "$root_disk_info" | tail -n1 |tr -s ' ' |cut -d' ' -f4)"
log_value root_disk_used_KiB "$root_disk_used" "$ms"
log_value root_disk_free_KiB "$root_disk_free" "$ms"
ms="$(get_ms)"
sda_stats="$(grep sda "$proc/diskstats" |head -n1 | ltrim)"
sda_reads="$(echo "$sda_stats" | tr -s ' ' |cut -d' ' -f5)"
sda_writes="$(echo "$sda_stats" | tr -s ' ' |cut -d' ' -f9)"
sda_io_queued="$(echo "$sda_stats" | tr -s ' ' |cut -d' ' -f13)"
log_value sda_reads_count "$sda_reads" "$ms"
log_value sda_writes_count "$sda_writes" "$ms"
log_value sda_io_queued_count "$sda_io_queued" "$ms"
ms="$(get_ms)"
eth0_stats="$(grep eth0: "$proc/1/net/dev" | ltrim)"
eth0_received_bytes="$(echo "$eth0_stats" |tr -s ' ' |cut -d' ' -f2)"
eth0_received_packets="$(echo "$eth0_stats" |tr -s ' ' |cut -d' ' -f3)"
eth0_received_errors="$(echo "$eth0_stats" |tr -s ' ' |cut -d' ' -f4)"
eth0_transmitted_bytes="$(echo "$eth0_stats" |tr -s ' ' |cut -d' ' -f10)"
eth0_transmitted_packets="$(echo "$eth0_stats" |tr -s ' ' |cut -d' ' -f11)"
eth0_transmitted_errors="$(echo "$eth0_stats" |tr -s ' ' |cut -d' ' -f12)"
log_value eth0_received_aggregate_B "$eth0_received_bytes" "$ms"
log_value eth0_received_packet_count "$eth0_received_packets" "$ms"
log_value eth0_received_error_count "$eth0_received_errors" "$ms"
log_value eth0_transmitted_aggregate_B "$eth0_transmitted_bytes" "$ms"
log_value eth0_transmitted_packet_count "$eth0_transmitted_packets" "$ms"
log_value eth0_transmitted_error_count "$eth0_transmitted_errors" "$ms"
}
log_stats
if [ "$cycle_s" -gt "-1" ]; then
while true; do
sleep "$cycle_s"
log_stats
done
fi