现在有很多著名的监控程序,比如Cacti,Nagios,Ntop,Smoking等,绘图是监控程序中最重要的组成部分,通过图像可以更加直观的反映出系统的网络流量,硬件负载等,从而减小了运维的工作量,其中rrdtool就是一款主流的绘图工具,Cacti就是用rrdtool来绘图的,之前我也有单独用rrdtool绘制过Oracle的性能图(PS:可以查阅我以前的博文),此外Gnuplot也是我个人比较喜欢用的一款绘图工具,用它可以灵活的绘制出各种监控图,用于自己编写的系统监控平台。以下我把自己在生产中的使用的监控平台简化后与大家分享。
首先用sar命令,sar是一个很强大的系统监控命令,这里我用它里获取系统的性能数据。
-u参数是报告CPU的使用情况,1指的是每秒报告一次,10指的是十次
[root@oracle~]#sar-u110
Linux2.6.18-194.el5(oracle.example.com)03/09/13
22:18:16CPU%user%nice%system%iowait%steal%idle
22:18:17all30.350.0041.290.000.0028.36
22:18:18all30.260.0039.490.000.0030.26
22:18:19all31.500.0038.500.000.0030.00
22:18:20all41.500.0038.500.000.0020.00
22:18:21all33.500.0039.500.000.0027.00
22:18:22all30.000.0039.500.000.0030.50
22:18:23all41.710.0038.190.000.0020.10
22:18:24all37.000.0039.500.000.0023.50
22:18:25all32.160.0038.190.000.0029.65
22:18:26all33.670.0039.700.000.0026.63
Average:all34.170.0039.240.000.0026.59
由上面的输出可以看到,数据中有无用的数据行,可以将数据稍加筛选后重定向到monitor.data文件中
[root@oraclehtml]#sar-u110|head-n-2|tail-11|grep. /var/www/html/monitor.data
[root@oraclehtml]#catmonitor.data
22:29:39CPU%user%nice%system%iowait%steal%idle
22:29:40all36.700.0040.070.000.0023.22
22:29:42all44.650.0041.860.000.0013.49
22:29:43all33.500.0039.500.000.0027.00
22:29:44all31.660.0040.200.000.0028.14
22:29:45all31.980.0039.090.000.0028.93
22:29:46all31.160.0038.690.000.0030.15
22:29:47all42.570.0038.610.000.0018.81
22:29:48all33.850.0040.510.000.0025.64
22:29:49all31.660.0038.190.000.0030.15
当把数据完整的整理到monitor.data文件后,就开始编写Gnuplot的配置文件monitor.conf
[root@oraclehtml]#vimmonitor.conf
settermpng
setxdatatime
setstyledatalines
setoutput cpu.png
settimefmt %H:%M:%S
setformatx %H:%M:%S
setxlabel TIME
setylabel CPU
plot monitor.data using1:3title %user , monitor.data using1:5title %sys , monitor.data using1:8title %idle
最后可以写个Shell脚本文件,把生成数据和图片的两步结合在一起,这里我就写一行吧
[root@oraclehtml]#vimmonitor.sh
#!/bin/sh
sar-u110|head-n-2|tail-11|grep. /var/www/html/monitor.data gnuplot/var/www/html/monitor.conf[root@oraclehtml]#chmod+xmonitor.sh
最后通过计划任务让操作系统每分钟执行一次脚本monitor.sh
[root@oraclehtml]#crontab-e
*/1****/var/www/html/monitor.sh
写一个简单的html页面测试,其中可以用javascript来实现页面自动刷新功能
html
body
tablewidth= 600 border= 0 cellpadding= 3 cellspacing= 0
tr
td strong center CPUMonitor /center /strong /td
/tr
/table
br
p imgsrc= cpu.png width= 600 height= 380 /p
/body
scriptlanguage= JavaScript
functionrefresh(){
window.location.reload();
}
setTimeout( refresh() ,1000);
/script
/html
访问监控页面http://192.168.3.3可以看到,这是把过去10秒钟系统CPU使用情况的数据绘制成了监控图
可以做一个增加系统CPU负载的测试,执行命令cat/dev/zero /dev/null,监控页面是每秒钟刷新一次,稍等一会就会自动刷新出下图
如图所示,可以很明显的发现表示CPUidle值的蓝线已经消失,说明在这个时间段内系统的CPU负载很高。
以上只是一个简单的自制监控系统,在实际生产中可以通过php,mysql,shell脚本配合编写具有查询、分析、导出等功能更加强大的监控平台。欢迎大家给我留言交流!谢谢!
怀着淡定从容的心态去面对,也就没有了真正意义上的寂寞了。