Mrtg 系统监控相关
时间:2006-03-01 来源:mantou
mrtgsysinfo: script that is executed on each system to report info Linux version: ---------------------------------- #!/bin/sh trap "/bin/echo Goodby;exit 0" 1 2 3 4 5 6 7 10 15 uptime ps -ef|grep -v UID|wc -l vmstat 1 2|grep -v procs|grep -v swpd ------------------------------------ Solaris version: ---------------------------------- #!/bin/sh trap "/bin/echo Goodby;exit 0" 1 2 3 4 5 6 7 10 15 uptime ps -ef|grep -v UID|wc -l vmstat 1 2|grep -v procs|grep -v swap mpstat 1 2| grep -v CPU ---------------------------------- Each system runs the script from inetd, so /etc/services & /etc/inetd.conf need to be updated. services: # added by dmz for mrtg stats 7260/tcp inetd.conf: # added by dmz 0330 for mrtg stats stream tcp nowait nobody /usr/local/bin/mrtgsysinfo mrtgsysinfo ------- Restart the inetd process and now you can telnet to port 7260 and get the info from the script. Now comes the fun part. MRTG was built do monitor network load and is only setup to receive 2 values (in/out) which we can use for our graph. I'm sure it could be modified to graph several values but I haven't dug into it that much yet. The mrtg format is: value1 value2 uptime hostname Using this format a little perl program is used to logon to a specified host and parse the stats info returning various combinations. I didn't convert the uptime so it currently only returns 0 for uptime. Note, this script needs Net::Telnet. mrtgnetload: ------------------------------------------ #!/usr/bin/perl use Net::Telnet (); sub usage() { print "Usage - mrtgnetload (host) (option) (linux|cpu#)\n"; print " (option) = load\n"; print " memory\n"; print " system_load\n"; print " user_load\n"; print " user_system_load\n"; print " idle_load\n"; print " cpu (defaults to cpu1)\n"; print " cpuuser (defaults to cpu1)\n"; print " cpusys (defaults to cpu1)\n"; exit 0; } $argc = @ARGV; if ( $argc < 2) { usage(); } $linux = 0; $host = $ARGV[0]; $option = $ARGV[1]; $show_cpu = 0; if ( $ARGV[2] ) { if ( $ARGV[2] eq "linux") { $linux = 1; } else { $show_cpu = $ARGV[2]; $show_cpu--; } } $port=7260; $t = new Net::Telnet (); $t->open(Host=>$host,Port=>$port,Timeout=>10); $count = 0; $cpu = 0; $line = $t->getline(); while ( ! $t->eof ) { if ( $count == 0 ) { ( $junk, $junk, $junk, $junk, $junk, $junk, $junk, $junk, $junk, $current_load, $junk, $junk) = split(" ", $line); $current_load =~ s/,//; $current_load = $current_load * 100; } elsif ( $count == 1) { ( $num_processes) = split(" ",$line); } elsif ( $count == 2 || $count == 3 ){ if ( $linux ) { ( $junk, $junk, $junk, $junk, $free_swap, $free_mem, $memory_cache, $junk, $junk, $junk, $junk, $junk, $junk, $user_load, $system_load, $idle_load ) = split(" ", $line); } else { ( $junk, $junk, $junk, $free_swap, $free_mem, $junk, $junk, $junk, $junk, $junk, $junk, $junk, $junk, $junk, $junk, $junk, $junk, $junk, $junk, $user_load, $system_load, $idle_load ) = split(" ", $line); } } else { ( $cpu_number[$cpu], $junk, $junk, $junk, $junk, $junk, $junk, $junk, $junk, $junk, $junk, $junk, $cpu_user, $cpu_system, $cpu_wait, $cpu_idle) = split(" ", $line); $cpu_user[$cpu_number[$cpu]] = $cpu_user; $cpu_system[$cpu_number[$cpu]] = $cpu_system; $cpu_wait[$cpu_number[$cpu]] = $cpu_wait; $cpu_idle[$cpu_number[$cpu]] = $cpu_idle; $cpu++; } $count++; $line = $t->getline(); } if ( $option eq "load" ) { print "$num_processes\n$current_load\n0\n$host\n"; } elsif ( $option eq "memory" ) { print "$free_swap\n$free_mem\n0\n$host\n"; } elsif ( $option eq "system_load" ) { print "$system_load\n$idle_load\n0\n$host\n"; } elsif ( $option eq "user_load" ) { print "$user_load\n$idle_load\n0\n$host\n"; } elsif ( $option eq "idle_load" ) { print "$num_processes\n$idle_load\n0\n$host\n"; } elsif ( $option eq "user_system_load" ) { print "$user_load\n$system_load\n0\n$host\n"; } elsif ( $option eq "cpu" ) { print "$cpu_user[$cpu_number[$show_cpu]]\n$cpu_system[$cpu_number[$show_cpu]]\n0\n$host\n"; } elsif ( $option eq "cpuuser" ) { print "$cpu_user[$cpu_number[$show_cpu]]\n$cpu_idle[$cpu_number[$show_cpu]]\n0\n$host\n"; } elsif ( $option eq "cpusys" ) { print "$cpu_system[$cpu_number[$show_cpu]]\n$cpu_idle[$cpu_number[$show_cpu]]\n0\n$host\n"; } else { usage(); } --------------------------------------- now all that needs to be done is to configure mrtg. Just follow the example below and create other entries for the info you want depending on the graph you want. #--------------------------------------------------------------- Target[hostname.load]: `/usr/local/bin/mrtgnetload hostname load` Options[hostname.load]: gauge, nopercent MaxBytes1[hostname.load]: 500 MaxBytes2[hostname.load]: 2500 Title[hostname.load]: System load of hostname PageTop[hostname.load]:System load of hostname YLegend[hostname.load]: System Load ShortLegend[hostname.load]: Legend1[hostname.load]: System Processes Legend2[hostname.load]: CPU Load X 100 LegendI[hostname.load]: Processes LegendO[hostname.load]: Load #--------------------------------------------------------------- have fun :) dmz
libnet-telnet-perl 需要安装。
相关阅读 更多 +