gpt4 book ai didi

python - 如何在 collectd 中运行多个 python 插件实例?

转载 作者:太空宇宙 更新时间:2023-11-04 01:10:46 26 4
gpt4 key购买 nike

我为 collectd 写了一个简单的 python 插件,我想为 db1db2 运行两个实例:

文件:/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) 被禁用。
如何运行两个实例?谢谢!

最佳答案

应该只有一个<Plugin python>...</Plugin>所有配置文件中的标记。
我重写了 redis_info插件接受多个 Module配置。


文件:/etc/collectd/plugins/redis_info.py

#!/usr/bin/env python

import collectd
import redis


CONFIGS = []


def config(conf):

collectd.info('------ config ------')

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)
continue

CONFIGS.append({
'host': host,
'port': port,
'db': db,
'key': key,
})


def read():

collectd.info('------ read ------')

for conf in CONFIGS:

rdb = redis.StrictRedis(host=conf['host'], port=conf['port'], db=conf['db'])
value = rdb.llen(conf['key'])
val = collectd.Values(plugin='redis_info')
val.type = 'gauge'
val.type_instance = conf['key']
val.values = [value]
val.dispatch()


collectd.register_config(config)
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>

<Module redis_info>
host localhost
port 6379
db 2
key "queue2"
</Module>
</Plugin>

关于python - 如何在 collectd 中运行多个 python 插件实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27834815/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com