Translated message

A translation of this page exists in English.

如何使用 MRTG 监控 Red Hat Enterprise Linux 上的 Mem 使用状态?

Solution In Progress - Updated -

Issue

Multi Router Traffic Grapher (MRTG)是一个监控网络链接上流量负载的工具。mrtg 生成包含 PNG 图像的 HTML 页面,这些图像提供此流量的可视化表示。

free 工具提供内存使用率输出。

以下是安装和配置 mrtg 的流程。在本例中,假设 IP 地址为 192.168.0.20

首先,确保使用 rpm -q mrtg 命令安装了 mrtg

现在,创建 MRTG 图形目录:

# mkdir -p /var/www/html/mrtg

启动 Apache 服务:

# service httpd start
# chkconfig httpd on

配置 mrtg 以分析内存使用率。在本文中,mrtg 将从 free 输出中获取数据。这可以通过一个使用命令 free 的简单脚本完成。换到 /var/lib/mrtg/ 目录,创建并打开文件 mrtg_mem.sh。在这个文件中,放入这些行:

#!/bin/bash
#run this script to check the mem usage.

usedmem=`/usr/bin/free | grep + | awk '{print $3}'`
freemem=`/usr/bin/free | grep + | awk '{print $4}'`
UPtime=`/usr/bin/uptime | awk '{print $3"" $4"" $5}'`

echo $usedmem
echo $freemem
echo $UPtime
echo 192.168.0.20

此脚本将显示 free 命令输出,它每三分钟执行一次。保存文件并使其可执行。

# chmod 755 mrtg_mem.sh

使用以下方法测试:

# ./mrtg_mem

如果脚本执行正确,三分钟后,会显示类似如下的信息:

938952
1078100
4days,5:57,
192.168.0.20

在同一目录中创建另一个脚本。

# vi mrtg.conf

此文件的内容应如下:

WorkDir: /var/www/html/mrtg/mem
Target[localhost]: `/var/lib/mrtg_mem.sh`
MaxBytes[localhost]: 2048000
kmg[localhost]: KB,MB
kilo[localhost]: 1024
Options[localhost]: gauge,nopercent,growright
YLegend[localhost]: Memory Usage:
Legend1[localhost]: Used Memory:
Legend2[localhost]: Free Memory:
LegendI[localhost]: Used Memory:
LegendO[localhost]: Free Memory:
Title[localhost]: Memory usage

执行以下命令:

# env LANG=C /usr/bin/mrtg /var/lib/mrtg/mrtg.conf

最后,创建一个如下的 cron 作业,以便 free 的结果在上述网页上每五分钟显示一次。

*/5 * * * * /usr/bin/env LANG=C /usr/bin/mrtg /var/lib/mrtg/mrtg.conf /dev/null 2>&1

通过进入 http://thissystemsip/mrtg/localhost.html 来测试这一点,这应该是一个带有系统内存使用率信息的网页。请注意,数据只在第 2 次数据收集中显示(第一次收集后 10 分钟)。在此之前,您将看到一个零和空的图形。

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content