设为首页 收藏本站
查看: 11365|回复: 0

[经验分享] ganglia python插件标准

[复制链接]

尚未签到

发表于 2015-4-26 11:53:41 | 显示全部楼层 |阅读模式
SourceForge              Visit project ganglia          Register Log In     
     
                       

      
      
         ganglia
                       
           Search:                                             
     

  • Preferences
  • Help/Guide
  • About Trac
            
     

  • Wiki
  • Timeline
  • Roadmap
  • Browse Source
  • Search
        
      
         Context Navigation
           

  • Start Page
  • Index
  • History
  • Last Change
                     
      
                    Gmond Python metric modules ¶
  One of the new features of Ganglia 3.1.x is the ability to create  C/Python metric gathering modules. These modules can be plugged directly  into gmond to monitor user-specified metrics.
  In previous versions (2.5.x, 3.0.x), the only way to add user-specified  metrics is via a command line tool called gmetric and the way to inject  metrics into gmond is simply to run gmetric via a cronjob or some other  process. While this works for most people, it makes user-specified  metrics difficult to manage.
  This document will dive into the specifics for writing a Python metric monitoring module.
  The following are prerequisites for building/using Python module support:


  • Ganglia 3.1.x
  • Python 2.3.4+ (this is the oldest tested version which comes  with Red Hat Enterprise Linux 4, older 2.3 versions should work as well)
  • Python development headers (usually in the form of python-devel binary packages)
Installation ¶
  If you are trying to install Python metric modules support on a RPM-based system, install the ganglia-gmond-modules-python RPM.  This includes everything needed for Python metric modules support to work.
  If you are building from source, please make sure that you include the --with-python option during configure.  If the Python interpreter is detected, this option will be added automatically.
Checklist ¶
  To confirm that your Ganglia installation has Python support correctly setup, double check the following:


  • gmond.conf has a line which reads something along the lines of include ("/etc/ganglia/conf.d/*.conf").  This is the directory where you should place configuration files for your Python modules as .pyconf files
  • You have modpython.so in /usr/lib{64}/ganglia
  • The directory /usr/lib{64}/ganglia/python_modules exists.  This is the directory where Python modules should be placed as .py files.
  These things should be automatically done for you if you installed  Python modules support via binary packages.  If that is not the case  please file a bug at the distribution's corresponding bug tracker.
Writing Python modules ¶
  Writing a Python module is very simple. You just need to write it  following a template and put the resulting Python module (.py) in  /usr/lib(64)/ganglia/python_modules. A corresponding Python  Configuration (.pyconf) file needs to reside in /etc/ganglia/conf.d/.
  If your Python module needs to access certain files on the server, keep  in mind that the module will be executed as the user which runs gmond.  In other words, if gmond runs as user nobody then your module will also run as nobody. So make sure that the user which runs gmond has the correct permissions to access the files in question.
  The Ganglia distribution comes with an example Python module in  /usr/lib(64)/ganglia/python_modules/example.py. Alternatively, this file  is also viewable from our SVN repository: http://ganglia.svn.sourceforge.net/viewvc/ganglia/branches/monitor-core-3.1/gmond/python_modules/example/example.py?view=markup
  Let's look at a real-life example of a Python module which monitors the  temperature of the host, by reading a file in the /proc file system,  let's call this temp.py:
def temp_handler(name):
    acpi_file = "/proc/acpi/thermal_zone/THRM/temperature"
    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
    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(None)
    for d in descriptors:
        v = d['call_back'](d['name'])
        print 'value for %s is %u' % (d['name'],  v)
  There are three functions that must exist in every python metric module. These functions are:


  • def metric_init(params):
  • def metric_cleanup():
  • def metric_handler():
  While the first two functions above must exist explicitly (ie. they must  be named as specified above), the metric_handler() function can  actually be named anything. The only requirement is that the call_back  function that is specified in the metric descriptor (described below)  must match the name of the metric_handler function. In addition if your  metric module supports multiple metrics, each being defined through  their own metric descriptor, your module may actually implement more  than one metric_handler function.
  Let's go through each function one by one. The temp_handler() function  simply parses the file with the temperature readings and returns it.  This is the metric_handler function that was described above. The name  of this function must match the function name that is specified for the call_back  attribute of the corresponding metric descriptor (see below). It must  also return the appropriately typed value. For example, if the value_type attribute of the metric descriptor specifies a UINT, then the metric_handler function must return a UINT data type.
  The metric_init() function creates and initializes metric descriptors  for each metric that the module supports. If the module supports a  single metric, then the return value from the metric_init() function can  be just a single descriptor. If however the module supports multiple  metrics, the return value must be a list of descriptors. Each metric  that is supported by the module must create and initialize a metric  descriptor in the following manner:


  • name: name of the metric
  • call_back: function to call when collecting metric data
  • time_max: maximum time in seconds between metric collection calls
  • value_type: string | uint | float | double
  • units: unit of your metric
  • slope: zero | positive | negative | both

    • If 'positive', RRD file generated will be of COUNTER type, otherwise will be of GAUGE type
    • If 'zero', the metric will appear in the "Time and String Metrics" or the "Constant Metrics" depending on the value_type of the metric

  • format: format string of your metric

    • Must correspond to value_type otherwise value of your metric will be unpredictable (reference: http://docs.python.org/library/stdtypes.html#string-formatting)

  • description: description of your metric

    • Which is visible in web frontend if you hover over host metric graph

  • group: group of your metric

    • Metrics in the web frontend hostview is grouped by this

  The metric descriptor can include additional attributes and values which  will be attached to the metric metadata as extra data. The extra data  will be ignored by Ganglia itself but can be used by the web front as  additional display or metric handling data. (The use of SPOOF_HOST and SPOOF_NAME extra attributes are examples that will be described in a later version.)
  In addition, the metric_init() function takes one parameter. This  parameter is a list of name/value pairs that correspond to configuration  parameters that were included in the gmond.conf file for a specific  metric module. These name/value pairs are read from the configuration  file, passed directly to the module and are only meaningful to the  metric module itself. The metric module must know what the params values  mean and how to apply them to the metric module functionality. How  these configuration parameters are defined is explained below.
  The corresponding temp.pyconf looks like this:
modules {
  module {
    name = "temp"
    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 = "temp"
    title = "Temperature"
    value_threshold = 70
  }
}
  The above configuration file contains two major sections with various sub-sections. The modules section contains configuration data that is specific to each module being loaded. It may contain either a single module sub-section or multiple sub-sections. Within each module sub-section is the name of the metric module, the language in which the module was written and zero or more module specific param(s). Each param sub-section has a name and a value. The name and value make up the name/value pair that is passed into the metric_init() function as a params list as described above. The rest of the configuration file follows the same format as for any other collection_group or metric.
Further reading ¶
  Additional information about Python modules can be found in the README file: http://ganglia.svn.sourceforge.net/viewvc/ganglia/branches/monitor-core-3.1/gmond/modules/python/README.in?view=markup
  Some helpful user-contributed resources:   


  • ganglia-pymodule_skeleton
  • pygmonlib
                                   
       Download in other formats:
      

  •            Plain Text
               
         Powered by Trac 0.11.2.1
         By Edgewall Software.      
  © 2011 Geeknet, Inc.   Terms of Use - Privacy Policy

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-60866-1-1.html 上篇帖子: 关于Python的常见看法 下篇帖子: Redis & Python/Django 简单用户登陆
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表