#!/usr/bin/bash

#############################################
#                                           #
#   Developed by Estevan Ramalho            #
#                                           #
#############################################

# Terminal Variables
RED="\033[33;31m" # text color to red
GREEN="\033[33;32m" # text color to green
CR="\e[0m" # text color reset
column_position="\r\033[70C"
BOLD="$(tput bold)" # font to bold
FR="$(tput sgr0)" # font reset
term_lines=`tput lines`
term_columns=`tput cols`
area_lines="9"
area_cols="30"
horizontal_blocks=""
vertical_blocks=""
ON="[$GREEN ON $CR ]"
OFF="[$RED OFF $CR]"

# Variables
# Database
db_connection=""
mysql_actual_state="none"
mysql_last_state="none"
# Cpu usage
previous_total=""
previous_idle=""
# Germand Status
gearmand_running=""
network_count="0"
#Boxes
draw_box="0"
force_draw="0"
# Arguments Variables
print="0"
box_active="0"

# Messages
press_to_quit="Press q to quit."
mysql_stopped=$(printf "%sMysql stopped.%s" $RED $CR)
no_info="No Info."

# OCP Variables
refresh_time="0.5"

# Remove cursor
tput civis

function main {

    database_connection_info
    mysql_handler
    terminal_cleaner

    while : ; do

        terminal_size
        define_blocks
        block="1"
        line="1"
        cols="0"

        mysql_handler
        box_handler
        main_title

        #tput cup 0 0
        #echo -en "A? $box_active D? $draw_box F? $force_draw"

        for (( i = 0; i < $vertical_blocks; i++ )) ; do

            for (( j = 0; j < $horizontal_blocks; j++ )) ; do

                tput cup $line $cols

                case $block in
                    "1")
                        block_1 $line $cols
                    ;;
                    "2")
                        block_2 $line $cols
                    ;;
                    "3")
                        block_3 $line $cols
                    ;;
                    "4")
                        block_4 $line $cols
                    ;;
                    "5")
                        block_5 $line $cols
                    ;;
                    "6")
                        block_6 $line $cols
                    ;;
                    "7")
                        block_7 $line $cols
                    ;;
                    "8")
                        block_8 $line $cols
                    ;;
                esac

                ((block++))
                cols=$(($cols + $area_cols))

            done

            line=$(($line + $area_lines))

            cols="0"

        done

        # HOW DO I GET OUT?
        quit_message

        # Print by SERIAL
        if [[ $print == 1 ]]; then
            tput cup $((term_lines++)) 0
            tput cnorm
            break
        fi

        # What to do?
        stty -echo
        read -n 1 -t $refresh_time key
        case $key in
            "q")
                terminal_cleaner
                reset
                tput cnorm
                break
            ;;
            "p")
                print="1"
            ;;
            "b")
                if [[ $box_active == "0" ]]; then
                    box_active="1"
                    force_box_redraw
                else
                    box_active="0"
                    terminal_cleaner
                fi
            ;;
            "r")
                terminal_cleaner
                force_box_redraw
            ;;
        esac

        unset key

    done

    exit 0

}

function box () {

    line_box=$1
    cols_box=$2

    printer $((line_box++)) $cols_box "┌────────────────────────────┐"
    printer $line_box $(($cols_box + 29)) "│"
    printer $((line_box++)) $cols_box "│"
    printer $line_box $(($cols_box + 29)) "│"
    printer $((line_box++)) $cols_box "│"
    printer $line_box $(($cols_box + 29)) "│"
    printer $((line_box++)) $cols_box "│"
    printer $line_box $(($cols_box + 29)) "│"
    printer $((line_box++)) $cols_box "│"
    printer $line_box $(($cols_box + 29)) "│"
    printer $((line_box++)) $cols_box "│"
    printer $line_box $(($cols_box + 29)) "│"
    printer $((line_box++)) $cols_box "│"
    printer $line_box $(($cols_box + 29)) "│"
    printer $((line_box++)) $cols_box "│"
    printer $((line_box++)) $cols_box "└────────────────────────────┘"

}

function box_handler {

    draw_box="0"

    if [[ $box_active == "1" && $force_draw == "1" ]]; then

        draw_box="1"

    fi

    force_draw="0"

}

function database_connection_info {

    db_user=`grep DBUSER /usr/local/opmon/etc/db.php | awk -F '"' '{print $2}'`
    db_password=`grep DBPASS /usr/local/opmon/etc/db.php | awk -F '"' '{print $2}'`
    db_address=`grep DBHOST /usr/local/opmon/etc/db.php | awk -F '"' '{print $2}'`
    db_connection="-u$db_user -h $db_address"

    if [[ "$db_password" ]]; then
        db_connection="$db_connection -p$db_password"
    fi

}

function define_blocks {

    # Cabalistic Numbers
    # 240 - 120 - 90 - 60 - 30
    if [[ $term_columns -ge "240" ]]; then
        horizontal_blocks="8"
        vertical_blocks="1"
    elif [[ $term_columns -ge "120" ]]; then
        horizontal_blocks="4"
        vertical_blocks="2"
    elif [[ $term_columns -ge "90" ]]; then
        horizontal_blocks="3"
        vertical_blocks="3"
    elif [[ $term_columns -ge "60" ]]; then
        horizontal_blocks="2"
        vertical_blocks="4"
    elif [[ $term_columns -ge "30" ]]; then
        horizontal_blocks="1"
        vertical_blocks="8"
    fi

}

function force_box_redraw {

    force_draw="1"

}

function get_options () {

    local OPTIND

    while getopts ":pbhr:" opt; do

        case $opt in
            "b")
                box_active="1"
                force_draw="1"
            ;;
            "p")
                print="1"
            ;;
            "h")
                show_help
            ;;
            "r")
                refresh_time=$OPTARG
            ;;
            :)
                echo -en "Option -$OPTARG requires an argument.\n" >&2
                show_help
                exit 1
            ;;
        esac

    done

}

function main_title {

    # Cabalistic Numbers
    # 240 - 120 - 90 - 60 - 30

    width=""
    spaces=""

    if [[ horizontal_blocks -eq "8" ]]; then
        width="240"
    elif [[ horizontal_blocks -eq "4" ]]; then
        width="120"
    elif [[ horizontal_blocks -eq "3" ]]; then
        width="90"
    elif [[ horizontal_blocks -eq "2" ]]; then
        width="60"
    else
        width="30"
    fi

    host_name_size=${#HOSTNAME}
    half_name_size=$(($host_name_size / 2))
    half_of_width=$(($width / 2))
    number_of_spaces=""

    if [ $half_name_size -lt $half_of_width ] ; then
        number_of_spaces=$(($half_of_width - $half_name_size))
    fi

    for (( s = 0; s < $number_of_spaces; s++ )) ; do
        spaces="$spaces "
    done

    printer 0 0 "$spaces$BOLD$HOSTNAME$FR"

    unset width spaces

}

function mem_converter () {

    # 1 GB = 1 048 576  kilobyte - KB or kB, Kbyte
    # 1 MB =     1 024  kilobyte - KB or kB, Kbyte
    giga_byte="1048576"
    mega_byte="1024"
    tmp_conv=""

    if [[ $1 -ge $giga_byte ]]; then
      tmp_conv=$(($1 / $giga_byte))
      printf "$BOLD%sGB$FR" $tmp_conv
    elif [[ $1 -ge $mega_byte ]]; then
      tmp_conv=$(($1 / $mega_byte))
      printf "$BOLD%sMB$FR" $tmp_conv
    else
      printf "$BOLD%sKB$FR" $tmp_conv
    fi

    unset giga_byte mega_byte tmp_conv

}

function mysql_handler {

    ret_db=$(test_database_connection)

    tmp_mysql=""

    if [[ $ret_db -eq "0" ]]; then
        tmp_mysql="stopped"
    fi

    if [[ $ret_db -eq "1" ]]; then
        tmp_mysql="started"
    fi

    if [[ $mysql_actual_state = "none" ]] && [[ $mysql_last_state = "none" ]]; then

        mysql_actual_state=$tmp_mysql
        mysql_last_state=$tmp_mysql

    fi

    mysql_actual_state=$tmp_mysql

    if [[ $mysql_actual_state != $mysql_last_state ]]; then

        terminal_cleaner

        force_box_redraw

        mysql_last_state=$tmp_mysql

    fi

    unset tmp_mysql ret_db

}

function quit_message {

    msg_size=`expr length "$press_to_quit"`
    tput cup $term_lines $(expr $term_columns - $msg_size)
    echo -en $press_to_quit
    tput cup $term_lines $(expr $term_columns + 1)
    unset msg_size

}

function show_help {

msg="
OpMon Control Pannel v3.1

Brings to you a summary of the system's health.

    Parameters

        -h      Show this helpful message.
        -p      Prints the summary and quits.
        -b      Show boxes around each block
        -r      Change the refresh time. Default 0.5 seconds

    On execution the commands bellow are avaliable.

        q       Leaves the program through The exit.
        p       Prints exacly like \"-p\"
        b       Same as \"-b\"
        r       Refresh screen

    This script's function it's to assist the analyst with the validation
of the OpMon's health.  It collects a few information from the system and
displays in groups called blocks.
    You  can resize the term size ( lines x columns ) as you need and the
blocks will auto-adjust in the left space. Try it out. ;)
    Remember to use the root user.

Utilization

 ./ocp [-h] [-p] [-b] [-r <seconds>]

This is the Help. My only friend, the Help.
"

    echo -en "$msg"
    tput cnorm

    unset msg

    exit 0

}

function terminal_cleaner {

    tput clear
    tput cup 1 0

}

function terminal_size {

    new_term_lines=`tput lines`

    new_term_columns=`tput cols`

    if [[ $term_lines -ne $new_term_lines ]] || [[ $term_columns -ne $new_term_columns ]]; then

        term_lines=$new_term_lines

        term_columns=$new_term_columns

        terminal_cleaner

        force_box_redraw

    fi

    unset new_term_lines new_term_columns

}

function test_database_connection {

    tmp_up=`ps -ef| grep "^mysql"| grep -v grep| wc -l`

    if [[ $tmp_up -eq 0 ]]; then
        echo 0
    else
        echo 1
    fi

    unset tmp_up

}

function test_user {

    if [[ $USER != "root" ]]; then
        echo -en "This tool must be executed with root user.\n"
        show_help
        exit 1
    fi

}

function printer () {

    tput cup $1 $2
    echo -en "$3"

}

function block_1 () {

    line_block_1=$1
    cols_block_1=$2

    if [[ $draw_box == "1" ]]; then
        box $line $cols
    fi

    cols_block_1=$((++cols_block_1))

    title="System Info"
    printer $((line_block_1++)) $cols_block_1 "$BOLD$title$FR"

    # OS Version
    OS=`cat /etc/redhat-release | awk '{gsub(" Enterprise| Linux|  Server| release","");print}'`
    printer $((line_block_1++)) $cols_block_1 "$OS"

    # OpMon Version
    if [[ $(test_database_connection) == 1 ]]; then
        query_opmon_version="select opmon_version from opmon4.program_status"
        opmon_version=`mysql -N $db_connection -e " $query_opmon_version"`
    else
        opmon_version=$no_info
    fi
    printer $((line_block_1++)) $cols_block_1 "OpMon Version: $opmon_version"

    # SELinux Informaton
    selinux_status=`getenforce`
    printer $((line_block_1++)) $cols_block_1 "Selinux: $selinux_status"

    # Uptime
    # Min -> 60 # Hour -> 3600 # Day -> 86400 # year -> 31536000
    uptime_info=`cat /proc/uptime | awk -F '.' '{print $1}'`
    years=""
    days=""
    hours="00"
    minutes="00"

    if [[ $uptime_info -ge "31536000" ]]; then
        tmp=$(($uptime_info / 31536000))
        years="$tmp years, "
        uptime_info=$(($uptime_info - (31536000 * $tmp) ))
    fi
    if [[ $uptime_info -ge "86400" ]]; then
        tmp=$(($uptime_info / 86400))
        days="$tmp days, "
        uptime_info=$(($uptime_info - (86400 * $tmp) ))
    fi
    if [[ $uptime_info -ge "3600" ]]; then
        tmp=$(($uptime_info / 3600))
        hours=$(printf "%.2d" $tmp)
        uptime_info=$(($uptime_info - (3600 * $tmp) ))
    fi
    if [[ $uptime_info -ge "60" ]]; then
        tmp=$(($uptime_info / 60))
        minutes=$(printf "%.2d" $tmp)
    fi
    printer $((line_block_1++)) $cols_block_1 "UpTime"
    printer $((line_block_1++)) $cols_block_1 "└ $years$days$hours:$minutes hs"

    kernel_version=`uname -r`
    printer $((line_block_1++)) $cols_block_1 "Kernel"
    printer $((line_block_1++)) $cols_block_1 "└ $kernel_version"

    unset line_block_1 cols_block_1 title
    unset OS kernel_version
    unset db_user db_password db_address
    unset query_version connect_data opmon_version
    unset tmp uptime_info years
    unset days hours minutes
    unset selinux_status

}

function block_2 () {

    line_block_2=$1
    cols_block_2=$2

    if [[ $draw_box == "1" ]]; then
        box $line $cols
    fi

    cols_block_2=$((++cols_block_2))

    title="System Load & CPU"
    printer $((line_block_2++)) $cols_block_2 "$BOLD$title$FR"

    cpu_usage=""
    if [[ -z "$previous_total" ]]; then
        read cpu us ni sy previous_idle rest < /proc/stat
        previous_total=$((us+ni+sy+previous_idle))
        cpu_usage="00"
        unset cpu us ni sy rest
    else
        read cpu us ni sy idle rest < /proc/stat
        total=$((us+ni+sy+idle))
        cpu_usage=$((100*( (total-previous_total) - (idle-previous_idle) ) / (total-previous_total) ))
        previous_idle=$idle
        previous_total=$total
        unset cpu us ni sy idle rest total
    fi
    printer $((line_block_2++)) $cols_block_2 "CPU Usage:   $BOLD$cpu_usage%$FR  "

    load_1_min=`cat /proc/loadavg |awk '{printf $1}'`
    load_5_min=`cat /proc/loadavg |awk '{printf $2}'`
    load_15_min=`cat /proc/loadavg |awk '{printf $3}'`

    printer $((line_block_2++)) $cols_block_2 "Load 1 Min:  $BOLD$load_1_min$FR  "
    printer $((line_block_2++)) $cols_block_2 "Load 5 Min:  $BOLD$load_5_min$FR  "
    printer $((line_block_2++)) $cols_block_2 "Load 15 Min: $BOLD$load_15_min$FR  "

    number_of_cores=`grep "processor" /proc/cpuinfo | wc -l`
    processor_freq=`grep "cpu MHz" /proc/cpuinfo | head -n 1 | awk '{gsub(/\.[0-9]+/,"");print $4}'`

    printer $((line_block_2++)) $cols_block_2 "Cpu Cores: $BOLD$number_of_cores$FR"
    printer $((line_block_2++)) $cols_block_2 "Freq. MHz: $BOLD$processor_freq$FR"

    unset line_block_2 cols_block_2 title
    unset cpu_usage
    unset load_1_min load_5_min load_15_min
    unset number_of_cores processor_freq

}

function block_3 () {

    line_block_3=$1
    cols_block_3=$2

    if [[ $draw_box == "1" ]]; then
        box $line $cols
    fi

    cols_block_3=$((++cols_block_3))

    title="System Memory"
    printer $((line_block_3++)) $cols_block_3 "$BOLD$title$FR"

    #  MemTotal:        5967364 kB
    #  MemFree:          281024 kB
    #  Buffers:          684500 kB
    #  Cached:          2659364 kB
    #  SwapTotal:       7812092 kB
    #  SwapFree:        7812092 kB

    mem_total=`grep ^MemTotal /proc/meminfo |awk '{print $2}'`
    mem_free=`grep ^MemFree /proc/meminfo |awk '{print $2}'`
    buffers=`grep ^Buffers /proc/meminfo |awk '{print $2}'`
    cached=`grep ^Cached /proc/meminfo |awk '{print $2}'`
    slab=`grep ^Slab /proc/meminfo |awk '{print $2}'`
    swap_total=`grep ^SwapTotal /proc/meminfo |awk '{print $2}'`
    swap_free=`grep ^SwapFree /proc/meminfo |awk '{print $2}'`

    cached=$(($cached + $slab))
    buffer_cached=$(($cached + $buffers))

    tmp_mem=$(($mem_total - $mem_free))
    tmp_mem=$(($tmp_mem - $buffers))
    mem_used=$(($tmp_mem - $cached))

    mem_total=$(mem_converter $mem_total)
    mem_used=$(mem_converter $mem_used)
    mem_free=$(mem_converter $mem_free)
    buffer_cached=$(mem_converter $buffer_cached)
    swap_free=$(mem_converter $swap_free)
    swap_total=$(mem_converter $swap_total)

    printer $((line_block_3++)) $cols_block_3 "RAM:  $mem_total   "
    printer $((line_block_3++)) $cols_block_3 "Use:  $mem_used    "
    printer $((line_block_3++)) $cols_block_3 "Free: $mem_free    "
    printer $((line_block_3++)) $cols_block_3 "Buff/Cached: $buffer_cached    "
    printer $((line_block_3++)) $cols_block_3 "Swap: $swap_total      "
    printer $((line_block_3++)) $cols_block_3 "Swap free: $swap_free      "

    unset line_block_3 cols_block_3 title
    unset mem_total mem_free tmp_mem
    unset mem_used  buffer_cached swap_free
    unset swap_total cached slab

}

function block_4 () {

    line_block_4=$1
    cols_block_4=$2

    if [[ $draw_box == "1" ]]; then
        box $line $cols
    fi

    cols_block_4=$((++cols_block_4))

    title="Disk Utilization"
    printer $((line_block_4++)) $cols_block_4 "$BOLD$title$FR"

    root_size=`/bin/df -khP |grep '\/$' |awk '{printf $2}'`
    root_used=`/bin/df -khP |grep '\/$' |awk '{printf $5}'`
    root_avail=`/bin/df -khP |grep '\/$' |awk '{printf $4}'`

    var_size=`/bin/df -khP |grep '\/var$' |awk '{printf $2}'`
    var_used=`/bin/df -khP |grep '\/var$' |awk '{printf $5}'`
    var_avail=`/bin/df -khP |grep '\/var$' |awk '{printf $4}'`

    mysql_size=`du -sh /var/lib/mysql |awk '{printf $1}'`

    if [[ -e /var/tmp/opmondb ]]; then
        opmondb_size=`du -sh /var/tmp/opmondb |awk '{printf $1}'`
    else
        opmondb_size="NA"
    fi

    printer $((line_block_4++)) $cols_block_4 "/ ┬─── Used: $BOLD$root_used$FR  "
    printer $((line_block_4++)) $cols_block_4 "  └ Size: $root_size Free: $root_avail "
    printer $((line_block_4++)) $cols_block_4 "/var ─ Used: $BOLD$var_used$FR  "
    printer $((line_block_4++)) $cols_block_4 "  └ Size: $var_size Free: $var_avail  "
    printer $((line_block_4++)) $cols_block_4 "/var/lib/mysql   $BOLD$mysql_size$FR  "
    printer $((line_block_4++)) $cols_block_4 "/var/tmp/opmondb $BOLD$opmondb_size$FR  "

    unset line_block_4 cols_block_4 title
    unset root_used root_size root_avail
    unset var_size var_used var_avail

}

function block_5 () {

    line_block_5=$1
    cols_block_5=$2

    if [[ $draw_box == "1" ]]; then
        box $line $cols
    fi

    cols_block_5=$((++cols_block_5))

    title="Main Services"
    printer $((line_block_5++)) $cols_block_5 "$BOLD$title$FR"

    # Main OpMon's Process
    # gearmand gearman-utils memcached mod_gearman_worker opdiscovery opmon

    opmon_status=`ps -ef |grep "opmon -d" |grep -v grep | wc -l`
    opmon_result=`[ $opmon_status -eq 0 ] && echo $OFF || echo $ON`
    printer $((line_block_5++)) $cols_block_5 "OpMon ............ $opmon_result"

    memcached_status=`ps -ef |grep "memcached -d" |grep -v grep |wc -l`
    memcached_result=`[ $memcached_status -eq 0 ] && echo $OFF || echo $ON`
    printer $((line_block_5++)) $cols_block_5 "Memcached ........ $memcached_result"

    gearmand_status=`ps -ef |grep "/usr/sbin/gearmand -d" |grep -v grep |wc -l`
    if [[ $gearmand_status -eq 0 ]];then
        gearmand_result=$OFF
        gearmand_running="0"
    else
        gearmand_result=$ON
        gearmand_running="1"
    fi
    printer $((line_block_5++)) $cols_block_5 "Gearmand ......... $gearmand_result"

    gearman_utils_status=`ps -ef |grep /usr/local/opmon/bin/gcontrol.php |grep -v grep |wc -l`
    gearman_utils_result=`[ $gearman_utils_status -eq 0 ] && echo $OFF || echo $ON`
    printer $((line_block_5++)) $cols_block_5 "Gearman-Utils .... $gearman_utils_result"

    gearman_worker_status=`ps -ef |grep /usr/bin/mod_gearman_worker |grep -v grep |wc -l`
    gearman_worker_result=`[ $gearman_worker_status -eq 0 ] && echo $OFF || echo $ON`
    printer $((line_block_5++)) $cols_block_5 "Gearman-Worker ... $gearman_worker_result"

    mysql_status=$(test_database_connection)
    mysql_result=`[ "$mysql_status" == 0 ] && echo $OFF || echo $ON`
    printer $((line_block_5++)) $cols_block_5 "Mysql ............ $mysql_result"

    unset line_block_5 cols_block_5 title
    unset opmon_status opmon_result
    unset memcached_status memcached_result
    unset gearmand_status gearmand_result
    unset gearman_utils_status gearman_utils_result
    unset gearman_worker_status gearman_worker_result
    unset mysql_status mysql_result
}

function block_6 () {

    line_block_6=$1
    cols_block_6=$2

    if [[ $draw_box == "1" ]]; then
        box $line $cols
    fi

    cols_block_6=$((++cols_block_6))

    title="Extra Services"
    printer $((line_block_6++)) $cols_block_6 "$BOLD$title$FR"

    apache_status=`ps -ef|grep httpd| grep -v grep| wc -l`
    apache_result=`[ $apache_status -eq 0 ] && echo $OFF || echo $ON`
    printer $((line_block_6++)) $cols_block_6 "Apache ........... $apache_result"

    postfix_status=`ps -ef| grep /usr/libexec/postfix/master| grep -v grep|wc -l`
    postfix_result=`[ $postfix_status -eq 0 ] && echo $OFF || echo $ON`
    printer $((line_block_6++)) $cols_block_6 "Postfix .......... $postfix_result"

    opdiscovery_status=`ps -ef |grep "opdiscovery.php -d" |grep -v grep|wc -l`
    opdiscovery_result=`[ $opdiscovery_status -eq 0 ] && echo $OFF || echo $ON`
    printer $((line_block_6++)) $cols_block_6 "OpDiscovery ...... $opdiscovery_result"

    syncfiles_status=`ps -ef|grep /usr/local/opmon/utils/syncfiles.sh| grep -v grep| wc -l`
    syncfiles_result=`[ $syncfiles_status -eq 0 ] && echo $OFF || echo $ON`
    printer $((line_block_6++)) $cols_block_6 "Syncfiles ........ $syncfiles_result"

    pacemaker_status=`ps -ef| grep pacemakerd| grep -v grep| wc -l`
    pacemaker_result=`[ $pacemaker_status -eq 0 ] && echo $OFF || echo $ON`
    printer $((line_block_6++)) $cols_block_6 "Pacemaker ........ $pacemaker_result"

    corosync_status=`ps -ef| grep corosync| grep -v grep| wc -l`
    corosync_result=`[ $corosync_status -eq 0 ] && echo $OFF || echo $ON`
    printer $((line_block_6++)) $cols_block_6 "Corosync ......... $pacemaker_result"

    unset line_block_6 cols_block_6 title
    unset postfix_status postfix_result
    unset opdiscovery_status opdiscovery_result
    unset syncfiles_status syncfiles_result
    unset pacemaker_status pacemaker_result
    unset corosync_status corosync_result

}

function block_7 () {

    line_block_7=$1
    cols_block_7=$2

    if [[ $draw_box == "1" ]]; then
        box $line $cols
    fi

    cols_block_7=$((++cols_block_7))

    title="BD & Workers"
    printer $((line_block_7++)) $cols_block_7 "$BOLD$title$FR"

    # Database engine and seconds behind master
    if [[ $(test_database_connection) == 1 ]]; then

        default_engine_query="select engine from information_schema.ENGINES where Support=\"DEFAULT\""
        default_engine=`mysql -N $db_connection -e "$default_engine_query"`

        label_3="Engine:"
        printer $((line_block_7++)) $cols_block_7 "$BOLD$label_3$FR $default_engine"

        sec_behind_master_query="show slave status\G"
        seconds=`mysql $db_connection -e "$sec_behind_master_query"| grep Seconds_Behind_Master| awk '{print $2}'`
        slave_lag=""

        if [[ "$seconds" ]]; then
            if [[ "$seconds" = "NULL" ]]; then
                slave_lag=$(printf "└ Slave lag: %sNULL%s" $RED $CR)
            else
                slave_lag=$(printf "└ Slave lag: $seconds   ")
            fi
            printer $((line_block_7++)) $cols_block_7 "$slave_lag"
        else
            printer $((line_block_7++)) $cols_block_7 "└ Slave lag: No sync.   "
        fi

        unset default_engine_query default_engine label_3
        unset seconds sec_behind_master_query slave_lag

    else

        printer $((line_block_7++)) $cols_block_7 "$mysql_stopped"
        printer $((line_block_7++)) $cols_block_7 ""

    fi

    printer $((line_block_7++)) $cols_block_7 ""

    # Get worker information
    # Queue Name | Worker Available | Jobs Waiting | Jobs Running
    if [[ $gearmand_running -eq "0" ]]; then
        printer $((line_block_7++)) $cols_block_7 "Workers    "
        printer $((line_block_7++)) $cols_block_7 "└ $no_info            "
        printer $((line_block_7++)) $cols_block_7 "                      "
        printer $((line_block_7++)) $cols_block_7 "                      "
    else
        worker_host=`gearman_top -b |grep "^ host" | awk '{print "AV:" $3 " WA:" $5 " RU:" $7}'`
        worker_service=`gearman_top -b |grep "^ service" | awk '{print "AV:" $3 " WA:" $5 " RU:" $7}'`
        label_1="Worker Host"
        printer $((line_block_7++)) $cols_block_7 "$BOLD$label_1$FR"
        printer $((line_block_7++)) $cols_block_7 "└ $worker_host    "
        label_2="Worker Service"
        printer $((line_block_7++)) $cols_block_7 "$BOLD$label_2$FR"
        printer $((line_block_7++)) $cols_block_7 "└ $worker_service   "
    fi

    unset line_block_7 cols_block_7
    unset title label_1 label_2
    unset worker_host worker_service

}

function block_8 () {

    line_block_8=$1
    cols_block_8=$2

    if [[ $draw_box == "1" ]]; then
        box $line $cols
    fi

    cols_block_8=$((++cols_block_8))

    title="Networking"
    printer $((line_block_8++)) $cols_block_8 "$BOLD$title$FR"

    # Get the configured network interfaces
    interfaces=(`/bin/ls -1 /etc/sysconfig/network-scripts/ |grep ifcfg |grep -v lo| cut -d'-' -f2`)

    network_tmp="0"

    for int in "${interfaces[@]}"; do

        tmp=`ip -4 address| grep "$int$"| wc -l`

        if [[ $tmp -eq 0 ]]; then

            continue

        else

            mac_address=`ip link show $int| grep "link/ether"| awk -F ' ' '{print toupper($2)}'`
            ip_address=`ip -4 address show $int| grep inet| grep "$int$"| awk -F ' ' '{print $2}'`
            int=`echo "$int" | awk '{print toupper($1)}'`

            network_tmp=$((++network_tmp))

            printer $((line_block_8++)) $cols_block_8 "┌ $int: $mac_address"

            for ip in ${ip_address[@]}; do

              network_tmp=$((++network_tmp))

              printer $((line_block_8++)) $cols_block_8 "└─ $ip"

            done

            unset ip int mac_address ip_address

        fi

        unset tmp

    done

    if [[ $network_count -eq 0 ]]; then

        network_count=$network_tmp

    fi

    if [[ $network_count -ne $network_tmp ]]; then

        network_count=$network_tmp

        terminal_cleaner

        force_box_redraw

    fi

    unset interfaces network_tmp
    unset line_block_8 cols_block_8 title actual_line

}

# Hold your horses!
test_user

# What should I do?
get_options $@

# Lets Begin...
main
