用MRTG监测Linux-CPU监控

运维 系统运维
用MRTG监测Linux系统网络、CPU、内存和硬盘情况?MRTG(Multi Router Traffic Grapher)是一个监控网络链路流量负载的工具软件,通过snmp协议得到设备的流量信息。本文讲述的是:CPU监控

  用MRTG监测Linux系统网络、CPU、内存和硬盘情况

  本文讲述的是:用MRTG监测Linux系统网络、CPU、内存和硬盘情况:

  原理介绍MRTG安装监控CPU监控内存监控硬盘监控

  3、获得我们需要的关于CPU 和Memory 的数据

  1), 获得CPU 的使用率和CPU 的闲置率

  为了获得CPU 的这两个数据,我们使用sar –u 1 3 这个命令,

  1.   [root@intel zwz]# sar -u 1 3  
  2.  
  3.   Linux 2.4.20-8 (intel) 08/30/2005  
  4.  
  5.   05:46:16 PM CPU %user %nice %system %idle  
  6.  
  7.   05:46:17 PM all 0.00 0.00 1.00 99.00  
  8.  
  9.   05:46:18 PM all 0.00 0.00 0.00 100.00  
  10.  
  11.   05:46:19 PM all 0.00 0.00 0.00 100.00  
  12.  
  13.   Average: all 0.00 0.00 0.33 99.67  
  14.  

  sar 命令执行后生成如上图所示的数据。我们需要的是三个带下划线的数据。其中:

  CPU 的使用率为:%user + %system = 0.00 + 0.33 = 0.33

  CPU 的闲置率为:%idle = 99.67

  好了只有sar 是不行的,因为mrtg 不能识别这些数据,mrtg 能识别两个数据,所以我们要把0.33 和99.67 传给mrtg,这很容易实现,我用perl 写了一个脚本(cpu.pl)来获得并输出这两个数据:脚本如下

  1.   #!/usr/bin/perl  
  2.  
  3.   system ("/usr/bin/sar -u 1 3|grep Average >cpu_info_file"); #sar 输出写入文件cpu_info_file  
  4.  
  5.   open (CPUINFO,"cpu_info_file"); #打开cpu_info_file 文件  
  6.  
  7.   @cpuinfo=; # 读去文件内容  
  8.  
  9.   close (CPUINFO); #关闭文件  
  10.  
  11.   foreach $line(@cpuinfo) { #分别获得我们需要的  
  12.  
  13.   @cpustatus=split(/ +/,$line); #每一个数值  
  14.  
  15.   }  
  16.  
  17.   $cpuused=$cpustatus[2]+$cpustatus[4];  
  18.  
  19.   $cpuidle=$cpustatus[5];  
  20.  
  21.   print "$cpuused\n"; #输出两个数值  
  22.  
  23.   print "$cpuidle";  
  24.  
  25.   system ("uptime");  
  26.  
  27.   system ("uname -n");  
  28.  

  ############### By Vitter :vitter@safechina.net#####################

  注意:在脚本里system ("/usr/local/bin/sar -u 1 3|grep Average >cpu_info_file") ,这句话中必须将sar 的全路径写全,而不能用system ("sar -u 1 3|grep Average >cpu_info_file") 。因为cpu.pl 是由mrtg 调用,mrtg 不知道你的系统路径。

  我将cpu.pl 脚本放在/usr/local/mrtg/bin 下,执行cpu.pl 会得到下面的结果:

  1.   [root@intel bin]# ./cpu.pl  
  2.  
  3.   0  
  4.  
  5.   100.00  
  6.  
  7.   12:07am up 1 day, 7:22, 2 users, load average: 0.07, 0.12, 0.09  
  8.  
  9.   TRSB  
  10.  

  好,我们需要的数值已经输出来了,下一步的工作就是要交给mrtg 了,mrtg 是通过一个配置文件来获得这两个数值的,通常情况下这个配置文件是由mrtg 的cfgmaker 命令来生成的, 但这次我们要自己写这个配置文件(cpu.cfg) : 这个配置文件我把他放在/usr/local/mrtg/etc 下,内容如下:

  1.   [root@intel etc]# vi cpu.cfg  
  2.  
  3.   WorkDir:/usr/local/apache_1.3.31/htdocs/mrtg/cpu/  
  4.  
  5.   Target[localhost]:`/usr/local/mrtg/bin/cpu.pl`  
  6.  
  7.   Xsize[localhost]: 300  
  8.  
  9.   Ysize[localhost]: 100  
  10.  
  11.   Ytics[localhost]: 10  
  12.  
  13.   MaxBytes[localhost]:100  
  14.  
  15.   Title[localhost]:CPU State  
  16.  
  17.   PageTop[localhost]:CPU State of Vitter-test Server  
  18.  
  19.   ShortLegend[localhost]: %  
  20.  
  21.   YLegend[localhost]: CPU (%)  
  22.  
  23.   Legend1[localhost]: Used  
  24.  
  25.   Legend2[localhost]: Total  
  26.  
  27.   LegendI[localhost]: CPU Used  
  28.  
  29.   LegendO[localhost]: CPU IDEL  
  30.  
  31.   Options[localhost]: growright,gauge,nopercent  
  32.  

  下面我们可以执行mrtg 了:

  1.   [root@intel etc]#/usr/local/mrtg/bin/mrtg /usr/local/mrtg/etc/cpu.cfg  
  2.  

  当第一次执行时会有报警,执行三次,就没有报警了。

【编辑推荐】

用MRTG在IIS上完成入侵检测功能

MRTG监控端的配置

MRTG的网络流量监测研究与应用(应用篇)

责任编辑:zhaolei 来源: ccw
相关推荐

2011-03-31 11:14:29

MRTG监测

2011-03-31 11:20:10

MRTG监测

2011-03-31 11:14:30

MRTG监测

2011-03-31 11:14:28

2011-03-30 13:29:49

MRTG

2011-03-31 09:02:22

MRTG监控CPU

2011-03-30 11:30:31

MRTG

2011-04-02 11:40:11

mrtg监控

2011-04-01 09:18:04

mrtg流量

2011-03-30 11:31:10

MRTG

2011-03-30 11:31:10

MRTG

2011-03-30 13:29:55

MRTG

2011-03-30 11:34:26

流量MRTG

2011-04-06 11:36:30

MRTG监控内存

2011-03-30 11:31:10

MRTG

2011-03-31 09:02:18

MRTG流量

2010-12-22 13:09:23

Linux性能监测CPU

2011-03-31 10:24:15

2011-04-06 13:50:34

LinuxMRTG监控

2010-01-27 10:01:20

点赞
收藏

51CTO技术栈公众号