diff --git a/metrics/cpu.sh b/metrics/cpu.sh index b6584b5..59c4615 100644 --- a/metrics/cpu.sh +++ b/metrics/cpu.sh @@ -1,7 +1,7 @@ #!/bin/sh collect () { - echo $(ps aux | awk '{sum+=$3} END {printf "%.1f\n", sum}' | tail -n 1) + report $(ps aux | awk '{sum+=$3} END {printf "%.1f\n", sum}' | tail -n 1) } docs () { diff --git a/metrics/disk_io.sh b/metrics/disk_io.sh new file mode 100644 index 0000000..3c05a50 --- /dev/null +++ b/metrics/disk_io.sh @@ -0,0 +1,31 @@ +# #!/bin/sh + +# if [ -z $DISK_IO_MOUNTPOINT ]; then +# if is_osx; then +# DISK_IO_MOUNTPOINT="disk0" +# else +# DISK_IO_MOUNTPOINT="/dev/vda" +# fi +# fi + +# if is_osx; then +# __disk_io_bgproc () { +# iostat -K -d -c 99999 -w $INTERVAL $DISK_IO_MOUNTPOINT | while read line; do +# echo $line | awk '{print $3}' > ./foobar +# done +# } +# else +# __disk_io_bgproc () { +# echo $(iostat -y -d 1 $DISK_IO_MOUNTPOINT) +# } +# fi + +# init () { +# mkfifo ./foobar +# #exec 3<> ./foobar +# __disk_io_bgproc > ./foobar & +# } + +# collect () { +# cat ./foobar +# } \ No newline at end of file diff --git a/metrics/disk_usage.sh b/metrics/disk_usage.sh index ed95e59..a06c7aa 100644 --- a/metrics/disk_usage.sh +++ b/metrics/disk_usage.sh @@ -9,8 +9,8 @@ if [ -z $DISK_USAGE_MOUNTPOINT ]; then fi collect () { - echo $(df | awk -v disk_regexp="^$DISK_USAGE_MOUNTPOINT" \ - '$0 ~ disk_regexp {printf "%.1f", $5}') + report $(df | awk -v disk_regexp="^$DISK_USAGE_MOUNTPOINT" \ + '$0 ~ disk_regexp {printf "%.1f", $5}') } docs () { diff --git a/metrics/memory.sh b/metrics/memory.sh index 8d67154..ba85739 100644 --- a/metrics/memory.sh +++ b/metrics/memory.sh @@ -1,22 +1,18 @@ #!/bin/sh if is_osx; then - declare -r __memory_os_memsize=$(sysctl -n hw.memsize) collect () { - echo $(vm_stat | awk -v total_memory=$__memory_os_memsize \ + report $(vm_stat | awk -v total_memory=$__memory_os_memsize \ 'BEGIN {FS=" *"; pages=0} /Pages (free|inactive|speculative)/ {pages+=$2} END {printf "%.1f", 100 - (pages * 4096) / total_memory * 100.0}') } - else - collect () { - echo $(free | awk '/buffers\/cache/{printf "%.1f", 100 - $4 / ($3 + $4) * 100.0}') + report $(free | awk '/buffers\/cache/{printf "%.1f", 100 - $4 / ($3 + $4) * 100.0}') } - fi docs () { diff --git a/metrics/network_io.sh b/metrics/network_io.sh new file mode 100644 index 0000000..59c7190 --- /dev/null +++ b/metrics/network_io.sh @@ -0,0 +1,41 @@ +#!/bin/sh + +if [ -z $NETWORK_IO_INTERFACE ]; then + if is_osx; then + NETWORK_IO_INTERFACE="en0" + else + NETWORK_IO_INTERFACE="eth0" + fi +fi + +declare -r __network_io_divisor=$(($INTERVAL*1024)) +__network_io_sample=(0 0) + +__network_io_calc_kBps() { + echo $1 $2 | awk -v divisor=$__network_io_divisor \ + '{printf "%.2f", ($1 - $2) / divisor}' +} + +if is_osx; then + __network_io_collect () { + netstat -bI $NETWORK_IO_INTERFACE | \ + awk "/$NETWORK_IO_INTERFACE/"'{print $7" "$10; exit}' + } +else + __network_io_collect () { + echo TODO + } +fi + +collect () { + local sample=( $(__network_io_collect) ) + if [ ${__network_io_sample[0]} -ne 0 ]; then + report "in" $(__network_io_calc_kBps ${sample[0]} ${__network_io_sample[0]}) + report "out" $(__network_io_calc_kBps ${sample[1]} ${__network_io_sample[1]}) + fi + __network_io_sample=( "${sample[@]}" ) +} + +docs () { + echo "Network traffic in kB/s." +} \ No newline at end of file diff --git a/metrics/swap.sh b/metrics/swap.sh index b2e7c8c..909ae74 100644 --- a/metrics/swap.sh +++ b/metrics/swap.sh @@ -1,17 +1,13 @@ #!/bin/sh if is_osx; then - collect () { - echo $(sysctl -n vm.swapusage | awk '{printf "%.1f", $6 / $3 * 100.0}') + report $(sysctl -n vm.swapusage | awk '{printf "%.1f", $6 / $3 * 100.0}') } - else - collect () { - echo $(free | awk '/Swap/{printf "%.1f", $3/$2 * 100.0}') + report $(free | awk '/Swap/{printf "%.1f", $3/$2 * 100.0}') } - fi docs () { diff --git a/sysmetricsd.sh b/sysmetricsd.sh index a6ca9af..4ee847a 100755 --- a/sysmetricsd.sh +++ b/sysmetricsd.sh @@ -1,7 +1,7 @@ #!/bin/sh # config -INTERVAL=2 +INTERVAL=1 REPORTER=stdout #init @@ -63,9 +63,23 @@ while true; do continue fi - result=$(__m_${metric}_collect) - __r_${REPORTER}_report $metric $result + report () { + local result + if [ -z $2 ]; then + label=$metric + result="$1" + else + label="$metric.$1" + result="$2" + fi + __r_${REPORTER}_report $label $result + } + + __m_${metric}_collect done sleep $INTERVAL -done \ No newline at end of file +done + +# trap 'kill $(jobs -pr)' SIGINT SIGTERM EXIT +# trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT \ No newline at end of file