Cacti定义了三种类型的模板,分别是
php
主机模板html
数据模板linux
图形模板web
主机模板(Host templates),它是图形模板和数据查询的一个集合(Associated Graph Templates + Associated Data Queries),描述了监控某一类型的机器须要监控的项目与如何生成图形。shell
数据模板(Data templates),它定义了 怎么经过数据收集方法接收数据,数据源中的数据如何进行保存,以及保存在什么地方。建立并更新 RRD 文件,该模板与 RRDTool 工具的 rrdtool create ; rrdtool update 命令相关。bash
图形模板(Graph templates),描述了如何进行绘图,生成的一张图像应该是什么样子的。包括使用哪些数据模板、展现哪些元素、是否使用 CDEF 进行计算汇总。该模板使用 rrdtool graph 命令。网络
模板关系说明tcp
【Graph Templates】和【Data Template】存在对应关系。当添加完监控设备后,设备绑定的【Graph Templates】会根据文件名自动去掉主机名称寻找对应名称的【Data Template】。如生成图象[××× Ser ver - ApacheStats08 - Total Hits]就会寻找[ApacheStats08 - Total Hits]数据模板。这里须要特别说明的是,若是监控设备所使用的【Host Templates】建立时有添加任意一个【Data Queries】的话,那最后生成图象的数量可能会有多个,其对应的数据模板依据【Data Queries】配置中的[XML Path]文件。ide
同时也定义数据收集方法,分别是:工具
Data Queries
Data Input Methods
数据输入方法(Data input methods),数据模板基于数据输入方法,数据输入方法描述了 Cacti 获取监控数据的各类方法。其中最经常使用的就是 SNMP,但这里咱们要用到一些自定义的模板。
数据查询(Data queries),它是一种特殊的获取监控数据的方式,它所获取的监控数据通常是某一方面的一系列数据,好比网络接口信息。通常包括一个 XML 文件,其中定义了要获取的数据,以及获取方法(通常是一个命令或脚本)。
Cacti进行监控很大一部分都是使用模板的形式完成的,而Cacti在安装完成以后,其自己自带了一部分模板,这些模板虽然可用,可是支持的监控项目太少,因而咱们要寻求更多模板以及更优化的内容。
在实际的工做工程中,咱们可能须要监控更多的指标。此时该怎么办呢?其实cacti官方提供了许多模板,并且不少志愿者也提供了大量的模板,咱们能够直接下载,解压缩,而后导入模板,直接使用。
Cacti 的官方模板库: http://docs.cacti.net/templates
cacti官方网站 -> forums -> Scripts and Templates, 这里的模板应有尽有,基本可以知足咱们的监控需求。
一、Cacti 模板通常加载方法
一个Cacti的模板通常有两部分组成。一部分是xml模板文件,另外一部分是xml的数据收集文件,虽然这两种文件都是xml格式的,可是你能够很显而易见的就能分出它们的不一样。由于它们的文件大小一点都不类似。
a、xml 模板文件要在 Cacti 的web 页面 Console 选项卡 Import Templates 项进行添加:
b、而xml数据收集文件,则是要放在 Cacti 部署目录的 cacti/resource/snmp_queries 目录下:
只要将两种类型的xml正确的导入/放入到应有的位置,那么模板就添加成功了。
一、【Data Queries】由【Data Input Methods】组成建立, 一般表现为 XML 格式的文件,并在其[Associated Graph Templates]指向所支持的【Graph Templates】;
二、【Host Templates】由【Graph Templates】组成建立,也可同时在[Associated Data Queries]绑定【Data Queries】;【Graph Templates】和【Data Template】存在对应关系。
三、【Devices】在添加时绑定【Host Templates】后,其[Associated Graph Templates]处于“Not Being Graphed”状态,点击右上方的[Create Graphs for this Host]进入到界面全选中后启动绘画,状态变动为“Is Being Graphed”开始生效;
四、【Devices】生成画象后,点击[Data Source List]看到【Data Source】,每行【Data Source】记录对应一个【Data Template】以及【Data Template】实例化后的[Supplemental Data Template Data]RRD文件。
下面咱们就来看一看如何使用模板?
添加主机模板(Linux / Windows 相似),并添加新主机进行监控
首先,基本上咱们使用的全部模板均可以到官方的站点上下载到:
http://forums.cacti.net/viewforum.php?f=12&sid=fe2532345bf037678cb1e9fb07b40f16
http://docs.cacti.net/templates
当咱们下载某个模板的时候,请必定要仔细的阅读其 Installation(至关重要),能够避免不少没必要要的错误。举个例子,好比我要下载cacti087e_linux_generic-20100519-yrg.zip, 而后,其做者给出了详细的安装说明:
Unzip the download
Via cacti web console, click on “Import Templates” and import the file “cacti_host_template_*.xml”
Create new device using the new imported host template
Check new graphs
OK, 咱们演示一下,若是添加并使用模板。
a. 在Cacti的Web页面Console->Import Templates导入响应的xml模板文件。
b. 在Linux上cacti/resource/snmp_queries中放入模板相关的xml数据收集文件。并确保属主和属组均为cacti。
c. 点击Console->Devices,打开设备管理页面。
d. 点击右上角的add,添加一个新的机器
一、添加模板
Import Templates -> 浏览 -> Import, 而后就OK了。
二、使用模板
添加设备:Devices -> Add , 而后主要编辑两个地方
General Host Options
SNMP Options
e. 当主机信息填好以后, 点击 Create 。
f. 此时你的页面左上角应该显示: Save Success,而且显示出了主机信息和SNMP信息,若是显示 SNMP error, 则要检查被监控机的snmp状态。
g. 这时咱们就能够添加相应的监控项了,在页面最下方的Associated Graph Templates中添加图形模板,在Associated Data Queries中添加数据模板。
h. 添加好相应的模板以后点击Save。此时左上角会出现Save Successful.
i. 点击右上角的Create Graphs for this Host,来为刚才经过模板所得到到的数据进行画图。
j. 选中须要画图的项,其中依然分为两部分:
经过Graph Templates中的内容都须要选中,而Data Query获取的内容比较详细,因此能够按需选择。
k. 选择好须要画图的项目后,点击右下角的Create,左上角会出现被建立出来的画图项。
为主机建立图形
而后勾选,须要建立哪些图形:
而后,再把此设备添加到Graph Trees, 方便管理
2.在Graphs Trees 中新添加主机。
a. 在Cacti Web界面Graph Trees中,选择相应的列(或者系统默认的Default Tree)。
b. 进去Tree以后,点击右上角的Add,添加一个新的显示项。
c. 在Tree Item Type中选择Host,而后在下面的Host中选择咱们刚才建立的主机。点击Create。
d. 此时,再进入Graphs界面,就能够看到在Default Tree下加入了咱们新建立的主机。
Graph Trees -> 选择一个Tree Name -> Add -> Tree Item Type(Host) -> Create
就这么简单,那么等一下子就会出图了。
添加图形模板
导入方式和主机模板的导入方式相同。导入后,能够在console -> templates -> Graph Templates中查看。
咱们以添加cacti_graph_template_linux_server_-_processes.xml.gz 为例,演示一下,如何添加并使用图形模板。下载,并查看其Installation.
unzip the file
$ gzip -d cacti_graph_template_linux_server_-_processes.xml.gz
use Console-> Import/Export -> Import Templates to import the XML file
这里,导入的过程就不截图演示了, 和上面导入主机模板的步骤同样。
如何把添加的图形模板,应用到特定的主机上呢?
Graph Management -> Host标签选择一个主机 -> Add -> Selected Graph Template -> Create
如何把主机上添加的图形模板删除呢?
Graph Management -> 选择主机 -> 选择 -> 而后 choose an action: Delete
在下一步,咱们选择第一个选项,保留数据源,只删除图形模板。
cacti之自定义监控脚本
若是在cacti官方论坛上,找不到合适的模板,这该如何是好? 此时可能就须要咱们自定义了。
在前面的文章中,已经提到。cacti数据收集方法主要有两类:
Data Queries 主要是定义好的xml文件
Data Input Methods 主要是 scripts, command (最经常使用)
cacti自定义监控脚本进行监控的具体流程以下:
自定义数据收集方法,脚本(xml, scripts, command)
数据收集了如何保存呢?数据模板。数据模板定义了:怎么经过数据收集方法获取数据,获取的数据如何保存,以及保存在什么地方。(也就是定义如何建立 rrd 文件)
把数据模板应用到特定的device上,就会建立数据源。(也就是调用rrdtool create , rrdtool update)
有了数据源,那么就能够绘图了。图形模板或者自定义,应用到相应的数据源上。(rrdtool graph)
如下是cacti官方文档:
http://docs.cacti.net/manual:087:3a_advanced_topics.1_data_input_methods
任何能够经过脚原本获取数据的设备均可以使用cacti来监控。
步骤一: Creating a Data Input Method
To create a new data input method, select the Data Input Methods option under the Management heading. Once on that screen, click Add on the right. You will be presented with a few fields to populate on the following screen.
When you are finished filling in all necessary fields, click the Create button to continue.You will be redirected back to the same page, but this time with two new boxes,Input Fields and Output Fields. The Input Fields box is used to define any fields that require information from the user. Any input fields referenced to in the input string must be defined here. The Output Fields box is used to define each field that you expect back from the script. All data input methods must have at least one output field defined, but may have more for a script.
To define a new field, click Add next to the input or output field boxes. You will be presented with some or all of the fields below depending on whether you are adding an input or output field.
When you are finished filling in all necessary fields, click the Create button to continue.You will be redirected back to the data input method edit page. From here you can continue to add additional fields, or click Save on this screen whenfinished.
步骤二: Making Your Scripts Work With Cacti
The simplest way to extend Cacti's data gathering functionality is through external scripts.Cacti comes with a number of scripts out of the box which are located in the scripts/ directory. These scripts are used by the data input methods that are present in a new installation of Cacti.
To have Cacti call an external script to gather data you must create a new data input method,making sure to specify Script/Command for the Input Type field. See the previous section, Creating a Data Input Method for more information about how to create a data input method. To gather data using your data input method, Cacti simply executes the shell command specified in the Input String field. Because of this, you can have Cacti run any shell command or call any script which can be written in almost any language.
What Cacti is concerned with is the output of the script. When you define your data input method, you are required to define one or more output fields. The number of output fields that you define here is important to your script\'s output.
Data input methods with more than one output field are handled a bit differently when writing scripts. Scripts that output more than one value should be formatted like the following:
<fieldname_1>:<value_1> <fieldname_2>:<value_2> ... <fieldname_n>:<value_n> ## Example script output using 3 fields 1min:0.40 5min:0.32 10min:0.01
Note: Spine requires, that multiple parameters are spit out by a single “print” statement. Do not printthem in a loop!
下面就来演示一下,进行自定义监控的整个流程。
回忆经过rrdtool 画图的步骤:
一、编写脚本,获取数据
二、周期性执行上述脚本获取数据
三、rrdtool create 建立数据源,而且经过 rrdtool update 更新数据
四、而后经过 rrdtool graph 进行绘图
步骤1、建立 Data Input Method , 搜集数据
[root@bogon ~]# vi tcpconn.sh #!/bin/bash # $1: hostname or ip # $2: snmp community if [[ $# -ne 2 ]];then echo "Usage: $0 [hostname | IP] snmp_community" exit fi SNMPNETSTAT=/usr/bin/snmpnetstat ESTABLISHED=$(${SNMPNETSTAT} -v2c -c $2 -Can -Cp tcp $1 | grep -i 'established' | wc -l) echo -n "established:$ESTABLISHED" ## 为脚本添加可执行权限 # chmod +x tcpconn.sh ## 把脚本复制到cacti安装目录的scripts目录下(重要) <path_cacti> # cp tcpconn.sh /web/cacti/scripts/
注意:
数据收集方法的输出格式比较特殊,并且规定必须按照这样的输出格式才行。
必须定义的数据输入方法的脚本 放到cacti <path_cacti> 安装目录下的 scripts 目录下,并赋予可执行权限
官方文档这样描述: Data inputmethods with more than one output field are handled a bit differently whenwriting scripts. Scripts that output more than one value should be formattedlike the following:
<fieldname_1>:<value_1> <fieldname_2>:<value_2> ... <fieldname_n>:<value_n>
步骤2、在Cacti 上添加数据输入方法(把数据输入方法附加到cacti,经过cacti来周期性的调用它)
【Data Input Methods】 - 【Add】
接下来你应该看到以下,填入相应的信息。
Please fill in Name, select Script/Command as Input Type and provide the command that should be used to retrieve the data. You may use <path_cacti> as a symbolical name for the path_to_your_cacti_installation. Those commands will be executed from crontab; so pay attention to providing full path to binaries if required (e.g./usr/bin/perl instead of perl). Enter all Input Parameters in <> brackets.
怎么执行咱们的脚本呢?
/bin/bash。
脚本的具体路径呢?
能够引用<path_cacti>, cacti 预先定义好的宏。
咱们的脚本须要传递参数,该如何传递呢?
<path_cacti>是一个宏,引用cacti的安装目录
若是脚本须要接受参数,请用<>括起来
而后点击"Create",你能够看到以下界面。下面的Input Fields, Output Fields,分别对应参数输入以及脚本的输出。
有几个Input Fields就须要Add几回,一样有多少个Output Fields也须要Add多少次。
添加Input Fields】
咱们看到,cacti已经定义了许多 Speial Type Code。它们相似于宏,咱们可以继承它并可以取得相应device相关的信息。咱们知道图形和数据收集方法都必须关联到特定的device上,在定义device时,就会指定其hostname等相关信息。若是咱们没有提供输入的参数,就让它自动去继承device的那些宏。若是使用hostname,那么此脚本应用到哪一个设备上,就会得到那个设备的hostname。若是snmp_community,那么就会自动引用咱们在添加设备时,指定的那个community。
好比,我在定义device时,填入的信息以下
那么,此时的Special Type Code: hostname==127.0.0.1, snmp_community==public
【添加Output Fields】
注意,当咱们接收到一个数据以后,就须要更新RRD文件,因此"Update RRD File"这里必须勾选。
三、定义数据模板(定义如何存储,如何建立 rrd 文件)
上一步中,咱们定义了数据收集方法并应用到cacti上,那么收集到的数据如何保存呢?保存在什么地方?如何建立rrd文件? 这些都是数据模板完成的,因此接下来咱们要定义数据模板。
而后,下面的定义,咱们能够参考其余的数据模板进行填写,若是不知道该怎么填,不妨先看看其余的数据模板是如何定义的。
咱们看到Name这里比较特殊 "|host_description|", 这样能自动获取device的Description。 以及"Internal Data Source Name",数据源的名称,注意其命名规则,a_zA_Z0-9_,而且不超过19个字符。
四、当咱们把数据模板应用到某个device上时,就建立数据源(Data Sources)
此时完成: rrdtool create , rrdtool update
注意,数据源必定是关联到某个device上的。咱们必须指定Host,而后再Add
而后选择对应的数据模板(刚才咱们添加的)和须要关联的主机。
此步骤完成后,就会建立相应的rrd文件了。
固然,这里的"Data Source Path"能够根据本身须要命名。而后稍等片刻,就会在/web/cacti/rra目录下建立rrd文件。
五、添加图形模板(定义如何绘图)
图形模板,也能够参考其余的模板进行填写
填写一些关于图形的基本信息(name, title),其余保持默认或者根据本身须要填写。点击"建立"以后,咱们还须要更详细的设置。
"Graph Item"是什么? 图形项,至关于图形上的线条、区域块、注释等等元素。每个Item表示一个元素。
而后Add "Graph Item Inputs"
六、把图形模板关联到device (执行 rrdtool graph)
console -> Graph Management -> 指定Host -> Add
好了,终于搞定了。
若是想要显示下面这种效果,该怎么作?
从新编辑咱们的graph templates, 点击咱们须要更改的模板名称:
在下面的设置中,"Graph Item Type"选择 GPRINT。
OK,就先说到这里,更多更高级的功能,还但愿你们去挖掘。
咱们在操做的过程当中,其实思路是比较清晰的,可是面对众多的选项,殊不知道如何是好,若是你了解rrdtool,这些都不是事儿,若是不了解,可能看起来就至关痛苦,由于你不知道具体在作什么?万丈高楼平地起,因此我仍是建议各位去看看rrdtool是如何工做的。
http://blog.sina.com.cn/s/blog_6e88948f01014ze7.html