PyBitmessage-2021-04-27/packages/collectd/pybitmessagestatus.py

67 lines
1.9 KiB
Python
Raw Normal View History

2017-12-20 09:41:36 +01:00
#!/usr/bin/env python2.7
import collectd
import json
import xmlrpclib
pybmurl = ""
api = ""
2019-07-09 18:22:45 +02:00
2017-12-20 09:41:36 +01:00
def init_callback():
global api
api = xmlrpclib.ServerProxy(pybmurl)
collectd.info('pybitmessagestatus.py init done')
2019-07-09 18:22:45 +02:00
def config_callback(obj_configuration):
2017-12-20 09:41:36 +01:00
global pybmurl
apiUsername = ""
apiPassword = ""
apiInterface = "127.0.0.1"
apiPort = 8445
2019-07-09 18:22:45 +02:00
for node in obj_configuration.children:
2017-12-20 09:41:36 +01:00
key = node.key.lower()
if key.lower() == "apiusername" and node.values:
apiUsername = node.values[0]
elif key.lower() == "apipassword" and node.values:
apiPassword = node.values[0]
elif key.lower() == "apiinterface" and node.values:
apiInterface = node.values[0]
elif key.lower() == "apiport" and node.values:
apiPort = node.values[0]
pybmurl = "http://" + apiUsername + ":" + apiPassword + "@" + apiInterface+ ":" + str(int(apiPort)) + "/"
collectd.info('pybitmessagestatus.py config done')
2019-07-09 18:22:45 +02:00
2017-12-20 09:41:36 +01:00
def read_callback():
try:
2019-07-09 18:22:45 +02:00
client_status = json.loads(api.clientStatus())
2017-12-20 09:41:36 +01:00
except:
collectd.info("Exception loading or parsing JSON")
return
2019-07-09 18:22:45 +02:00
con_data = ["networkConnections", "numberOfPubkeysProcessed",
"numberOfMessagesProcessed", "numberOfBroadcastsProcessed"]
for i in con_data:
2017-12-20 09:41:36 +01:00
metric = collectd.Values()
metric.plugin = "pybitmessagestatus"
if i[0:6] == "number":
metric.type = 'counter'
else:
metric.type = 'gauge'
metric.type_instance = i.lower()
try:
2019-07-09 18:22:45 +02:00
metric.values = [client_status[i]]
2017-12-20 09:41:36 +01:00
except:
collectd.info("Value for %s missing" % (i))
metric.dispatch()
2019-07-09 18:22:45 +02:00
2017-12-20 09:41:36 +01:00
if __name__ == "__main__":
main()
else:
collectd.register_init(init_callback)
collectd.register_config(config_callback)
collectd.register_read(read_callback)