一、Gmond扩展Python度量模块
官方:http://sourceforge.net/apps/trac/ganglia/wiki/ganglia_gmond_python_modules
官方文档中文版:http://cryinstall.com/?p=184
查找本地gmond是否安装
# netstat -ano | grep 8649
或者
# talnet localhost 8649
查找gmond动态链接库文件路径
# locate */ganglia/*.so
/opt/ganglia/lib/python/gmon/Gmetricmodule.so
/opt/ganglia/lib/python/gmon/Networkmodule.so
/opt/ganglia/lib/python/gmon/Processmodule.so
/usr/local/lib64/ganglia/modcpu.so
/usr/local/lib64/ganglia/moddisk.so
/usr/local/lib64/ganglia/modload.so
/usr/local/lib64/ganglia/modmem.so
/usr/local/lib64/ganglia/modmulticpu.so
/usr/local/lib64/ganglia/modnet.so
/usr/local/lib64/ganglia/modproc.so
/usr/local/lib64/ganglia/modpython.so
/usr/local/lib64/ganglia/modsys.so
# cd /usr/local/lib64/ganglia/
modcpu.so moddisk.so modload.so modmem.so modmulticpu.so modnet.so modproc.so modsys.so
安装 ganglia-python-3.3.0-1.x86_64.rpm
# rpm -ivh ganglia-python-3.3.0-1.x86_64.rpm
# cd /usr/local/lib64/ganglia/
# ls
modcpu.so moddisk.so modload.so modmem.so modmulticpu.so modnet.so modproc.so modpython.so modsys.so python_modules
modpython.so 和 python_modules目录在安装ganglia-python后产生
扩展Python度量模块完成
二、开发python模块
查找python模块配置文件存放路径
# vim /etc/ganglia/gmond.conf
/include 搜索,一般会有如下格式的一行,该目录即为存放路径,如没有这行,手动添加一行
include ("/usr/local/etc/conf.d/*.conf")
创建配置文件foo.conf,让gmond知道这里有ganglia插件
# cd /usr/local/etc/conf.d
# vim foo.conf
粘贴下面的配置代码
modules {
module {
name = "foo" #模块名,该文件存放于params = "/usr/lib64/ganglia/python_modules"指定的路径下
language = "python" #声明使用python语言
params = "/usr/local/lib64/ganglia/python_modules" #指定编写的python脚本放置位置,可不写用默认
}
}
collection_group {
collect_every = 60
time_threshold = 60
metric {
name = "foo_count" #python主模块名称
title = "Foo Count" #动态库路径
value_threshold = 1.0 #编写的python脚本放置位置
}
}
编写自定义Python模块
# cd /usr/local/lib64/ganglia/python_modules
# vim foo.py
粘贴以下代码:
#coding=utf-8
import random
def get_foo_count(name):
'''可定制业务方法,返回一个整数值'''
return random.randrange(5, 100) + 10
def metric_init(params):
'''metric'''
global descriptors
d1 = {
'name': 'foo_count',
'call_back': get_foo_count, # 名字同业务方法一致
'time_max': 90, # 最大调度间隔时间
'value_type': 'uint',
'units': 'C',
'slope': 'both',
'format': '%u',
'description': 'Number of Foo',
'groups': 'foo_group'
}
descriptors = [d1]
return descriptors
def metric_cleanup():
"清除metric"
if __name__ == '__main__':
metric_init({})
for d in descriptors:
v = d['call_back'](d['name'])
print 'value for %s is %u' % (d['name'], v)
三、重启gmond
# /etc/init.d/gmond restart
(完)