使用 Ansible 统计服务器资源利用率
分享一个 ansible playbook,统计服务器 CPU、内存、磁盘利用率,3 条 shell 脚本实现统计:
CPU 利用率统计:
top -bn1 | grep load | awk '{printf "CPU Load: %.2f\n", $(NF-2)}'
1
内存利用率统计:
free -m | awk 'NR==2{printf "Memory Usage: %s/%sMB (%.2f%%)\n", $3,$2,$3*100/$2 }'
1
磁盘利用率统计(列出每块磁盘利用率):
df -h -t ext2 -t ext4 | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print "Disk
Ansible playbook server-cpu-mem-disk-usage.yml
---
- name: Statistics CPU Memory Disk Utilization
hosts: node
become: no
remote_user: root
gather_facts: no
tasks:
- name: "Statistics CPU Memory Disk Utilization..."
shell: |
free -m | awk 'NR==2{printf "Memory Usage: %s/%sMB (%.2f%%)\n", $3,$2,$3*100/$2 }'
df -h -t ext2 -t ext4 -t xfs| grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print "Disk Usage:"" " $1 " " $3"/"$2" ""("$5")"}'
top -bn1 | grep 'load average' | awk '{printf "CPU Load: %.2f\n", $(NF-2)}'
top -bn1 | grep Cpu |awk '{printf "Cpu usage: (%.2f%%)\n",100-$8}'
register: out
- debug: var=out.stdout_lines
网友评论