Install MRTG in Linux to check CPU, network, and memory usage

Source: Internet
Author: User
Tags snmp mrtg

Preparations

# yum -y install gd-devel# yum -y install libmcrypt# yum -y install libmcrypt-devel# yum -y install freetype# yum -y install freetype-devel# yum -y install libtool-ltdl# yum -y install libtool-ltdl-devel# yum -y install libjpeg-devel# yum -y install libpng-devel# yum -y install libmcrypt# yum -y install libmcrypt-devel# yum -y install zlib-1.2.3

1. Install SNMP in yum

1.

# yum -y install net-snmp*

2. Modify/etc/snmp/snmpd. conf

# vim /etc/snmp/snmpd.conf

Find the following line:

# sec.name source community com2sec notConfigUser default public

Set public
Change to another name. To prevent hacker attacks, I changed it to jdeyes.

Remove the comment of the following line.

#view mib2   included  .iso.org.dod.internet.mgmt.mib-2 fc

Add the following row to row 55th:

view    systemview    included   .1.3.6.1.2.1.2

Set the following lines

access  notConfigGroup ""      any       noauth    exact  systemview none none

Modified:

access  notConfigGroup ""      any       noauth    exact  mib2 none none

3. Enable the snmpd service

# service snmpd start# chkconfig snmpd on

4. view the port enabling status

# netstat -tunlp |grep snmptcp        0      0 0.0.0.0:199             0.0.0.0:*               LISTEN      4973/snmpd          udp        0      0 0.0.0.0:161             0.0.0.0:*                           4973/snmpd    

Ii. install and configure MRTG

1. Download MRTG

# wget http://oss.oetiker.ch/mrtg/pub/mrtg-2.17.4.tar.gz

2. Install mrtg-2.17.4

# tar zxvf mrtg-2.17.4.tar.gz# cd mrtg-2.17.4 # ./configure --prefix=/usr/local/mrtg --sysconfdir=/etc/mrtg --with-gd=/usr/local/gd2/include --with-gd-lib=/usr/local/gd2/lib --with-gd-inc=/usr/local/gd2/include --with-png=/usr/local/include --with-png-lib=/usr/local/lib --with-png-inc=/usr/local/include --with-zlib=/usr/local/zlib/include --with-zlib-lib=/usr/local/zlib/include --with-zlib-inc=/usr/local/zlib/include# make# make install

3. basic configuration

Generate the main configuration file

# /usr/local/mrtg/bin/cfgmaker jdeyes@localhost > /etc/mrtg.cfg

Edit/etc/mrtg. cfg

# vim /etc/mrtg.cfg

Set

# WorkDir:/home/http/mrtg

Remove the comment and change it

Workdir:/home/htdocs/monitor/MRTG (here is the default home directory of your httpd)

Remove the comment of the following line

# Options[_]: growright, bits

Generate the MRTG webpage homepage File

# /usr/local/mrtg/bin/indexmaker /etc/mrtg.cfg --output=/home/htdocs/monitor/mrtg/index.html --title="MRTG"

4. Start MRTG

# env LANG=C /usr/local/mrtg/bin/mrtg /etc/mrtg.cfg

This command outputs some error information, which can be safely ignored. You can execute this command three times in a row.

5. The web pages generated by MRTG are static. to refresh the pages continuously, add the preceding commands to crontab.

# vim /etc/crontab 

Add the following line

*/5 * * * * root env LANG=C /usr/local/mrtg/bin/mrtg /etc/mrtg.cfg

Note: This row indicates that the refresh interval is refreshed every three minutes. You can modify the refresh interval as needed.

Restart the SNMP service:

# sudo /etc/init.d/snmpd restart

You can run the following command:

# snmpwalk -v 2c -c ferdinand localhost system

To check whether the installation is successful.

Iii. Example: memory usage monitoring

1. Create a folder to store scripts

# mkdir -pv /home/htdocs/monitor/mrtg/sh

2. Create a script file

# vim /home/htdocs/monitor/mrtg/sh/mrtg.memory

Add the following script:

#!/bin/bash# run this script to check the mem usage.totalmem=`/usr/bin/free |grep Mem |awk '{print $2}'`usedmem=`/usr/bin/free |grep Mem |awk '{print $3}'`UPtime=`/usr/bin/uptime | awk '{print $3""$4""$5}'`echo $totalmemecho $usedmemecho $UPtimeecho localhost

3. grant it the operation permission.

# chmod +x /home/htdocs/monitor/mrtg/sh/mrtg.memory

4. Example: CPU usage monitoring

Sysstat
Install with yum

# yum install sysstat

Sysstat installation package is: sysstat-5.0.5-1.i386.rpm, installed sysstat-5.0.5-1.i386.rpm
Then there will be iostat, mpstat, SAR, sa function, sysstat-5.0.5-1.i386.rpm

Start sysstat

# /etc/init.d/sysstat start

Set sysstat to start automatically

# /sbin/chkconfig sysstat on

1. Create a script file

# vim /home/htdocs/monitor/mrtg/sh/mrtg.cpu

Add the following script:

#! /Bin/bash # This program is a small external program used to compute CPU! #1. start to use SAR to reset the CPU user and system memory usage rate 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 localhost

2. grant it the operation permission.

# chmod +x /home/htdocs/monitor/mrtg/sh/mrtg.cpu

Edit the MRTG configuration file

# vim /etc/mrtg.cfg

Add

# Memorytarget [Memory]: '/home/htdocs/monitor/MRTG/SH/mrtg. memory 'maxbytes [Memory]: 4096000 title [Memory]: Memory usagesshortlegend [Memory]: & kmg [Memory]: kb, mbkilo [Memory]: 1024 ylegend [Memory]: memory usage: legend1 [Memory]: total memory: legend2 [Memory]: used memory: legendi [Memory]: total memory: legendo [Memory]: used memory: options [Memory]: growright, gauge, nopercentpagetop [Memory]: 

Now, MRTG has been installed and configured. You only need to configure an access address on nginx or Apache!
The final effect is as follows:







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.