用MRTG监测Linux系统网络、CPU、内存和硬盘情况
本文讲述的是:用MRTG监测Linux系统网络、CPU、内存和硬盘情况:
3)、获得硬盘空间总量和使用量
用命令df -kl查看硬盘空间情况
- [root@intel bin]# df -kl
- Filesystem 1k-blocks Used Available Use% Mounted on
- /dev/sda5 2063504 1169204 789480 60% /
- /dev/sda1 101089 8127 87743 9% /boot
- /dev/sda2 8262068 2324004 5518368 30% /data
- /dev/sda7 2063504 1364412 594272 70% /home
- /dev/sda6 2063504 1288716 669968 66% /usr
- /dev/sda3 2063536 350040 1608672 18% /var
- /dev/sdb1 17654736 2964312 13793600 18% /data1
总量=2063504+ 101089+8262068+2063504+2063504+2063536 +17654736 =34271941
使用量=1169204+8127+2324004+1364412+1288716+350040+2964312=9469231
- [root@intel bin]# vi df.pl
- #!/usr/bin/perl
- # This script was written on RedHat 9.0, it assumes that the command
- # output(df -kl) looks like this:
- # Filesystem 1k-blocks Used Available Use% Mounted on
- # /dev/sda5 2063504 1169204 789480 60% /
- # /dev/sda1 101089 8127 87743 9% /boot
- # /dev/sda2 8262068 2324004 5518368 30% /data
- # /dev/sda7 2063504 1364412 594272 70% /home
- # /dev/sda6 2063504 1288716 669968 66% /usr
- # /dev/sda3 2063536 350040 1608672 18% /var
- # /dev/sdb1 17654736 2964312 13793600 18% /data1
- #
- # In which case, this script returns :
- #
- # 34271941
- # 9469231
- # when run.
- foreach $filesystem (`df -kl | grep -v "Filesystem"`)
- {
- @df = split(/\s+/,$filesystem);
- $total += $df[1];
- $usage += $df[2];
- }
- print "$total\n";
- print "$usage\n";
- system ("uptime");
- system ("uname -n");
编辑配置文件(df.cfg)
- [root@intel etc]#vi df.cfg
- WorkDir:/usr/local/apache_1.3.31/htdocs/mrtg/df/
- Target[localhost]: `/usr/local/mrtg/bin/df.pl`
- Xsize[localhost]:300
- Ysize[localhost]:100
- Ytics[localhost]:10
- Title[localhost]: Vfocus.Net SERVER Disk Space (34,271,941 kB / 36GB) Megabytes used
- Unscaled[localhost]: dwym
- MaxBytes[localhost]: 34271941
- PageTop[localhost]: Vfocus.Net SERVER Disk Space(34,271,941 kB / 36GB) Megabytes used
- kmg[localhost]: KB,MB,GB
- LegendI[localhost]: Total Disk Space
- LegendO[localhost]: Used Disk Space
- Legend1[localhost]: Total Disk Space
- Legend2[localhost]: Used Disk Space
- YLegend[localhost]: Megabytes
- ShortLegend[localhost]: &
- Options[localhost]: growright,gauge,nopercent
- [root@intel etc]# /usr/local/mrtg/bin/mrtg /usr/local/mrtg/etc/df.cfg
执行三次,就没有报警了。
用indexmaker作index文件
- [root@intel bin]# ./indexmaker --title="硬盘监控" --output=/usr/local/apache_1.3.31/htdocs/mrtg/df/index.html /usr/local/mrtg/etc/df.cfg
- [root@intel bin]# ./indexmaker --title="CPU监控" --output=/usr/local/apache_1.3.31/htdocs/mrtg/cpu/index.html /usr/local/mrtg/etc/cpu.cfg
- [root@intel bin]# ./indexmaker --title="内存监控" --output=/usr/local/apache_1.3.31/htdocs/mrtg/mem/index.html /usr/local/mrtg/etc/mem.cfg
我们把mrtg 加入到crontab 中,让他定时执行:
- */3 * * * * /usr/local/mrtg/bin/mrtg /usr/local/mrtg/etc/mem.cfg
- */3 * * * * /usr/local/mrtg/bin/mrtg /usr/local/mrtg/etc/cpu.cfg
- */3 * * * * /usr/local/mrtg/bin/mrtg /usr/local/mrtg/etc/net1.cfg
- */3 * * * * /usr/local/mrtg/bin/mrtg /usr/local/mrtg/etc/df.cfg
用MRTG监测Linux系统网络、CPU、内存和硬盘情况就介绍到这里了。
【编辑推荐】