<commit_before>#!/usr/bin/env bash
#
# Check bandwidth plugin for Nagios
#
# Usage: check_bandwidth.sh [-i interface] [-s sleep] [-w warning] [-c critical]
#     -i, --interface         Interface name (eth0 by default)
#     -s, --sleep             Sleep time between both statistics measures
#     -w, --warning           Warning value (KB/s)
#     -c, --critical          Critical value (KB/s)
#     -h, --help              Display this screen
#
# (c) 2014, <NAME> <<EMAIL>>
# https://github.com/bdossantos/nagios-plugins
#

while [[ -n "$1" ]]; do
  case $1 in
    --interface | -i)
      interface=$2
      shift
      ;;
    --sleep | -s)
      sleep=$2
      shift
      ;;
    --warning | -w)
      warn=$2
      shift
      ;;
    --critical | -c)
      crit=$2
      shift
      ;;
    --help | -h)
      sed -n '2,11p' "$0" | tr -d '#'
      exit 3
      ;;
    *)
      echo "Unknown argument: $1"
      exec "$0" --help
      exit 3
      ;;
  esac
  shift
done

warn=${warn:=5000}
crit=${crit:=6000}
interface=${interface:=eth0}
sleep=${sleep:=5}

rx1=$(cat "/sys/class/net/${interface}/statistics/rx_bytes")
tx1=$(cat "/sys/class/net/${interface}/statistics/tx_bytes")
sleep "$sleep"
rx2=$(cat "/sys/class/net/${interface}/statistics/rx_bytes")
tx2=$(cat "/sys/class/net/${interface}/statistics/tx_bytes")

tx_bps=$((tx2 - tx1))
rx_bps=$((rx2 - rx1))
tx_kbps=$((tx_bps / 1024))
rx_kbps=$((rx_bps / 1024))

status="tx $1: $tx_kbps kb/s rx $1: $rx_kbps kb/s"
if [[ $rx_kbps -ge $warn ]] || [[ $tx_kbps -ge $warn ]]; then
  if [[ $rx_kbps -ge $crit ]] || [[ $tx_kbps -ge $crit ]]; then
    exit_status=2
    echo "CRITICAL - $status"
  else
    exit_status=1
    echo "WARNING - $status"
  fi
else
  exit_status=0
  echo "OK - $status"
fi

exit ${exit_status:=3}<commit_msg>Check if network interface statistics are fetchable<commit_after>#!/usr/bin/env bash
#
# Check bandwidth plugin for Nagios
#
# Usage: check_bandwidth.sh [-i interface] [-s sleep] [-w warning] [-c critical]
#     -i, --interface         Interface name (eth0 by default)
#     -s, --sleep             Sleep time between both statistics measures
#     -w, --warning           Warning value (KB/s)
#     -c, --critical          Critical value (KB/s)
#     -h, --help              Display this screen
#
# (c) 2014, <NAME> <<EMAIL>>
# https://github.com/bdossantos/nagios-plugins
#

while [[ -n "$1" ]]; do
  case $1 in
    --interface | -i)
      interface=$2
      shift
      ;;
    --sleep | -s)
      sleep=$2
      shift
      ;;
    --warning | -w)
      warning=$2
      shift
      ;;
    --critical | -c)
      critical=$2
      shift
      ;;
    --help | -h)
      sed -n '2,11p' "$0" | tr -d '#'
      exit 3
      ;;
    *)
      echo "Unknown argument: $1"
      exec "$0" --help
      exit 3
      ;;
  esac
  shift
done

warning=${warning:=5000}
critical=${critical:=8000}
interface=${interface:=eth0}
sleep=${sleep:=1}

if [[ $warning -ge $critical ]]; then
  echo "UNKNOWN - warning ($warning) can't be greater than critical ($critical)"
  exit 3
fi

if [[ ! -f "/sys/class/net/${interface}/statistics/rx_bytes" ]] ||
  [[ ! -f "/sys/class/net/${interface}/statistics/tx_bytes" ]]; then
  echo "CRITICAL - Could not fetch '${interface}' interface statistics"
  exit 2
fi

rx1=$(cat "/sys/class/net/${interface}/statistics/rx_bytes")
tx1=$(cat "/sys/class/net/${interface}/statistics/tx_bytes")
sleep "$sleep"
rx2=$(cat "/sys/class/net/${interface}/statistics/rx_bytes")
tx2=$(cat "/sys/class/net/${interface}/statistics/tx_bytes")

tx_bps=$((tx2 - tx1))
rx_bps=$((rx2 - rx1))
tx_kbps=$((tx_bps / 1024))
rx_kbps=$((rx_bps / 1024))

status="tx $1: $tx_kbps kb/s | rx $1: $rx_kbps kb/s"
if [[ $rx_kbps -ge $warning ]] || [[ $tx_kbps -ge $warning ]]; then
  if [[ $rx_kbps -ge $critical ]] || [[ $tx_kbps -ge $critical ]]; then
    exit_status=2
    echo "CRITICAL - $status"
  else
    exit_status=1
    echo "WARNING - $status"
  fi
else
  exit_status=0
  echo "OK - $status"
fi

exit ${exit_status:=3}
{'fraction_non_alphanumeric': 0.18459229614807404, 'fraction_numerical': 0.026013006503251626, 'mean_word_length': 2.4267352185089974, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '872079', 'n_tokens_mistral': 1547, 'n_tokens_neox': 1449, 'n_words': 362}