This repository has been archived on 2024-12-03. You can view files and clone it, but cannot push or open issues or pull requests.
PyBitmessage-2024-12-03/packages/collectd/pybitmessagestatus.py

69 lines
2.0 KiB
Python
Raw Normal View History

2017-12-20 09:41:36 +01:00
#!/usr/bin/env python2.7
import collectd
import json
2024-05-26 02:50:42 +02:00
from six.moves import xmlrpc_client as xmlrpclib
2017-12-20 09:41:36 +01:00
pybmurl = ""
api = ""
2017-12-20 09:41:36 +01:00
def init_callback():
global api
api = xmlrpclib.ServerProxy(pybmurl)
collectd.info('pybitmessagestatus.py init done')
2017-12-20 09:41:36 +01:00
def config_callback(ObjConfiguration):
global pybmurl
apiUsername = ""
apiPassword = ""
apiInterface = "127.0.0.1"
apiPort = 8445
for node in ObjConfiguration.children:
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://{}:{}@{}:{}/".format(apiUsername, apiPassword, apiInterface, str(int(apiPort)))
2017-12-20 09:41:36 +01:00
collectd.info('pybitmessagestatus.py config done')
2017-12-20 09:41:36 +01:00
def read_callback():
try:
clientStatus = json.loads(api.clientStatus())
except (ValueError, TypeError):
collectd.info("Exception loading or parsing JSON")
return
except: # noqa:E722
2017-12-20 09:41:36 +01:00
collectd.info("Exception loading or parsing JSON")
return
for i in ["networkConnections", "numberOfPubkeysProcessed",
"numberOfMessagesProcessed", "numberOfBroadcastsProcessed"]:
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:
metric.values = [clientStatus[i]]
except (TypeError, KeyError):
2017-12-20 09:41:36 +01:00
collectd.info("Value for %s missing" % (i))
metric.dispatch()
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)