From 4ac0d30e4d44d163a7c71a4c28991c372c0c89fa Mon Sep 17 00:00:00 2001 From: sigoa Date: Fri, 22 Dec 2017 20:07:15 +0000 Subject: [PATCH] V0.6 (#3) * This type of data is called metadata * Systemd config file - tested on Debian 9, you may have to adjust paths/uids if your deployment differs * Add collectd monitoring script * Only write PID after last fork - should fix systemd integration --- README.md | 2 +- packages/collectd/pybitmessagestatus.py | 60 +++++++++++++++++++++++++ packages/systemd/bitmessage.service | 18 ++++++++ src/bitmessagemain.py | 2 +- src/singleinstance.py | 9 ++-- 5 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 packages/collectd/pybitmessagestatus.py create mode 100644 packages/systemd/bitmessage.service diff --git a/README.md b/README.md index 7a161d04..0ea6144b 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Bitmessage is a P2P communications protocol used to send encrypted messages to another person or to many subscribers. It is decentralized and trustless, meaning that you need-not inherently trust any entities like root certificate authorities. It uses strong authentication, which means that the sender of a -message cannot be spoofed, and it aims to hide "non-content" data, like the +message cannot be spoofed, and it aims to hide metadata, like the sender and receiver of messages, from passive eavesdroppers like those running warrantless wiretapping programs. diff --git a/packages/collectd/pybitmessagestatus.py b/packages/collectd/pybitmessagestatus.py new file mode 100644 index 00000000..1db9f5b1 --- /dev/null +++ b/packages/collectd/pybitmessagestatus.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python2.7 + +import collectd +import json +import xmlrpclib + +pybmurl = "" +api = "" + +def init_callback(): + global api + api = xmlrpclib.ServerProxy(pybmurl) + collectd.info('pybitmessagestatus.py init done') + +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://" + apiUsername + ":" + apiPassword + "@" + apiInterface+ ":" + str(int(apiPort)) + "/" + collectd.info('pybitmessagestatus.py config done') + +def read_callback(): + try: + clientStatus = json.loads(api.clientStatus()) + except: + collectd.info("Exception loading or parsing JSON") + return + + for i in ["networkConnections", "numberOfPubkeysProcessed", "numberOfMessagesProcessed", "numberOfBroadcastsProcessed"]: + 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: + collectd.info("Value for %s missing" % (i)) + metric.dispatch() + +if __name__ == "__main__": + main() +else: + collectd.register_init(init_callback) + collectd.register_config(config_callback) + collectd.register_read(read_callback) diff --git a/packages/systemd/bitmessage.service b/packages/systemd/bitmessage.service new file mode 100644 index 00000000..1a9f7f47 --- /dev/null +++ b/packages/systemd/bitmessage.service @@ -0,0 +1,18 @@ +[Unit] +Description=Bitmessage Daemon +After=network.target auditd.service + +[Service] +ExecStart=/usr/bin/python2 /usr/src/PyBitmessage/src/bitmessagemain.py +ExecReload=/bin/kill -HUP $MAINPID +KillMode=process +Restart=on-failure +Type=forking +PIDFile=/var/lib/bitmessage/.config/PyBitmessage/singleton.lock +User=bitmessage +Group=nogroup +WorkingDirectory=/var/lib/bitmessage +Environment="HOME=/var/lib/bitmessage" + +[Install] +WantedBy=multi-user.target diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 83a41919..91032fe5 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -363,7 +363,7 @@ class Main: # fork not implemented pass else: - shared.thisapp.lock() # relock + shared.thisapp.lock(True) # relock and write pid shared.thisapp.lockPid = None # indicate we're the final child sys.stdout.flush() sys.stderr.flush() diff --git a/src/singleinstance.py b/src/singleinstance.py index 7a025945..883c83cc 100644 --- a/src/singleinstance.py +++ b/src/singleinstance.py @@ -36,7 +36,7 @@ class singleinstance: self.initialized = True atexit.register(self.cleanup) - def lock(self): + def lock(self, writePid = False): if self.lockPid is None: self.lockPid = os.getpid() if sys.platform == 'win32': @@ -68,9 +68,10 @@ class singleinstance: sys.exit(-1) else: pidLine = "%i\n" % self.lockPid - self.fp.truncate(0) - self.fp.write(pidLine) - self.fp.flush() + if writePid: + self.fp.truncate(0) + self.fp.write(pidLine) + self.fp.flush() def cleanup(self): if not self.initialized: