Skip to content

Commit

Permalink
update fuzzer.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
DDmitriy248 committed Aug 27, 2023
1 parent dea95db commit 26348a8
Showing 1 changed file with 60 additions and 29 deletions.
89 changes: 60 additions & 29 deletions docker/runtime-fuzzer/scripts/fuzzer.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
#!/bin/bash
set -e

VOLUME_DIR='/home/ubuntu/fuzzer/fuzzing-seeds-dir/'
CORPUS_DIR='/home/ubuntu/fuzzer/corpus/'
ARTIFACT_DIR='/home/ubuntu/fuzzer/artifacts'
WORK_DIR=$(pwd -P)
VOLUME_DIR="$WORK_DIR/fuzzing-seeds-dir/"
CORPUS_DIR="$WORK_DIR/corpus/"
ARTIFACT_DIR="$WORK_DIR/artifacts"
ARCHIVE_PATH="/opt/download-archives/"
# DOCKER PARAMS
CONTAINER_NAME=node-fuzzer
IMAGE='ghcr.io/gear-tech/gear-node-fuzzer:latest'
DOCKER_EXIT_CODE=''
# ALERTING PARAMS
GROUP_ID='***'
BOT_TOKEN='***'

# Function to check if error was OOM
# Function to check container
function _check_need_arch {
cmd=$(tail -n 50 node-fuzzer.log | grep 'ERROR: libFuzzer: out-of-memory')
if [[ $cmd ]]; then
_dcode=$(docker inspect ${CONTAINER_NAME} --format='{{.State.ExitCode}}' )
echo "Container exit code: $_dcode"
cmd=$(docker logs -f --tail 100 ${CONTAINER_NAME} | grep 'ERROR: libFuzzer: out-of-memory')
if [ "$_dcode" -eq 137 ]; then
echo "Container was stopped manually"
return 0
elif [[ $cmd ]]; then
echo "Archiving doesn't needed due to OOM error"
return 0
else
Expand All @@ -31,7 +40,7 @@ function _check_container_runtime {
}

# Function to start the container and wait for it to stop
function start_container {
function _start_container {
# Start the container in the background
if [ ! "$(docker ps -a -q -f name=${CONTAINER_NAME})" ]; then
if [ "$(docker ps -aq -f status=exited -f name=${CONTAINER_NAME})" ]; then
Expand All @@ -50,36 +59,58 @@ function start_container {
docker wait node-fuzzer
}

function archive_logs {
# Send message+logfile to telegram
function _alert_tg {
docker logs -f --tail 100 ${CONTAINER_NAME} >& node-fuzzer.log
text_message="<b>Node Fuzzer Alert</b> 🔥
<b>Problem:</b> Node Fuzzer Container terminated due to an error.
Please check logs.
<b>Archive link:</b>
<a href='http://ec2-3-101-133-141.us-west-1.compute.amazonaws.com/$1'>Link</a>"

echo "$WORK_DIR"
curl -s \
--data "text=$text_message" --data "chat_id=$GROUP_ID" \
'https://api.telegram.org/bot'$BOT_TOKEN'/sendMessage?parse_mode=HTML'
curl -v \
-F "chat_id=$GROUP_ID" \
-F document=@$WORK_DIR/node-fuzzer.log \
'https://api.telegram.org/bot'$BOT_TOKEN'/sendDocument'
rm node-fuzzer.log
}

function _archive_logs {
ARCHIVE_NAME="node-fuzzer_logs_$(date +%Y-%m-%d_%H-%M-%S).tar.gz"
_dcode=$(docker inspect ${CONTAINER_NAME} --format='{{.State.ExitCode}}' )
echo "Container exit code: $_dcode"
if [ "$_dcode" != 137 ]; then
# Get the logs from the container and archive them with the current timestamp in the filename
docker logs node-fuzzer >& node-fuzzer.log
echo "Copy fuzzing-seeds"
cp ${VOLUME_DIR}fuzzing-seeds ./

if [ _check_need_arch ]; then
echo "Creating tar archive: ${ARCHIVE_NAME}"
# Tar logs and seeds
tar -czvf ${ARCHIVE_PATH}/${ARCHIVE_NAME} node-fuzzer.log fuzzing-seeds
fi
echo "Clean tmp files"
rm node-fuzzer.log fuzzing-seeds
else
echo "Container was killed manually"
fi
# Get the logs from the container and archive them with the current timestamp in the filename
docker logs node-fuzzer >& node-fuzzer.log
split -C 1024m --additional-suffix=.log --numeric-suffixes node-fuzzer.log node-fuzzer_part
rm node-fuzzer.log
echo "Copy fuzzing-seeds"
cp ${VOLUME_DIR}fuzzing-seeds ./
echo "Creating tar archive: ${ARCHIVE_NAME}"
# Tar logs and seeds
tar -czvf ${ARCHIVE_PATH}/${ARCHIVE_NAME} *.log fuzzing-seeds corpus artifacts
# Clean temp files
echo "Clean tmp files"
rm *.log fuzzing-seeds
rm -rf ./atrifacts/*
}

function start {
# Loop to keep restarting the container if it stops due to an error
while true; do
echo "########## $(date) ###########"
echo "Start container: ${CONTAINER_NAME}"
start_container
echo "Start archiving logs"
archive_logs
_start_container
if ! _check_need_arch; then
echo "Start archiving logs"
_archive_logs
echo "Create alert"
_alert_tg $ARCHIVE_NAME
fi
_check_container_runtime
# Clean up the container
docker rm ${CONTAINER_NAME}
Expand Down

0 comments on commit 26348a8

Please sign in to comment.