#!/bin/bash
# info: list system rrd charts
# options: [format]
#
# List available rrd graphics, its titles and paths.


#----------------------------------------------------------#
#                    Variable&Function                     #
#----------------------------------------------------------#

format=${1-shell}

# Includes
source $VESTA/func/main.sh
source $VESTA/conf/vesta.conf

# Define json function
json_list_rrd() {
    i=1
    echo "{"
    for type in $rrd_types; do
        for rrd in $(ls $RRD/$type |grep rrd$ |sed "s/\.rrd$//g"); do
            if [ "$i" -ne 1 ]; then
                echo -e "\t},"
            fi
            if [ "$type" = 'la' ]; then
                title="Load Average"
            fi
            if [ "$type" = 'mem' ]; then
                title="Memory Usage"
            fi
            if [ "$type" = 'net' ]; then
                title="Bandwidth Usage $rrd"
            fi
            if [ "$type" = 'web' ] || [ "$type" = 'ftp' ] ||\
               [ "$type" = 'ssh' ]; then
                title="$(echo $rrd| tr '[:lower:]' '[:upper:]') Usage"
            fi
            if [ "$type" = 'db' ]; then
                db_type=$(echo $rrd|cut -f 1 -d _ |sed -e 's/mysql/MySQL/g' \
                    -e 's/pgsql/PostgreSQL/g' )
                db_host=$(echo $rrd|cut -f 2 -d _ )
                title="$db_type Usage on $db_host"
            fi
            echo -e "\t\"$i\": {"
            echo -e "\t\t\"TYPE\": \"$type\"",
            echo -e "\t\t\"RRD\": \"$rrd\"",
            echo -e "\t\t\"TITLE\": \"$title\","
            echo -e "\t\t\"TIME\": \"$TIME\","
            echo -e "\t\t\"DATE\": \"$DATE\""
            (( ++i))
        done
    done
    if [ "$i" -gt 1 ]; then
        echo -e "\t}"
    fi
    echo "}"
}

# Define jshell function
shell_list_rrd() {
    if [  -z "$nohead" ]; then
        echo "PATH"
        echo "---------"
    fi
    for type in $rrd_types; do
        for rrd in $(ls $RRD/$type |grep rrd$ |sed "s/\.rrd$//g"); do
            echo "$RRD/$type/$rrd.rrd"
        done
    done
}



#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

# Checking enabled systems
rrd_types="la mem net"

if [ -n "$WEB_SYSTEM" ] && [ "$WEB_SYSTEM" != 'no' ]; then
    rrd_types="$rrd_types web"
fi

if [ -n "$DB_SYSTEM" ] && [ "$DB_SYSTEM" != 'no' ]; then
    rrd_types="$rrd_types db"
fi

if [ -n "$MAIL_SYSTEM" ] && [ "$MAIL_SYSTEM" != 'no' ]; then
    rrd_types="$rrd_types mail"
fi

if [ -n "$FTP_SYSTEM" ] && [ "$FTP_SYSTEM" != 'no' ]; then
    rrd_types="$rrd_types ftp"
fi

rrd_types="$rrd_types ssh"


# Listing domains
case $format in
    json)   json_list_rrd ;;
    plain)  nohead=1; shell_list_rrd ;;
    shell)  shell_list_rrd | column -t ;;
esac


#----------------------------------------------------------#
#                       Vesta                              #
#----------------------------------------------------------#

exit
