作者:白金 网名:platinum(chinaunix) 超超白金(白金论坛)
欢迎转载,转载请保留上述信息
以下是我的配置文档,根据个人不同再进行细节更改
mrtg.conf
[code:1:8cf569541e]
### Global Defaults
# to get bits instead of bytes and graphs growing to the right
Options[_]: growright
######################################################################
# System: platinum.3322.org
# Description: Linux platinum.3322.org
# Contact: Root (configure /etc/snmp/snmp.local.conf)
# Location: Unknown (edit /etc/snmp/snmpd.conf)
######################################################################
### Interface 3 >> Descr: 'eth1' | Name: '' | Ip: '0.0.0.0' | Eth: '4c-00-10-b5-
2e-1b' ###
Target[platinum.3322.org_3]: 3:holdata@holdata.3322.org
SetEnv[platinum.3322.org_3]: MRTG_INT_IP="0.0.0.0" MRTG_INT_DESCR="eth1"
MaxBytes[platinum.3322.org_3]: 80000
Xsize[platinum.3322.org_3]: 600
Ysize[platinum.3322.org_3]: 200
Ytics[platinum.3322.org_3]: 9
Title[platinum.3322.org_3]: Traffic Analysis for ADSL -- platinum.3322.org
PageTop[platinum.3322.org_3]: Traffic Analysis for ADSL -- platinum.3322.org
H1>
### Interface 2 >> Descr: 'eth0' | Name: '' | Ip: '192.168.0.1' | Eth: '4c-00-10
-b4-e1-2c' ###
Target[platinum.3322.org_2]: 2:holdata@holdata.3322.org
SetEnv[platinum.3322.org_2]: MRTG_INT_IP="192.168.0.1" MRTG_INT_DESCR="eth0"
MaxBytes[platinum.3322.org_2]: 10000000
Xsize[platinum.3322.org_2]: 600
Ysize[platinum.3322.org_2]: 200
Ytics[platinum.3322.org_2]: 9
Title[platinum.3322.org_2]: Traffic Analysis for LAN -- server.platinum.3322.org
PageTop[platinum.3322.org_2]: Traffic Analysis for LAN -- platinum.3322
.org
Target[managemem]:`/usr/local/sbin/mem.pl`
#Target[managemem]: memTotalReal.0&memAvailReal.0:holdata@holdata.3322.org
Xsize[managemem]: 600
Ysize[managemem]: 200
Ytics[managemem]: 9
Unscaled[managemem]: dwym
MaxBytes[managemem]: 262144
Title[managemem]:Memory State of HOLDATA Server
PageTop[managemem]:Memory State of HOLDATA Server
ShortLegend[managemem]: &
kmg[managemem]:kB,MB
kilo[managemem]:1024
YLegend[managemem]: Memory Usage
Legend1[managemem]: &Total Memory&
Legend2[managemem]: &Available Memory&
LegendI[managemem]: &Total Memory&
LegendO[managemem]: &Available Memory&
Options[managemem]: growright,gauge,nopercent
Target[disk]: `/usr/local/sbin/df.pl`
Xsize[disk]: 600
Ysize[disk]: 200
Ytics[disk]: 10
Title[disk]: HOLDATA SERVER Disk Space (76,524,755 kB / 80GB) Megabytes used
Unscaled[disk]: dwym
MaxBytes[disk]: 76524755
PageTop[disk]: PLATINUM SERVER Disk Space(76,524,755 kB / 80GB) Megabytes use
d
kmg[disk]: KB,MB,GB
LegendI[disk]: Total Disk Space
LegendO[disk]: Used Disk Space
Legend1[disk]: Total Disk Space
Legend2[disk]: Used Disk Space
YLegend[disk]: Megabytes
ShortLegend[disk]: &
Options[disk]: growright,gauge,nopercent
Target[cpu]:`/usr/local/sbin/cpu.pl`
Xsize[cpu]:600
Ysize[cpu]:200
Ytics[cpu]:10
MaxBytes[cpu]:100
Title[cpu]: CPU State of HOLDATA Server
PageTop[cpu]:CPU State of HOLDATA Server
ShortLegend[cpu]:%
YLegend[cpu]:CPU Loading(%)
Legend1[cpu]: Load over 5 minutes&
Legend2[cpu]: Load over last 15 minutes&
Legend3[cpu]: Average over last 5 minutes
Legend4[cpu]: Average over last 15 minutes
LegendO[cpu]:CPU SYS&
LegendI[cpu]:CPU USER&
Options[cpu]:growright,gauge,nopercent
Target[load]: `/usr/local/sbin/load.sh`
Xsize[load]: 600
Ysize[load]: 200
Ytics[load]: 10
MaxBytes[load]: 3000
PageTop[load]: HOLDATA SERVER Load Average
Options[load]: growright, gauge, nopercent
YLegend[load]: Load average (10E-2)
ShortLegend[load]: (10E-2)&
Legend1[load]: Load over 5 minutes&
Legend2[load]: Load over last 15 minutes&
Legend3[load]: Average over last 5 minutes
Legend4[load]: Average over last 15 minutes
LegendO[load]: 15 minute stagger&&
Title[load]: System load
[/code:1:8cf569541e]
以下是要调用的SHELL的具体代码
df.pl
[code:1:8cf569541e]
#!/usr/bin/perl
# This script was written on Debian 3.0, it assumes that the command
# output(df -kl) looks like this:
# Filesystem 1k-blocks Used Available Use% Mounted on
# /dev/md0 95645100 30401312 64272080 33% /
# /dev/hde1 14119 1159 12231 9% /boot
#
# In which case, this script returns :
# 95659219
# 30402503
# when run.
foreach $filesystem (`df -kl | grep -v "Filesystem"`)
{
@df = split(/s+/,$filesystem);
$total += $df[1];
$usage += $df[2];
}
print "$total
";
print "$usage
";
[/code:1:8cf569541e]
cpu.pl
[code:1:8cf569541e]
# !/bin/bash
cpuusr=`/usr/bin/sar -u 1 3|grep Average|awk '{print$3}'`
cpusys=`/usr/bin/sar -u 1 3|grep Average|awk '{print$5}'`
UPtime=`/usr/bin/uptime`
#|awk '{print$3""$4""$5}'`
echo $cpuusr
echo $cpusys
echo $UPtime
echo holdata.3322.org
[/code:1:8cf569541e]
load.sh
[code:1:8cf569541e]
#!/bin/sh
# first the load 5 and 15 min avg
# multiply * 100 to avoid floats
# it helps if mrtg "period" is a multiple of 5 mins
uptime | sed -e 's/^.*average.*: (.*)$/1/' -e 's/ //g' |
awk -F, '{ printf("%.0f
",$2*100); printf("%.0f
",$3*100) }'
# the uptime
uptime | sed 's:^.* up (.*), [0-9][0-9]* users.*$:1:'
# my name
uname -n
[/code:1:8cf569541e]
mem.pl,直接抓取memTotalReal.0&memAvailReal.0包也可以,但好像UCD-SNMP不可以
LINUX系统安装,默认是UCD-SNMP,我现在换成了最新版本的NET-SNMP[code:1:8cf569541e]
#!/usr/bin/perl
# setup local vars
my($machine, $os);
#=======================================================
# == Enter your default machine name and os here ==
$machine = "holdata.3322.org"; # Enter machine to monitor here
$os = "linux"; # Currently there is only linux and sun
# == You shouldn't need to edit anything below this line ==
#========================================================
# This allows command args to override defaults listed above
if (scalar(@ARGV) > 2)
{
print("USAGE: cpuinfo.pl {machine} {os}
");
exit(-1);
}
if ($ARGV[0] ne '' && $ARGV[0] ne '#')
{
$machine = $ARGV[0];
}
if ($ARGV[1] ne '' && $ARGV[1] ne '#')
{
$os = $ARGV[1];
}
# Validate the os
SWITCH:
{
if ($os =~ /^sun$/){last SWITCH;}
if ($os =~ /^linux$/){last SWITCH;}
# DEFAULT: Die if we can't figure out what the os is
die "Can't figure out which OS the machine is.
";
}
# Execute the appropriate subroutine based on the os
&$os;
exit(0);
#=======================================================
# Subroutines: names of subroutines are supported OSs.
#========================================================
sub sun
{
# Run commands
# $getcpu = `rsh $machine "sar -u 1 10" | grep Average`;
# $getuptime = `rsh $machine "uptime"`;
# Parse though getcpu and get data
# $getcpu =~ /^Averages+(d+)s+(d+)s+/;
# $outputusr = $1;
# $outputsys = $2;
# Print getcpu data for mrtg
# print $outputusr."
";
# print $outputsys."
";
#foreach $_ (` | grep -v "Filesystem"`)
#{
# ($device, $size, $used, $free, $percent, $mount) = split(/s+/);
# chop($percent);
# print "$percent
";
#}
# Parse though getuptime and get data
$getuptime =~ /^s+(d{1,2}:d{2}..)s+ups+(d+)s+(......),/;
# Print getuptime data for mrtg
print $2." ".$3."
";
# Print machine name for mrtg
print $machine."
";
}
sub linux
{
# Run commands
# $getcpu = ` /usr/local/bin/sar -u 1 10 | grep "Average:"`;
$getuptime = `/usr/bin/uptime`;
# Parse though getcpu and get data
# $getcpu =~ /^Average:s+(d+).(d+)%s+(d+).(d+)%s+(d+).(d+)%s+(d+).(d+)%/;
# $getcpuusr = $1;
# $getcpusys = $5;
foreach $_ (`/usr/bin/sar -r 1 2 | grep "Average"`)
{
($average, $kbmemfree, $kbmemused, $memused, $kbmemshrd, $kbbuffers, $kbcached, $kbswpfree, $kbswpused, $swpused) = split(/s+/);
chop($user);
chop($nice);
print "$kbmemused
";
print "$kbmemfree
";
}
# Print getcpu data for mrtg
# print $getcpuusr."
";
# print $getcpusys."
";
}
[/code:1:8cf569541e]
然后再/etc/crontab里加上每2分钟运行一次mrtg 指定路径的mrtg.conf就可以了
照上面看来,MRTG可以监测任何东西,只要有数据,MRTG就能根据数据绘制出图形,自己可以编写小程序来获取你想要绘制的图形的数据
[color=red:8cf569541e]
根据不同的需要,可以再进一步改进
我这个MRTG系统已经根据自己的实际情况改进了好几次了,实际就是改写SHELL
效果见