<commit_before>#!/usr/bin/bats

export ZOOKEEPER_HOST=127.0.0.1
export ZOOKEEPER_PORT=2181
export MON_IFS=$(cat /opt/bro/etc/node.cfg | grep interface | \
	sed 's/interface=//' | paste -sd " " -)

export MYTMPDIR=$(mktemp -d)
trap 'rm -rf "${TMPDIR}"' EXIT INT TERM HUP


#----------------------------------------------------------------------------
#                               INTERFACE
#----------------------------------------------------------------------------

@test "Check each monitor interface is live" {
  local timeout_sec=5
  local timeout_pkt=5
  local results=()

  # Timeout after 5 seconds or 5 packets
  for interface in $MON_IFS; do
    packets_1=$(ethtool -S ${interface} | awk '/rx_packets/ {print $2}')
    sleep ${timeout_sec}
    packets_2=$(ethtool -S ${interface} | awk '/rx_packets/ {print $2}') 
    packets=$(echo `expr ${packets_2} - ${packets_1}`)
    echo "${interface} had ${packets} packets in ${timeout_sec} secs."
    [ $packets -gt 0 ]
  done
}

@test "Check interface errors" {

  for interface in $MON_IFS; do
    count=$(ethtool -S ${interface} | grep error | grep -v " 0$" | wc -l )
    echo "${interface} had ${count} error types."
    echo ">> Check \`ethtool -S ${interface}\` << "
    [ $count -eq 1 ]
  done
}

#----------------------------------------------------------------------------
#                               BRO
#----------------------------------------------------------------------------


@test "Check that broctl is running" {
  sudo /opt/bro/bin/broctl status
}

#----------------------------------------------------------------------------
#                               ZOOKEEPER
#----------------------------------------------------------------------------

@test "Check that zookeeper is running" {
  service zookeeper status
}

@test "Check that zookeeper is listening" {
  ss -lnt | grep ${ZOOKEEPER_PORT} 
}

@test "Check that client can connect to zookeeper" {
  echo "" | ncat  ${ZOOKEEPER_HOST} ${ZOOKEEPER_PORT} 
}

#----------------------------------------------------------------------------
#                               KAFKA
#----------------------------------------------------------------------------

@test "Check that kafka is running" {
  service kafka status
}

@test "Check that kafka is connected to zookeeper" {
  kafka_pid=$(systemctl show -p MainPID kafka.service | cut -d= -f2) 
  echo "kafka_pid: ${kafka_pid}"
  kafka_socket=$(sudo ss -ntp | grep "${kafka_pid}" | \
	grep "${ZOOKEEPER_PORT}" | awk '{ print $4 }')<commit_msg>Added more checks. Needs love on LS/ES side.<commit_after>#!/usr/bin/bats

export ZOOKEEPER_HOST=127.0.0.1
export ZOOKEEPER_PORT=2181
export MON_IFS=$(cat /opt/bro/etc/node.cfg | grep interface | \
	sed 's/interface=//' | paste -sd " " -)

#----------------------------------------------------------------------------
#                               INTERFACE
#----------------------------------------------------------------------------

@test "Check each monitor interface is live" {
  local timeout_sec=5
  local timeout_pkt=5
  local results=()

  # Timeout after 5 seconds or 5 packets
  for interface in $MON_IFS; do
    packets_1=$(ethtool -S ${interface} | awk '/rx_packets/ {print $2}')
    sleep ${timeout_sec}
    packets_2=$(ethtool -S ${interface} | awk '/rx_packets/ {print $2}') 
    packets=$(echo `expr ${packets_2} - ${packets_1}`)
    echo "${interface} had ${packets} packets in ${timeout_sec} secs."
    [ $packets -gt 0 ]
  done
}

@test "Check for interface errors" {
  for interface in $MON_IFS; do
    count=$(ethtool -S ${interface} | grep error | grep -v " 0$" | wc -l )
    echo "${interface} had ${count} error types."
    echo ">> Check \`ethtool -S ${interface}\` << "
    [ $count -eq 0 ]
  done
}

@test "Check monitor interface for tx packets" {
  for interface in $MON_IFS; do
    pkts=$(ethtool -S ${interface} | awk '/tx_packets/ {print $2}')
    echo "WARNING: Monitor interface ${interface} has sent ${pkts} packets".
    [ $pkts -eq 0 ]
  done
}

@test "Check PF_RING settings" {
  cap_tx=$(cat /proc/net/pf_ring/info | awk '/Capture TX/ {print $4}')
  echo "WARNING: PR_RING is currently set to capture TX packets."
  [ "${cap_tx}" = "No" ]
}

#----------------------------------------------------------------------------
#                               BRO
#----------------------------------------------------------------------------

@test "Check that broctl is running" {
  # This will fail if a worker is crashed as well
  sudo /opt/bro/bin/broctl status
}

@test "Check for bro-detected packet loss" {
  local notice_log='/data/bro/logs/current/notice.log'
  if [ ! -f "${notice_log}"]; then
    skip "No notice.log to check for packet loss errors."
  fi

  capture_cnt=$(cat ${notice_log} | grep 'CaptureLoss::Too_Much_Loss'|wc -l)
  drop_cnt=$(cat ${notice_log} | grep 'PacketFilter::Dropped_Packets'|wc -l)

  if [ $capture_cnt > 0 ]; then
    if [ $drop_cnt > 0 ]; then
      echo "Sensor is dropping packets before Bro can process them."
      [ $drop_cnt -eq 0 ]
    else
      echo "Packets are being dropped prior to sensor receiving them."
      [ $capture_cnt -eq 0 ]
    fi
  fi
}

#----------------------------------------------------------------------------
#                               ZOOKEEPER
#----------------------------------------------------------------------------

@test "Check that zookeeper is running" {
  service zookeeper status
}

@test "Check that zookeeper is listening" {
  ss -lnt | grep ${ZOOKEEPER_PORT} 
}

@test "Check that client can connect to zookeeper" {
  echo "" | ncat  ${ZOOKEEPER_HOST} ${ZOOKEEPER_PORT} 
}

#----------------------------------------------------------------------------
#                               KAFKA
#----------------------------------------------------------------------------

@test "Check that kafka is running" {
  service kafka status
}

@test "Check that kafka is connected to zookeeper" {
  kafka_pid=$(systemctl show -p MainPID kafka.service | cut -d= -f2) 
  echo "kafka_pid: ${kafka_pid}"
  kafka_socket=$(sudo ss -ntp | grep "${kafka_pid}" | \
	grep "${ZOOKEEPER_PORT}" | awk '{ print $4 }')
{'fraction_non_alphanumeric': 0.31089430894308945, 'fraction_numerical': 0.009268292682926829, 'mean_word_length': 1.4350752177355504, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '1088748', 'n_tokens_mistral': 1844, 'n_tokens_neox': 1774, 'n_words': 527}