ganglia系统监控2

利用ganglia监控nginx

第一步,配置nginx,开启nginx_status监控:
在nginx.conf中添加如下配置:
server {
    listen 12345;#监听的端口
    server_name 127.0.0.1;#当前机器的ip或域名
    location /nginx_status {
        stub_status on;
        access_log off;
        #allow xx.xx.xx.xx;#允许访问的IP地址
        #deny all;
        allow all;
    }
}
重启nginx,可以到http://127.0.0.1:12345/nginx_status查看结果

第二步,配置ganglia客户端,收集nginx_status数据:
在/etc/ganglia目录下新建conf.d和python_modules目录,把下载的扩展文件分别复制到这两个目录下,修改配置文件nginx_status.pyconf里的nginx监控访问路径和nginx启动文件的路径;接下来需要修改gmond.conf文件,添加如下两段:
在modules下添加python支持:
module {
      name = "python_module"
      path = "modpython.so"#这里需要确定ganglia安装目录下的lib64里是否有这个文件
      params = "/etc/ganglia/python_modules"
  }
在文件最后导入python配置:
include ("/etc/ganglia/conf.d/*.pyconf")
重启gmond服务:service gmond restart,这样就开始收集这台机器上的nginx监控数据。
如果没有modpython.so模块,可以yum -y install ganglia-gmond-python

第三步,配置监控前端页面:
在gmetad运行的机器上把下载到的graph.d下的文件复制到ganglia web页面目录下的graph.d目录下面。
然后修改conf.php,找到$optional_graphs,去掉注释,修改为 $optional_graphs = array('nginx_accepts_ratio','nginx_scoreboard'),保存,刷新监控页面即可看到监控图.
我的博客可以下载各种资源包.
gmond -m 查看程序错误情况.

2 python模块扩展

2.1 安装python模块进行功能扩展

yum install ganglia-gmond-python

2.2 检查是否安装成功

  1. gmond.conf 有这一行代码include ("/etc/ganglia/conf.d/*.conf").这个目录是放模块的配置文件的,python模块的配置文件的后缀名应该是.pyconf

  2. 在/etc/ganglia/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/lib/ganglia/python_modules"
    
    }
    
    }
    
     
    
    include ('/etc/ganglia/conf.d/*.pyconf')
    params 指明了python 模块存放的目录。

    include ('/etc/ganglia/conf.d/*.pyconf') 指明了python模块配置文件的目录。

  3. 在 /usr/lib/ganglia下有modpython.so。该文件是 Ganglia Python 扩展的动态库文件。

  4. /usr/lib/ganglia/python_modules文件夹存在。所有的python模块存放在这个位置,后缀名是.py 

2.3 定制一个pyphton模块

定制一个python模块很简单,只需按照一定的模板编写.py文件,然后将这个模块(.py)放在/usr/lib/ganglia/python_modules 目录下。对应的配置文件(.pyconf)放在/etc/ganglia/conf.d/目录下。

python模块可能要访问服务器的多个文件,由于运行python模块的用户和运行gmond的用户是一致的,所以必须保证运行gmond的用户有访问这些文件的权限。

在安装好ganglia-gmond-python后,已经自带了一个例子/usr/lib/ganglia/python_modules/example.py。下面将针对example.py解释python模块的格式,以及它的配置文件。

2.4 python模块模板

以example为例(安装完ganglia-gmond-python 后已经自带)

import random
descriptors = list()
Random_Max = 50
Constant_Value = 50

def Random_Numbers(name):
    '''Return a random number.'''
    global Random_Max
    return int(random.uniform(0,Random_Max))

def Constant_Number(name):
    '''Return a constant number.'''
    global Constant_Value
    return int(Constant_Value)

def metric_init(params):
    '''Initialize the random number generator and create the
    metric definition dictionary object for each metric.'''
    global descriptors
    global Random_Max
    global Constant_Value
    random.seed()

    print '[pyexample] Received the following parameters'
    print params

    if 'RandomMax' in params:
        Random_Max = int(params['RandomMax'])
    if 'ConstantValue' in params:
        Constant_Value = int(params['ConstantValue'])

    d1 = {'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'}

    d2 = {'name': 'PyConstant_Number',
        'call_back': Constant_Number,
        'time_max': 90,
        'value_type': 'uint',
        'units': 'N',
        'slope': 'zero',
        'format': '%hu',
        'description': 'Example module constant (constant number)'}

    descriptors = [d1,d2]
    return descriptors

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

#This code is for debugging and unit testing    
if __name__ == '__main__':
    params = {'RandomMax': '500',
        'ConstantValue': '322'}
    metric_init(params)
    for d in descriptors:
        v = d['call_back'](d['name'])
        print 'value for %s is %u' % (d['name'],  v)

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

  • def metric_init(params):

  • def metric_cleanup():

  • def metric_handler(name):

前面两个方法的名字必须是一定的,而最后一个 metric_handler可以任意命名。

__main__是便于debug用,可以单独调试该模块,以检测是否有错。

下面将对每个方法的功能做解释。

def metric_init(params):

对模块的初始化,在gmond服务被启动的时候,运行一次。

该方法必须返回一个词典列表,每个词典表示了一个metric的信息。每个词典的格式如下:

     d1 = {'name': 'PyRandom_Numbers',             #metric的名字
        'call_back': Random_Numbers,    #收集到数据后调用的方法
        'time_max': 90,               #没有什么用。。。
        'value_type': 'uint',         #string | uint | float | double
        'units': 'N',                 # metric的单位
        'slope': 'both',              #zero | positive | negative | both
        'format': '%u',               #必须和value_type对应 (reference: http://docs.python.org/library/stdtypes.html#string-formatting)
        'description': 'Example module metric (random numbers)',    #对metric的描述,在前端可以看到
        'groups': 'example,random'}           #这个metric属于的组,如果没有定义,会分到no_group metric中
slope的选项 zero | positive | negative | both
  • This value maps to the data source types defined for RRDTool

  • If 'positive',表示数据的变化率(calculating the rate of change)

  • If 'negative', ????

  • 'both' 直接显示值

  • If 'zero', 将显示在 "Time and String Metrics" 或者 "Constant Metrics"中(根据metric的value_type)

在example这个例子中,d2的slope是zero,最后显示在Constant Metrics中,而不显示在下面的面板里。

def metric_cleanup():

gmond关掉的时候执行,不能返回值。

def metric_handler(name):

可以取任何的名字,要在call_back中调用,参数name在是metric字典里定义的name。

2.4 pyconf模板

pyconf是python模块的配置文件,位置是/etc/ganglia/conf.d/example.pyconf(没有自带,需自己创建example.pyconf), 代码如下

 modules{

  module {

    name = "example"

    language = "python"

    # The following params are examples only

    #  They are not actually used by the temp module

    param RandomMax {

      value = 600

    }

    param ConstantValue {

      value = 112

    }

  }

}

collection_group {

  collect_every = 10

  time_threshold = 50

  metric {

    name = "PyRandom_Numbers"
   #要显示的metric,与example.py中的d1名字对应
    title = "Random"
            #metric在网页上显示的标题
    value_threshold = 70

  }

  metric {

    name = "PyConstant_Number"
  #要显示的metric,与example.py中的d2名字对应
    title = "Constant"
          #metric在网页上显示的标题
    value_threshold = 70

  }

}
pyconf的文件名最好和你建立的python文件名对应。包含两个模块, modules 和collection_group。

Modules:对每个模块进行配置

name:模块名,同时必须与创建的python文件名一致

language: 语言

param:参数列表,所有的参数作为一个dict(map)传给python脚本的metric_init(params)函数。

本例中,metric_init调用时, params={“RandomMax”:”600”,”ConstantValue”:”112”}

collection_group:需要收集的metric列表,一个模块中可以扩展任意个metric

collect_every: 汇报周期,以秒为单位。

metric:可以有多个,定义每个metric的信息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值