redis-001安装

阅读量: zyh 2017-12-21 09:34:44
Categories: > Tags:

docker 安装

Docker Hub

redisName=redis
versionTag=6
dataPath=/export/docker-data-redis
mkdir -p ${dataPath}
# redis.conf 请参考官方默认配置文档,默认配置文档地址: https://redis.io/topics/config
wget -P ${dataPath} https://raw.githubusercontent.com/redis/redis/${versionTag}.0/redis.conf
docker run --name ${redisName}_${versionTag} \
--mount "type=bind,src=${dataPath},dst=/data" \
-p 6379:6379 \
--restart always \
-d redis:${versionTag} redis-server /data/redis.conf

ℹ️配置改动:bind 0.0.0.0

编译安装

💥redis6需要gcc版本9。通过下列命令进入特定版本的环境来编译安装

# 安装包
yum install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils -y
# 进入特定shell环境
scl enable devtoolset-9 bash
vim /usr/local/src/redis.sh
#!/bin/bash
# by zyh
# DownUrl: redis源码包
# RedisMaxMem:redis内存限制
# RedisPort: redis端口
# RedisSentinelPort: redis哨兵端口
# AUTHPWD: redis认证密码和哨兵认证密码
# RedisBaseDir: redis安装路径
# 安装完毕后,会输出
# 1. redis信息
# 2. systemd redis 配置
# 3. systemd redis-sentinel 配置
# redis.conf 中从配置已注释,若变更为从,开启注释
# redis-sentinel.conf 中默认监控的是 localhost,需改成主redis的ip


BaseDir=`cd "$(dirname "$0")"; pwd`
DownUrl=http://download.redis.io/releases/redis-stable.tar.gz
RedisMaxMem=1g
RedisPort=6379
RedisSentinelPort=26379
AUTHPWD=123456

RedisBaseDir=/export/redis_${RedisPort}
RedisBaseName=${RedisBaseDir##*/}

export TOP_PID=$$
trap 'exit 1' TERM

exit_script(){
    kill -s TERM $TOP_PID
}

yum install gcc-c++ make -y

[[ -d ${RedisBaseDir} ]] && echo "${RedisBaseDir}已存在" && exit_script || mkdir -p ${RedisBaseDir}
ss -tnalp | grep redis | awk '{print $4}' | awk -F':' '{print $2}' | while read line;do
    [[ $line -eq ${RedisPort} ]] && echo "${RedisPort}已被占用" && exit_script
done

cd ${BaseDir} && mkdir redis
wget ${DownUrl} -O redis.tar.gz
tar xf redis.tar.gz --strip-components 1 -C redis
cd redis
make PREFIX=${RedisBaseDir} install || exit_script
mkdir ${RedisBaseDir}/{etc,data,logs}

cat>${RedisBaseDir}/etc/redis.conf <<EOF
# Base
bind 0.0.0.0
protected-mode yes
port ${RedisPort}
daemonize yes
pidfile "${RedisBaseDir}/redis.pid"
loglevel warning
logfile "${RedisBaseDir}/logs/redis.log"
dir "${RedisBaseDir}/data/"
# Auth
requirepass ${AUTHPWD}
# Replica master
## 必须有一个从服务,才允许写入
min-replicas-to-write 1
## 从服务有10秒时间来发送异步确认,若超过则认为从不可用,进而因为 min-replicas-to-write 1 导致不可写入
min-replicas-max-lag 10
# Replica slave
## master 认证
#masterauth <master-password>
## 指定 master 地址
#replicaof <masterip> <masterport>
replica-read-only yes
replica-priority 100
# Memory
maxmemory ${RedisMaxMem}
maxmemory-policy allkeys-lru
# RDB
save 900 1
save 300 10
save 60 10000
dbfilename redis_${RedisPort}.rdb
# AOF
appendonly yes
appendfilename appendonly_${RedisPort}.aof
appendfsync everysec
no-appendfsync-on-rewrite no
aof-use-rdb-preamble yes
# rewrite command
rename-command FLUSHDB GOD_FLUSHDB
rename-command FLUSHALL GOD_FLUSHALL
#rename-command CONFIG GOD_CONFIG
rename-command KEYS GOD_KEYS
EOF
cat>${RedisBaseDir}/etc/redis-sentinel.conf <<EOF
protected-mode no
port ${RedisSentinelPort}
daemonize yes
pidfile "${RedisBaseDir}/redis-sentinel.pid"
logfile "${RedisBaseDir}/logs/redis-sentinel.log"
dir "${RedisBaseDir}/data"

# 配置监听的主服务器
## mymaster 自定义一个主服务器的昵称
## 代表监控的主服务器
## 6379代表端口
## 2代表只有两个或两个以上的哨兵认为主服务器不可用的时候,才会进行failover操作。
sentinel monitor mymaster localhost ${RedisPort} 2

# 访问master所需的认证账户密码
## mymaster 自定义的主服务器昵称
## 123456 master 配置的密码
sentinel auth-pass mymaster ${AUTHPWD}

# 客户端访问哨兵所需的auth认证
requirepass ${AUTHPWD}

sentinel down-after-milliseconds mymaster 60000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1
EOF

cat>${RedisBaseDir}/redis.sh << EOF
#!/bin/bash
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
BASEDIR=${RedisBaseDir}
REDISPORT=${RedisPort}
EXEC=\$BASEDIR/bin/redis-server
CLIEXEC=\$BASEDIR/bin/redis-cli
PIDFILE=\$BASEDIR/redis.pid
CONF="\$BASEDIR/etc/redis.conf"
AUTH="$AUTHPWD"
case "\$1" in
    start)
        [[ -f \$PIDFILE ]] && kill -0 \`cat \$PIDFILE\` 2>>\$BASEDIR/crash.log && echo "\$PIDFILE exists, process is already running or crashed" || {
                echo "Starting Redis server..."
                \$EXEC \$CONF
        }
        ;;
    stop)
        if [ ! -f \$PIDFILE ]
        then
                echo "\$PIDFILE does not exist, process is not running"
        else
                PID=\$(cat \$PIDFILE)
                echo "Stopping ..."
                [[ -z \$AUTH ]] && \$CLIEXEC -p \$REDISPORT shutdown || \$CLIEXEC -p \$REDISPORT -a \$AUTH shutdown
                while [ -x /proc/\${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac
EOF
cat>${RedisBaseDir}/redis-sentinel.sh << EOF
#!/bin/bash
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
BASEDIR=${RedisBaseDir}
REDISPORT=${RedisSentinelPort}
EXEC=\$BASEDIR/bin/redis-sentinel
CLIEXEC=\$BASEDIR/bin/redis-cli
PIDFILE=\$BASEDIR/redis-sentinel.pid
CONF="\$BASEDIR/etc/redis-sentinel.conf"
AUTH="$AUTHPWD"
case "\$1" in
    start)
        [[ -f \$PIDFILE ]] && kill -0 \`cat \$PIDFILE\` 2>>\$BASEDIR/crash-sentinel.log && echo "\$PIDFILE exists, process is already running or crashed" || {
                echo "Starting Redis Sentinel server..."
                \$EXEC \$CONF
        }
        ;;
    stop)
        if [ ! -f \$PIDFILE ]
        then
                echo "\$PIDFILE does not exist, process is not running"
        else
                PID=\$(cat \$PIDFILE)
                echo "Stopping ..."
                [[ -z \$AUTH ]] && \$CLIEXEC -p \$REDISPORT shutdown || \$CLIEXEC -p \$REDISPORT -a \$AUTH shutdown
                while [ -x /proc/\${PID} ]
                do
                    echo "Waiting for Redis Sentinel to shutdown ..."
                    sleep 1
                done
                echo "Redis Sentinel stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac
EOF

chmod u+x ${RedisBaseDir}/*.sh


#修改内核参数
grep -q net.core.somaxconn /etc/sysctl.conf || echo "net.core.somaxconn = 511" >> /etc/sysctl.conf
grep -q vm.overcommit_memory /etc/sysctl.conf || {
    echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf && sysctl -p
}
grep -q '/sys/kernel/mm/transparent_hugepage/enabled' /etc/rc.local || {
    echo never > /sys/kernel/mm/transparent_hugepage/enabled
    echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local
}

echo '-------------------------------------------------- redis base info-----------------------------------------------------------'
echo 'redis相关信息如下:'
echo "
根路径:${RedisBaseDir}
启动脚本:${RedisBaseDir}/redis.sh
启动脚本:${RedisBaseDir}/redis-sentinel.sh
"
echo '-------------------------------------------------- redis systemd info-----------------------------------------------------------'
echo "
cat >/etc/systemd/system/redis.service << EOF
[Unit]
# 描述服务
Description=Redis
# 描述服务类别
After=network.target

# 服务运行参数的设置
[Service]
# 后台运行的形式
Type=forking
# 运行命令,路径必须是绝对路径
ExecStart=${RedisBaseDir}/redis.sh start
# 停止命令,路径必须是绝对路径
ExecStop=${RedisBaseDir}/redis.sh stop
# 表示给服务分配独立的临时空间
PrivateTmp=true

# 运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
[Install]
WantedBy=multi-user.target
EOF
"

echo '-------------------------------------------------- redis-sentinel systemd info-----------------------------------------------------------'
echo "
cat>/etc/systemd/system/redis-sentinel.service << EOF
[Unit]
# 描述服务
Description=RedisSentinel
# 描述服务类别
After=network.target

# 服务运行参数的设置
[Service]
# 后台运行的形式
Type=forking
# 运行命令,路径必须是绝对路径
ExecStart=${RedisBaseDir}/redis-sentinel.sh start
# 停止命令,路径必须是绝对路径
ExecStop=${RedisBaseDir}/redis-sentinel.sh stop
# 表示给服务分配独立的临时空间
PrivateTmp=true

# 运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
[Install]
WantedBy=multi-user.target
EOF
"

以下是废弃的。

#!/bin/bash
# by zyh
# 2018-06-21
# DownUrl: redis源码包
# RedisBaseDir: redis安装路径
# RedisPort: redis端口
# RedisMaxMem:redis内存限制
# ZabbixBase: zabbix 根路径
# 安装完毕后,会输出
# 1. redis信息
# 2. zabbix需要额外手动添加的命令, 并在zabbix_server_web里,给机器关联上 <Template Redis Auto Discovert Active mode> 模板
# 3. monit需要额外手动添加的配置


BaseDir=`cd "$(dirname "$0")"; pwd`
DownUrl=http://download.redis.io/releases/redis-stable.tar.gz
RedisMaxMem=1g
RedisPort=6379
RedisBaseDir=/export/redis_${RedisPort}
ZabbixBase=/etc/zabbix

if [[ ${ZabbixBase} == '/usr/local/zabbix' ]];then
    ZabbixShell=${ZabbixBase}/shell
    ZabbixEtc=${ZabbixBase}/etc/zabbix_agentd.conf.d/redis.conf
else
    ZabbixShell=/etc/zabbix/shell
    ZabbixEtc=/etc/zabbix/zabbix_agentd.d/redis.conf
fi
RedisBaseName=${RedisBaseDir##*/}

export TOP_PID=$$
trap 'exit 1' TERM

exit_script(){
    kill -s TERM $TOP_PID
}

yum install gcc-c++ -y

[[ -d ${RedisBaseDir} ]] && echo "${RedisBaseDir}已存在" && exit_script
ss -tnalp | grep redis | awk '{print $4}' | awk -F':' '{print $2}' | while read line;do
    [[ $line -eq ${RedisPort} ]] && echo "${RedisPort}已被占用" && exit_script
done

cd ${BaseDir} && mkdir redis
wget ${DownUrl} -O redis.tar.gz
tar xf redis.tar.gz --strip-components 1 -C redis
cd redis
make PREFIX=${RedisBaseDir} install
mkdir ${RedisBaseDir}/{etc,data,logs}

cat>${RedisBaseDir}/etc/redis.conf <<EOF
bind 0.0.0.0
protected-mode yes
port ${RedisPort}
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile ${RedisBaseDir}/redis.pid
loglevel warning
logfile "${RedisBaseDir}/logs/redis.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename redis_${RedisPort}.rdb
dir ${RedisBaseDir}/data/
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
rename-command FLUSHDB GOD_FLUSHDB
rename-command FLUSHALL GOD_FLUSHALL
rename-command CONFIG GOD_CONFIG
rename-command KEYS GOD_KEYS
maxmemory ${RedisMaxMem}
maxmemory-policy allkeys-lru
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
EOF

cat>${RedisBaseDir}/redis.sh << EOF
#!/bin/bash
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
BASEDIR=${RedisBaseDir}
REDISPORT=${RedisPort}
EXEC=\$BASEDIR/bin/redis-server
CLIEXEC=\$BASEDIR/bin/redis-cli
PIDFILE=\$BASEDIR/redis.pid
CONF="\$BASEDIR/etc/redis.conf"
case "\$1" in
    start)
        [[ -f \$PIDFILE ]] && kill -0 \`cat \$PIDFILE\` 2>>\$BASEDIR/crash.log && echo "\$PIDFILE exists, process is already running or crashed" || {
                echo "Starting Redis server..."
                \$EXEC \$CONF
        }
        ;;
    stop)
        if [ ! -f \$PIDFILE ]
        then
                echo "\$PIDFILE does not exist, process is not running"
        else
                PID=\$(cat \$PIDFILE)
                echo "Stopping ..."
                \$CLIEXEC -p \$REDISPORT shutdown
                while [ -x /proc/\${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac
EOF
chmod u+x ${RedisBaseDir}/redis.sh

#修改内核参数
grep -q net.core.somaxconn /etc/sysctl.conf || echo "net.core.somaxconn = 511" >> /etc/sysctl.conf
grep -q vm.overcommit_memory /etc/sysctl.conf || {
    echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf && sysctl -p
}
grep -q '/sys/kernel/mm/transparent_hugepage/enabled' /etc/rc.local || {
    echo never > /sys/kernel/mm/transparent_hugepage/enabled
    echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local
}

#修改zabbix监控
cat>${ZabbixEtc}<<EOF
##redis monitor
UserParameter=redis_info[*],${ZabbixShell}/redis_info.sh \$1 \$2 \$3 \$4
UserParameter=ip_port_discovery[*],${ZabbixShell}/ip_port_discovery.sh \$1
EOF
mkdir ${ZabbixShell}
cat>${ZabbixShell}/redis_info.sh<<"EOF"
#!/bin/bash
REDISPATH="/export/redis/bin/redis-cli"
HOST=$1
PORT=$2
REDIS_INFO="$REDISPATH -h $HOST -p $PORT info"
[[ $# -ne 3 ]] && [[ $# -ne 4 ]] && {
 exit
}
if [[ $# -eq 3 ]];then
case $3 in
exist)
 result=`$REDISPATH -h $HOST -p $PORT ping 2>/dev/null |grep -c PONG`
 echo $result
;;
cluster)
        result=`$REDIS_INFO|/bin/grep cluster|awk -F":" '{print $NF}'`
        echo $result
;;
uptime_in_seconds)
        result=`$REDIS_INFO|/bin/grep uptime_in_seconds|awk -F":" '{print $NF}'`
        echo $result
;;
connected_clients)
        result=`$REDIS_INFO|/bin/grep connected_clients|awk -F":" '{print $NF}'`
        echo $result
;;
client_longest_output_list)
        result=`$REDIS_INFO|/bin/grep client_longest_output_list|awk -F":" '{print $NF}'`
        echo $result
;;
client_biggest_input_buf)
        result=`$REDIS_INFO|/bin/grep client_biggest_input_buf|awk -F":" '{print $NF}'`
        echo $result
;;
blocked_clients)
        result=`$REDIS_INFO|/bin/grep blocked_clients|awk -F":" '{print $NF}'`
        echo $result
;;
#内存
maxmemory)
        result=`$REDIS_INFO|/bin/grep maxmemory|awk -F":" '{print $NF}'|awk 'NR==1'`
        echo $result
;;
used_memory)
        result=`$REDIS_INFO|/bin/grep used_memory|awk -F":" '{print $NF}'|awk 'NR==1'`
        echo $result
;;
used_memory_rss)
        result=`$REDIS_INFO|/bin/grep -w used_memory_rss|awk -F":" '{print $NF}'|awk -F'k' '{print $1}'`
        echo $result
;;
used_memory_pct)
 A=`$REDIS_INFO|/bin/grep used_memory|awk -F":" '{print $NF}'|awk 'NR==1'`
 B=`$REDIS_INFO|/bin/grep maxmemory|awk -F":" '{print $NF}'|awk 'NR==1'`
 result=`echo $A $B | awk '{printf"%0.2f",$1/$2}'`
        echo $result
;;
used_memory_peak)
        result=`$REDIS_INFO|/bin/grep used_memory_peak|awk -F":" '{print $NF}'|awk 'NR==1'`
        echo $result
;;
used_memory_lua)
        result=`$REDIS_INFO|/bin/grep used_memory_lua|awk -F":" '{print $NF}'`
        echo $result
;;
mem_fragmentation_ratio)
        result=`$REDIS_INFO|/bin/grep mem_fragmentation_ratio|awk -F":" '{print $NF}'`
        echo $result
;;
#rdb
rdb_changes_since_last_save)
        result=`$REDIS_INFO|/bin/grep rdb_changes_since_last_save|awk -F":" '{print $NF}'`
        echo $result
;;
rdb_bgsave_in_progress)
        result=`$REDIS_INFO|/bin/grep rdb_bgsave_in_progress|awk -F":" '{print $NF}'`
        echo $result
;;
rdb_last_save_time)
        result=`$REDIS_INFO|/bin/grep rdb_last_save_time|awk -F":" '{print $NF}'`
        echo $result
;;
rdb_last_bgsave_status)
        result=`$REDIS_INFO|/bin/grep -w "rdb_last_bgsave_status" | awk -F':' '{print $2}' | /bin/grep -c ok`
        echo $result
;;
rdb_current_bgsave_time_sec)
        result=`$REDIS_INFO|/bin/grep -w "rdb_current_bgsave_time_sec" | awk -F':' '{print $2}'`
        echo $result
;;
#rdbinfo
aof_enabled)
        result=`$REDIS_INFO|/bin/grep -w "aof_enabled" | awk -F':' '{print $2}'`
        echo $result
;;
aof_rewrite_scheduled)
        result=`$REDIS_INFO|/bin/grep -w "aof_rewrite_scheduled" | awk -F':' '{print $2}'`
        echo $result
;;
aof_last_rewrite_time_sec)
        result=`$REDIS_INFO|/bin/grep -w "aof_last_rewrite_time_sec" | awk -F':' '{print $2}'`
        echo $result
            ;;
aof_current_rewrite_time_sec)
        result=`$REDIS_INFO|/bin/grep -w "aof_current_rewrite_time_sec" | awk -F':' '{print $2}'`
        echo $result
            ;;
aof_last_bgrewrite_status)
        result=`$REDIS_INFO|/bin/grep -w "aof_last_bgrewrite_status" | awk -F':' '{print $2}' | /bin/grep -c ok`
        echo $result
;;
#aofinfo
aof_current_size)
        result=`$REDIS_INFO|/bin/grep -w "aof_current_size" | awk -F':' '{print $2}'`
        echo $result
;;
aof_base_size)
        result=`$REDIS_INFO|/bin/grep -w "aof_base_size" | awk -F':' '{print $2}'`
        echo $result
;;
aof_pending_rewrite)
        result=`$REDIS_INFO|/bin/grep -w "aof_pending_rewrite" | awk -F':' '{print $2}'`
        echo $result
;;
aof_buffer_length)
        result=`$REDIS_INFO|/bin/grep -w "aof_buffer_length" | awk -F':' '{print $2}'`
        echo $result
;;
aof_rewrite_buffer_length)
        result=`$REDIS_INFO|/bin/grep -w "aof_rewrite_buffer_length" | awk -F':' '{print $2}'`
        echo $result
;;
aof_pending_bio_fsync)
        result=`$REDIS_INFO|/bin/grep -w "aof_pending_bio_fsync" | awk -F':' '{print $2}'`
        echo $result
;;
aof_delayed_fsync)
        result=`$REDIS_INFO|/bin/grep -w "aof_delayed_fsync" | awk -F':' '{print $2}'`
        echo $result
;;
#stats
total_connections_received)
        result=`$REDIS_INFO|/bin/grep -w "total_connections_received" | awk -F':' '{print $2}'`
        echo $result
;;
total_commands_processed)
        result=`$REDIS_INFO|/bin/grep -w "total_commands_processed" | awk -F':' '{print $2}'`
        echo $result
;;
instantaneous_ops_per_sec)
        result=`$REDIS_INFO|/bin/grep -w "instantaneous_ops_per_sec" | awk -F':' '{print $2}'`
        echo $result
;;
rejected_connections)
        result=`$REDIS_INFO|/bin/grep -w "rejected_connections" | awk -F':' '{print $2}'`
        echo $result
;;
expired_keys)
        result=`$REDIS_INFO|/bin/grep -w "expired_keys" | awk -F':' '{print $2}'`
        echo $result
;;
evicted_keys)
        result=`$REDIS_INFO|/bin/grep -w "evicted_keys" | awk -F':' '{print $2}'`
        echo $result
;;
keyspace_hits)
        result=`$REDIS_INFO|/bin/grep -w "keyspace_hits" | awk -F':' '{print $2}'`
        echo $result
;;
keyspace_misses)
        result=`$REDIS_INFO|/bin/grep -w "keyspace_misses" | awk -F':' '{print $2}'`
        echo $result
;;
pubsub_channels)
        result=`$REDIS_INFO|/bin/grep -w "pubsub_channels" | awk -F':' '{print $2}'`
        echo $result
;;
pubsub_channels)
        result=`$REDIS_INFO|/bin/grep -w "pubsub_channels" | awk -F':' '{print $2}'`
        echo $result
;;
pubsub_patterns)
        result=`$REDIS_INFO|/bin/grep -w "pubsub_patterns" | awk -F':' '{print $2}'`
        echo $result
;;
latest_fork_usec)
        result=`$REDIS_INFO|/bin/grep -w "latest_fork_usec" | awk -F':' '{print $2}'`
        echo $result
;;
connected_slaves)
        result=`$REDIS_INFO|/bin/grep -w "connected_slaves" | awk -F':' '{print $2}'`
        echo $result
;;
master_link_status)
        result=`$REDIS_INFO|/bin/grep -w "master_link_status"|awk -F':' '{print $2}'|/bin/grep -c up`
        echo $result
;;
master_last_io_seconds_ago)
        result=`$REDIS_INFO|/bin/grep -w "master_last_io_seconds_ago"|awk -F':' '{print $2}'`
        echo $result
;;
master_sync_in_progress)
        result=`$REDIS_INFO|/bin/grep -w "master_sync_in_progress"|awk -F':' '{print $2}'`
        echo $result
;;
slave_priority)
        result=`$REDIS_INFO|/bin/grep -w "slave_priority"|awk -F':' '{print $2}'`
        echo $result
;;
#cpu
used_cpu_sys)
        result=`$REDIS_INFO|/bin/grep -w "used_cpu_sys"|awk -F':' '{print $2}'`
        echo $result
;;
used_cpu_user)
        result=`$REDIS_INFO|/bin/grep -w "used_cpu_user"|awk -F':' '{print $2}'`
        echo $result
;;
used_cpu_sys_children)
        result=`$REDIS_INFO|/bin/grep -w "used_cpu_sys_children"|awk -F':' '{print $2}'`
        echo $result
;;
used_cpu_user_children)
        result=`$REDIS_INFO|/bin/grep -w "used_cpu_user_children"|awk -F':' '{print $2}'`
        echo $result
;;
*)
 echo "argu error"
;;
esac
#db0:key
   elif [[ $# -eq 4 ]];then
case $4 in
keys)
        result=`$REDIS_INFO| /bin/grep -w "db0"| /bin/grep -w "$1" | /bin/grep -w "keys" | awk -F'=|,' '{print $2}'`
        echo $result
;;
expires)
        result=`$REDIS_INFO| /bin/grep -w "db0"| /bin/grep -w "$1" | /bin/grep -w "expires" | awk -F'=|,' '{print $4}'`
        echo $result
;;
avg_ttl)
        result=`$REDIS_INFO|/bin/grep -w "db0"| /bin/grep -w "$1" | /bin/grep -w "avg_ttl" | awk -F'=|,' '{print $6}'`
        echo $result
;;
*)
     echo "argu error" ;;
esac
fi
EOF
cat>${ZabbixShell}/ip_port_discovery.sh<<"EOF"
#!/bin/bash
#$1是要发现的进程部分词汇
portarray=(`sudo ss -tnlp|egrep -i "$1"|awk {'print $4'}|awk -F':' '{if ($NF~/^[0-9]*$/) print $NF}'|sort|uniq`)
iparray=`curl -s http://169.254.169.254/latest/meta-data/local-ipv4`
length=${#portarray[@]}
printf "{\n"
printf '\t'"\"data\":["
for ((i=0;i<$length;i++))
  do
     printf '\n\t\t{'
     printf "\"{#IP}\":\"${iparray}\",\"{#TCP_PORT}\":\"${portarray[$i]}\"}"
     if [ $i -lt $[$length-1] ];then
                printf ','
     fi
  done
printf "\n\t]\n"
printf "}\n"
EOF
chmod a+x ${ZabbixShell}/redis_info.sh
chmod a+x ${ZabbixShell}/ip_port_discovery.sh
sed -i '2s#/export/redis#'"${RedisBaseDir}"'#' ${ZabbixShell}/redis_info.sh

echo '--------------------------------------------------我是 redis 信息-----------------------------------------------------------'
echo 'redis相关信息如下:'
echo "
根路径:${RedisBaseDir}
启动脚本:${RedisBaseDir}/redis.sh
"
echo '--------------------------------------------------我是 zabbix 监控信息----------------------------------------------------------'
echo '请先安装 zabbix.'
echo 'zabbix监控因使用了ss命令,故而需要开启sudo相关信息'
echo "
#zabbix用户可以以sudo执行ss
zabbix ALL = NOPASSWD: /usr/sbin/ss
#zabbix用户使用sudo无需tty
Defaults:zabbix    !requiretty
"

echo '---------------------------------------------------我是 monit 监控信息----------------------------------------------------------'
echo '请先安装 monit.'
echo 'monit配置文件如下:'
echo "
check process ${RedisBaseName} with pidfile ${RedisBaseDir}/redis.pid
  start program = \"${RedisBaseDir}/redis.sh start\"
  stop program = \"${RedisBaseDir}/redis.sh stop\"
if changed pid then alert
"