Tag Archives: gmond

GMond Python Module Notes

https://github.com/ganglia/monitor-core/wiki/Ganglia-GMond-Python-Modules

To create a custom GMond Python module requires a config file, and a module file which has three required functions: metric_init, metric_cleanup, & get_value*. In your config file (/etc/ganglia/conf.d/MODULE_NAME.pyconf) follow the structure:

modules {
  module {
    name = 'MODULE-NAME'
    language = 'python'

    param KEY {
      value = 'SOMETHING'
    }
  }
}

collection_group {
  collect_every = 30
  time_threshold = 30

  metric {
    name_match = "MODULE-NAME_(.+)"
  }
}

In your python module file (/usr/ganglia/lib64/ganglia/python_modules/MODULE_NAME.py) you need three functions:

  • metric_init : This is called when gmond starts, then not called again. You need to return a description of your checks (more about that below). It is passed a hash of params from the “param” block in the conf file.
  • metric_cleanup : Probably do not need it, but have to include it or it complains. Called when gmond if shutting down if you have any cleanup to do. Just use the block below…
  • get_value : Does not need to be named get_value, but whatever you pass to the ‘call_back’ value in the descriptors hash. This function is called by gmond for each metric each time it is run. After the initial run it will be called directly without going through metric_init. Gets passed the ‘name’ value from the descriptors hash. It must return the metric value.
Desc_Skel = {
    'name'        : 'METRIC_NAME', 
    'call_back'   : get_value, 
    'time_max'    : 10, # Does not do anything??
    'value_type'  : 'float', 
    'format'      : '%f', # https://docs.python.org/2/library/stdtypes.html#string-formatting
    'units'       : '', 
    'slope'       : 'both', # zero|positive|negative|both, but probably 'both'
    'description' : 'METRIC DESCRIPTION', # Only used when you run 'gmond -m'
    'groups'      : 'MODULE_NAME', # Not used the way we use gmond
}

If you want to have multiple metrics, pass them as an array in your return from the metric_init block.

Beyond that you can do whatever you want that is valid python. For debugging, include the following block at the bottom of your code:

# the following code is for debugging and testing
if __name__ == '__main__':
    descriptors = metric_init(PARAMS)
    for d in descriptors:
        print (('%s = %s') % (d['name'], d['format'])) % (d['call_back'](d['name']))

Random Notes:

  • Global variables are maintained between calls
  • To control frequency of calls I find it best to set a global variable with a time, and another with values. Then in my get_value function have it only refresh the data if it has been less then some time frame.
  • Use ‘gmond -m|grep MODULE_NAME’ to make sure gmond is finding your module.
  • View the system logs for gmond startup output that might tell you why your module is not working.
  • For some reason the first pass of checks seems to run as ‘root’ and not the user specified in the gmond.conf file. This seems like a bug, but can be useful if you just need elevated privileges for your metric_init, but not follow up calls. Also can be the cause of your check initially working, and then later failing.