运维常用脚本

Nginx日志日志割接及压缩

$ cat  /data/tools/ngx_acclog.sh
#!/bin/bash

# ------------------------------------------------------
# - 功能:                                    
# -     每天零晨对Nginx日志进行切割          
# -                 dengwei 2016-02-14 09:36:15
# ------------------------------------------------------

source /etc/profile
source /etc/bashrc

PROCESS=$(ps -ef |grep '[n]ginx: master' |awk '{print $2}')
# 使用'[n]ginx' 可以避免 grep -v grep 的使用
test -z "$PROCESS" && exit 1

ngx_acclog_bak()
{
    local process="$PROCESS"
    local prefix='/data/backup/logs/ngx_access'
    local date=$(date -d "yesterday" +"%Y%m%d")
    local year_month=$(date -d "yesterday" +"%Y/%m")
    local bakdir="$prefix/$year_month"
    local nginx_sbin=$(readlink /proc/$process/exe)
    local logfiles=$(find /proc/$process/fd -maxdepth 1 -type l |xargs -I {} readlink {} |grep 'access.*.log')
    # local maxsize=1000000 # 1M byte

    test ! -d $bakdir && mkdir -p $bakdir

    for log in $logfiles; do
        #size=$(stat --printf="%s" log)
        #(( $size < $maxsize )) && continue
        local file=$(basename $log |sed 's!\.log$!!')
        mv $log ${bakdir}/${file}-${date}.log
    done

    /etc/init.d/nginx reload

    cd $bakdir && find ./ -name "access[-_]*\.log" -type f -print0 | ionice -c 3 nice -n 19 xargs -0 -I {} xz {}
}

ngx_acclog_bak

创建key用户登陆

$ cat useradd-dba.sh 
#!/bin/bash

# tee /etc/sudoers.d/custom <<'EOF'
# Cmnd_Alias PROHIBIT = /sbin/reboot, /sbin/poweroff, /sbin/shutdown
# %wheel ALL=(ALL)       NOPASSWD: ALL, ! PROHIBIT
# EOF

group=dba
getent group | grep $group || groupadd $group

user=user1
id $user &> /dev/null || useradd -g $group -G wheel $user
mkdir  -p /home/$user/.ssh
chmod 700 /home/$user/.ssh
tee -a /home/$user/.ssh/authorized_keys <<'EOF'
user1公钥
EOF
chmod 600 /home/$user/.ssh/authorized_keys
chown -R $user.$group /home/$user/.ssh

user=user2
id $user &> /dev/null || useradd -g $group -G wheel $user
mkdir  -p /home/$user/.ssh
chmod 700 /home/$user/.ssh
tee -a /home/$user/.ssh/authorized_keys <<'EOF'
user2公钥
EOF
chmod 600 /home/$user/.ssh/authorized_keys
chown -R $user.$group /home/$user/.ssh

用户名和密码一致,首次登陆会强制改密码(先输入当前密码,然后输入新密码)

#!/bin/bash

users=(chenbin chenjun dongji huyahui lilianghui)
group=develop

egrep opffs /etc/group >& /dev/null
if [ $? -ne 0 ]
then
    groupadd $group
fi

for user in  ${users[@]}
do
    id $user >& /dev/null
    if [ $? -ne 0 ]
    then
        useradd -g $group -G wheel  $user
        echo $user | passwd --stdin $user
        chage -d 0 $user
    fi
done

收集硬盘日志:

#!/bin/bash
omsa_ttyperc_log()
{
    err_log="/tmp/omsa_err_log"
    file_time=$(date +%m%d)
    dir_name="/var/log"
    file_name="lsi_${file_time}.log"
    host_name=$(hostname)
    SN_numb=$(dmidecode -s system-serial-number)
    DCP_ip="10.40.6.64"
    DCP_dir="/var/log/TTY_log"
    comms=(omreport omconfig)

    for comm in ${comms[@]}
    do
        if ! which $comm >/dev/null 2>@1; then
            echo "$comm command not found"
            echo "no $comm installed!" > $err_log
            exit 1
        fi
    done

    perc_id=$(omreport storage controller |awk '$1 ~/^ID/ {print $3}')

    if [ -f ${dir_name}/${file_name} ];then
        echo "" > ${dir_name}/${file_name}
    fi
    
    omconfig storage controller action=exportlog controller=${perc_id} >/dev/null 2>$err_log 

    if [ $? -eq 0 ];then
        echo "tty perc log 收集成功!"
        if ! which expect  >/dev/null 2>@1; then
                yum -y install expect
        fi

        expect -c "
        spawn rsync ${dir_name}/${file_name} ${DCP_ip}:${DCP_dir}/${SN_numb}_${host_name}.log
        expect \"password:\"
        send \"密码\n\"
        expect off
        "
    else
        echo "tty perc log 收集失败!"
    fi
}

#main
omsa_ttyperc_log

你可能感兴趣的:(运维常用脚本)