MRTG教程(二):MRTG配置文件的生成工具cfgmaker(上)

MRTG配置文件的生成工具cfgmaker

如何使用cfgmaker工具來創建你所需要的mrtg的配置文件。

 
內容

cfgmaker
的說明
配置

過濾器的詳細介紹

預定義的過濾器名稱

各種過濾器的例子

模板詳細介紹

可寫的模板變量

預定義的模板變量

主機和系統相關的變量

接口目標相關的變量

接口網絡配置變量

接口名稱/描述以及別名變量

模板的助手函數

模板文件的例子

Template Example 1: Eliminating Rejected Targets From Appearing
Template Example 2: Simplier Version of Example 1
Template Example 3: Creating CPU Targets for Hosts
實例
 
 
 
 

cfgmaker的說明
cfgmaker
根據某個路由器或其他SNMP可管理設備獲取的信息來創建MRTG的配置文件。:

[community@]router


Coomunity
是你所創建配置文件的設別的community名稱。如果沒有指定,缺省爲 'public' 如果你不知道某個設備的community名稱,可以先試試這個設備。如果使用的community名稱不正確,將無法從設備中獲得任何響應信息。


路由器是SNMP可管理設備的IP地址或者是DNS域名。你可以爲該名稱用 : 分割來指定6項更多的選項。語法如下::

router[:[prt][:[tmout][:[retr][:[backoff][:vers]]]]]


特別有意思的是最後一項參數, vers 。如果將該參數設置爲 2 , 將通過SNMP V2來查詢你的設備。它允許你提取設備中的64位的流量計數器,並且對於快速的接口來說能夠更加有效(不會有計數器溢出情況的發生)。注意在命令行中的router出現的順序與所生成的配置文件中的參數順序一致。第一個指定的router其配置文件的配置信息行將首先被生成,其後的配置行是下一個router的,以此類推。


注意cfg配置文件的第一行會包含要來生成配置文件使用的所有命令行選項。這主要是爲了然後在增加新主機或進行其他全局修改時,能夠很容易地再次生成配置文件。

配置
除了 --output --global 選項以外,其他所有的選項僅僅影響命令行中跟在其後的router。如果命令行中的某個選項在後面再次以其他值出現,新的值將覆蓋舊的值並對後續的router起作用。這樣選項可以爲路由器組或者獨立的路由器進行裁減。

參看 --output --global 選項來了解其在命令行中何處出現、出現次數如何影響所生成的配置文件。

參看後面給出的例子來了解如何爲多個路由器設置不同的選項。

--help
打印幫助信息並退出。
--man
打印手冊信息並退出。

--version
打印cfgmaker的版本信息。cfgmaker的版本應當與所使用的mrtg的版本一致。


--ifref nr|ip|eth|descr|name
選擇接口識別方式。缺省爲 nr ,表示通過路由器編號來識別路由器的接口。不幸的是SNMP樹中的接口編號是可以改變的。某些路由器會在增加新的接口後更改其編號,另一些更搞笑,他們會在每次月圓的的日子更改他們的編號。


爲了搞定這樣令人沮喪的問題,mrtg能夠通過其他4中屬性來識別接口。這四種方式沒有一種能夠適用與所有的接口,不過你可以選一種適合你的方式。注意特別是某些路由器的所有接口卡都用相同的以太網地址,從而導致問題頻出。

Select ip to identify the interface by its IP number. Use eth to use the ethernet address for identification. Use descr to use the Interface description. or use name to use the Interface name.

If your chosen method does not allow unique interface identification on the device you are querying, cfgmaker will tell you about it.

--ifdesc nr|ip|eth|descr|name|type|alias
Select what to use as the description of the interface. The description appears in the Title[] property for the target as well as the text header in the HTML code defined in the target's PageTop[]. Default is to use nr which is just the interface number which isn't always useful to the viewer of the graphs.

There are 6 other properties which could be used. Use ip if you want to use the interface's IP-address. Use eth if you want to use the interface's ethernet address. If you want a better description, you can use either descr, name or alias. Exactly what each of these do varies between different equipment so you might need to experiment. For instance, for a serial interface on a Cisco router running IOS using name might result in "S0" being the interface description , descr might result in "Serial0" and alias might result in "Link to HQ" (provided that is what is used as the interface's description in the router's configuration).

Finally, if you want to describe the interface by it's Btype (i.e "ethernetCSMA", "propPointtoPoint" etc) you can use type.

--if-filter 'filter-e­xpression'
First of all, this is under some developement and is experimental.

Use this if you want to have better control over what interfaces gets included into the configuration. The filter-e­xpression is evaluated as a piece of Perl code and is expected to return a truth value. If true, include the interface and if false, exclude the interface.

For a further discussion on how these filters work, see the section DETAILS ON FILTERS below.

--if-template 'template-file'
First of all, this is under some development and is experimental.

Use this if you want to control what the line for each target should look like in the configuration file. The contents of the file template-file will be evaluated as a Perl program which generates the lines using certain variables for input and output.

For a further discussion on how these templates work, see the section DETAILS ON TEMPLATES below.

--host-template 'template-file'
First of all, this is under some development and is experimental.

Use this if you want to have some extra targets related to the host itself such as CPU utilization, ping response time to the host, number of busy modems etc. The contents of the file template-file will be evaluated once per host as a Perl program which generates the lines using certain variables for input and output.

For a further discussion on how these templates work, see the section DETAILS ON TEMPLATES below.

--community 'community-string'
Use this to set the community for the routers following on the command line to community-string. Individual routers might overrride this community string by using the syntax community@router.
--enable-ipv6 This option enables IPv6 support. It requires the appropriate perl modules; if they are not found then IPv6 is disabled (see the ipv6 documentation).

cfgmaker will use IPv6 or IPv4 depending on the target. If the target is a numeric address, the protocol depends on the type of address. If the target is a hostname, cfgmaker will try to resolve the name first to an IPv6 address then to an IPv4 address.

IPv6 numeric addresses must be specified between square braces.

For example:

cfgmaker --enable-ipv6 [2001:760:4::1]:165:::2


If the target has both an IPv6 address and an IPv4 address with the same hostname, cfgmaker first queries the target using IPv6 and falls back to IPv4 if it fails. This is useful for targets which don't support SNMP over IPv6.

--use-16bit This option forces the use of 16bit SNMP request IDs. Some broken SNMP agents do not accept 32bit request IDs. Try to avoid this option as much as possible, complain to your agent vendor instead.

--snmp-options :[port][:[timeout][:[retries][:[backoff][:version]]]]
Use this to set the default SNMP options for all routers following on the command line. Individual values might be omitted as well as trailing colons. Note that routers might override individual (or all) values specified by --snmp-options by using the syntax

router[:[port][:[timeout][:[retries][:[backoff][:version]]]]]


--global bla: abc
Use this to add global options to the generated config file. You can call --global several times to add multiple options. The line will appear in the configuration just before the config for the next router appearing on the command line.

--global "workdir: /home/mrtg"
If you want some default Options you might want to put
--global "options[_]: growright,bits"
Specifying --global after the last router on the command line will create a line in the configuration file which will appear after all the routers.
--noreversedns Do not try to reverse lookup IP numbers ... a must for DNS free environments.
--no-down Normally cfgmaker will not include interfaces which are marked anything but administratively and operationally UP. With this switch you get them all.
--show-op-down Include interfaces which are operatively down.
--zero-speed speed
  Assign this speed in bits-per-second to all interfaces which return 0 for ifSpeed and ifHighSpeed. Some switches, notably Foundry equipment, return a speed of zero for some interfaces. For example, to have all interfaces reporting zero set to 100Mbps, use --zero-speed=100000000.
--subdirs format
  Give each router its own subdirectory for the HTML and graphics (or .rrd) files. The directory name is the given format string with a couple of pattern replacements. The string HOSTNAME will be replaced by the hostname of the router (however you specified it on the cfgmaker commandline -- it may be an actual hostname or just an IP address), and SNMPNAME will be replaced with the device's idea of its own name (the same name that appears on the right side of the Title lines). For instance, a call like:

cfgmaker --subdirs=HOSTNAME__SNMPNAME [email protected]


would result in the generation of lines looking something like:

Directory[10.10.0.18_1]: 10.10.0.18__fp2200-bothrip-1.3



--output file Write the output from cfgmaker into the file file. The default is to use STDOUT. --output is expected to appear only once on the command line. If used multiple times, the file specified by the last --output will be used.
--nointerfaces Don't generate configuration lines for interfaces.

This makes cfgmaker skip all steps related to interfaces which means it will not do any polling of the router to retrieve interface information which speeds up the execution of cfgmaker and it will neither run any interface templates.

--interfaces This makes cfgmaker generate configuration lines for interfaces (the default behaviour).

The main usage of this option is to negate an --nointerfaces appearing earlier on the command line.


過濾器的詳細介紹
過濾器的目的是確定接受哪些接口,拒絕哪些接口。這是通過爲每個接口執行其過濾器表達式,並根據執行結果來確定的,過濾器表達式是一小段perl代碼。如果執行結果爲 true , 則接受該接口,否則拒絕。

在使用過濾器時,要記住perl有自己的 true false 的判斷標準。 空串和 字符串 0 都是 false 整數值 0 以及其他任何未定義的值都是 false 任何應用都是 true.

由於過濾器是perl代碼,以下perl表達式需要了解。
:

表達式可以用括號分組 () . 表達式可以用以下布爾操作符進行組合。


and (equivalent with && )
Boolean and of the two e­xpressions, is only true if both e­xpressions are true. Example: e­xpression1 and e­xpression2
or (equivalent with || )
Boolean or of the two e­xpressions, is true if either or both e­xpressions are true. Example: e­xpression1 or e­xpression2
not (equivalent with ! )
Boolean negation of a single e­xpression. Example: not e­xpression . Yet another example: !e­xpression
(For more details on this I recommend a book on Perl)

預定義的過濾器名稱
爲了方便起見,在過濾器中有許多預定義的變量。注意這些變量在接口模板中同樣可用。(不是主模板)

Caveat: All these variables' names begin with a dollar sign ($), which is a syntactic requirement for scalar variables in Perl. The danger here is that the dollar sign in many shells is an active character (often used for shell variables exactly as in Perl variables) so it is important to ensure that the Perl e­xpression isn't evaluated by the command line shell as shell code before being passed to cfgmaker as command line arguments. In shells like Bourne shell, ksh shell or bash shell, placing the entire e­xpression within single qoutes will avoid such accidental evaluation:

--if-filter=($default_iftype && $if_admin)


$if_type
This is an integer specifying the interface type as per the SNMP standards and as reported by the polled device. A complete list of interface types would be impractical for this document , but there are a number predefined varables below. Normally, cfgmaker puts in the target's PageTop this iftype value within paranthesis after the name of the interface type. (e.g propPointToPointSerial (22) ).

Here's a list of some of the most common interface types by number:

  6 ethernetCsmacd

  7 iso88023Csmacd

  9 iso88025TokenRing

15 fddi

19 E1

20 basicISDN

21 primaryISDN

22 propPointToPointSerial

23 ppp

24 softwareLoopback

30 ds3

32 frame-relay

33 rs232

37 atm

39 sonet

44 frameRelayService

46 hssi

49 aal5

53 propVirtual

62 Fast Ethernet (100BaseT)

63 ISDN & X.25

69 Full Duplex Fast Ethernet (100BaseFX)

94 Asymetric Digital Subscriber Loop (ADSL)

117 Gigabit Ethernet

134 ATM Sub Interface



=item B<$default>


True if and only if cfgmaker normally should accepted the interface based on the interfaces administrative and operational state (taking the flags --no-down and --show-op-down into account) and it's type (and a few other things).

$default_ifstate
True if and only if cfgmaker would have accepted the interface based on it's operational and administrative states (also taking into account the presence of the flags --no-down and --show-op-down).
$default_iftype
True if and only if cfgmaker would have accepted the interface based on it's type (and a few type specific details in addition).
$if_admin
True if and only if the interface is in an adminstrative up state.
$if_oper
True if and only if the interface is in an operational up state.
A number of variables are also predefined to easily decide if an interface belong to a certain cathegory or not. Below is all those variables listed together with which if_type numbers each variable will be true for. Note that some variables refer to other variables as well.

$if_is_ethernet
True for ethernet interfaces (nr 6, 7, 26, 62, 69 and 117).
$if_is_isdn
True for various ISDN interface types (nr 20, 21, 63, 75, 76 and 77)
$if_is_dialup
True for dial-up interfaces such as PPP as well as ISDN. (nr 23, 81, 82 and 108 in addition to the numbers of $if_is_isdn).
$if_is_atm
True for miscellaneous ATM related interface types (nr 37, 49, 107, 105, 106, 114 and 134).
$if_is_wan
True for WAN interfaces point to point, Frame Relay and High Speed Serial ( 22,32,44,46)
$if_is_lan
True for LAN interfaces (8, 9, 11, 15, 26, 55, 59, 60 and 115 in addition to the numbers of $if_is_ethernet).
$if_is_dsl
True for ADSL, RDSL, HDSL and SDSL (nr 94, 95, 96, 97)
$if_is_loopback
True for software loopback interfaces (nr 24)
$if_is_ciscovlan
True for Cisco VLAN interfaces (interfaces with the word Vlan or VLAN in their ifdescs)
$if_vlan_id
Returns the vlan id associated with a specific port on Cisco Catalyst switches under both Catalyst OS and IOS. If it is not a vlan interface, will return undef.
$if_MTU
Returns the Maximum Transfer Unit associated with a specific port.
Besides that, you can also use the variables defined for templates below. Further, all the variables available in cfgmaker is at the scripts disposal even if the use of such features is discouraged. More shortcuts in the form of variables and functions will be made avaiable in the future instead.
各種過濾器的例子

The following filter will not affect which interfaces get's included or excluded, it will make cfgmaker behave as normally.

'--if-filter=$default'


The following filter will make cfgmaker exclude PPP (23) interfaces:

'--if-filter=$default && $if_type!=23'


The following filter will make cfgmaker behave as usual except that it will consider the operational state of an interface irrelevant but still reject all interfaces which are administratively down.:

'--if-filter=$if_admin && $default_iftype'


模板詳細介紹
The contents of the template files are evaluated as a Perl program. A number or Perl variables are available for the program to read and others are used to be written to.

As quite a few of the predefined variables has values which are are supposed to be used in HTML code some of them have an HTML-escaped`variant, e.g $html_syslocation is the HTML escaped variant of $syslocation. The HTML escaping means that the chars `< , > and & are replaced by < , > and & and that newlines embedded in the string are prepended with <BR> and appended with a space character (if a newline is last in the string it is not touched).

可寫的模板變量
These are the variables available to store the configuration lines in. Some of them are initialized prior to the evaluation of the template but such content normally is comments for inclusion in the final configuration file so those variables might be reset to the empty string in the template code to eliminate the comments. The other way around is also possible, the contents of these variables might be extended with further information for various reasons such as debugging etc.

Once the template has been evaluated, the following happens: if the template is a interface template and the actual interface for some reason is rejected and thus needs to be commented out, all the lines in the variable $target_lines are turned into comments by adding a hash mark ( # ) at their beginning. Then all the variables $head_lines, $problem_lines , $target_lines and $separator_lines are concatenated together to form the lines to add to the configuration file.

$target_lines
This variable is the placeholder for the configuration lines created by the template. $target_lines is predefined to be empty when the template code is evaluated.
$head_lines
This variable is intended to be the placeholder for the comment line appearing just before the target in the configuration file. It is initialized with that comment line before the evaluation of the template code and if the template doesn't modify $head_lines during evaluation, the comment will look like usual in the config file.
$problem_lines
This variable is intended to be the placholder for the comment lines describing any problems which might have been encountered when trying to add the target into the configuration. For host templates it's normally not used and for those it's predefined as the empty string. For interface templates $problem_lines is predefined with the error description comments which cfgmaker normally would use for rejected interfaces or as the empty string for accepted interfaces.

It is possible to test against $problem_lines to find out if an interface will be included or rejected but this is not recommended. Test against $if_ok instead.

$separator_lines
This variable is the placeholder for the string to use as the separator between the code for individual targets. The contents of this variable is put after each target (so the lines will appear after the end of the last target in the config as well).
預定義的模板變量
All the variables below are available for interface templates to use. For host templates, only those listed under Host and System Variables are available.

For interface templates the variables listed under Predefined Filter Variables are also available.

主機和系統相關的變量
$router_name
This is the fully qualified name for the router. It is affected by the following items on the command line: the router name itself and --dns-domain.
$router_connect
This is the reference string for the router being polled. It is on the form community@router possibly followed by some snmp options. It is affected by the following items on the command line: the router name itself, --community, --snmp-options and --dns-domain. (There's no HTML escaped variant available)
$directory_name
This variable should contain the directory name as cfgmaker normally would use as the value for the Directory[] directive. The value is determined by the --subdirs command line option. If --subdirs isn't specified $directory_name will be the empty string. (There's no HTML escaped variant available)
$syscontact
This variable is the router's SNMP sysContact value. (HTML escaped variant: $html_syscontact)
$sysname
This variable is the router's SNMP sysName value. (No HTML escaped variant available)
$syslocation
This variable is the router's SNMP sysLocation value. (HTML escaped variant: $html_syslocation)
$sysdescr
This variable is the router's SNMP sysDescr value. It is normally not used by cfgmaker but might be useful in a template. (HTML escaped variant: $html_sysdescr)
接口目標相關的變量
$target_name
This is what cfgmaker normally would use as the the name of the target. The target name is what is found within the square brackets, [] , for target directives. (There's no HTML escaped variant available)
$if_ref
This the reference string for the interface. It is expected to be used in the Target[xyz] directive to distinguish what interface to use. The value of this variable is affected by the --ifref command line option. It is normally used together with $router_connect. (There's no HTML escaped variant available)
$if_ok
This variable is true if the interface is going to be included into the configuration file, otherwise false. Don't test against other variables such as $problem_lines to find out if an interface will be rejected or not, use this $if_ok instead.
$default_target_lines
This variable contains all the target lines which cfgmaker by default outputs for this interface. It's useful if you want to have the standard target but want to add some extra lines to it by using a template.
By default cfgmaker uses the following directives for each target it generates: Target[], SetEnv[], MaxBytes[], Title[], PageTop[] and if there is any directory specified also the Directory[] directive.

To facilitate the creation of templates which generates target configs which are similar to the default one, each of the above mentioned directive lines have a corresponding variable containing the line as cfgmaker would have output it by default.

Note that none of these have a HTML escaped variant, text in them is HTML escaped where needed. Also note that they do not have any newline at the end.

$default_target_directive
This variable contains the default string for the Target[] directive line.
$default_setenv_directive
This variable contains the default string for the SetEnv[] directive line.
$default_directory_directive
This variable contains the default string for the Directory[] directive line which means it is an empty string (with no newline) if there's no directory.
$default_maxbytes_directive
This variable contains the default string for the MaxBytes[] directive line.
$default_title_directive
This variable contains the default string for the Title[] directive line.
$default_pagetop_directive
This variable contains the default string for the PageTop[] directive lines.
接口網絡配置變量
$if_ip
This variable should contain the IP-address of the interface, if any has been assigned to it. (There's no HTML escaped variant available)
$ifindex
This variable is the SNMP ifIndex for the interface which per definition always is an integer. (There's no HTML escaped variant available)
$if_index
Equivalent with $ifindex.
$if_eth
Contains the ethernet address of the interface, if any. (There's no HTML escaped variant available)
$if_speed
This variable is the speed in bytes/second (with prefixes). (There's no HTML escaped variant available)
$if_speed_str
This variable is a cooked speed description which is either in bits or bytes depending on wether or not the bits option is active and also with the proper prefix for the speed (k, M, G etc). (No HTML escaped variant available)
$if_type_desc
This variable is a textual description of the interface type. (HTML escaped variant: $html_if_type_desc)
$if_type_num
This variable the integer value corresponding to the interface type (for a listing for the value for the more common interface types, see the section DETAILS ON FILTERS above). (No HTML escaped variant available)
$if_dns_name
This is the DNS name for the interface. (No HTML escaped variant available)
接口名稱/描述以及別名變量
It might seem confusing with both Name, Description and Alias in this context and to some extent it is. Name and Description are usually supported on most equipment but how they are used varies, both between manufacturers as well as between different cathegories of equipment from the same manufacturer. The Alias is at least supported by Cisco IOS, and that variable contains whatever is used in the IOS statement called description for the interface (not to be confused with the SNMP variables for Description).

For better control from the command line consider $if_title_desc which contents are controlled by the --if-descr command line option.

$if_snmp_descr
This variable should contain the raw description of the interface as determined by the SNMP polling of the router. (HTML escaped variant: $html_if_snmp_descr)
$if_snmp_name
The raw name for the interface as provided by SNMP polling. (HTML escaped variant: $html_if_snmp_name)
$if_snmp_alias
The raw ifAlias for the interface as provided by SNMP polling. (HTML escaped variant: $html_if_snmp_alias)
$if_cisco_descr
The raw CiscolocIfDescr for the interface as provided by SNMP polling. (HTML escaped variant: $html_if_cisco_descr)
$if_description
This is the cooked description string for the interface, taking into account the SNMP values found for the interface's RDescr, ifAlias and CiscolocIfDescr. (HTML escaped variant: $html_if_description)
$if_title
The full string cfgmaker by default would have used for the Title[] directive in the configuration as well as the content of the topmost H1 tag in the PageTop[]. Is composed by the contents of $desc_prefix, $if_title_desc and $sysname.

As $if_title depends on $if_title_desc, it is possible to indirectly control $if_title by using the command line option --if-descr.:

(HTML escaped variant: $html_if_title)


$if_port_name
If the host is a Cisco Catalyst LAN switch, this variable is the name of that port. (No HTML escaped variant available)
$desc_prefix
This variable is a prefix of the description of what the target is to use in the Title[] directive and in the H1 section of the PageTop[] . Default is Traffic analysis for . (HTML escaped variant: $html_desc_prefix)
$if_title_desc
This is the description of the interface normally used by cfgmaker as part of the variable $if_title. The latter is used as the full string in the Title[] directove and the H1 section in the PageTop[].

$if_title_desc is controlled by the command line option --if-descr which indirectly controls the contents of $if_title:

(HTML escaped variant: $html_if_title_desc


模板的助手函數
The following functions exists to facilitate the writing of host and interface templates.

html_escape(string)
html_escape() takes a string as an argument and returns a new string where the following substitutions has been done: the chars <`, > and & are replaced by < , > and & and that newlines embedded in the string are prepended with <BR> and appended with a space character (newlines at the end of the string are not touched).


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章