centos 6.4 系统CPU,内存,负载,IO监控脚本

来源:互联网 发布:2003网络歌手名单大全 编辑:程序博客网 时间:2024/05/22 05:05

今天提供一个Linux 系统监控脚本,监控的指标有CPU,内存使用率,负载,IO等, 并且将
主机的这些指标插入到数据库里,便于历史分析。


--1创建一张监控表,用于记录各项批指标信息

CREATE TABLE tbl_monitor (

 id   serial,

 hostname character varying(64),           --主机名

 add_time datetime,                                --记录插入时间
cpu_useratio numeric(5,2),                     --cpu使用率

 mem_useratio numeric(5,2),                   --内存使用率
cpu_load numeric(5,2),                             --负载(load)值
 io_wa smallint,                                          --系统IO等侍情况

 constraint pk_tbl_monitor PRIMARY KEY (ID) );

 

---2监控脚本

#/bin/bash

##### get cpu mem current information
file_dir="/backup/scripts/"
vmstat 1 3 > ${file_dir}/cpu_file.txt
free -m > ${file_dir}/mem_file.txt
cpu_file="${file_dir}/cpu_file.txt"
mem_file="${file_dir}/mem_file.txt"

### declare variable
v_hostname="`hostname`"
v_hostip="10.10.10.10"
v_time="`date +%F\ %T`"
v_cpuidle=`cat ${cpu_file} | sed -n '$'p | awk '{print $15}'`
v_cpuuse=`echo "scale=2; 100.00-${v_cpuidle}" | bc`
v_memtotal=`cat ${mem_file} | sed -n '2'p | awk '{print $2}'`
v_memused=`cat ${mem_file} | sed -n '2'p | awk '{print $3}'`
v_memratio=`echo "scale=2; ${v_memused}*100/${v_memtotal}" | bc`
v_load=`uptime | awk '{print $10}'| tr -d ","`
v_io=`cat ${cpu_file} | sed -n '$'p | awk '{print $(NF-1) }'`
#v_email="francs.tan@sky-mobi.comFrancs3@163.com"

## cpu alarm
if [ ${v_cpuidle} -lt 95 ]; then
 echo "`date +%F\ %T` ${v_hostip}: CPU usage alarm ,please check ! " |  mutt -s "CPU usage  ${v_cpuuse}% , ${v_hostname} " ${v_email}
fi


#CREATE TABLE tbl_monitor ( id   serial, hostname character varying(64), add_time datetime, cpu_useratio numeric(5,2), mem_useratio numeric(5,2),cpu_load numeric(5,2), io_wa smallint,  constraint pk_tbl_monitor PRIMARY KEY (ID) );
## insert data to monitor database
mysql -uroot -p"123456"  -e " insert into yunwei.tbl_monitor(hostname,add_time,cpu_useratio, mem_useratio , cpu_load  ,io_wa ) values ( '${v_hostname}', '${v_time}', '${v_cpuuse}', '${v_memratio}', '${v_load}', '${v_io}' );"

## remove temp file
rm -f ${cpu_file}
rm -f ${mem_file}

原创粉丝点击