Created
January 18, 2016 16:58
-
-
Save sjmf/62ebe5cb4d4b895141d8 to your computer and use it in GitHub Desktop.
BuildAX wget scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script which may be added to crontab to request data from LRS using wget, | |
# then convert to a csv file using the bin2csv utility. | |
# | |
# Logic uses the current time via a date call to create a file name and then | |
# asks for last 5 minutes of readings | |
# | |
cd ${0%/*} # cd to working dir of script | |
# Login variables- adjust as required | |
ROUTER="openlab-bax4" | |
USER="admin" | |
PASS="buildAX2014CLab" | |
# Address to which failure mails will be sent | |
MAILTO="s.j.finnigan@ncl.ac.uk" | |
STOP_MAIL="/tmp/baxwget_stopmail" | |
# Folder to store temporary donwloaded files in | |
#FOLDER=`mktemp -d` | |
FOLDER="/tmp/bax2sql" | |
mkdir -p $FOLDER # Create if it doesn't exist | |
# Folder to move files after processing | |
#DEST="/tmp/bax2sql/" | |
#mkdir -p $DEST | |
# Tools directory | |
TOOLDIR=. | |
# alter T60 as the number of seconds of data to download | |
# alter TOFFSET to account for time difference with LRS | |
PERIOD=600 # 60 seconds = 1 minute (release frequency) | |
T60=$(( $PERIOD + 1 )) | |
THR=3601 | |
TOFFSET=3600 | |
TOFFSET=60 | |
# Retry counter (will decrement to zero) | |
RETRY=3 | |
# Emails sent in response to failure | |
read -d '' MAIL_HEADER <<EOM | |
This is an automated message sent by the baxwget script: | |
EOM | |
read -d '' FAIL_MAIL <<EOM | |
The script will attempt to reset the LRS via telnet. If you do not receive | |
further mail, then the script has corrected the problem and will continue working. | |
EOM | |
read -d '' RETRY_MAIL <<EOM | |
The script failed to retrieve data from the router after retrying. | |
No more mail will be sent until the next success. | |
EOM | |
read -d '' SUCCESS_MAIL <<EOM | |
The script succeeded in retrieving data from the LRS. Normal service is resumed. | |
EOM | |
# Request session authentication cookie | |
login() { | |
echo "Logging in to ${ROUTER}" | |
wget -q --save-cookies ${FOLDER}/cookies.txt \ | |
--delete-after \ | |
--no-http-keep-alive \ | |
--post-data "user=${USER}&pass=${PASS}" \ | |
--keep-session-cookies \ | |
http://${ROUTER}/WWW/login.htm | |
# Return success/failure of login | |
grep "failed" ${FOLDER}/cookies.txt && return 1 | |
return 0 | |
} | |
# Request data from the LRS | |
# adjust start= as required | |
fetch() { | |
echo "Retrieving http://${ROUTER}/fetch?type=BT&start=${TSTARTADJ}&end=${TEND}" | |
wget -O ${FOLDER}/${FILE}.BIN \ | |
--content-on-error \ | |
--load-cookies ${FOLDER}/cookies.txt \ | |
--tries=11 --read-timeout=5 \ | |
--timeout=5 \ | |
--no-http-keep-alive \ | |
--content-disposition \ | |
http://${ROUTER}/fetch?type=BT\&start=${TSTARTADJ}\&end=${TEND} | |
status=$? | |
if [ $status != 0 ]; | |
then | |
echo "Non-zero return: $status" | |
sed "s/<[^>]\+>//g" ${FOLDER}/${FILE}.BIN | |
rm ${FOLDER}/${FILE}.BIN | |
fi | |
return $status # Return return status of wget | |
} | |
# Failure handler | |
retry() { | |
echo -e "\nFetch Failed. Retries remaining: $RETRY" | |
echo "Attempting to reset LRS..." | |
./telnet-reset $ROUTER | |
RETRY=$(($RETRY-1)) | |
if (( $RETRY > 0 )) | |
then | |
# if [ ! -e $STOP_MAIL ] | |
# then | |
# mailx -r "$MAILTO" -s "$ROUTER Fetch Failed" $MAILTO <<EOM | |
#$MAIL_HEADER | |
#Fetch Failed on $ROUTER. | |
#File $FILE was not successfully fetched. (Retries remaining: $RETRY) | |
#$FAIL_MAIL | |
#EOM | |
# fi | |
sleep 15 | |
login # Need to log-in again | |
process # Retry | |
else | |
if [ ! -e $STOP_MAIL ] | |
then | |
mailx -r "$MAILTO" -s "$ROUTER Fetch Retries Exceeded" $MAILTO <<EOM | |
$MAIL_HEADER | |
$RETRY_MAIL | |
EOM | |
touch $STOP_MAIL | |
fi | |
exit 1 | |
fi | |
} | |
# Grab the file from the router and decode it | |
process() { | |
#echo $FILE | |
echo Request: $TSTARTADJ to $TEND | |
# Run fetch method- it returns success/failure of wget command | |
if fetch; | |
then | |
# Succeeded | |
if [ -e $STOP_MAIL ] | |
then | |
rm $STOP_MAIL | |
mailx -r "$MAILTO" -s "$ROUTER Fetch Succeeded" $MAILTO <<EOM | |
$MAIL_HEADER | |
$SUCCESS_MAIL | |
EOM | |
fi | |
else | |
# Failure handler | |
retry | |
fi | |
# convert binary file to csv | |
# java -jar bin2csv.jar $FILE $FILE.csv | |
${TOOLDIR}/bin2csv ${FOLDER}/${FILE}.BIN ${FOLDER}/${FILE}.csv && \ | |
echo "Added ${FILE}.csv" | |
# Remove binary | |
rm ${FOLDER}/${FILE}.BIN | |
# add cp or mv code as required or wput at this point | |
#mv ${FOLDER}/${FILE}.csv ${DEST}/${FILE}.csv | |
} | |
# Main logic | |
if ! login; | |
then | |
echo "Login Failed" | |
else | |
# Set destination filename (global variables) | |
FILE=$(date +%Y-%m-%d-%H-%M-%S -d now) | |
TSTART=$(($(date +%s -d now)-946684800-$TOFFSET)) | |
TSTARTADJ=$(($TSTART-$T60)) # Adjust start to subtract T60 | |
TEND=`expr $TSTARTADJ + $T60` | |
# Do the thing | |
process | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# m h dom mon dow command | |
*/10 * * * * /opt/baxwget/baxwget.sh >> /var/log/buildax/wget.log 2>&1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/expect | |
set timeout 20 | |
set user admin | |
set pass buildAX2014CLab | |
set host openlab-bax4.ncl.ac.uk | |
spawn telnet $host | |
expect "Login:" | |
send "$user\n" | |
expect "Password:" | |
send "$pass\n" | |
expect "Welcome!" | |
send "reset\n" | |
sleep 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment