---
title: "Ganglia python扩展 - 配置文件说明"
author: "wks3000"
date: "2015-08-23T18:54:02Z"
tags: ["param", "params"]
categories: ["开源软件"]
slug: "ganglia-python"
draft: false
lastmod: "2015-08-23T18:54:02Z"
id: 2237448
url: "https://wks3000.iteye.com/blog/2237448"
---

一、.pyconf配置说明

modules {

  module {

    name     = "example"     //指标名称，也是生成.rrd文件的文件名，此处跟.py文件名保持一致

    language = "python"

    enabled  = yes             //  取值yes或者no

 

    param RandomMax {

      value = 600

    }

    param ConstantValue {

      value = 112

    }

  }

}

 

collection\_group {         //  只有配置collection\_group，gmod才会去采集相关metric数据

  collect\_every  = 10       //   采集间隔时间，单位：秒

  time\_threshold = 50    //   发送全部collection\_group数据的最长时间间隔，单位：秒

 

  metric {                                               //  定义采集哪些metric

    name = "PyRandom\_Numbers"      //  与metric\_init中定义的metric name保持一致

    title = "You metric name displayed on gweb"   // 定义在gweb界面上显示的名字，不填写时使用name定义

    value\_threshold = 90         //  当超过此阀值时就会发送全部collection\_group的数据，不管time\_threshold时间是否到了

  }

}

 

man gmond.conf中有关于collection\_group的更说细说明和用法：

 

* * *

collection\_group

You can specify as many collection\_group section as you like within the limitations of memory.  A collection\_group has the following attributes: collect\_once,

collect\_every and time\_threshold.  A collection\_group must also contain one or more metric sections.

 

The metric section has the following attributes: (one of name or name\_match; name\_match is only permitted if pcre support is compiled in), value\_threshold and title.

For a list of available metric names, run the following command:

 

% gmond -m

 

Here is an example of a collection group for a static metric...

 

collection\_group {

collect\_once   = yes

time\_threshold = 1800

metric {

name = "cpu\_num"

title = "Number of CPUs"

}

}

 

This collection\_group entry would cause gmond to collect the cpu\_num metric once at startup (since the number of CPUs will not change between reboots).  The metric

cpu\_num would be send every 1/2 hour (1800 seconds).  The default value for the time\_threshold is 3600 seconds if no time\_threshold is specified.

 

The time\_threshold is the maximum amount of time that can pass before gmond sends all metrics specified in the collection\_group to all configured udp\_send\_channels.

A metric may be sent before this time\_threshold is met if during collection the value surpasses the value\_threshold (explained below).

 

Here is an example of a collection group for a volatile metric...

 

collection\_group {

collect\_every = 60

time\_threshold = 300

metric {

name = "cpu\_user"

value\_threshold = 5.0

title = "CPU User"

}

metric {

name = "cpu\_idle"

value\_threshold = 10.0

title = "CPU Idle"

}

}

 

This collection group would collect the cpu\_user and cpu\_idle metrics every 60 seconds (specified in collect\_every).  If cpu\_user varies by 5.0% or cpu\_idle varies

by 10.0%, then the entire collection\_group is sent.  If no value\_threshold is triggered within time\_threshold seconds (in this case 300), the entire collection\_group

is sent.

 

Each time the metric value is collected the new value is compared with the old value collected.  If the difference between the last value and the current value is

greater than the value\_threshold, the entire collection group is send to the udp\_send\_channels defined.

 

It’s important to note that all metrics in a collection group are sent even when only a single value\_threshold is surpassed.

 

In addition a user friendly title can be substituted for the metric name by including a title within the metric section.

 

By using the name\_match parameter instead of name, it is possible to use a single definition to configure multiple metrics that match a regular expression.  The perl

compatible regular expression (pcre) syntax is used.  This approach is particularly useful for a series of metrics that may vary in number between reboots (e.g.

metric names that are generated for each individual NIC or CPU core).

 

Here is an example of using the name\_match directive to enable the multicpu metrics:

 

metric {

name\_match = "multicpu\_(\[a-z]+)(\[0-9]+)"

value\_threshold = 1.0

title = "CPU-\\\\2 \\\\1"

}

 

Note that in the example above, there are two matches: the alphabetical match matches the variations of the metric name (e.g. idle, system) while the numeric match

matches the CPU core number.  The second thing to note is the use of substitutions within the argument to title.

 

If both name and name\_match are specified, then name is ignored.

 

* * *

二、metric\_init中metric定义说明

metric\_init函数中可以配置多个metric（每个metric都是一个字典），每个metric有十项配置，至少要配置前八项：

> metric = {
> 
> 'name': 'PyRandom\_Numbers',
> 
> 'call\_back': Random\_Numbers,
> 
> 'time\_max': 90,
> 
> 'value\_type': 'uint',
> 
> 'units': 'N',
> 
> 'slope': 'both',
> 
> 'format': '%u',
> 
> 'description': 'Example module metric (random numbers)',
> 
> 'groups': 'example,random',
> 
> 'extra\_data':'extradata'
> 
> }
> 
> 有两点需要说明：
> 
> 1.   其中每一项的值都可以不配置，为空时fill\_metric\_info函数会赋给一个默认值。
> 
> 2.   fill\_metric\_info函数不会判断extra data的key的全法性，因为key是自定义的，只会检查其 value 的合法性，不为string类型时打印错误。