Ganglia 3.1.x下扩展Python模块(翻译自官方wiki)


仅供参考,不负责任

Gmond扩展Python度量模块

在最新的Ganglia3.1.x版本中,我们可以创造出C/Python的度量收集模块,这些模块能被直接插入到gmond中以用来监视用户自定义的度量。
在早期的版本中(2.5.x,3.0.x),添加用户自定义的度量只有通过命令行调用gmetric这么一种方法,它可以简单地通过一个cronjob或者其他进程将度量插入到gmond中。虽然这种方法对大多数人来说是可行的,但是他使得用户自定义的度量难以管理。
本文将介绍自己编写一个python度量监视模块的一些细节问题

以下的组件是建立/使用Python支持所必须的

  • Ganglia 3.1.x
  • Python 2.3.4+(这是在RHEL4上测试的最老的支持版本,2.3以后的版本应该都能正常工作)
  • Python开发头文件(通常在python-devel这个软件包中) 

安装

RPM安装

如果你安装Python度量模块在基于RPM软件包管理的系统上,安装“ganglia-gmond-modules-python”这个软件包即可。这包含了Python模块开发的所有东西。

APT安装

执行 apt-get install ganglia-monitor
然后看下面的注意事项

源码安装

如果你是从源码编译的,确保添加了 –with-python这个选项。如果Python解释器被检测到,这个选项是会被自动添加

检查

为了确保Ganglia的安装有了Python支持的设置,检查一下以下设置:

  • gmond.conf 有一行 include (“/etc/ganglia/conf.d/*.conf”),这是你应该放置.pyconf格式的python模块配置文件所在的目录。
  • modpython.conf 这个文件应该存在于/etc/ganglia/conf.d 他包含了pyconf文件的所在位置
  • modpython.so应该在/usr/lib{64}/ganglia中
  • /usr/lib{64}/ganglia/python_modules这个目录应该存在,这是你放置以.py结尾的python模块文件所在的目录

如果你通过二进制文件安装python模块支持,上面的这些应该会被自动生成。假如有些文件缺失的话,请把bug提交给我们

Ubuntu 10.10 注意事项

Ubuntu 10.10 没有Python支持的设置,你需要:

  • 建立/etc/ganglia/conf.d/modpython.conf,让他看起来像这样,https://sourceforge.net/apps/trac/ganglia/browser/trunk/monitor-core/gmond/modules/conf.d/modpython.conf.in
modules {
module {
name = "python_module"
path = "/usr/lib(64)/ganglia/modpython.so"
params = "/usr/lib(64)/ganglia/python_modules"
}
}
include('/etc/ganglia/conf.d/*.pyconf')
  • 建立目录 /usr/lib(64)/ganglia/python_modules
  • 确保 /usr/lib(64)/ganglia/modpython.so 已经存在(如果你通过apt安装的话,Ubuntu 10.10 将会默认有这个文件)

编写自定义Python模块

编写一个Python模块是非常简单的。你只需要根据模板编写,完成之后将以.py结尾的模块放到/usr/lib(64)/ganglia/python_modules下面。同时将对应的.pyconf格式的配置文件放置在/etc/ganglia/conf.d/下。
如果你的Python模块需要访问服务器上的一些文件,记住他是以运行gmond进程的用户执行的。换句话说,如果gmond以nobody运行,那么你的模块就是nobody运行。所以确保运行gmond的用户有权限访问这些文件。Ganglia有示例模块在/usr/lib(64)/ganglia/python_modules/example.py中。

示例模块

让我们来看一下一个监测主机温度的实在的Python模块实例,它通过读取/proc文件系统,让我们称他为temp.py

acpi_file = "/proc/acpi/thermal_zone/THRM/temperature"

def temp_handler(name):
try:
f = open(acpi_file, 'r')

except IOError:
return 0

for l in f:
line = l.split()

return int(line[1])

def metric_init(params):
global descriptors, acpi_file

if 'acpi_file' in params:
acpi_file = params['acpi_file']

d1 = {'name': 'temp',
'call_back': temp_handler,
'time_max': 90,
'value_type': 'uint',
'units': 'C',
'slope': 'both',
'format': '%u',
'description': 'Temperature of host',
'groups': 'health'}

descriptors = [d1]

return descriptors

def metric_cleanup():
'''Clean up the metric module.'''
pass

#This code is for debugging and unit testing
if __name__ == '__main__':
metric_init({})
for d in descriptors:
v = d['call_back'](d['name'])
print 'value for %s is %u' % (d['name'], v)     Python 的详细介绍:请点这里
Python 的下载地址:请点这里

推荐阅读:

《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码]

Python脚本获取Linux系统信息

  • 1
  • 2
  • 下一页

相关内容