Press "Enter" to skip to content

Month: August 2017

运维实例:批量统计分区占用情况,并形成Excel报表

假设有一天,你有一个任务,要统计线上所有服务器的/export分区占用情况,比如下面这一台服务器:

$ df -hT
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda1      ext4    20G  5.2G   13G  29% /
tmpfs          tmpfs  127G     0  127G   0% /dev/shm
/dev/sda3      ext4   514G  279G  209G  58% /export
/dev/sdb1      ext4   1.1T  518G  526G  50% /export/Data/jd_search

像这样一台服务器,除了有个/export分区,还有个/export/Data/jd_search(只有一部分机器有这个分区),现在要统计这2个分区的占用情况,并且最终形成Excel报表,要怎么操作呢?

1,使用批量脚本获得所有机器的/export分区占用情况
#准备远程机器列表

$ vim ip_list
10.191.172.201
10.190.143.38
10.187.110.4
10.190.49.237
10.190.198.192
10.190.163.211

使用ansible将本机的(admin用户的)key推送到所有机器上(的admin用户),实现从本机免密码登陆到所有机器

$ vim deploy_ssh_key.yml   #准备push ssh-key
- hosts: all
  remote_user: admin
  gather_facts: no

  tasks:
  - name: install ssh key
    authorized_key: user=admin
                    key="{{ lookup('file', '/home/admin/.ssh/id_rsa.pub') }}" 
                    state=present
Leave a Comment

运维实例:批量替换配置文件+批量重启服务

工作中遇到的一个小问题,记录一下。假设有一千台机器运行着nginx但每台机器上的nginx目录可能不太一样,比如
/export/servers/openresty-local/nginx/
/export/servers/openresty/nginx/
/export/servers/openresty-ngx-ump/nginx/
现在要替换掉nginx目录下的某个脚本,并且重启这个nginx服务。需要用到的工具有Ansbile和自己写的批量替换脚本batch_op.py

#准备远程机器列表
$ vim ip_list
10.191.172.201
10.190.143.38
10.187.110.4
10.190.49.237
10.190.198.192
10.190.163.211
#准备替换脚本
$ vim /tmp/b.sh
#!/bin/bash

if [ $(id -u) != "0" ]; then
    echo "Error: need root to run this script."
    exit 1
fi

a=`ps -ef | grep openresty | grep -v grep | awk '{ print $11 }' | cut -d\/ -f1,2,3,4`

if [ ! $a ]; then
    echo 'Error! Not found $a directory.'
elif [ -f $a/lualib/resty/nginx_diviner.lua ]; then
   cp $a/lualib/resty/nginx_diviner.lua $a/lualib/resty/nginx_diviner.lua.bak.`date +%Y%m%d%H%M%S` && \
   wget -q -O $a/lualib/resty/nginx_diviner.lua http://172.22.193.65:8003/nginx_diviner.lua && \
   chown admin:admin $a/lualib/resty/nginx_diviner.lua* && \
   $a/nginx/sbin/nginx -s reload && \
   echo "Found and replaced $a/lualib/resty/nginx_diviner.lua"
else
   echo "Not Found $a/lualib/resty/nginx_diviner.lua"
   exit 1
fi

if [ "$a" != "/export/servers/openresty" ] && [ -f /export/servers/openresty/lualib/resty/nginx_diviner.lua ]; then
   cp /export/servers/openresty/lualib/resty/nginx_diviner.lua /export/servers/openresty/lualib/resty/nginx_diviner.lua.bak.`date +%Y%m%d%H%M%S` && \
   wget -q -O /export/servers/openresty/lualib/resty/nginx_diviner.lua http://172.22.193.65:8003/nginx_diviner.lua && \
   chown admin:admin /export/servers/openresty/lualib/resty/nginx_diviner.lua && \
   echo "Extra Operation! also found and replaced /export/servers/openresty/lualib/resty/nginx_diviner.lua"
fi
Leave a Comment