使用 SNMP 和 Cacti 监控 Linux 服务器
SNMP(简单网络管理协议)用于收集设备内部发生的数据,如负载、磁盘状态、带宽之类。像Cacti这样的网络监控工具用这些数据来生成图标以达到监控的目的。
在一个典型的Cacti和SNMP部署中,会有一台或多台启用了SNMP的设备,以及一台独立的用来从那些设备收集SNMP回馈的监控服务器。请记住,所有需要监控的设备必须启用SNMP。在本教程中,出于演示目的,我们将在同一台Linux服务器上配置Cacti和SNMP。
在Debian或Ubuntu上配置SNMP
要在基于Debian的系统上安装SNMP代理(snmpd),请运行以下命令:
<span class="pln">root@server</span><span class="pun">:~#</span><span class="pln"> apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install snmpd</span>
然后,如下编辑配置文件。
<span class="pln">root@server</span><span class="pun">:~#</span><span class="pln"> vim </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">snmp</span><span class="pun">/</span><span class="pln">snmpd</span><span class="pun">.</span><span class="pln">conf</span>
<span class="com"># 使snmpd 监听再所有接口上</span>
<span class="pln">agentAddress udp</span><span class="pun">:</span><span class="lit">161</span>
<span class="com"># 定义一个只读的 community 'myCommunity' 和源网络</span>
<span class="pln">rocommunity myCommunity </span><span class="lit">172.17</span><span class="pun">.</span><span class="lit">1.0</span><span class="pun">/</span><span class="lit">24</span>
<span class="pln">sysLocation </span><span class="typ">Earth</span>
<span class="pln">sysContact email@domain</span><span class="pun">.</span><span class="pln">tld</span>
在编辑完配置文件后,重启snmpd。
<span class="pln">root@server</span><span class="pun">:~#</span><span class="pln"> service snmpd restart</span>
在CentOS或RHEL上配置SNMP
要安装SNMP工具和库,请运行以下命令。
<span class="pln">root@server</span><span class="pun">:~#</span><span class="pln"> sudo yum install net</span><span class="pun">-</span><span class="pln">snmp</span>
然后,如下编辑SNMP配置文件。
<span class="pln">root@server</span><span class="pun">:~#</span><span class="pln"> vim </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">snmp</span><span class="pun">/</span><span class="pln">snmpd</span><span class="pun">.</span><span class="pln">conf</span>
<span class="com"># 定义一个使用 community 'myCommunity' 和源网络 172.17.1.0/24 的用户 'myUser'</span>
<span class="pln">com2sec myUser </span><span class="lit">172.17</span><span class="pun">.</span><span class="lit">1.0</span><span class="pun">/</span><span class="lit">24</span><span class="pln"> myCommunity</span>
<span class="com"># 将 myUser 加到 'myGroup' 组,定义组权限</span>
<span class="kwd">group</span><span class="pln"> myGroup v1 myUser</span>
<span class="kwd">group</span><span class="pln"> myGroup v2c myUser</span>
<span class="pln">view all included </span><span class="pun">.</span><span class="lit">1</span>
<span class="pln">access myGroup </span><span class="str">""</span><span class="pln"> any noauth exact all all none</span>
<span class="pln">root@server</span><span class="pun">:~#</span><span class="pln"> service snmpd restart</span>
<span class="pln">root@server</span><span class="pun">:~#</span><span class="pln"> chkconfig snmpd on</span>
重启snmpd服务,然后添加到启动服务列表。
测试SNMP
SNMP可以通过运行snmpwalk命令进行测试。如果SNMP已经配置成功,该命令会生成大量输出。
<span class="pln">root@server</span><span class="pun">:~#</span><span class="pln"> snmpwalk </span><span class="pun">-</span><span class="pln">c myCommunity </span><span class="lit">172.17</span><span class="pun">.</span><span class="lit">1.44</span><span class="pun">-</span><span class="pln">v1</span>
<span class="pln">iso</span><span class="pun">.</span><span class="lit">3.6</span><span class="pun">.</span><span class="lit">1.2</span><span class="pun">.</span><span class="lit">1.1</span><span class="pun">.</span><span class="lit">1.0</span><span class="pun">=</span><span class="pln"> STRING</span><span class="pun">:</span><span class="str">"Linux mrtg 3.5.0-17-generic #28-Ubuntu SMP Tue Oct 9 19:31:23 UTC 2012 x86_64"</span>
<span class="pln">iso</span><span class="pun">.</span><span class="lit">3.6</span><span class="pun">.</span><span class="lit">1.2</span><span class="pun">.</span><span class="lit">1.1</span><span class="pun">.</span><span class="lit">2.0</span><span class="pun">=</span><span class="pln"> OID</span><span class="pun">:</span><span class="pln"> iso</span><span class="pun">.</span><span class="lit">3.6</span><span class="pun">.</span><span class="lit">1.4</span><span class="pun">.</span><span class="lit">1.8072</span><span class="pun">.</span><span class="lit">3.2</span><span class="pun">.</span><span class="lit">10</span>
<span class="pln">iso</span><span class="pun">.</span><span class="lit">3.6</span><span class="pun">.</span><span class="lit">1.2</span><span class="pun">.</span><span class="lit">1.1</span><span class="pun">.</span><span class="lit">3.0</span><span class="pun">=</span><span class="typ">Timeticks</span><span class="pun">:</span><span class="pun">(</span><span class="lit">2097</span><span class="pun">)</span><span class="lit">0</span><span class="pun">:</span><span class="lit">00</span><span class="pun">:</span><span class="lit">20.97</span>
<span class="pun">~~</span><span class="pun">输出截断</span><span class="pun">~~</span>
<span class="pln">iso</span><span class="pun">.</span><span class="lit">3.6</span><span class="pun">.</span><span class="lit">1.2</span><span class="pun">.</span><span class="lit">1.92</span><span class="pun">.</span><span class="lit">1.1</span><span class="pun">.</span><span class="lit">2.0</span><span class="pun">=</span><span class="typ">Gauge32</span><span class="pun">:</span><span class="lit">1440</span>
<span class="pln">iso</span><span class="pun">.</span><span class="lit">3.6</span><span class="pun">.</span><span class="lit">1.2</span><span class="pun">.</span><span class="lit">1.92</span><span class="pun">.</span><span class="lit">1.2</span><span class="pun">.</span><span class="lit">1.0</span><span class="pun">=</span><span class="typ">Counter32</span><span class="pun">:</span><span class="lit">1</span>
<span class="pln">iso</span><span class="pun">.</span><span class="lit">3.6</span><span class="pun">.</span><span class="lit">1.2</span><span class="pun">.</span><span class="lit">1.92</span><span class="pun">.</span><span class="lit">1.2</span><span class="pun">.</span><span class="lit">2.0</span><span class="pun">=</span><span class="typ">Counter32</span><span class="pun">:</span><span class="lit">0</span>
<span class="pln">iso</span><span class="pun">.</span><span class="lit">3.6</span><span class="pun">.</span><span class="lit">1.2</span><span class="pun">.</span><span class="lit">1.92</span><span class="pun">.</span><span class="lit">1.3</span><span class="pun">.</span><span class="lit">1.1</span><span class="pun">.</span><span class="lit">2.7</span><span class="pun">.</span><span class="lit">100.101</span><span class="pun">.</span><span class="lit">102.97</span><span class="pun">.</span><span class="lit">117.108</span><span class="pun">.</span><span class="lit">116.1</span><span class="pun">=</span><span class="typ">Timeticks</span><span class="pun">:</span><span class="pun">(</span><span class="lit">1</span><span class="pun">)</span><span class="lit">0</span><span class="pun">:</span><span class="lit">00</span><span class="pun">:</span><span class="lit">00.01</span>
<span class="pln">iso</span><span class="pun">.</span><span class="lit">3.6</span><span class="pun">.</span><span class="lit">1.2</span><span class="pun">.</span><span class="lit">1.92</span><span class="pun">.</span><span class="lit">1.3</span><span class="pun">.</span><span class="lit">1.1</span><span class="pun">.</span><span class="lit">3.7</span><span class="pun">.</span><span class="lit">100.101</span><span class="pun">.</span><span class="lit">102.97</span><span class="pun">.</span><span class="lit">117.108</span><span class="pun">.</span><span class="lit">116.1</span><span class="pun">=</span><span class="typ">Hex</span><span class="pun">-</span><span class="pln">STRING</span><span class="pun">:</span><span class="lit">07</span><span class="pln"> DD </span><span class="lit">0B</span><span class="lit">12</span><span class="lit">00</span><span class="lit">39</span><span class="lit">27</span><span class="lit">00</span><span class="lit">2B</span><span class="lit">06</span><span class="lit">00</span>
配置带有SNMP的Cacti
在本教程中,我们将在同一台Linux服务器上设置Cacti和SNMP。所以,到刚刚配置SNMP的Linux服务器上去[安装Cacti][2]吧。
安装完后,Cacti网页界面可以通过“http://172.17.1.44/cacti ”来访问,当然,在你的环境中,请将IP地址换成你的服务器的地址。
安装过程中Cacti的路径一般都是正确的,但是如有必要,请再次检查以下。
在首次安装过程中,Cacti默认的用户名和密码是“admin”和“admin”。在首次登录后会强制你修改密码。
添加设备到Cacti并管理
Cacti将���据先前配置的SNMP字符串注册设备。在本教程中,我们将只添加启用了SNMP的本地服务器。
要添加设备,我们必须以管理员登录,然后转到Cacti管理员面板中的控制台。点击 控制台 > 设备。
那里可能已经有一个名为‘localhost’的设备。我们不需要它,因为我们要创建全新的图表。我们可以将该设备从列表中删除,使用“添加”按钮来添加新设备。
接下来,我们设置设备参数。
现在设备已经添加,我们来指定想要创建的图表模板。你可以在该页面的最后部分中找到这块内容。
然后,我们继续来创建图表。
这里,我们创建用于平均负载、RAM和硬盘、处理器的图表。
接口的图表和64位计数器
默认情况下,Cacti在SNMP查询中使用32位计数器。32位计数器对于大多数带宽图表而言已经足够了,但是对于超过100Mbps的带宽,它就无能为力了。如果已经知道带宽会超过100Mbps,建议你使用64位计数器。使用64位计数器一点也不麻烦。
注意: Cacti会花费大约15分钟来产生新图表,除了耐心等待,你别无选择。
创建图表树
这些截图展示了如何创建图表树,以及如何添加图表到这些树中。
我们可以验证图表树中的图表。
用户管理
最后,我们创建一个只具有查看我们刚创建的图表权限的用户。Cacti内建了用户管理系统,而且是高度可定制的。
在完成这些步骤后,我们可以使用‘user1’来登录进去,并验证只有该用户可以查看该图表。
至此,我们在网络监控系统中部署了一台Cacti服务器。Cacti服务器比较稳定,可以处理大量图表而不会出问题。
希望本文对你有所帮助。
--------------------------------------分割线 --------------------------------------
--------------------------------------分割线 --------------------------------------
Cacti 的详细介绍:请点这里
Cacti 的下载地址:请点这里
via: http://xmodulo.com/monitor-linux-servers-snmp-cacti.html
作者:Sarmed Rahman 译者:GOLinux 校对:wxy