简体   繁体   中英

Bash Scripting: Output multiple lines to one line

We currently have a server monitor script monitoring 63 servers, and we want to add another 40 servers to this script. The problem being is that we would like to keep the entire contents of the script on a single monitor (turned 90degrees).

I am wondering if at all it is possible to output 2 servers on one line, and if it is possible how its done. for example

We currently have

web1                        | 2.11 | 2.05 | 1.95 | (116) HTTP Processes
web2                        | 0.06 | 0.12 | 0.15 | (113) HTTP Processes

data1                       | 1.04 | 0.93 | 0.90 |
data2                       | 0.36 | 0.52 | 0.43 |
data3                       | 0.41 | 0.31 | 0.28 |
data4                       | 1.48 | 1.41 | 1.28 |
data5                       | 1.10 | 1.07 | 1.10 |
data6                       | 5.60 | 4.78 | 4.08 |
data7                       | 0.30 | 0.31 | 0.29 |
data8                       | 1.44 | 2.18 | 2.00 |
data9                       | 0.11 | 0.22 | 0.26 |
data10                      | 0.19 | 0.25 | 0.31 |

sql1                        | 0.42 | 0.58 | 0.61 |
sql2                        | 0.26 | 0.24 | 0.27 |
sql3                        | 0.06 | 0.08 | 0.09 |

What we would like to do is:

web1           | 2.11 | 2.05 | 1.95 | (116) HTTP Processes
web2           | 0.06 | 0.12 | 0.15 | (113) HTTP Processes

data1          | 1.04 | 0.93 | 0.90 | - data6          | 5.60 | 4.78 | 4.08 |
data2          | 0.36 | 0.52 | 0.43 | - data7          | 0.30 | 0.31 | 0.29 |
data3          | 0.41 | 0.31 | 0.28 | - data8          | 1.44 | 2.18 | 2.00 |
data4          | 1.48 | 1.41 | 1.28 | - data9          | 0.11 | 0.22 | 0.26 |
data5          | 1.10 | 1.07 | 1.10 | - data10         | 0.19 | 0.25 | 0.31 |

sql1           | 0.42 | 0.58 | 0.61 |
sql2           | 0.26 | 0.24 | 0.27 |
sql3           | 0.06 | 0.08 | 0.09 |

etc etc

As you can see we want to group certain server types together (web, cassandra, sql, grid).

The script monitors average loads, so need to fit that in too (plenty of space on the monitor to display this)

Possible or am i asking the impossible?

The current script:

cleanquit () {
echo "$(tput sgr0)"
clear
exit $?
 }

 trap cleanquit SIGINT

 clear

 while [ 1 ]
 do
 tput cup 0 0

 echo "$(tput sgr0)"

 for i in web1 web2 data1 data2 data3 data4
if [ $i == "space" ]; then
    echo "$(tput setaf 7)"
    UPS=""
else

if [ $i == "self" ]; then
        UPTIME=$(cat /proc/loadavg);
    else
        UPTIME=$(ssh root@$i cat /proc/loadavg);
    fi

    if [ -z "$UPTIME" ]; then
        tput cuu1
        tput el
        printf " $(tput setaf 1)%-25s\t | CONNECTION FAILED  |\n" $i;
    else
        thisloadavg1=$(echo $UPTIME|awk '{ print $1}' | bc -q 2>/dev/null)
        thisloadavg2=$(echo $UPTIME|awk '{ print $2}' | bc -q 2>/dev/null)
        thisloadavg3=$(echo $UPTIME|awk '{ print $3}' | bc -q 2>/dev/null)
        additional=""

        if [ ${i:0:3} == "web" -o ${i:0:4} == "grid" ]; then
            additional=$(ssh root@$i ps aux | grep "sbin/http" | wc -l)
            if [ $additional -gt 0 ]; then
                additional="("$additional") HTTP Processes"
            else
                additional=""
            fi
        fi

        if [ $i == "self" ]; then

            additional=$(ps aux | grep "sbin/http" | wc -l)
            if [ $additional -gt 0 ]; then
                additional="("$additional") HTTP Processes"
            else
                additional=""
            fi

        fi

        if [ $(echo "$thisloadavg1 > 5.0" | bc) -eq 1 ]; then
            printf " $(tput setaf 1)%-25s\t $(tput setaf 7)|$(tput setaf 1) %0.2f $(tput setaf 7)|$(tput setaf 1) %0.2f $(tput setaf 7)|$(tput setaf 1) %0.2f $(tput setaf 7)| %s %s %s\n" $i $thisloadavg1 $thisloadavg2 $thisloadavg3 $additional;
        else
            if [ $(echo "$thisloadavg1 > 3.0" | bc) -eq 1 ]; then
                printf " $(tput setaf 3)%-25s\t $(tput setaf 7)|$(tput setaf 3) %0.2f $(tput setaf 7)|$(tput setaf 3) %0.2f $(tput setaf 7)|$(tput setaf 3) %0.2f $(tput setaf 7)| %s %s %s \n" $i $thisloadavg1 $thisloadavg2 $thisloadavg3 $additional;
            else
                if [ $(echo "$thisloadavg1 > 1.5" | bc) -eq 1 ]; then
                    printf " $(tput setaf 6)%-25s\t $(tput setaf 7)|$(tput setaf 6) %0.2f $(tput setaf 7)|$(tput setaf 6) %0.2f $(tput setaf 7)|$(tput setaf 6) %0.2f $(tput setaf 7)| %s %s %s \n" $i $thisloadavg1 $thisloadavg2 $thisloadavg3 $additional;
                else
                    printf " $(tput setaf 7)%-25s\t | %0.2f | %0.2f | %0.2f | %s %s %s\n" $i $thisloadavg1 $thisloadavg2 $thisloadavg3 $additional;
                fi
            fi
        fi
    fi

fi
tput el
done

echo

tput sgr0
tput ed
sleep 2;
done`

您可以使用column标准过滤器来列出任意输入。

The pr filter can put data in columns. It was designed to lay out line printer pages with headers and footers, but at least GNU pr lets you turn off those things. Try pr -bt3 <input.txt for three-column output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM