1 安裝ganglia
1.1安裝環境
CentOS, fedora
1.2單機版安裝步驟
假設機器的IP地址是192.168.1.253, 首先安裝好所需要的軟件
1 gmond的設置文件位置是/etc/gmond.conf,打開其進行編輯,只需修改下面的內容:
cluster {
name = "cluster1"
owner = "owner1"
latlong = "unspecified"
url = "unspecified"
}
udp_send_channel {
host = 192.168.1.253
port = 8649
ttl = 1
}
udp_recv_channel {
port = 8649
}
2 gmetad的設置文件位置是/etc/gmetad.conf,打開其進行編輯,只需修改下面的內容:
1.4 啟動服務
1 啟動gmond
chkconfig gmond on
service gmond start
2 啟動gmetad
chkconfig gmetad on
service gmetad start
3啟動httpd
chkconfig httpd on
service httpd start
打開localhost/ganglia就可以看到結果了。
2 python模塊擴展
yum install ganglia-gmond-python
gmond.conf 有這一行代碼include ("/etc/ganglia/conf.d/*.conf").這個目錄是放模塊的配置文件的,python模塊的配置文件的后綴名應該是.pyconf
在/etc/ganglia/conf.d下有modpython.conf。這個文件的內容是:
/*params指明了python模塊存放的目錄。
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')
include ('/etc/ganglia/conf.d/*.pyconf') 指明了python模塊配置文件的目錄。
在 /usr/lib/ganglia下有modpython.so。該文件是 Ganglia Python 擴展的動態庫文件。
/usr/lib/ganglia/python_modules文件夾存在。所有的python模塊存放在這個位置,后綴名是.py
定制一個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模塊的格式,以及它的配置文件。
以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用,可以單獨調試該模塊,以檢測是否有錯。
下面將對每個方法的功能做解釋。
對模塊的初始化,在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中
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中,而不顯示在下面的面板里。
gmond關掉的時候執行,不能返回值。
可以取任何的名字,要在call_back中調用,參數name在是metric字典里定義的name。
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
}
}
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的信息。