我为 编写了一个简单的 python 插件collectd
,我想为db1
和运行两个实例db2
:
文件:/etc/collectd/plugins/redis_info.py
#!/usr/bin/env python
import collectd
import redis
REDIS = None
HOST = 'localhost'
PORT = 6379
DB = 0
KEY = None
def config(conf):
global REDIS, HOST, PORT, DB, KEY
for node in conf.children:
key = node.key.lower()
val = node.values[0]
if key == 'host':
HOST = val
elif key == 'port':
PORT = int(val)
elif key == 'db':
DB = int(val)
elif key == 'key':
KEY = val
else:
collectd.warning('redis_info plugin: Unknown config key: %s' % key)
def init():
global REDIS, HOST, PORT, DB, KEY
REDIS = redis.StrictRedis(host=HOST, port=PORT, db=DB)
def read():
global REDIS, HOST, PORT, DB, KEY
value = REDIS.llen(KEY)
val = collectd.Values(plugin='redis_info')
val.type = 'gauge'
val.type_instance = KEY
val.values = [value]
val.dispatch()
collectd.register_config(config)
collectd.register_init(init)
collectd.register_read(read)
文件:/etc/collectd/collectd.conf.d/redis_info.conf
<Plugin python>
ModulePath "/etc/collectd/plugins/"
Import "redis_info"
<Module redis_info>
host localhost
port 6379
db 1
key "queue1"
</Module>
</Plugin>
<Plugin python>
ModulePath "/etc/collectd/plugins/"
Import "redis_info"
<Module redis_info>
host localhost
port 6379
db 2
key "queue2"
</Module>
</Plugin>
但是,只有一个实例 (db2) 有效,前一个实例 (db1) 被禁用。
如何运行两个实例?谢谢!