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