Added brookengine command and its example configs.
authorStanislaw Klekot <dozzie@jarowit.net>
Wed, 10 May 2017 18:45:53 +0000 (20:45 +0200)
committerStanislaw Klekot <dozzie@jarowit.net>
Wed, 10 May 2017 18:45:53 +0000 (20:45 +0200)
bin/brookengine [new file with mode: 0755]
examples/brookengine.conf [new file with mode: 0644]
examples/queries.logging [new file with mode: 0644]
examples/queries.yaml [new file with mode: 0644]

diff --git a/bin/brookengine b/bin/brookengine
new file mode 100755 (executable)
index 0000000..3761ddd
--- /dev/null
@@ -0,0 +1,136 @@
+#!/usr/bin/python
+
+import optparse
+import os
+import sys
+import socket
+import json
+
+from seismometer.daemonshepherd.control_socket import ControlSocketClient
+from seismometer.daemonshepherd.filehandle import EOF
+
+#-----------------------------------------------------------------------------
+# command line options {{{
+
+parser = optparse.OptionParser(
+    usage = "\n  %prog [options]"
+            "\n  %prog [--socket=PATH] reload"
+            "\n  %prog [--socket=PATH] list"
+            "\n  %prog [--socket=PATH] {start|stop|restart|cancel-restart} <query>",
+    #description = "",
+)
+
+parser.add_option(
+    "--config", dest = "config",
+    default = "/etc/brookengine/brookengine.conf",
+    help = "configuration file", metavar = "FILE",
+)
+parser.add_option(
+    "--plugins", dest = "plugins",
+    default = "/etc/brookengine/plugin",
+    help = "path to plugins", metavar = "PATH",
+)
+parser.add_option(
+    "--socket", dest = "socket",
+    default = "/var/run/brookengine/queries",
+    help = "path to control socket", metavar = "PATH",
+)
+
+(options, args) = parser.parse_args()
+
+command = None
+COMMANDS = {
+    "reload":         { "args": 0, "cmd": "reload",         "output": None },
+    "list":           { "args": 0, "cmd": "ps",             "output": "line-json" },
+    "start":          { "args": 1, "cmd": "start",          "output": None },
+    "stop":           { "args": 1, "cmd": "stop",           "output": "command" },
+    "restart":        { "args": 1, "cmd": "restart",        "output": "command" },
+    "cancel-restart": { "args": 1, "cmd": "cancel_restart", "output": None },
+}
+
+if len(args) > 0:
+    command = args.pop(0)
+    if command not in COMMANDS:
+        parser.error("unrecognized command: %s" % (command,))
+    if len(args) != COMMANDS[command]["args"]:
+        parser.error("wrong number of arguments for command %s" % (command,))
+
+# }}}
+#-----------------------------------------------------------------------------
+# execute Fluentd {{{
+
+if command is None:
+    os.execv("/usr/sbin/fluentd", [
+        "brook-databus",
+        "--config", options.config,
+        "--plugin", options.plugins,
+        #"--daemon", options.pidfile,
+        #"--user", options.user,
+        #"--group", options.group,
+        #"--log", options.log_file,
+    ])
+
+# }}}
+#-----------------------------------------------------------------------------
+# administrative commands {{{
+
+# code copied directly from daemonshepherd
+
+conn = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+try:
+    conn.connect(options.socket)
+except socket.error, e:
+    print >>sys.stderr, e
+    sys.exit(1)
+
+supervisor = ControlSocketClient(conn)
+
+if COMMANDS[command]["args"] == 0:
+    supervisor.send({"command": COMMANDS[command]["cmd"]})
+elif COMMANDS[command]["args"] == 1:
+    supervisor.send({
+        "command": COMMANDS[command]["cmd"],
+        "daemon": args[0],
+    })
+
+reply = supervisor.read(blocking = True)
+supervisor.close()
+
+if reply is EOF:
+    print >>sys.stderr, \
+          "Empty reply from daemonshepherd, probably a server bug."
+    sys.exit(1)
+
+if reply.get("status") == "error" and "message" in reply:
+    print >>sys.stderr, "error: %s" % (reply["message"],)
+    sys.exit(1)
+if reply.get("status") != "ok": # unrecognized errors
+    print >>sys.stderr, json.dumps(reply)
+    sys.exit(1)
+
+# data transformation for `brookengine list' command
+if command == "list":
+    for rec in reply["result"]:
+        rec["query"] = rec.pop("daemon")
+
+if COMMANDS[command]["output"] == "line-json":
+    for rec in reply["result"]:
+        print json.dumps(rec)
+elif COMMANDS[command]["output"] == "line":
+    for rec in reply["result"]:
+        print rec
+elif COMMANDS[command]["output"] == "command":
+    if "result" in reply:
+        result = reply["result"]
+        if result["output"] is not None and result["output"] != "":
+            print result["output"]
+        if "signal" in result:
+            print "## command died on signal %d" % (result["signal"],)
+            sys.exit(255)
+        else:
+            print "## command exited with code %d" % (result["exit"],)
+            sys.exit(result["exit"])
+
+# }}}
+#-----------------------------------------------------------------------------
+# vim:ft=python:foldmethod=marker
diff --git a/examples/brookengine.conf b/examples/brookengine.conf
new file mode 100644 (file)
index 0000000..73487f6
--- /dev/null
@@ -0,0 +1,23 @@
+<source>
+  type forward
+  bind 127.0.0.1
+  port 5168
+</source>
+
+<match fluent.**>
+  type null
+</match>
+
+<match **>
+  type brook_queries
+  #port 5268
+  #bind 127.0.0.1
+
+  control_socket /var/run/brookengine/queries
+  queries /etc/brookengine/queries.yaml
+  logging /etc/brookengine/queries.logging
+  brook_send_address 127.0.0.1:5168
+  #brook_read_address 127.0.0.1:5268
+</match>
+
+# vim:ft=fluentd
diff --git a/examples/queries.logging b/examples/queries.logging
new file mode 100644 (file)
index 0000000..1a35765
--- /dev/null
@@ -0,0 +1,15 @@
+version: 1
+root:
+  level: INFO
+  handlers: [syslog]
+formatters:
+  syslog_formatter:
+    format: "[%(name)s] %(message)s"
+handlers:
+  syslog:
+    class: seismometer.logging.SysLogHandler
+    formatter: syslog_formatter
+    facility: daemon
+    process_name: brook-queries
+
+# vim:ft=yaml
diff --git a/examples/queries.yaml b/examples/queries.yaml
new file mode 100644 (file)
index 0000000..d4960ad
--- /dev/null
@@ -0,0 +1,11 @@
+defaults:
+  stdout: log
+
+daemons: {}
+
+#daemons:
+#  collectd:
+#    start_command: >-
+#      brook --get
+#      | transform-to-collectd
+#      | socat - unix:/var/run/collectd-unixsock > /dev/null