Linux 磁盘、CPU、内存获取脚本
脚本
脚本
#!/bin/bash
# 获取要监控的本地服务器IP地址
IP=`/usr/sbin/ifconfig | grep inet | grep -vE 'inet6|127.0.0.1' | awk '{print $2}'`
# 获取cpu总核数
cpu_num=`grep -c "model name" /proc/cpuinfo`
cpu_user=`top -b -n 1 | grep Cpu | awk '{print $2}' | cut -f 1 -d "%"`
# 获取内核空间占用CPU百分比
cpu_system=`top -b -n 1 | grep Cpu | awk '{print $4}' | cut -f 1 -d "%"`
# 获取空闲CPU百分比
cpu_idle=`top -b -n 1 | grep Cpu | awk '{print $8}' | cut -f 1 -d "%"`
# 获取等待输入输出占CPU百分比
cpu_iowait=`top -b -n 1 | grep Cpu | awk '{print $10}' | cut -f 1 -d "%"`
# 获取CPU中断次数
cpu_interrupt=`vmstat -n 1 1 | sed -n 3p | awk '{print $11}'`
# 获取CPU上下文切换次数
cpu_context_switch=`vmstat -n 1 1 | sed -n 3p | awk '{print $12}'`
#3、获取CPU负载信息
# 获取CPU15分钟前到现在的负载平均值
cpu_load_15min=`uptime | awk '{print $12}' | cut -f 1 -d ','`
# 获取CPU5分钟前到现在的负载平均值
cpu_load_5min=`uptime | awk '{print $11}' | cut -f 1 -d ','`
# 获取CPU1分钟前到现在的负载平均值
cpu_load_1min=`uptime | awk '{print $10}' | cut -f 1 -d ','`
# 获取任务队列(就绪状态等待的进程数)
cpu_task_length=`vmstat -n 1 1 | sed -n 3p | awk '{print $1}'`
#4、获取内存信息
# 获取物理内存总量
mem_total=`free | grep Mem | awk '{print $2}'`
# 获取操作系统已使用内存总量
mem_sys_used=`free | grep Mem | awk '{print $3}'`
# 获取操作系统未使用内存总量
mem_sys_free=`free | grep Mem | awk '{print $4}'`
# 获取应用程序已使用的内存总量
mem_user_used=`free | sed -n 3p | awk '{print $3}'`
# 获取应用程序未使用内存总量
mem_user_free=`free | sed -n 3p | awk '{print $4}'`
# 获取交换分区总大小
mem_swap_total=`free | grep Swap | awk '{print $2}'`
# 获取已使用交换分区大小
mem_swap_used=`free | grep Swap | awk '{print $3}'`
# 获取剩余交换分区大小
mem_swap_free=`free | grep Swap | awk '{print $4}'`
# 总大小
disk_rate=`df -h |sed -n '6p'|awk '{print $5}'|cut -f 1 -d '%'`
# 可用
disk_used=`df -h |sed -n '6p'|awk '{print $4}'|cut -f 1 -d 'G'`
# 未使用
disk_free=`df -h |sed -n '6p'|awk '{print $3}'|cut -f 1 -d 'G'`
# 容量
disk_total=`df -h |sed -n '6p'|awk '{print $2}'|cut -f 1 -d 'G'`
time=$(date "+%Y-%m-%dT%H:%M:%S+08:00")
echo "{\"@timestamp\":\"$time\",\"host\":\"$IP\",\"cpu_num\":$cpu_num,\"cpu_user\":$cpu_user,\"cpu_system\":$cpu_system,\"cpu_idle\":$cpu_idle,\"cpu_iowait\":$cpu_iowait,\"cpu_interrupt\":$cpu_interrupt,\"cpu_context_switch\":$cpu_context_switch,\"cpu_load_15min\":$cpu_load_15min,\"cpu_load_5min\":$cpu_load_5min,\"cpu_load_1min\":$cpu_load_1min,\"cpu_task_length\":$cpu_task_length,\"mem_total\":$mem_total,\"mem_sys_used\":$mem_sys_used,\"mem_sys_free\":$mem_sys_free,\"mem_user_used\":$mem_user_used,\"mem_user_free\":$mem_user_free,\"mem_swap_total\":$mem_swap_total,\"mem_swap_used\":$mem_swap_used,\"mem_swap_free\":$mem_swap_free,\"disk_rate\":$disk_rate,\"disk_used\":$disk_used,\"disk_free\":$disk_free,\"disk_total\":$disk_total}" >> /home/sys/system.json
输出结果
{
"@timestamp": "2023-02-16T09:13:17+08:00",
"host": "192.168.0.124",
"cpu_num": 4,
"cpu_user": 1.4,
"cpu_system": 2.9,
"cpu_idle": 95.8,
"cpu_iowait": 0,
"cpu_interrupt": 1,
"cpu_context_switch": 1,
"cpu_load_15min": 0.13,
"cpu_load_5min": 0.25,
"cpu_load_1min": 0.63,
"cpu_task_length": 0,
"mem_total": 7990016,
"mem_sys_used": 286220,
"mem_sys_free": 6972252,
"mem_user_used": 0,
"mem_user_free": 8257532,
"mem_swap_total": 8257532,
"mem_swap_used": 0,
"mem_swap_free": 8257532,
"disk_rate": 7,
"disk_used": 47,
"disk_free": 3.3,
"disk_total": 50
}
哇!又赚了一天人民币