Use MRTG to monitor Linux system network, CPU, memory, and hard disk

Source: Internet
Author: User
Tags snmp perl script squid proxy mrtg

With the increasing popularity of Linux applications, a large number of network servers use the Linux operating system. To fully measure the network running status, you need to make more detailed and accurate measurements on the network status. The establishment of the SNMP protocol provides powerful support for Internet measurement. Multirouter Traffic Grapher (MRTG) is a typical network traffic statistical analysis tool based on SNMP. It consumes a small amount of system resources, so many plug-ins are also attached to mrtg. It obtains traffic information from the device through the SNMP protocol, and displays the traffic load to the user in HTML document containing JPEG format, which is very intuitive.
Working Principle
Total data traffic of network servers (total data transfer of network cards), CPU usage, and packet transfer rate (or traffic) of special services (such as Squid proxy service) it is a must for network administrators. When the CPU usage of the host is too high, the system may be unstable, so you need to pay attention to which service or who is trying to steal our data. Therefore, in terms of network management, it is necessary to understand the traffic status of our hosts and limit or increase bandwidth based on traffic.
We can download the MRTG software from the http://www.mrtg.org. Go to http://www.stat.ee.ethz.ch/mrtg/to get the output result of the mrtgtraffic monitoring chart.
MRTG is written in Perl and uses zlib, Gd, and PNG functions (zlib is used to compress charts and GD is used to draw charts ). Servers already contain the following software: Perl (perl-5.0xx or above), zlib (zlib-1.1.3-xx or above), Gd (gd-1.3.xx or above), LibPNG, and Apache.

Experiment Name: Use MRTG to monitor CPU and memory loads in Linux
OS: RedHat 9.0
Kernel used: linux-2.4.20
Experimental Module: mrtg-2.11.1.tar.gz
Author: Vitter
Function Description:
We know that MRTG is an excellent traffic monitoring tool that can monitor the traffic of many network devices, as long as this device supports the SNMP protocol. In addition, we can monitor the CPU and memory load of Linux systems.
Principle description:
Previously, I used MRTG to monitor network device traffic through SNMP. After completing this experiment, I learned more about mrtg. MRTG can provide powerful chart functions. If you can give him two numbers, remember to give them two numbers at regular intervals. MRTG can provide you with a line chart based on the two numbers you provided at regular intervals.
Here, we want to monitor the CPU and memory loads. Among them,
CPU monitoring includes:
CPU usage,
CPU idle rate;
Memory includes:
Memory usage,
Total amount of memory.
Therefore, when monitoring the CPU, the data we need to obtain is the CPU usage and the idle rate of the CPU. When monitoring the memory, the data we need to obtain is the memory usage and the total amount of memory. Okay, that's enough to know. Start the experiment.

1. Install MRTG
Because MRTG requires materials through the SNMP communication protocol, you must first install the corresponding software SNMP on Linux.
[Root @ intel root] # tar zxvf mrtg-2.11.1.tar.gz
[Root @ intel root] # cd mrtg-2.11.1
[Root @ intel mrtg-2.11.1] #./configure-Prefix =/usr/local/MRTG
[Root @ intel mrtg-2.11.1] # Make
[Root @ intel mrtg-2.11.1] # make install
2. Use MRTG to monitor the network:
Because MRTG Uses SNMP to monitor information, modify the SNMP configuration file.
[Root @ intel bin] # vi/etc/snmp/snmpd. conf
# View mib2 shortded .iso.org. DOD. Internet. Mgmt. mib-2 FC
To:
View mib2 shortded .iso.org. DOD. Internet. Mgmt. mib-2 FC
Then
Access notconfiggroup "" any noauth exact systemview none
To:
Access notconfiggroup "" any noauth exact mib2 none

Start the SNMP service
[Root @ intel bin] # service snmpd start
Next, we use the generator maker to generate the MRTG configuration file.
[Root @ intel bin] #. /producer maker -- Global "workdir:/usr/local/apache_1.3.31/htdocs/MRTG" -- Global "options [_]: growright, bits "-- output/usr/local/MRTG/etc/net1.cfg public@218.249.159.55
Global indicates which devices are valid (if there are multiple devices)
Workdir refers to the Access Directory of your Apache
Options is used to specify some specific options. Here, growright and bits are used to specify the default options configuration. For common applications, the default options configuration can meet the requirements.
Ifref indicates the options used to identify the device interface. Here, the IP address is used to identify the network device interface. Ifref can be specified as NR, IP, Eth, descr, and name. NR indicates using the ifindex of the interface interface interface in the mibii database to identify the interface; IP indicates using the IP address recognition interface; ETH indicates using the interface's physical address identification interface; descr indicates that the interface description is used to identify the interface; name indicates that the interface name is used to identify the interface. Generally, the IP address is unique, but in some cases the interface does not have an IP address, such as a switch. The NR (interface number) is unique for the interface. Therefore, you can use an IP address in general cases. In other cases, you need to use Nr.
-- Output/usr/local/MRTG/etc/net1.cfg indicates to generate the configuration file in the/usr/local/MRTG/etc directory.
Public@218.249.159.55 monitoring 218.249.159.55 using public as the community name through SNMP protocol to monitor the device 218.249.159.55.
OK, start MRTG In the last step.
[Root @ intel bin] #./MRTG/usr/local/MRTG/etc/net1.cfg
Then use indexmaker as the index file
[Root @ intel bin] #. /indexmaker -- Title = "traffic monitoring" -- output =/usr/local/apache_1.3.31/htdocs/MRTG/index.html/usr/local/MRTG/etc/net1.cfg
Then, Use http to access http: // 218.249.159.55/mrtg.
OK Network Monitoring complete

The following are CPU monitoring and memory monitoring.

3. obtain the required CPU and memory data.

1), to obtain the CPU usage and idle rate of the CPU
To obtain the CPU data, we use the SAR-u 1 3 command,
[Root @ intel zwz] # Sar-u 1 3
Linux 2.4.20-8 (Intel) 08/30/2005

05:46:16 CPU % USER % nice % System % idle
05:46:17 pm all 0.00 0.00 1.00 99.00
05:46:18 pm all 0.00 0.00 0.00 100.00
05:46:19 pm all 0.00 0.00 0.00 100.00
Average: All0.000.000.33 99.67
The SAR command is executed to generate the data shown in. We need three underlined data. Where:
CPU usage: % USER + % system = 0.00 + 0.33 = 0.33
CPU idle rate: % idle = 99.67
Well, only SAR does not work, because MRTG cannot identify the data. MRTG can recognize two data types, so we need to transmit 0.33 and 99.67 to MRTG, which is easy to implement, I wrote a script (CPU. PL) to obtain and output the two data: the script is as follows:

#! /Usr/bin/perl
System ("/usr/bin/SAR-u 1 3 | grep average> cpu_info_file"); # SAR output Write File cpu_info_file
Open (cpuinfo, "cpu_info_file"); # Open the cpu_info_file File
@ Cpuinfo = <cpuinfo>; # Read the file content
Close (cpuinfo); # close the file
Foreach $ line (@ cpuinfo) {# obtain the required
@ Cpustatus = Split (/+/, $ line); # each value
}
$ Cpuused = $ cpustatus [2] + $ cpustatus [4];
$ Cpuidle = $ cpustatus [5];
Print "$ cpuused/N"; # output two values
Print "$ cpuidle ";
System ("uptime ");
System ("uname-n ");
############## By Vitter: vitter@safechina.net #####################

Note: In the script, system ("/usr/local/bin/SAR-u 1 3 | grep average> cpu_info_file "), in this sentence, the full path of SAR must be written completely, instead of using system ("SAR-u 1 3 | grep average> cpu_info_file "). Because CPU. pl is called by MRTG, MRTG does not know your system path.
I put the CPU. pl script under/usr/local/MRTG/bin and execute CPU. pl To get the following results:

[Root @ intel bin] #./CPU. pl
0
100.00
Am up 1 day, 2 users, load average: 0.07, 0.12, 0.09
Trsb
Well, the values we need have been output. The next step is to hand over the values to MRTG, which is obtained through a configuration file, generally, this configuration file is generated by the MRTG producer maker command, but this time we need to write this configuration file (CPU. CFG): Put this configuration file under/usr/local/MRTG/etc. The content is as follows:
[Root @ intel etc] # vi CPU. cfg

Workdir:/usr/local/apache_1.3.31/htdocs/MRTG/CPU/
Target [localhost]: '/usr/local/MRTG/bin/CPU. pl'
X size [localhost]: 300
Y size [localhost]: 100
Ytics [localhost]: 10
Maxbytes [localhost]: 100
Title [localhost]: CPU state
Pagetop [localhost]: Shortlegend [localhost]: %
Ylegend [localhost]: CPU (%)
Legend1 [localhost]: Used
Legend2 [localhost]: Total
Legendi [localhost]: CPU used
Legendo [localhost]: CPU idel
Options [localhost]: growright, gauge, nopercent

Now we can execute MRTG:
[Root @ intel etc] #/usr/local/MRTG/bin/MRTG/usr/local/MRTG/etc/CPU. cfg
An alarm is triggered when the first execution is performed. If the execution is performed three times, no alarm is reported.

2) obtain the memory usage and total amount
To get the memory usage and total amount, I use the free-M command:
[Root @ intel etc] # Free-m
Total used free shared buffers cached
Mem:501 454470 71 234
-/+ Buffers/cache: 148 353
Swap: 1019 8 1010

Run the free command to generate the data shown in. We need two underlined data. Where:
Memory usage: 454 m
Total memory volume: 501 MB
Here we use a Perl script (MEM. pl) to obtain and output the two data. The script is as follows:

[Root @ intel bin] # vi mem. pl

#! /Usr/bin/perl
System ("/usr/bin/free-M | grep mem> mem_info_file ");
Open (meminfo, "mem_info_file ");
@ Meminfo = <meminfo>;
Close (meminfo );
Foreach $ line (@ meminfo ){
@ Memstatus = Split (/+/, $ line );
}
$ Memused = $ memstatus [2];
$ Memtotal = $ memstatus [1];
Print "$ memused/N ";
Print "$ memtotal/N ";
System ("uptime ");
System ("uname-n ");
########## By Vitter vitter@safechina.net ##################
I also set mem. put the pl script under/usr/local/MRTG/bin/. This script is called by mrtg. Now we can complete the corresponding configuration file (mem. CFG. place cfg in the/usr/local/MRTG/etc directory. The content is as follows:
[Root @ intel etc] # vi mem. cfg

Workdir:/usr/local/apache_1.3.31/htdocs/MRTG/MEM/
Target [localhost]: '/usr/local/MRTG/bin/MEM. pl'
X size [localhost]: 300
Y size [localhost]: 100
Ytics [localhost]: 10
Maxbytes [localhost]: 1006
Title [localhost]: memory state of Vitter-test Server
Pagetop [localhost]: Shortlegend [localhost]: B
Kmg [localhost]: m
Ylegend [localhost]: memory usage
Legend1 [localhost]: Used
Legend2 [localhost]: Total
Legendi [localhost]: Used
Legendo [localhost]: Total
Options [localhost]: growright, gauge, nopercent

Now we can execute MRTG:
[Root @ intel etc] #/usr/local/MRTG/bin/MRTG/usr/local/MRTG/etc/MEM. cfg
An alarm is triggered when the first execution is performed. If the execution is performed three times, no alarm is reported.

3) obtain the total disk space and usage
Run DF-KL to view disk space
[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
Total amount = 2063504 + 101089 + 8262068 + 2063504 + 2063504 + 2063536 + 17654736 = 34271941
Usage = 1169204 + 8127 + 2324004 + 1364412 + 1288716 + 350040 + 2964312 = 9469231
[Root @ intel bin] # vi DF. pl

#! /Usr/bin/perl
# This script was writdecade 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 ");

Edit the configuration file (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'
X size [localhost]: 300
Y size [localhost]: 100
Ytics [localhost]: 10
Title [localhost]: vfocus. Net server disk space (34,271,941 KB/36 GB) megabytes used
Unscaled [localhost]: dwym
Maxbytes [localhost]: 34271941
Pagetop [localhost]: 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
If the task is executed three times, no alert is reported.
Use indexmaker as the index file
[Root @ intel bin] #. /indexmaker -- Title = "hard disk monitoring" -- output =/usr/local/apache_1.3.31/htdocs/MRTG/DF/index.html/usr/local/MRTG/etc/DF. cfg
[Root @ intel bin] #. /indexmaker -- Title = "CPU monitoring" -- output =/usr/local/apache_1.3.31/htdocs/MRTG/CPU/index.html/usr/local/MRTG/etc/CPU. cfg
[Root @ intel bin] #. /indexmaker -- Title = "memory monitoring" -- output =/usr/local/apache_1.3.31/htdocs/MRTG/MEM/index.html/usr/local/MRTG/etc/mem. cfg
We add MRTG to crontab and let him regularly execute:
*/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

Now you can monitor the network, CPU, and memory. Here is my example: http://www2.vfocus.net/mrtg/ http://www2.vfocus.net/mrtg/cpu/ http://www2.vfocus.net/mrtg/mem/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.