| Subcribe via RSS

Cacti实现短信报警

8月 25th, 2008 | 6 Comments | Posted in Cacti < by John.Lv >

一、 软件环境:cacti(需Threshold插件),飞信机器人(fetion)
二、 安装过程:
Cacti的Threshold插件下载及安装配置参见:Cacti配置e-mail报警
飞信机器人(fetion)下载及安装配置参见:系统监控:linux命令行-飞信客户端发送免费报警短信
三、 配置过程:
1.按照Cacti配置e-mail报警配置并启用thold
2.编辑cacti/plugins/thold/thold-functions.php
在thold–functions.php查找thold_mail($global_alert_address, ”, $subject, $msg, $file_array);行在此行下面加入:

exec("echo $subject >>/var/www/html/cacti/plugins/thold/alter.log");
exec("/var/www/html/cacti/plugins/thold/sendsms.sh");

查找 thold_mail($item["notify_extra"], ”, $subject,
$msg, $file_array);行在此行下面加入:

exec("echo $subject >>/var/www/html/cacti/plugins/thold/alter.log"); #将报警信息记录到alter.log
exec("/var/www/html/cacti/plugins/thold/sendsms.sh"); #执行sendsms.sh

备注:本人的thold安装在/var/www/html/cacti/plugins/thold/目录中,如果注释掉thold_mail,只通过短信发送报警。不注释掉thold_mail,会通过email和短信同时发送报警

thold-functions.php生成的alter.log范例如下:

192.168.1.207 - Used Space - G: Label: [hdd_percent] is still above threshold of 85 with 99

编写脚本sendsms.sh自动调用飞信机器人发送报警

#!/bin/sh
#send sms by fetion
#Write by John.Lv
if [ ! -e "/var/www/html/cacti/plugins/thold/alert.log" ];then #判断alter.log是否存在
echo "Usage:alert.log does not exist"
exit
fi
if [ -n "`cat /var/www/html/cacti/plugins/thold/alert.log`" ]; then #判断alter.log是否有报警信息
admin="135xxxxxxxx"  #短信接收人,需在你飞信的好友列表中
echo "sms $admin "`cat /var/www/html/cacti/plugins/thold/alert.log` >>/var/www/html/cacti/plugins/thold/sms.txt #发送alter.log中的报警信息给admin
echo "exit" >> /var/www/html/cacti/plugins/thold/sms.txt #退出飞信
else
echo "Usage:no alert"
exit
fi
if [ -n "`cat /var/www/html/cacti/plugins/thold/sms.txt`" ] ; then
/var/www/html/cacti/plugins/thold/install/fetion -u 13512345678 -p "123456" -b /var/www/html/cacti/plugins/thold/sms.txt  -EN
#调用fetion发送短信,命令格式和参数说明参见:系统监控:linux命令行-飞信客户端发送免费报警短信
rm -f /var/www/html/cacti/plugins/thold/sms.txt 1>/dev/null 2>&1
rm -f /var/www/html/cacti/plugins/thold/alert.log 1>/dev/null 2>&1
else
echo "Usgae:no message to send"
exit
fi

ok,配置完成了,现在就可以通过短信接收报警信息了

阅读内文 Tags:

Cacti实现MSN报警

8月 22nd, 2008 | 6 Comments | Posted in Cacti < by John.Lv >

一、 软件环境:cacti(需Threshold插件),MSN机器人(SendMsg)
二、 安装过程:
Cacti的Threshold插件参见:Cacti配置e-mail报警
MSN机器人(SendMsg)参见:系统监控:msn在线机器人实时报警
三、 配置过程:
1.按照Cacti配置e-mail报警配置并启用thold
2.编辑cacti/plugins/thold/thold-functions.php
在thold–functions.php查找thold_mail($global_alert_address, ”, $subject, $msg, $file_array);行在此行下面加入:

exec("echo $subject >>/var/www/html/cacti/plugins/thold/alter.log");
exec("sh /var/www/html/cacti/plugins/thold/sendmessage.sh");

查找 thold_mail($item["notify_extra"], ”, $subject,
$msg, $file_array);行在此行下面加入:

exec("echo $subject >>/var/www/html/cacti/plugins/thold/alter.log");
exec("sh /var/www/html/cacti/plugins/thold/sendmessage.sh");

备注:本人的thold安装在/var/www/html/cacti/plugins/thold/目录中,如果注释掉thold_mail,只通过MSN发送报警。不注释掉thold_mail,会通过email和MSN同时发送报警

thold-functions.php生成的alter.log范例如下:

192.168.1.207 - Used Space - G: Label: [hdd_percent] is still above threshold of 85 with 99

编写脚本sendmessage.sh自动调用SendMsg发送报警

#!/bin/sh
echo `echo -n "date:"&&date +%Y-%m-%d-%H:%M` >> /var/www/html/sendMsg/msn.txt.1
#得到当前的日期+时间
cat /var/www/html/cacti/plugins/thold/alter.log >>/var/www/html/sendMsg/msn.txt.1
#读取alter.log的报警信息
now=`date +%Y-%m-%d-%H:%M`
SA=(disk)
msnaddr=(lvming104@hotmail.com) #报警接收人msn
sendMsg()
{
num=0
while [ $num -lt 1 ];
do
wget --post-data "sender=test@live.cn&password=12346&recipient=${1}&message=${2}" http://127.0.0.1/sendMsg/index.php -O /var/www/html/sendMsg/index.php.1 >/dev/null 2>&1
#sender:发送人msn,password:密码。/var/www/html/sendMsg为sendMsn安装目录
if [ -f /var/www/html/sendMsg/index.php.1 ]; then
if cat /var/www/html/sendMsg/index.php.1 |grep -i successfully >/dev/null 2>&1;then
num=1 #判断信息发送成功
elif cat /var/www/html/sendMsg/index.php.1 |grep -i "The user appears to be offline" >/dev/null 2>&1;then
num=1 #判断msn接受人为是否在线状态
echo "The user is offline."
exit 0
elif cat /var/www/html/sendMsg/index.php.1 |grep -i "Something went wrong trying to connect to the server" >/dev/null 2>&1;then
num=1 #判断msn 服务器存在连接问题
echo "MSN server is wrong."
exit 0
else
num=0 #除了以上三种情况退出循环外,其他情况重试。
fi
rm -f /var/www/html/sendMsg/index.php.1
else
num=0
fi
done
}
 
mv /var/www/html/sendMsg/msn.txt /var/www/html/sendMsg/bak/msn$now.txt -f 1>/dev/null 2>&1
mv /var/www/html/sendMsg/msn.txt.1 /var/www/html/sendMsg/msn.txt -f 1>/dev/null 2>&1
rm /var/www/html/cacti/plugins/thold/alter.log -f 1>/dev/null 2>&1

for i in 0
do
if cat /var/www/html/sendMsg/msn.txt ; then
messages=`cat /var/www/html/sendMsg/msn.txt ` #读取要发送的信息
sendMsg "${msnaddr[$i]}" "$messages" #发送报警信息
else
continue
fi
done

ok,配置完成了,现在就可以通过MSN接收报警信息了:如下所示

下次更新Cacti实现短信报警,敬请期待

阅读内文 Tags:

Cacti配置e-mail报警

8月 20th, 2008 | 10 Comments | Posted in Cacti < by John.Lv >

一、 软件环境:cacti(需Threshold插件)
二、 安装过程:
Cacti的安装各位baidu一下,这里就不详细介绍了。
安装Threshold插件
V0.3.9下载地址:http://mirror.cactiusers.org/downloads/plugins/thold-0.3.9.tar.gzv
安装Threshold插件,只需将解压出来的Thold目录拷贝到cacti/plugins/目录下,并修改cacti配置文件(cacti目录下的include/config.php)。在配置中查找$plugins = array();行,在此行下面加入:

$plugins[] = 'thold';

刷新cacti首页,你将看到多出来一个Threshld选项卡。如下所示

三、 配置过程:
1.设置Threshold插件:选中console选项卡,在左侧菜单中点击Settings,然后点击Alerting/Thold选项卡


2.设置发送警报通知:在Default Alerting Options中选择Send notifications
3.设置出现down机的情况时(即Monitor出现如图所示 )发送警报通知:选择Dead Hosts notifications
报警邮件范例如下:

Host Error : 192.168.1.220 (192.168.1.220) is DOWN
Message : Host did not respond to SNMP           (down机报警)
Host Notice : 192.168.1.207 (192.168.1.207) returned from DOWN state   (恢复)

4.设置警报发送纯文本的电子邮件没有图(默认是HTML电子邮件与图表嵌入在电子邮件):选择Send alerts as text
5.设置周末不执行报警检查:选择Weekend exemptions
6.设置报警的门槛:Default Trigger Count
7.设置Emailing Options

Mail Services:SMTP。(可以选择PHP mail,sendmail,SMTP三种方式)
From Email Address:按实际设置
From Name:按需要设置(该处显示的name就是email的发件人姓名)

8.设置Sendmail Options(Mail Services设置为sendmail需设置此项)

Sendmail Path:/usr/sbin/sendmail(路径是服务器上Sendmail的,仅用于邮件服务选定为Sendmail)

9.设置SMTP Options(Mail Services设置SMTP需设置此项)

SMTP Hostname:SMTP服务器如:mail.test.com
SMTP Port:25
SMTP Username: 通过SMTP发送邮件用来验证的用户名,如果你不要求身份验证留空
SMTP Password:通过SMTP发送邮件用来验证的密码,如果你不要求身份验证留空

测试:点击Send a Test Email,测试成功如下所示:

收到的测试邮件范例:

This is a test message generated from Cacti. This message was sent to test the configuration of your Mail Settings.
Your email settings are currently set as follows
Method: SMTP
Host: mail.ips.com.cn
Port: 25
Authenication: true
Username: john.lv
Password: (Not Shown for Security Reasons)

配置完成后别忘记save。
下面我以磁盘空间报警为例,来设置具体的报警信息(CPU,DISK,服务,网络等等)
在console选项卡下左侧菜单中点击Threshold Templates 。

在如下图所示中点击Add按钮来添加一个Threshold模板

添加磁盘监控的模板,无论是linux还是windows都是使用的Host MIB

设置按照百分比来监控的,在Data Field选择hdd_percent,可以选择hdd_total,hdd_used,hdd_percent三种

设置以下信息(参照下图:)
选择Enabled 激活检查和报警提醒
High Threshold文本框中填入0.085,意思是当硬盘使用率超过85%时,发送邮件报警。
Threshold CDEF下拉框选择Divide by 1024,
Alert E-Mail填入报警的接收邮箱
完成Threshold模板的添加。

应用刚才创建的Threshold模板
在console选项卡下点击Management ->Devices,选择要应用此模板的Host,点击最上面的Create Graphs for this Host链接,然后点击Auto-create thresholds链接来应用刚创建的Threshold模板。

设置完成后在console选项卡下左侧菜单中点击Thresholds

在此可以管理已创建的警报,如下:

点击threshld选项卡可以查看警报的触发情况,如下:

磁盘报警邮件范例:

Host: 192.168.1.207 (192.168.1.207)
URL: http://192.168.1.211/cacti//graph.php?local_graph_id=&rra_id=1
Message: 192.168.1.207 - Used Space - D: Label:tool [hdd_percent] went
above threshold of 0.085 with 0.0896

Ok,磁盘空间的邮件报警就正式配置完成了。

阅读内文 Tags:

[转载]RRDTool 中文手册-简易入门(二)

8月 19th, 2008 | No Comments | Posted in Cacti, RRDTool, 系统监控, 读书笔记 < by Michael Field >

三、RRD数据库更新实例

    在简易入门(一)中已经介绍过update 命令:它使用1个或多个参数,其格式为: : . 如果你知道可以用一个 N 表示当前的时间,你一定会高兴的。或者你可以用Perl中的 time 函数来指定时间:
    perl: perl -e ‘print time, “\n” ‘
    shell: date +%s
    如何按照固定间隔运行某个程序各操作系统不同。不过可以用伪码来表示:
    - Get the value and put it in variable “$speed”
    - rrdtool update speed.rrd N:$speed

    就这些。每5分钟运行一次上面的脚本。在你想知道图像看起来是啥样时,运行上面的例子。你可以把他们放入一个脚本中。运行此脚本后,查看我们刚才创建的graph。

四、SNMP相关知识

    我可以想像到只有少数的人能够每5分钟从他们的汽车中读取一次真实的数据。其他人不得不清算其他类型的计数器。你可以测量打印机打印的页数,咖啡机做的咖啡杯数,计算使用的电流的设备,都可以。递增的计数器可以被监视,并且用你已经学会的方法被绘制成图像。稍后我们就能够监视像温度计这样的值了。
    大多数对RRDtool感兴趣的人会使用一个跟踪网络设备传输数据量的计数器。这样我们下一步就来作这个。我们会从解释如何收集数据开始。

    有些人会说有些工具可以帮助你收集数据。他们是对的!不过,我感觉理解这些工具不是必须的非常重要。在你能够确定为什么事情发生了错误,你要知道他们是如何工作的。

    本例中使用的工具在本文档前面非常简短地提到过,它就是所谓的SNMP。它是与联网设备交谈的方式。

下面用到的工具名为 snmpget ,以下是关于它是如何工作的说明:

snmpget device password OID

snmpget -v[version] -c[password] device OID
    对于device, 你要用设备的名称或者IP地址来替换。对于password, 你需要使用SNMP领域中称为 comunity read string 来替换。对于某些设备来说,缺省的 public 可以工作,但是该设置可能会被关闭,调整或者由于安全和机密的原因而被保护起来。请阅读你的设备或程序的文档。

    接下来有一个称为OID的参数,它用来表示 对象标识符 。

    刚开始学习SNMP时,它看起来有些令人困惑。在你看到MIB管理信息基时,就不会那么困难了。MIB是用来描述数据的倒状树,它有只有一个根结点,并且由根结点开始有多个分支。这些分支都以另一个结点终结,他们继续向下分支,如此继续。所以的分支都有一个名称,它们构成了一个我们能够沿着往下的路径。我们所沿着的这些分支都被命名了:iso, org, dod, internet, mgmt 和mib-2.这些名称也可以用数字方式记录,就像 1 3 6 1 2 1。
    iso.org.dod.internet.mgmt.mib-2 (1.3.6.1.2.1)

    有些程序会使用前导点 . , 令人感到许多困惑。在一个OID中并 没有前导点。为了显示OID缩记法和OID完整记法的区别,(通常约定)在使用OID的完整记法时使用前导点。通常这些程序在返回数据给你时,会省略却缺省的部分。这些程序有的有几个缺省前缀,这会让事情显得更加糟糕。
    Ok, lets continue to the start of our OID: we had 1.3.6.1.2.1 From there, we are especially interested in the branch interfaces which has number 2 (e.g., 1.3.6.1.2.1.2 or 1.3.6.1.2.1.interfaces).

    好了,我们继续OID的初步学习:我们有一个1.3.6.1.2.1的OID,我们对(其下面的) interface 分支特别感兴趣,其编号为2(例如:1.3.6.1.2.1.2 或 1.3.6.1.2.1.interface)。

    首先,我们得要有几个SNMP的程序。先看看你的OS平台上是否有已编译好的软件包。这是最便捷的方式。如果没有,你就得下载源代码、并编译。互联网上到处都是源代码、程序。你可以用你喜欢的任何方式、搜索引擎来找到相关的信息。

    假设你已经有了这些程序。先试着收集大多数系统都有的数据。记住:那些引起我们兴趣的树,它上面的部分都有一个简略名。

    在此我会给出一个在Fedora Core 3操作系统上可用的例子。如果对你的操作系统不可用,请查看snmp的手册,并作相应的调整让它能够运行。

snmpget -v2c -c public myrouter system.sysDescr.0
    该设备应当回应其自身的描述,该描述可能是一个空的。只有在你从某个设备获得响应后,你才能够继续。其中可能需要使用不同的口令、或者不同的设备。
snmpget -v2c -c public myrouter interfaces.ifNumber.0

    最好你得到的结果是一个数字。如果这样的话,你就可以继续往下,并试试另一个叫做 snmpwalk 的程序。

snmpwalk -v2c -c public myrouter interfaces.ifTable.ifEntry.ifDescr
    如果该命令返回一系列的接口,这就对了。以下是该命令运行的返回结果的例子:
[user@host /home/alex]$ snmpwalk -v2c -c public cisco 2.2.1.2
interfaces.ifTable.ifEntry.ifDescr.1 = "BRI0: B-Channel 1"
interfaces.ifTable.ifEntry.ifDescr.2 = "BRI0: B-Channel 2"
interfaces.ifTable.ifEntry.ifDescr.3 = "BRI0" Hex: 42 52 49 30
interfaces.ifTable.ifEntry.ifDescr.4 = "Ethernet0"
interfaces.ifTable.ifEntry.ifDescr.5 = "Loopback0"
    对于cisco设备,我想监视 ‘Ethernet0` 接口,从上面的输出结果能够看到该接口的编号是 4。 我试着运行如下命令:
[user@host /home/alex]$ snmpget -v2c -c public cisco 2.2.1.10.4 2.2.1.16.4

interfaces.ifTable.ifEntry.ifInOctets.4 = 2290729126
interfaces.ifTable.ifEntry.ifOutOctets.4 = 1256486519
    这样,我就有两个OID需要监视,他们是(这次使用完整记法):
    1.3.6.1.2.1.2.2.1.10

    1.3.6.1.2.1.2.2.1.16
    这两个OID都有接口编号4。
    别被糊弄了,我可不是试一次就搞定的。我花了一些数据来搞清这些数字都是什么意思。把这些编号转换成描述文字大有帮助… 至少在大家谈论MIB和OID时,你知道他们是什么东西。别忘了接口编号(如果它不是附属接口,就是0),如果用snmpget没有得到响应,试试 snmpwalk。

五、应用RRDtool的实例

    实际运用的例子
    开始有趣的东东吧。首先,创建一个新的数据库。它包含输入和输出2个计数器的数据。该数据被放入能够进行平均的归档中。他们一次使用1、6、24、或 288个样本。他们同时被存入保存最大数字的归档中。稍后会解释。样本间隔时间为300秒,也就是5分钟,这是一个好的开始。:

    1个样本 “平均” 保留5分钟的周期
    6个样本 每30分钟进行一次平均
    24个样本 每2小时进行一次平均
    288个样本 每1天进行一次平均

    我们试着与MRTG互相兼容,MRTG存储以下数据::

    600 5分钟样本数: 2天和2小时
    600 30分钟样本数: 12.5天
    600 2小时样本数: 50天
    732 1天样本数: 732天

    这些范围被补充进去,因此在数据库中保存的数据总数大约797天。RRDtool存储不同的数据,它不会在 每日 归档停止的地方开始 每周 归档。对于这两个归档,最新的数据会是在 now 附近,因此我们需要比MRTG保存更多的数据!
    我们需要::

    600个5分钟的样本 (2天和2小时)
    700个30分钟的样本 (2天和2小时,加12.5天)
    775个2小时的样本 (上述+50天)
    797个1天的样本 (上述+732天,环型回绕最大797)

rrdtool create myrouter.rrd \
DS:input:COUNTER:600:U:U \
DS:output:COUNTER:600:U:U \
RRA:AVERAGE:0.5:1:600 \
RRA:AVERAGE:0.5:6:700 \
RRA:AVERAGE:0.5:24:775 \
RRA:AVERAGE:0.5:288:797 \
RRA:MAX:0.5:1:600 \
RRA:MAX:0.5:6:700 \
RRA:MAX:0.5:24:775 \
RRA:MAX:0.5:288:797

接下来要做的就是收集数据并把它保存起来。以下是一个例子。它是用伪码写的,你得根据OS调整后让它能够运行。:

while not the end of the universe
do
get result of
snmpget router community 2.2.1.10.4
into variable $in
get result of
snmpget router community 2.2.1.16.4
into variable $out

rrdtool update myrouter.rrd N:$in:$out

wait for 5 minutes
done

在收集了1天的数据后,试着用下面的命令创建图像::

rrdtool graph myrouter-day.png --start -86400 \
DEF:inoctets=myrouter.rrd:input:AVERAGE \
DEF:outoctets=myrouter.rrd:output:AVERAGE \
AREA:inoctets#00FF00:"In traffic" \
LINE1:outoctets#0000FF:"Out traffic"

这会产生一个具有1天流量值的图像。1天有24(小时)x60(分钟)x60(秒)。我们从当前时间-86400秒开始。我们用DEF把输入和输出字节数定义成myrouter.rrd数据库中的的平均值,并且绘制输入流量区和输出流量线。

阅读内文 Tags: , , ,

[转载]RRDTool 中文手册-简易入门(一)

8月 18th, 2008 | No Comments | Posted in Cacti, RRDTool, 系统监控, 读书笔记 < by Michael Field >

一、简介

    RRDtool是由Tobias Oetiker 编写并由全球各地的许多人贡献的工具。本篇文档的作者是Alex van den Bogaerdt 主要是帮助你理解RRDtool是什么,它能够帮助你作些什么。 RRDtool的文档对于有些人来说过于技术化。本教程帮助你理解RRDtool的基本概念。它为你自学RRDtool的文档做好准备。本文档还重点介绍了网络统计方面的知识。


1、RRDtool是什么东西?

    RRDtool是指Round Robin Database 工具(环状数据库)。Round robin是一种处理定量数据、以及当前元素指针的技术。想象一个周边标有点的圆环--这些点就是时间存储的位置。从圆心画一条到圆周的某个点的箭头--这就是指针。就像我们在一个圆环上一样,没有起点和终点,你可以一直往下走下去。过来一段时间,所有可用的位置都会被用过,该循环过程会自动重用原来的位置。这样,数据集不会增大,并且不需要维护。RRDtool处理RRD数据库。它用向RRD数据库存储数据、从RRD数据库中提取数据。


2、RRD中可以存放什么样的数据?

    可以适合时间序列的数据。就是说你必须能够在时间的几个点上度量某些值,并提供这些信息给RRDtool。如果你能够做到这一点,RRDtool就能够存储它们。这些数值必须是数字,但是不一定要是整数,在与MRTG合用时。(下一节会给出更详细的介绍)。
    下面的例子是关于SNMP的,SNMP是简单网络过来协议的缩写。简单是指协议简单--并不表示管理或监视网络简单。读完本篇文档后,你应当能够对人们谈论的SNMP的东西有更多的理解。现在,只要知道SNMP可以用来查询设备中保持的计数器的值就可以了。我们要存放到RRD数据库中的正是这些计数器中的数值。


3、RRDtool可以用来干什么?

    RRDtool源自MRTG(多路由器流量绘图器)。MRTG是有一个大学连接到互联网链路的使用率的小脚本开始的。MRTG后来被当作绘制其他数据源的工具使用,包括温度、速度、电压、输出量等等。
    很可能你会从使用RRDtool来存储和处理通过SNMP收集到的数据开始。这些数据很可能是某个网络或计算机接收或发送的字节数(比特数)。它也可以用来显示潮水的波浪、阳光射线、电力消耗、展会的参观人员、机场附近的噪音等级、你喜欢的度假区的温度、电冰箱的温度、以及任何你可以想象的东西。
    你最需要一个度量数据,以及能够提供这些数据给RRDtool的感应器就可以了。RRDtool会让你创建数据库、存储数据、提取数据、创建用于在Web浏览器中显示的PNG格式的图像。这些PNG图像以来于你收集的数据,它可以是网络平均使用率、峰值。


4、问题探讨

    首先:再读一遍!你可能漏了某些地方。如果你无法编译源代码,而且你的操作系统相当普遍,很可能不是RRDtool的错误。互联网上有已经编译好的版本。如果来自可信赖的网站,就下载一个。
    另一种情况是程序能够工作,但是没有得到预期的结果,可能是配置方面的问题。检查你的配置,与后面例子的配置比较一下。
    RRDtool有一个邮件列表及其归档。读一下几周的列表并搜索归档。没有搜索过归档就提问是很粗鲁的做法:你遇到的问题可能已经由其他人解决了!尽管不是全部,大多数问题都会这样,邮件列表不是经济为了某个参与者服务的。看看RRDtool的文档了解邮件列表的位置和使用方法。
    我建议你马上就花点时间订阅邮件列表,只要发送一封主题为 subscribe 的邮件到 rrd-user-request@list.ee.ethz.ch 就可以了。如果要退定该邮件列表,只需要发送主题为 unsubscribe 到同样的地址即可。


二、学习实例
1、第一个RRD数据库

    在我看来,学习某个东西的最好办法就是实践。为什么现在不开始呢?我们会创建一个数据库,放一些数值到它里面,然后提取这些数据。你的输出应当与本文档中的输出是一样的。
    我们会从一些简单的入手,然后把汽车与路由器比较,或者将公里和比特、字节数比较。他们都是一样的:都是某些时段的某些数值。
    假设我们有一个向互联网发送数据和接收数据的设备。该设备保留一个计数器,该计数器在开启设备时设置为0,并在每传送一个字节就加1。该计数器可能会有一个最大值。如果该值达到最大时,在加一个字节的计数,该计数器就会再次从0开始。这与世界上的许多计数器都是一样的,比如车辆上的里程计数器。
    关于网络的讨论通常用每秒比特数来衡量,因此我们要习惯这种用法。把一个字节看成是8个比特,并且开始用比特而不是字节来思考问题。不过,计数器仍然用字节数为单位来计量!在SNMP世界里,大部分的计数器都是32比特的。这就意味着他们计数范围是0-4294967。我们在例子里会用到这些数值。该设备在被查询时,会返回计数器的当前值。我们知道从上次查询设备开始到现在的时间,因此我们现在就知道每秒 平均传输了多少字节数。这不难计算。首先用文字来描述,然后计算:

用当前查询到的计数器值,减去上一次查询的计数器值
把当前查询时间和上次查询时间作上述同样操作(秒)
将(1)的结果除以(2)的结果,得到的结果就是每秒的字节数。乘以8就得到每秒的比特数(bps)

  • 实例一
    • 你现在正在开车。12:05时你看了一下仪表盘上的里程计数器,它显示这俩车已经行驶了12345公里。12:10分时,你有看了一下里程计数器,它显示 12357公里。这表示你在5分钟内行驶了12公里。科技人员会把它转换成米/秒,这样可以更好的进行比较(每5分钟的字节数)和(每秒比特数)。
      我们行驶了12公里,也就是12000米。我们在5分钟内,或者说是300秒内完成。我们的速度是1200米/300秒,或者说是40米/秒。
      我们可以用公里/小时来计算速度:12乘以5分钟就是一个小时,因此我们必须把12公里乘以12得到144公里/小时。对于和我一样说英语的朋友来说,就是90英里/小时,因此不要在家里或我生活的地方尝试这个速度 :)
      记住:这些数值都仅仅是平均值。如果无法从数字中得到你是否以固定的速度在行驶。本教程后面有一个例子说明这个问题。我希望你理解在计算米/秒或者比特/秒。唯一的差别在于收集数据的方式。即使是K这个单位也是一样的,因为在网络术语中,K同样表示1000。
      我们现在要创建一个数据库,在此数据库中我们能够保存所有这些有趣的数字。启动这个程序的方法可能在各个操作系统上各不相同,但是我假设你可以搞清楚它是否与你的操作系统不同,键入下面的行作为一个长长的行(为了可读性,我得把他分成几行)并且用 \ 来分割。
    rrdtool create test.rrd \
    --start 920804400 \
    DS:speed:COUNTER:600:U:U \
    RRA:AVERAGE:0.5:1:24 \
    RRA:AVERAGE:0.5:6:10


    2、创建了什么东西?

      我们创建的rrd数据库名为test (test.rrd),它的起始时间是我写这篇文档的当天下午,也就是1999年3月7日(该日期转换成920804400秒)。我们的数据库存放一个名为 ’speed’ 的数据源(DS),它表示一个计数器。该计数器每5分钟(缺省)读取一次。在同一个数据库中,保存有2个环状归档(RRA),一个是每次读取时的平均数据(例如:没有东西进行平均)并保留24个样本(24乘以5分钟是2小时)。另一个RRA有6个平均值(半小时)并包含10个这样的平均值(例如5个小时)。

      RRDtool使用来源于UNIX世界的特殊时间戳。该时间戳是自1979年1月1日UTC时间开始到当前逝去的秒数。该时间戳的值被转换成本地时间,它在不同的时区会不一样。
      可能你不是和我在地球的同一个地方。这就是说时区不同。在所有的例子中,我所说的时间当中,小时可能对你来说是错误的。这对这些例子中的结果有一点影响,在阅读时,只需要修正时间中的小时即可。例如:我看到 12:05 的话,在英国的家伙看到的时间就是 11:05 。

    现在我们得向数据库中填入一些数字。我们希望读到以下数据:
    12:05 12345 KM
    12:10 12357 KM
    12:15 12363 KM
    12:20 12363 KM
    12:25 12363 KM
    12:30 12373 KM
    12:35 12383 KM
    12:40 12393 KM
    12:45 12399 KM
    12:50 12405 KM
    12:55 12411 KM
    13:00 12415 KM
    13:05 12420 KM
    13:10 12422 KM
    13:15 12423 KM

  • 我们用下面的命令把上述数据填入数据库:
  • rrdtool update test.rrd 920804700:12345 920805000:12357 920805300:12363
    rrdtool update test.rrd 920805600:12363 920805900:12363 920806200:12373
    rrdtool update test.rrd 920806500:12383 920806800:12393 920807100:12399
    rrdtool update test.rrd 920807400:12405 920807700:12411 920808000:12415
    rrdtool update test.rrd 920808300:12420 920808600:12422 920808900:12423

    正如你说看到的那样,可以在一个命令里向数据库中填入多个值。为了可读性,我得只用三个数据,实际一行里运行的最大数据与操作系统相关。
    我们可以使用 rrdtool fetch 命令从数据库中提取数据。

    rrdtool fetch test.rrd AVERAGE --start 920804400 --end 920809200
      该命令会返回如下输出结果:
      speed

      920804700: nan
      920805000: 4.0000000000e-02
      920805300: 2.0000000000e-02
      920805600: 0.0000000000e+00
      920805900: 0.0000000000e+00
      920806200: 3.3333333333e-02
      920806500: 3.3333333333e-02
      920806800: 3.3333333333e-02
      920807100: 2.0000000000e-02
      920807400: 2.0000000000e-02
      920807700: 2.0000000000e-02
      920808000: 1.3333333333e-02
      920808300: 1.6666666667e-02
      920808600: 6.6666666667e-03
      920808900: 3.3333333333e-03
      920809200: nan
      如果不是像上面的输出结果,可能哪里有错误。也许你的操作系统会打印出不同的格式 NaN 。 NaN 表示 非数字 。 如果你的操作系统输出 U 或 UNKN 或者其他类似东西都是正常的。如果其他地方错误,可能是因为你的过程中的那些步骤出错了(当然假设我的教程是完全正确的 :-))。这样的话,删除数据库文件然后再重新尝试。 有时事情就会变化。本例哟难道的数据像 0.04 而不是 4.0000e-02 。这些实际上是一样的数字,只是写法不同而已。如果rrdtool今后的版本显示略有不同是输出也不要大惊小怪。本文档中的例子对于RRDtool 1.2.0版本都是正确的。


    3、绘第一张图

    建几个图示的时候到了,试试下面的命令:

    rrdtool graph speed.png \
    --start 920804400 --end 920808000 \
    DEF:myspeed=test.rrd:speed:AVERAGE \
    LINE2:myspeed#FF0000

  • 如图一

    • 该命令会创建名为speed.png的图像文件,该图像从12:00开始,到13:00。有一个名为myspeed的变量定义,它使用来自 test.rrd数据库的 speed RRA中的数据。绘制的线条是2像素高,表示myspeed变量。颜色是红色的.
      你会注意到图像的起始不是12:00而是12:05。这是因为在此时间之前的数据不够计算出平均值。这只会在缺少某些样本的情况下发生,不会经常发生。
      颜色是由红、绿、蓝构成的。对每种颜色成分,你可以用16进制来表示使用多少,其中00表示不包含,FF表示完全包含。白色是由红、绿、蓝组成的:FFFFFF。黑色是全部不包含:000000。


    4、用几种数学方法来绘图

      查看图像时,你会注意到横轴下标为 12:10、12:20、12:30、12:40、12:59。有时某些下标不适合(可能是12:00和13:00)会被忽略掉。
      纵轴显示我们输入的范围。下面提供的公里数,以及除以300妙的结果,我们得到非常小的数值。为了更加精确,第一个值是12(12567-12456),除以300后得到0.04,RRDtool显示时为 40m 表示 40/1000 。 其中的 m 与米、公里、或者毫米都没有任何关系!RRDtool不知道我们的数据单位,它只处理没有单位的数据。
      如果我们用米来衡量我们的距离,就会是这样:(12′357′000-12′345′000)/300 = 12′000/300 = 40.
      因为许多人都对这样的数值范围感觉更好,我们就来修正一下。我们将重新创建数据库,并存储正确的数据。但是有更好的办法:在创建png文件时进行一些计算!
    rrdtool graph speed2.png \
    --start 920804400 --end 920808000 \
    --vertical-label m/s \
    DEF:myspeed=test.rrd:speed:AVERAGE \
    CDEF:realspeed=myspeed,1000,\* \
    LINE2:realspeed#FF0000

  • 如图二


  • 注意:不要忘记操作符 * 后面的 。这个反斜杆用来将*从操作系统可能解释的符号转义,而不是直接传递给rrdtool命令。
    在查看PNG文件后,你会注意到 m 不见了。正确的结果就是这样。同样,在图像中加入了一个标注。出了上面提到的几点外,PNG看起来应当是一样的。

      计算是在CDEF部分中指定的,使用逆波兰表达式( RPN )表示的。我们要求RRDtool所作的事情是:`取数据源myspeed, 以及数值1000;把他们相乘` 。在此不要被RPN表达式困扰了,后面会详细介绍。同时,你可能想读一读关于CDEF的教程。以及Steve Rader的RPN表达式的教程。不过哦首先看我这篇教程吧。
      如果我们用1000乘以这些数值,显示把同样的数据显示成公里/小时也是可以的。
      要修改米/秒为单位的值:
      计算米/小时: value * 3′600
      计算公里/小时: value / 1′000
      合起来: value * (3′600/1′000) 或者 value * 3.6

    下面我们来创建这个PNG文件,并加入更多的魔幻功能…

    rrdtool graph speed3.png \
    --start 920804400 --end 920808000 \
    --vertical-label km/h \
    DEF:myspeed=test.rrd:speed:AVERAGE \
    "CDEF:kmh=myspeed,3600,*" \
    CDEF:fast=kmh,100,GT,kmh,0,IF \
    CDEF:good=kmh,100,GT,0,kmh,IF \
    HRULE:100#0000FF:"Maximum allowed" \
    AREA:good#00FF00:"Good speed" \
    AREA:fast#FF0000:"Too fast"

  • 如图三


  • 这个图像看起来更好。速度用KM/H表示,有一个附加的线条表示最大允许的速度(在我行驶的道路上的最大限速)。我还修改了速度的显示颜色,把它从线条改为区块。


    5、一个更复杂的图例

      现在计算更加复杂一些。对于在限速内的速度衡量方法是:
      检查公里/小时是否大于100 ( kmh,100 ) GT
      如果是,返回0,否则返回公里/小时。 ((( kmh,100 ) GT ), 0, kmh) IF

      对于上述的限速值:
      检查公里/小时是否大于100 ( kmh,100 ) GT
      如果是,返回公里/小时,否则返回0。 ((( kmh,100) GT ), kmh, 0) IF

    我愿意相信RRDtool的绘图功能能够处理的数据没有任何虚拟的限制。我会解释他们是如何工作的,不过看看下面的PNG图像:

    rrdtool graph speed4.png \
    --start 920804400 --end 920808000 \
    --vertical-label km/h \
    DEF:myspeed=test.rrd:speed:AVERAGE \
    "CDEF:kmh=myspeed,3600,*" \
    CDEF:fast=kmh,100,GT,100,0,IF \
    CDEF:over=kmh,100,GT,kmh,100,-,0,IF \
    CDEF:good=kmh,100,GT,0,kmh,IF \
    HRULE:100#0000FF:"Maximum allowed" \
    AREA:good#00FF00:"Good speed" \
    AREA:fast#550000:"Too fast" \
    STACK:over#FF0000:"Over speed"

  • 如图四

  • 第一部分完
    本文从网上转载,本人重新整理和附图,出处未考证。

    阅读内文 Tags: , , ,

    cacti数据备份与迁移

    8月 8th, 2008 | No Comments | Posted in 1.系统服务, Cacti < by Michael Field >

    原来的监控服务器因为是在电信机房的所以很多网通的被监控主机有很多timeout,而且随着被监控服务器的不断增加存在问题也越来越显著。

    一、工作流
    安装mysql;php;apache;rrdtool后;
    将cacti目录tarball后,复制到双线机房新监控服务器上解压;
    cactid目录tarball后,复制到双线机房新监控服务器上解压;
    mysql数据库dump或者tarball后,复制到双线机房新监控服务器上解压;

    #mysqldump -p cacti >cacti.sql

    重新赋予cactiuser对于数据库cacti所有权限;

    grant all on cacti.* to cactiuser@localhost identified by 'cactipassword';

    二、故障发生
    打开cacti可以正常登录和控制,检查graph management,打开所有的图无显示;于是打开debug模式:
    *Turn On Graph Debug Mode.

    RRDTool Says:
        ERROR: This RRD was created on other architecture

    三、解决方案
    google、baidu了一下,发生这个问题的原因是因为所有rrd库需要全部重新dump和restore一次;
    好了上脚本:
    从老的cacti服务器上dump所有的rrd文件为xml file,共计9K多张:

    #!/bin/sh

    rrddump="/usr/bin/rrdtool dump"
    xmldir=/tmp/rraxml

    for file in `find /var/www/html/cacti/rra/ -mtime -2 |awk -F\/ '{print $NF}'`
    #遍历rra目录,查找近2天修改过的rrd文件;
      do
        ${rrddump} ${file} > ${xmldir}/${file}.xml
      done

    tar走rraxml目录到新监控服务器上解压:restore脚本如下:

    #!/bin/sh
    rrdrestore="/usr/bin/rrdtool restore"
    xmldir=/tmp/rraxml/
    for file in `find ${xmldir} |awk -F\/ '{print $NF}' |sed s/.xml//g`
    #遍历xml目录,并去掉xml后缀,直接还原到rra目录下
      do
        ${rrdrestore} ${xmldir}${file}.xml /cacti/rra/${file}
      done
    chown cactiuser:cactiuser /cacti/rra -R

    完成后,再次打开cacti监控显示正常;迁移成功;
    用这个方法其实是用来备份rra的,这次正好顺便备份一下;

    阅读内文 Tags: , , ,

    cacti进阶应用三: IDC 质量监控-评估节点的网络品质

    8月 8th, 2008 | 2 Comments | Posted in 1.系统服务, Cacti < by Michael Field >

    一、实际需求
    公司租用多个idc机房,为了实时的和历史的查看idc网络状态,记录该节点的网络流量和做分析统计使用,都具有许多现实意义;
    比如:该机房的总出口带宽,当前总在线人数,平均每个客户端的下载速度(各地速度不同这里指的是平均速率),带宽使用率等等,依此来评估该机房是否能达到当初的预测效果。
    实际效果如下图所示:数据仅为举例使用;

    各个IDC数据如下图:

    其中:
    - tcp est connections是所有机房连接数总计;
    - download speed是每个连接的下载速度;
    - Total outbound是所有服务器对外提供服务器的带宽总计;
    - boundwidth used是总租用带宽除以当前流量的带宽使用率;
    - this node boundwidth是该节点租用的带宽数;
    目的:
    可以实时的掌握idc机房总体信息;
    可以历史的查询过去时间段内的局部信息;
    可以对机房的网络质量做出及时判断;
    可以针对每个机房的网络情况作出及时的调整;
    以下我来谈一下具体实现(方法各有不同,达到效果即可,欢迎提供技术交流):

    二、数据采集
    1、tcp est connections汇总方法:
    我提供的方法是在每台机器上部署snmp自定义脚本指定独立的oid传送给监控服务器:
    详细参考我之前写的:Cacti进阶引用二:使用cacti获取snmp发送的自定义信息后作图
    脚本原理:使用snmpwalk oid得到每个idc机房所有服务器的establish 连接数,进行汇总累加;
    2、total outbound汇总计算:
    方法一:使用rrdtool fetch(推荐使用)
    指令如下:

    #rrdtool fetch xxhost_traffic.rrd -s -10m AVERAGE |sed -n 3p |awk '{printf "traffic_out: %.f KB tarffic_in: %.f KB\n", $2/1024,$3/1024}'
    traffic_out: 13569 KB tarffic_in: 354 KB

    说明:我这里取得数值是10分钟前的流量,这是为了避免和cacti每5分钟取值产生冲突,也能保证取值准确;
    fetch取值都是科学计数,这里用printf进行来还原为可读的数值。(要和谐一下!)
    方法二:使用snmpwalk取流量值(不推荐使用,大家了解一下)
    指令如下:

    #snmpwalk -v2c -c public 127.0.0.1 .1.3.6.1.2.1.31.1.1.1.10
    说明:oid指的是ifHCOutOctets = Counter64的数值(snmp v2c 64bit解决溢出问题),需要在5分钟之后再取一次,然后将后一次的数据减去5分钟前的数据,得出的就是这5分钟内的流量;

    方法三:本地ifconfig取流量,方法与snmpwalk有相似之处:

    #ifconfig eth0 |grep "TX bytes" |awk '{print $6}' |cut -c 7-
    说明:其中RX为接受bytes数,TX为发送bytes数,如果取traffic out数据(即提供服务的数据),就上面的指令;同时也需要每5分钟取值一次然后相减,得出每5分钟的流量;可以在本地写脚本,用snmp传送到监控服务器;

    3、带宽使用率和下载速度(即每个连接的平均速度)计算
    带宽使用率=当前使用带宽 / 租用总带宽
    下载速度=当前使用带宽 / 总连接数

    4、汇总数据到文本:
    文本的内容和格式如下:

    xiantao,links: 3087,speed: 349.6262,bw: 1054,bound: 2048,bw_percent: 51%
    datong,links: 2332,speed: 968.2333,bw: 2205,bound: 4096,bw_percent: 53%
    nantong,links: 4279,speed: 598.0313,bw: 2499,bound: 4096,bw_percent: 61%
    Total,links: 153610.0,speed: 607.8250,bw: 87278.0,bound: 112640.0,bw_percent: 76%
    说明:以上数据为举例,total行中的数据应该为全部idc的总和(当前流量和总租用带宽)或者平均(下载速度、带宽使用率),%符号和单位都可以不用添加我们可以在cacti中设定;

    5、cacti取值脚本
    内容如下:

    #!/bin/sh
    workfile=result.txt #汇总数据的文本
    node=$1
    if [ -f $workfile ] && [ ! -z $node ];then
      echo -n `cat $workfile |grep -i $node |awk -F, '{print $2" "$3" "$4" "$6" "$5}' |sed s/": "/:/g |sed s/"%"//g`
    else
      echo -n "links:0 speed:0 bw:0 bw_percent:0 bound: 0"
    fi

    输出格式:

    links:6369 speed:460.3096 bw:2863 bw_percent:69.8 bound:4096

    三、创建cacti作图模板
    完成以上步骤,就可以在cacti中创建相应的模板啦,为我们辛苦取到的数据画图:
    1、登录cacti点击左侧菜单的data input motheds -> add
    如图:

    说明:设定如图所示,input string这里要在我们上面写的取值脚本后面跟,因为脚本后面要跟一个行参;Allow Empty Input的checkbox勾选;
    2、设定这个的input fields属性
    如图:

    说明:这里Special Type Code我们取主机名即 hostname,这样cacti就会知道是取devices name值;因为我们后面要创建host templates,然后再创建graph的时候把这个值赋给脚本;
    3、设定output fields属性
    就是我们脚本输出的5个数据;以bound举例,其他相同,如图:

    说明:依次创建5个数据的output fields,第一列的name必须与脚本输出的名字相同,最后点击save保存;
    4、创建data templates模板
    如图:


    说明:
    Data Source Item依次创建刚才输出的5个output fields这里的下拉菜单就是我们data input methods设定选择和你创建的Internal Data Source Name一一对应不能出错;
    Maximum Value这里设定尽量高些,免得出现溢出无法出图;
    Custom Data无需设定,其他全部默认;这里有个问题就是你每完成一个就要先点击save,否则会丢失之前修改的内容;
    5、创建graph templates模板,点击graph templates->add
    如图:


    说明:Vertical Label 这里是纵坐标名字可以填一个node status什么的随你喜欢,其他可以全部默认;
    Graph Template Items -> add 以TCP EST Connections为例:
    如图:

    说明:
    Data Source 下拉菜单选择之前我们在data templates里面创建的links一栏
    color自行选择(一定要选)
    Opacity/Alpha Channel cacti0.8.7新增默认就好;
    Graph Item Type 选择AREA(就是图的类型,AREA指的是区块表示,STACK是在原来区块上堆叠区块)
    Text Format 说明文字,自己写吧
    Insert Hard Return 勾选的话就会输入一个回车符
    CDEF Function不用选,因为我们输入的数据已经是正确数据,
    GPRINT Type:只有在Graph Item Type 选择GPRINT才用到,我们后面的%就是通过这个实现的;
    图中item 2、3、4添加方法如下图:

    说明:
    这里全部使用的Graph Item Type为GPRINT类型,GPRINT Type选择Exact Numbers(不打印小数点类型)
    Consolidation Function 选择LAST 即当前数值;其他的max min average不用说了吧;对应你自己的选择在text format中打印标签名;
    顺便说一下加%的GPRINT Type的方法:
    点击Graph Management->GPRINT Presets->add 如图:可参看rrdgraph_graph官方说明

    Graph Item Inputs部分会根据Graph Template Items自动创建默认即可;
    点击save保存,完成设定;
    6、创建host templates并画图:
    点击add;然后如下图:

    save保存,然后选择左侧菜单的devices->add开始作图,设定如下图:

    说明:这里重要的一点就是Hostname一栏一定要对应我们脚本中的行参值,就是你数据文本里面的第一列;其他snmp等全部为空;

    点击save->Create Graphs for this Host->勾选Graph Template Name中的
    Create: node_boundstatus graph->create 作图成功,大功告成;
    7、一次添加所有的idc host devices并加入到graph trees。

    四、心得总结
    我在做这个监控前,也没有想到过cacti这些附加功能,所以在做任何一个技术的时候,我们都应该多想想是否还有其他我们可以利用的价值。这里起到一个抛砖引玉的效果,希望网友们有更好的idea呈现给我们~

    阅读内文 Tags: , , , , , , , ,

    如何调试Cacti

    8月 6th, 2008 | 1 Comment | Posted in Cacti < by Martian Guo >

    本文转自 cacti.net

    Cacti users sometimes complain about NaN’s in their graphs. Unfortunately, there are several reasons for this result. The following is a step-by-step procedure I recommend for debugging this

    To debug the NaN’s:

    1. Check Cacti Log FilePlease have a look at your cacti log file. Usually, you’ll find it at <path_cacti>/log/cacti.log. Else see Settings, Paths. Check for this kind of error:
      Code:
      CACTID: Host[...] DS[....] WARNING: SNMP timeout detected \
      [500 ms], ignoring host '........'

      For “reasonable” timeouts, this may be related to a snmpbulkwalk issue. To change this, see Settings, Poller and lower the value for The Maximum SNMP OID’s Per SNMP Get Request. Start at a value of 1 and increase it again, if the poller starts working. Some agent’s don’t have the horsepower to deliver that many OID’s at a time. Therefore, we can reduce the number for those older/underpowered devices.

    2. Check Basic Data Gathering:For scripts, run them as cactiuser from cli to check basic functionality. E.g. for a perl script named your-perl-script.pl with parameters “p1 p2″ under *nix this would look like:
      Code:
      su - cactiuser
      /full/path/to/perl your-perl-script.pl p1 p2
      ... (check output)

      For snmp, snmpget the _exact_ OID you’re asking for, using same community string and snmp version as defined within cacti. For an OID of .1.3.6.1.4.something, community string of “very-secret” and version 2 for target host “target-host” this would look like

      Code:
      snmpget -c very-secret -v 2c target-host .1.3.6.1.4.something
      .... (check output)
    3. Check cacti’s poller:First make sure that crontab always shows poller.php. This program will either call cmd.php, the PHP based poller _or_ cactid, the fast alternative, written in C. Define the poller you’re using at “Settings” -> “Poller”. Cactid has to be implemented seperately, it does not come with cacti by default.Now, clear ./log/cacti.log (or rename it to get a fresh start)Then, change “Settings -> Poller Logging Level” to DEBUG for _one_ polling cycle. You may rename this log as well to avoid more stuff added to it with subsequent polling cycles.
      Now, find the host/data source in question. The Host[<id>] is given numerically, the <id> being a specific number for that host. Find this <id> from the Devices menue when editing the host: The url contains a string like &id=<id>.

      Check, whether the output is as expected. If not, check your script (e.g. /full/path/to/perl). If ok, proceed to next step

      This procedure may be replaced by running the poller manually for the failing host only. To do so, you need the <id>, again. If you’re using cmd.php, set the DEBUG logging level as defined above and run

      Code:
      php -q cmd.php <id> <id>

      If you’re using cactid, you may override logging level when calling the poller:

      Code:
      ./cactid --verbosity=5 <id> <id>

      All output is printed to STDOUT in both cases. This procdure allows for repeated tests without waiting for the next polling interval. And there’s no need to manually search for the failing host between hundreds of lines of output.

    4. Check MySQL updatingIn most cases, this step make be skipped. You may want to return to this step, if the next one fails (e.g. no rrdtool update to be found)From debug log, please find the MySQL update statement for that host concerning table poller_output. On very rare occasions, this will fail. So please copy that sql statement and paste it to a mysql session started from cli. This may as well be done from some tool like phpmyadmin. Check the sql return code.
    5. Check rrd file updatingDown in the same log, you should find some
      Code:
      rrdtool update <filename> --template ...

      You should find exactly one update statement for each file.

      RRD files should be created by the poller. If it does not create them, it will not fill them either. If it does, please check your Poller Cache from Utilities and search for your target. Does the query show up here?

    6. Check rrd file ownershipIf rrd files were created e.g. with root ownership, a poller running as cactiuser will not be able to update those files
      Code:
      cd /var/www/html/cacti/rra
      ls -l localhost*
      -rw-r--r--  1 root      root      463824 ocalhost_load_1min_5.rrd
      -rw-r--r--  1 cactiuser cactiuser 155584 localhost_mem_buffers_3.rrd
      -rw-r--r--  1 cactiuser cactiuser 155584 localhost_mem_swap_4.rrd
      -rw-r--r--  1 cactiuser cactiuser 155584 localhost_proc_7.rrd
      -rw-r--r--  1 cactiuser cactiuser 155584 localhost_users_6.rrd
      Code:
      chown cactiuser:cactiuser *.rrd

      will help.

    7. Check rrd file numbersYou’re perhaps wondering about this step, if the former was ok. But due to data sources MINIMUM and MAXIMUM definitions, it is possible, that valid updates for rrd files are suppressed, because MINIMUM was not reached or MAXIMUM was exceeded.Assuming, you’ve got some valid rrdtool update in step 3, perform a
      Code:
      rrdtool fetch <rrd file> AVERAGE

      and look at the last 10-20 lines. If you find NaN’s there, perform

      Code:
      rrdtool info <rrd file>

      and check the ds[...].min and ds[...].max entries, e.g.

      Code:
      ds[loss].min = 0.0000000000e+00
      ds[loss].max = 1.0000000000e+02

      In this example, MINIMUM = 0 and MAXIMUM = 100. For a ds.[...].type=GAUGE verify, that e.g. the number returned by the script does not exceed ds[...].MAX (same holds for MINIMUM, respectively).

      If you run into this, please do not only update the data source definition within the Data Template, but perform a

      Code:
      rrdtool tune <rrd file> --maximum <ds-name>:<new ds maximum>

      for all existing rrd files belonging to that Data Template.

      At this step, it is wise to check step and heartbeat of the rrd file as well. For standard 300 seconds polling intervals (step=300), it is wise to set minimal_heartbeat to 600 seconds. If a single update is missing and the next one occurs in less than 600 seconds from the last one, rrdtool will interpolate the missing update. Thus, gaps are “filled” automatically by interpolation. Be aware of the fact, that this is no “real” data! Again, this must be done in the Data Template itself and by using rrdtool tune for all existing rrd files of this type.

    8. Check rrdtool graph statementLast resort would be to check, that the corract data sources are used. Goto Graph Management and select your Graph. Enable DEBUG Mode to find the whole rrdtool graph statement. You should notice the DEF statements. They specify the rrd file and data source to be used. You may check, that all of them are as wanted.
    9. MiscellaneousUp to current cacti 0.8.6h, table poller_output may increase beyond reasonable size.This is commonly due to php.ini’s memory settings of 8MB default. Change this to at least 64 MB.
      To check this, please run following sql from mysql cli (or phpmyadmin or the like)

      Code:
      select count(*) from poller_output;

      If the result is huge, you may get rid of those stuff by

      Code:
      truncate table poller_output;

      As of current SVN code for upcoming cacti 0.9, I saw measures were taken on both issues (memory size, truncating poller_output).

    10. RPM Installation?Most rpm installations will setup the crontab entry now. If you’ve followed the installation instructions to the letter (which you should always do ;-) ), you may now have two poller running. That’s not a good thing, though. Most rpm installations will setup cron in /etc/cron.d/cacti.
      Now, please check all your crontabs, especially /etc/crontab and crontabs of users root and cactiuser. Leave only one poller entry for all of them. Personally, I’ve chosen /etc/cron.d/cacti to avoid problems when updating rpm’s. Mosten often, you won’t remember this item when updating lots of rpm’s, so I felt more secure to put it here. And I’ve made some slight modifications, see

      Code:
      prompt> vi /etc/cron.d/cacti
      */5 * * * *  cactiuser /usr/bin/php -q /var/www/html/cacti/poller.php \
      > /var/local/log/poller.log 2>&1

      This will produce a file /var/local/log/poller.log, which includes some additional informations from each poller’s run, such as rrdtool errors. It occupies only some few bytes and will be overwritten each time.

      If you’re using the crontab of user “cactiuser” instead, this will look like

      Code:
      prompt> crontab -e -u cactiuser
      */5 * * * *  /usr/bin/php -q /var/www/html/cacti/poller.php \
      > /var/local/log/poller.log 2>&1
    11. Not NaN, but 0 (zero) values?Pay attention to custom scripts. It is required, that external commands called from there are in the $PATH of the cactiuser running the poller. It is therefor recommended to provide /full/path/to/external/command.User “criggie” reported an issue with running smartctl. It was complaining “you are not root” so a quick chmod +s on the script fixed that problem.Secondly, the script was taking several seconds to run. So cacti was logging a “U” for unparseable in the debug output, and was recording NAN. So my fix there was to make the script run faster - it has to complete in less than one second, and the age of my box make that hard.
    阅读内文 Tags:

    Cacti常见问题集:FAQ 转载

    7月 9th, 2008 | No Comments | Posted in Cacti < by Michael Field >

    以前自己曾经解决的,当时自己记住了,过了好长时间,重新在新的机器上安装cacti的时候,却忘记提前修改,导致一些不必要的麻烦。帖出来希望对自己或者对大家有帮助。

    1、一般说来,图片的流量统计描述都是 |host_description| - Traffic - |query_ifName| 按照这个形式来描述的,对于华为的设备,Gi显示成GigabitEthernet,可能导致后面的模块号,端口好无法显示。如 GigabitEthernet4/3/2显示成GigabitEthernet,解决办法:

    Console -> Settings -> Visual -> Maximum Field Length: 默认 15,我修改成30就OK了。

    The maximum number of characters to display for a data query field.

    2、对于Traffic 大于800Mbits/s的无法显示问题,在创建graph前修改 Data Templates -> Interface Traffic -> 1: traffic_in -> Maximum Value Use Per-Data Source Value 默认是100000000 后面再添 几个0就OK了,同样 2: traffic_out也要修改哦,最好在使用new graph创建新的graph之前修改后,否则之后修改会有一定的麻烦,甚至无法正常工作。

    3、以前都是直接下载tar.gz源代码包安装的,按照readme就可以搞定一切。目前在FC4下可以直接通过yum instal cacti就安装完成,安装后却不知道怎么使用,也不知道安装到什么地方了,怎么让cacti工作。

    首先了解rpm包安装了那些有用的文件:

    rpm程序把cacit的php文件放到/usr/share/cacti目录下。

    在/etc/httpd/conf.d/cacit.conf

    # Cacti: An rrd based graphing tool
    #
    Alias /cacti    /usr/share/cacti
    <Directory /usr/share/cacti/>
            Order Deny,Allow
            Deny from all
            Allow from 127.0.0.1
    </Directory>

    在apache的配置里面只允许localhost访问/cacti这个虚拟目录。

    [root@elm ~]# cd /etc/cron.d
    [root@elm cron.d]# more cacti
    */5 * * * *     cacti   /usr/bin/php /usr/share/cacti/poller.php > /dev/null 2>&1

    定期执行程序的cron配置。

    下面介绍如何让cacti工作:

    首先,配置数据库:

    [root@elm local]# cd /usr/share/doc/cacti-0.8.6h/

    [root@elm cacti-0.8.6h]# ls

    cacti.sql  docs  LICENSE  README

    [root@elm cacti-0.8.6h]# mysql

    Welcome to the MySQL monitor.  Commands end with ; or \g.

    Your MySQL connection id is 312 to server version: 4.1.16
    Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
    mysql> create database cacti_test;
    Query OK, 1 row affected (0.12 sec)
    mysql> \q
    Bye
    [root@elm cacti-0.8.6h]# mysql cacti_test < cacti.sql

    修改cacit的配置文件:

    [root@elm cacti-0.8.6h]# cd /usr/share/cacti/include/
    [root@elm include]# cat db.php
    <?
    /* make sure these values refect your actual database/host/user/password */
    $database_type = "mysql";
    $database_default = "cacti_test";
    $database_hostname = "localhost";
    $database_username = "cactiuser";
    $database_password = "cactipass";
    $database_port = "3306";
    ?>

    根据自己的需求修改。

    OK,打开Firefox在地址栏输入: http://localhost/cacti进行配置。

    默认用户名密码:admin:admin

    5、如果在配置完毕后可以生成rrd文件,且里面有数据,但是在浏览器上无法看到png图片。现查看apache的log

    如果出现:

    [Thu Feb 09 15:12:24 2006] [error] [client 127.0.0.1] File does not exist: /var/www/html/favicon.ico
    ERROR: opening '/usr/share/cacti/rra/localhost_mem_buffers_3.rrd': Permission denied
    ERROR: opening '/usr/share/cacti/rra/localhost_mem_buffers_3.rrd': Permission denied
    ERROR: opening '/usr/share/cacti/rra/localhost_mem_buffers_3.rrd': Permission denied
    ERROR: opening '/usr/share/cacti/rra/localhost_mem_buffers_3.rrd': Permission denied
    [root@elm ~]# cd /usr/share/cacti/rra
    [root@elm rra]# ls -lat

    -rw-r--r--  1 cacti cacti 141484  2月  9 19:15 localhost_load_1min_5.rrd
    -rw-r--r--  1 cacti cacti  47836  2月  9 19:15 localhost_mem_buffers_3.rrd
    -rw-r--r--  1 cacti cacti  47836  2月  9 19:15 localhost_mem_swap_4.rrd
    -rw-r--r--  1 cacti cacti  47836  2月  9 19:15 localhost_proc_7.rrd
    -rw-r--r--  1 cacti cacti  47836  2月  9 19:15 localhost_users_6.rrd
    -rw-r--r--  1 cacti cacti  94660  2月  9 19:15 tplink_input_13.rrd
    -rw-r--r--  1 cacti cacti  94660  2月  9 19:15 tplink_input_16.rrd
    drwxr-xr-x  2 cacti root    4096  2月  9 18:35 .
    drwxr-xr-x  4 root  root    4096  2月  9 14:30 ..

    Other也有R权限,那么请把机器的SElinux关闭,目前我不知道其他办法。

    [root@elm rra]# cd /etc/selinux/

    [root@elm selinux]# more config

    # This file controls the state of SELinux on the system.
    # SELINUX= can take one of these three values:
    #       enforcing - SELinux security policy is enforced.
    #       permissive - SELinux prints warnings instead of enforcing.
    #       disabled - SELinux is fully disabled.
    SELINUX=disabled
    # SELINUXTYPE= type of policy in use. Possible values are:
    #       targeted - Only targeted network daemons are protected.
    #       strict - Full SELinux protection.


    SELINUXTYPE=targeted

    注意SELINUX=disabled;重新启动机器后就OK了。

    6、我只想显示整数怎么办,不显示小数点?

    修改Graph Templates下的Graph Template Items的GPRINT Type,改成Exact Number

    7、我原来使用的是rrdtool-1.0.x 升级到 1.2.x 后部分图像无法显示(Zoom)

    修改Configuration -> Settings -> RRDTool Utility Version -> 1.2.x

    阅读内文 Tags: , , ,

    Cacti进阶应用二:使用cacti获取snmp发送的自定义信息后作图

    7月 9th, 2008 | 15 Comments | Posted in Cacti < by Michael Field >

    一、实际需求

    服务器运行了lighttpd,主要为了统计当前连接数,然后cacti来画出当前连接数的统计图:这里说一下有几个方法来实现这个功能:
    1、snmpget直接取值,经常发生取值不准确的问题:

    # snmpget -c public -v 2c 127.0.0.1 .1.3.6.1.2.1.6.9.0
    TCP-MIB::tcpCurrEstab.0 = Gauge32: 0

    2、用snmp抓取所有链接后统计行数,虽然信息比较准确,但效率低下,如果监控点超过千台,那几乎数据都是timeout收场:

    # snmpget -c public -v 2c 127.0.0.1 .1.3.6.1.2.1.6.13.1.3 |wc -l
    725

    3、在本机上写脚本统计后用snmp发送信息,cacti收集后画图,具体方法;

    二、解决方案

    snmp提供了发送自定义信息的功能。查看snmpd.conf会看到有一个小节是关于外部脚本的,Executables/scripts。这个小节里面也举了例子,告诉你怎么通过snmp发送信息。

    在snmpd.conf任意位置添加一行:
    exec .1.3.6.1.4.1.2021.18 tcpCurrEstab /etc/tcpconn.sh
    其中tcpCurrEstab是命令的名称,后面是命令以及参数。命令的名称可以随便起。脚本执行的结果类似下面:

    # /etc/tcpconn.sh
    80

    脚本内容如下:

    $ cat tcpconn.sh
    #!/bin/sh
    conn=`netstat -s -t | grep connections\ established |awk '{print $1}'`
    echo $conn

    重启snmpd,然后远程通过snmpwalk接收一下数据看看:

    # snmpwalk -c public -v 2c 127.0.0.1 .1.3.6.1.4.1.2021.18   
    UCD-SNMP-MIB::ucdavis.18.1.1 = INTEGER: 1
    UCD-SNMP-MIB::ucdavis.18.2.1 = STRING: "tcpCurrEstab"
    UCD-SNMP-MIB::ucdavis.18.3.1 = STRING: "/etc/tcpconn_total.sh"
    UCD-SNMP-MIB::ucdavis.18.100.1 = INTEGER: 0
    UCD-SNMP-MIB::ucdavis.18.101.1 = STRING: "488"
    UCD-SNMP-MIB::ucdavis.18.102.1 = INTEGER: 0
    UCD-SNMP-MIB::ucdavis.18.103.1 = ""

    其中我们需要的是UCD-SNMP-MIB::ucdavis.18.101.1 = STRING: “488″这一行:

    # snmpwalk -v 1 192.168.1.201 -c public .1.3.6.1.4.1.2021.18.101.1
    UCD-SNMP-MIB::ucdavis.18.101.1 = STRING: "488"

    能采集到数据之后,就可以配置cacti来接收了。在cacti界面中console->Templates->Data Templates,然后点击右上角的Add,Data Templates中的name是给这个数据模板的命名,Data Source中的name将来显示在Data Sources中,我这里添加“|host_description| - Tcp Conn. - ESTBLISHED”,选get snmp data,Internal Data Source Name也可以随便添,这个用来给rrd文件命名。设置完后就可以save了,save之后会发现下面多了一些选项,在最下面那个添上我们需要的数据的 OID“.1.3.6.1.4.1.2021.18.101.1”,可以保存了。

    此后需要创建一个Graph Templates,好让cacti生成图片。在cacti界面中console->Templates->Graph Templates,然后点击右上角的Add,Templates中的name是给这个数据模板的命名,Graph Template中的name是将来显示在图片上面中间的内容,我这里添加“|host_description| - Tcp Conn. - ESTBLISHED”,其他保持默认,保存之后上面会出来一些选项。

    在Graph Template Items中添加一个item,Data Source选之前添加的,color选择一个图片的颜色,Graph Item Type选AREA,也就是区域,也可以选其他的线条,Text Format设置说明。然后再添加一个,Graph Item Type选GPRINT,Consolidation Function选LAST,也就是当前的值,Text Format输入current。你还可以添加一些Graph Item Type为COMMENT的注释说明等。
    现在只要为host添加这个画图模板就可以看到画出来的图了。

    阅读内文 Tags: , ,

    Cacti进阶应用一:汇总流量图-将数据合并后作图

    6月 24th, 2008 | 7 Comments | Posted in Cacti < by Michael Field >

    在使用Cacti方便快捷的建立监控图时,往往根据实际应用必须监控几台甚至上百台服务器的汇总流量图来观察该应用服务器组的总流量等指标。

    这里我们就来介绍如何用cacit快速的建立汇总流量图,其他汇总图建立方法相同;
    一、创建CDEF
    1、点击cacti左边菜单中的graph Management下的CDEFs
    2、点击CDEF‘s右边的add,增加一个名为Turn All Data into Bits项
    3、在CDEF Items表格中点击add,输入如下图:依次创建以下三个Items;

    4、完成后如下图:

    CDEF item创建完成。

    二、创建new graph
    1、点击cacti左边菜单中的graph Management

    2、在右边点击add出现如下图,直接选择create

    3、在Graph Configuration表格中的title输入新图的名字;如IDC all interface traffic in(bits/sec)

    4、表格最下的选项Vertical Label是纵轴的标识,这里我们填入Bits Per Second

    5、完成后点击右下方的create,会在当前页面中间出现如下表格:

    6、点击graph item 表格中的add按钮,输入信息如下图:

    说明:因为我们会在最后汇总数据所以添加时除了data source和类型相互关联不可以选错,都可以任意搭配;
    请多熟悉其中的关联性,建议多测试几次;
    data source:数据来源就是你要汇总的流量之一;
    color:选择颜色这里可以任意选择;
    Graph Item Type:图形(我选择的是AREA区域类型,要累加的流量图请选择stack)
    CDEF Function:这里一定要选择“Turn bytes into Bits”
    点击create后,我们就创建了第一个流量图,会在页面上显示出来;继续添加其他流量图,方法同上;

    7、汇总流量数据,并标识当前流量、平均流量、最大流量以及总流量数;
    完成所有流量图的添加后,开始汇总流量,继续创建graph item,如下图所示:

    这里切记选择我们上面所创建的CDEF‘s Functiion “Trun all data into bits”
    并在text format栏中写入ALL Traffic_in
    然后依次添加图上的标识栏见以下步骤:
    当前流量,图释1:

    平均流量,图释2:

    最大流量,图释3:

    总流量统计,图释4:

    截图的时候最后多了一个光标;
    Text Format处填写:Total Bound: |sum:auto:total:2:auto|

    8、完成后如下图所示,表示操作成功,请根据以上方法继续添加traffic out流量

    三、进阶
    看了以上介绍大家可以试着用这个方法在Graph Templates中创建一个汇总流量的模板,这样以后增加汇总图是就更加高效和迅速啦。
    因为之前看到相关的介绍只有英文的,特别在次给大家做个中文和图片介绍,希望能给各位带来帮助。

    有问题请在此留言~

    阅读内文