Winse Blog

走走停停, 熙熙攘攘, 忙忙碌碌, 不知何畏.

Ganglia扩展-Python

最简单的添加metric的方式使用 gmetric

1
/usr/local/ganglia/bin/gmetric -n "TITLE" -v VALUE -t int16 -g GROUP

有时指标计算复杂,通过简单的shell不能满足功能需要。我们可以使用python模块来定制。

安装

默认安装会检查Python环境,符合条件会自动的安装Python模块。

1
2
3
4
5
6
7
8
9
10
11
12
[root@cu2 ganglia-3.7.2]# yum install -y python-devel
[root@cu2 ganglia-3.7.2]# ./configure --with-gmetad --enable-gexec --enable-status --prefix=/usr/local/ganglia
...
Checking for python
checking for python... /usr/bin/python
checking Python version... 2.6
checking Python support... yes
checking Perl support... no
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
...
[root@cu2 ganglia-3.7.2]# make && make install

安装成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[root@cu2 ganglia]# pwd
/usr/local/ganglia
[root@cu2 ganglia]# ll lib64/ganglia/
total 704
-rwxr-xr-x 1 root root 87344 Feb  1 16:52 modcpu.so
-rwxr-xr-x 1 root root 84566 Feb  1 16:52 moddisk.so
-rwxr-xr-x 1 root root 17896 Feb  1 16:52 modgstatus.so
-rwxr-xr-x 1 root root 84526 Feb  1 16:52 modload.so
-rwxr-xr-x 1 root root 86280 Feb  1 16:52 modmem.so
-rwxr-xr-x 1 root root 31695 Feb  1 16:52 modmulticpu.so
-rwxr-xr-x 1 root root 84928 Feb  1 16:52 modnet.so
-rwxr-xr-x 1 root root 84246 Feb  1 16:52 modproc.so
-rwxr-xr-x 1 root root 53994 Feb  1 16:52 modpython.so
-rwxr-xr-x 1 root root 85584 Feb  1 16:52 modsys.so
[root@cu2 ganglia]# ll etc/conf.d/
total 4
-rw-r--r-- 1 root root 408 Feb  1 16:52 modpython.conf

[root@cu2 ganglia]# vi etc/gmetad.conf
 rrdtool_dir

[root@cu2 ganglia]# cat etc/conf.d/modpython.conf 
/*
  params - path to the directory where mod_python
           should look for python metric modules

  the "pyconf" files in the include directory below
  will be scanned for configurations for those modules
*/
modules {
  module {
    name = "python_module"
    path = "modpython.so"
    params = "/usr/local/ganglia/lib64/ganglia/python_modules"
  }
}

include ("/usr/local/ganglia/etc/conf.d/*.pyconf")

Hello World

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[root@cu2 ganglia]# cd lib64/ganglia/
[root@cu2 ganglia]# mkdir python_modules
[root@cu2 ganglia]# cd python_modules/

[root@cu2 python_modules]# cp ~/ganglia-3.7.2/gmond/python_modules/example/example.py ./
[root@cu2 python_modules]# 

[root@cu2 python_modules]# cd /usr/local/ganglia/etc/conf.d/
[root@cu2 conf.d]# vi example.pyconf
modules {
  module {
    name = "example"
    language = "python"
    param RandomMax {
      value = 600
    }
    param ConstantValue {
      value = 112
    }
  }
}

collection_group {
  collect_every = 10
  time_threshold = 50
  metric {
    name = "PyRandom_Numbers"
    title = "Random"
    value_threshold = 70
  }
  metric {
    name = "PyConstant_Number"
    title = "Constant"
    value_threshold = 70
  }
}

[root@cu2 conf.d]# service gmond restart

example.py 初始化函数 metric_initexample.pyconf 文件获取配置、返回可用指标对象( call_back 关联执行的handler; groups 数据的分组)。

模块中必须包含的三个方法是:

  • def metric_init(params):
  • def metric_cleanup():
  • def metric_handler(name):

前面两个方法的名字必须是一定的,而最后一个 metric_handler与 metric_init 返回对象的callback对应。__main__ 函数用于debug,可以单独调试该模块,以检测是否有错。 更详细的内容看官网的文档Ganglia-GMond-Python-Modules

参考

–END

Comments