Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include UDP sent packet statistics in net stats #1225

Merged
merged 1 commit into from
Sep 14, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion scripts/net-stats.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ cd "$(dirname "$0")"
# shellcheck source=scripts/configure-metrics.sh
source configure-metrics.sh

packets_sent=0
packets_sent_diff=0
packets_received=0
packets_received_diff=0
receive_errors=0
Expand All @@ -22,6 +24,10 @@ update_netstat() {
net_stat=$(netstat -suna)

declare stats
stats=$(echo "$net_stat" | awk 'BEGIN {tmp_var = 0} /packets sent/ {tmp_var = $1} END { print tmp_var }')
Copy link
Contributor

@rob-solana rob-solana Sep 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

netsnmp()
{
  # $1: section name, e.g. "udp"                                                                                                                                                                                                                                                   
  # $2-....: field names to query, e.g. "udp"                                                                                                                                                                                                                                      
  #  outputs field values in order                                                                                                                                                                                                                                                 
  declare -a names=( )
  declare -a values=( )
  declare section=$1

  shift

  while read -r -a line; do
    [[ ${line[0]} == "${section}:" ]] || continue
    names=( "${line[@]:1}" )
    read -r -a line
    values=( "${line[@]:1}" )
  done < /proc/net/snmp

  while [[ -n $1 ]]; do
    for ((i=0; i<${#names[*]}; i++)); do
      [[ ${names[i]} == "$1" ]] && echo "${values[i]}" && break
    done
    shift
  done
}
 

for your purposes:

packets_sent_diff=$(($(netsnmp Udp OutDatagrams) - packets_sent))

packets_sent_diff=$((stats - packets_sent))
packets_sent="$stats"

stats=$(echo "$net_stat" | awk 'BEGIN {tmp_var = 0} /packets received/ {tmp_var = $1} END { print tmp_var }')
packets_received_diff=$((stats - packets_received))
packets_received="$stats"
Expand All @@ -39,7 +45,7 @@ update_netstat

while true; do
update_netstat
report="packets_received=$packets_received_diff,receive_errors=$receive_errors_diff,rcvbuf_errors=$rcvbuf_errors_diff"
report="packets_sent=$packets_sent_diff,packets_received=$packets_received_diff,receive_errors=$receive_errors_diff,rcvbuf_errors=$rcvbuf_errors_diff"

echo "$report"
./metrics-write-datapoint.sh "net-stats,hostname=$HOSTNAME $report"
Expand Down