批量获取主机cpu、内存、磁盘占用率

为实现每日定时输出服务器资源使用情况,使用ansible playbook收集服务器信息并配合crontab定时执行任务

1、将要统计的服务器ip记录至ip.txt

#将要统计的主机记录至ansible配置文件
[root@ecs-79cf-0708497 ~]# vim /root/ssh/ip.txt
192.168.7.187
192.168.6.233
192.168.6.106
192.168.6.185
192.168.6.228
192.168.6.134
192.168.1.80
192.168.1.25
192.168.1.184
192.168.1.64
192.168.1.84
192.168.1.33
192.168.1.207
192.168.1.220
192.168.6.61
192.168.6.214

2、发送密钥(所有主机密码相同,-p 后******替换为密码)

[root@ecs-79cf-0708497 ssh]# cat ssh-key.sh 
#!/bin/bash
for i in `cat ip.txt`
  do 
   sshpass -p "******" ssh-copy-id -i ~/.ssh/id_dsa.pub -o StrictHostKeyChecking=no root@"$i" &> /dev/null && echo $i OK!! ||echo $i ERORR!!
   
  done
[root@ecs-79cf-0708497 ssh]# sh ssh-key.sh

3、 将要统计的主机记录至ansible配置文件

[root@ecs-79cf-0708497 ~]# vim /etc/ansible/hosts
[fenquall]
192.168.7.187
192.168.6.233
192.168.6.106
192.168.6.185
192.168.6.228
192.168.6.134
192.168.1.80
192.168.1.25
192.168.1.184
192.168.1.64
192.168.1.84
192.168.1.33
192.168.1.207
192.168.1.220
192.168.6.61
192.168.6.214

4、编写yaml文件,shell内参数为实际在所有主机执行的命令 

[root@ecs-79cf-0708497 fenqu]# cat fenqu-cpu-mem-disk-all.yml 
- name: Statistics CPU Memory Disk Utilization
  hosts: fenquall
  become: no
  remote_user: root
  gather_facts: no
  tasks:
    - name: "Statistics CPU Memory Disk Utilization..."
      shell: |
        top -bn1 | sed -n '3p' | awk '{print $2+$4}' | awk '{printf("%s%%\n",$1)}'
        free -m | awk 'NR==2{printf" %s/%sMB (%.2f%%)\n", $3,$2,$3*100/$2 }'
        df -hT | grep -w "/" | grep -v 192.168.3.215 | awk '{print $6}'
        hostname
      register: out
    - debug: var=out.stdout_lines

 5、对ansible命令输出值进行优化

[root@ecs-79cf-0708497 fenqu-cpu-mem-disk]# cat all.sh
#!/bin/bash
 ansible-playbook /root/fenqu/fenqu-cpu-mem-disk-all.yml > /root/fenqu/all1.txt
 grep "MB" -C3 /root/fenqu/all1.txt | grep -v "out.stdout_lines" |  tr -d "\n" | sed s/[[:space:]]//g  | sed s/ok/"\n"/g  > /root/fenqu/all2.txt && sed -i '1d' /root/fenqu/all2.txt && sed -i s/:\\[//g /root/fenqu/all2.txt && sed -i s/\]\=\>\{\"/\\t/g /root/fenqu/all2.txt &&  sed -i s/\]\=\>\{\"/\\t/g /root/fenqu/all2.txt && sed -i s/\",\"/\\t/g /root/fenqu/all2.txt && sed -i s/\"--//g /root/fenqu/all2.txt &&  sed -i s/\"]}//g  /root/fenqu/all2.txt && sed -i s/\"]--//g /root/fenqu/all2.txt && sed -i s/\"]//g /root/fenqu/all2.txt
#按第五列排序
 cat /root/fenqu/all2.txt | sort -k5 > /root/xunjian/$(date '+%Y-%m-%d-%H:%M')cpumemdiskall.txt

6、执行脚本

[root@ecs-79cf-0708497 fenqu-cpu-mem-disk]# sh all.sh
[root@ecs-79cf-0708497 fenqu-cpu-mem-disk]# cd xunjian/
[root@ecs-79cf-0708497 xunjian]# cat 2023-08-21-15\:30cpumemdiskall.txt

 7、设置定时任务

[root@ecs-79cf-0708497 ~]# crontab -l
10 13 * * * /usr/sbin/ntpdate  time.windows.com
0 16 * * * /bin/bash /root/fenqu-cpu-mem-disk/all.sh

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值