SSH登录消息提示的定制

针对MySQL服务器,每次通过SSH登录的时候如果可以输出一些有用的信息,无疑可以让我们更方便快捷的了解当前系统的运行状态。

1、实验环境:

  • CentOS 6.8 x86_64
  • Percona 5.6.35

2、相关代码

2.1、配置文件

## file: /etc/leidy.cfg
CONFIGPORTS="3306 3307 3308"    ## all ports for mysql instances
WARNUSAGE=70                    ## disk partition warn usage
  • CONFIGPORTS: 该服务器所有mysql实例所对应的端口号列表;
  • WARNUSAGE: 磁盘分区使用量的告警阀值,如果超出红色显示,否则显示为绿色。

这里之所以将这两个配置项独立出来,是因为在自动化运维的过程中要保证所有的程序都是通用的,而配置文件是可以有差异化的;这样我们就只需要维护一个版本的程序就能兼容所有的环境。

2.2、脚本代码

#!/bin/bash
#
# Filename: print_mysql_info.sh
#
# Usage:
#   First:
#     Define the variable CONFIGPORTS and WARNUSAGE in the configuration file:/etc/leidy.cfg,
#       CONFIGPORTS:  all ports for mysql instances
#       WARNUSAGE:    disk partition warn usage
#   Second:
#     - Copy the script to the directory:/etc/profile.d/ ;
#   OR:
#     - Add the script to the timed task and update the file /etc/motd regularly.
#
# Description:
# This script is used to display information about mysql instances and the usage of each disk partition
# when user login host via SSH.
#
# History:
#   2017/05/15  V1.0  first release.
# Copyright by Leidongya at shulidata inc.
# Contact email: contact@leidy.cn

[[ -s "/etc/leidy.cfg" ]] && source /etc/leidy.cfg

CURRENTPIDS=$(pidof mysqld)
EXISTPORTS=
MYSQLVERSION=$(mysqladmin -V | awk -F '[ -]+' '{print $5}')
CUSTOMINFO="MySQL is the most popular Open Source SQL database management system"         ## Custom information

### Color Setting ###
NORMAL="\033[0m"
RED='\033[00;31m'
GREEN='\033[00;32m'

## Print the string centered. ##
function center() {
  local message=$1
  local tmp_var=$(echo -e $message | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g")  ## delete color
  local str_len=${#tmp_var}
  echo -e "${message}" | /usr/bin/awk \
    '{
      z = 80 - '"$str_len"'
      y = int(z / 2)
      x = z - y
      printf "* %*s%s%*s *\n", x, "", $0, y, ""
    }'
}


echo "************************************************************************************"
center "Welcome to ${RED}${HOSTNAME}${NORMAL}"
center "BY ${LOGNAME}"
center "${CUSTOMINFO}"
echo "********************************** MySQL Information *******************************"

for pid in $CURRENTPIDS
do
  port=$(ss -l -p -n | awk -F '[ :]+' '/"mysqld",'"$pid"',/{print $5}')   ## get listening port by pid
  [[ -z "$port" ]] || EXISTPORTS="${EXISTPORTS} ${port}"
  if kill -0 $pid > /dev/null 2>&1 ; then     ## ensure mysql is running or not
    COLOR=$GREEN
    STATUS="RUNNING"
  else
    COLOR=$RED
    STATUS="SHUTDOWN"
  fi
  center "PID : ${COLOR}${pid}${NORMAL}"
  center "Port Number: ${COLOR}${port:-NULL}${NORMAL}"
  center "Running Status: ${COLOR}${STATUS}${NORMAL}"
  center "MySQL Version: $MYSQLVERSION"
  echo "*     ************************************************************************     *"
done

## Check each port of the configuration ##
for port in $CONFIGPORTS
do
  echo $EXISTPORTS | grep -sqw "$port"    ## Skip the port that has been checked
  if [[ $? -ne 0 ]]; then
    STATUS="SHUTDOWN"
    center "Port Number: ${RED}${port:-NULL}${NORMAL}"
    center "Running Status: ${RED}${STATUS}${NORMAL}"
    center "MySQL Version: $MYSQLVERSION"
    echo "*     ************************************************************************     *"
  fi
done

echo "********************************* Storage Information ******************************"
df -ThP | awk \
  '{
    if (($6+0)>'"$WARNUSAGE"')
      print $1,$2,$3,$4,$5,"'"$RED"'"$6"'"$NORMAL"'",$7;
    else
      print $1,$2,$3,$4,$5,"'"$GREEN"'"$6"'"$NORMAL"'",$7;
    }' | while read -r line
do
  center "$line"
done
echo "************************************************************************************"
echo

3、配置方法(两种方法可以任选一个)

3.1、定时更新/etc/motd

$ crontab -uroot -l
* * * * * (/bin/sh /path/to/print_mysql_info.sh > /etc/motd 2>&1)

3.2、bash登录时自动调用:

$ cp print_mysql_info.sh /etc/profile.d/

4、效果图

暂无评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注

Time limit is exhausted. Please reload CAPTCHA.