Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- SERVER="server.example.net"
- PORT=1337
- USER="fakeuser"
- PASSWORD="fakepass"
- INTERVAL=3 # Update interval
- command_exists () {
- type "$1" &> /dev/null ;
- }
- if command_exists netcat; then
- NETBIN="netcat"
- elif command_exists nc; then
- NETBIN="nc"
- else
- echo "netcat not found, install it."
- exit 1
- fi
- RUNNING=true
- clean_up () {
- echo "Quit." >&2
- RUNNING=false
- rm -f /tmp/fuckbash
- }
- trap clean_up SIGINT SIGTERM EXIT
- STRING=""
- while $RUNNING; do
- rm -f /tmp/fuckbash
- PREV_TOTAL=0
- PREV_IDLE=0
- AUTH=true
- TIMER=0
- while $RUNNING; do
- if $AUTH; then
- echo "${USER}:${PASSWORD}"
- AUTH=false
- fi
- sleep $INTERVAL
- if ! $RUNNING; then
- exit 0
- fi
- # Connectivity
- Online="\"online4\": true, "
- Online="\"online6\": true, "
- # Uptime
- Uptime=$(awk '{ print int($1) }' /proc/uptime)
- # Load Average
- Load1=$(awk '{ print $1 }' /proc/loadavg)
- Load=$(echo "$Load1+0.17"|bc)
- # Memory
- MemTotal="24483804"
- MemFree=$(free | awk '/Mem/{print $7}')
- MemUsed=$(($MemTotal - $MemFree))
- SwapFree=$(awk '/SwapFree/ {print $2}' /proc/meminfo)
- SwapTotal=0
- SwapUsed=0
- # Disk
- HDDTotal=328318646
- HDDUsed=97348700
- # HDDUsed=$(echo "$(df -Tlm --total | tail -1 | awk '{print $4}')*4" | bc)
- # CPU
- # Get the total CPU statistics, discarding the 'cpu ' prefix.
- CPU=($(sed -n 's/^cpu\s//p' /proc/stat))
- IDLE=${CPU[3]} # Just the idle CPU time.
- # Calculate the total CPU time.
- TOTAL=0
- for VALUE in "${CPU[@]}"; do
- let "TOTAL=$TOTAL+$VALUE"
- done
- # Calculate the CPU usage since we last checked.
- let "DIFF_IDLE=$IDLE-$PREV_IDLE"
- let "DIFF_TOTAL=$TOTAL-$PREV_TOTAL"
- let "DIFF_USAGE=(1000*($DIFF_TOTAL-$DIFF_IDLE)/$DIFF_TOTAL+5)/10"
- # Remember the total and idle CPU times for the next check.
- PREV_TOTAL="$TOTAL"
- PREV_IDLE="$IDLE"
- # Network traffic
- NET=($(grep ":" /proc/net/dev | grep -v -e "lo" -e "tun" | awk '{a+=$2}{b+=$10}END{print a,b}'))
- NetRx="$((${NET[0]}*8))"
- NetTx="$((${NET[1]}*8))"
- if [ "$PREV_NetRx" == "" ]; then
- PREV_NetRx="$NetRx"
- PREV_NetTx="$NetTx"
- fi
- let "SpeedRx=($NetRx-$PREV_NetRx)/$INTERVAL"
- let "SpeedTx=($NetTx-$PREV_NetTx)/$INTERVAL"
- PREV_NetRx="$NetRx"
- PREV_NetTx="$NetTx"
- echo -e "update {$Online \"uptime\": $Uptime, \"load\": $Load, \"memory_total\": $MemTotal, \"memory_used\": $MemUsed, \"swap_total\": $SwapTotal, \"swap_used\": $SwapUsed, \"hdd_total\": $HDDTotal, \"hdd_used\": $HDDUsed, \"cpu\": ${DIFF_USAGE}.0, \"network_rx\": $SpeedRx, \"network_tx\": $SpeedTx }"
- done | $NETBIN $SERVER $PORT | while IFS= read -r -d $'\0' x; do
- if [ ! -f /tmp/fuckbash ]; then
- if grep -q "IPv6" <<< "$x"; then
- echo "Connected." >&2
- echo 4 > /tmp/fuckbash
- exit 0
- elif grep -q "IPv4" <<< "$x"; then
- echo "Connected." >&2
- echo 6 > /tmp/fuckbash
- exit 0
- fi
- fi
- done
- wait
- if ! $RUNNING; then
- echo "Exiting"
- rm -f /tmp/fuckbash
- exit 0
- fi
- # keep on trying after a disconnect
- echo "Disconnected." >&2
- sleep 3
- echo "Reconnecting..." >&2
- done
Add Comment
Please, Sign In to add comment