Permalink
Browse files

Added simple config handling and daemon handling.

  • Loading branch information...
1 parent fc1d9f4 commit 9a09820a64ae3987217e15d9deb3a46749f0a0d6 @jbohman committed Jun 3, 2010
Showing with 211 additions and 0 deletions.
  1. +5 −0 README
  2. +13 −0 config.yaml
  3. +1 −0 logsandra/__init__.py
  4. +6 −0 logsandra/config/__init__.py
  5. +51 −0 logsandra/logsandra.py
  6. 0 logsandra/utils/__init__.py
  7. +135 −0 logsandra/utils/daemon.py
View
5 README
@@ -1 +1,6 @@
Logsandra
+
+Work in progress!
+
+Requirements:
+ - PyYaml
View
@@ -0,0 +1,13 @@
+# Webservice
+webservice_enabled: True
+webservice_address: localhost
+webservice_port: 8080
+
+# Cassandra cluster to connect to
+cassandra_address: localhost
+cassandra_port: 9160
+
+# List of paths (files and directories) to monitor
+paths:
+ - /var/log/:
+ recursive: True
View
@@ -0,0 +1 @@
+VERSION = (0, 1)
@@ -0,0 +1,6 @@
+# Global import
+import yaml
+
+def parse(config_file):
+ file_handler = open(config_file)
+ return yaml.load(file_handler.read())
View
@@ -0,0 +1,51 @@
+# Global imports
+import sys
+import os
+import optparse
+import time
+
+# Local imports
+import config
+from utils.daemon import Daemon
+
+
+class Application(Daemon):
+
+ # TODO: setup application here, monitor + pylon webservice
+ def run(self):
+ while 1:
+ time.sleep(10)
+
+
+if __name__ == '__main__':
+
+ default_working_directory = os.curdir
+ default_path = os.path.split(os.path.dirname(__file__))[0]
+ default_config_file = os.path.join(default_path, 'config.yaml')
+
+ usage = 'usage: %prog [options] start|stop|restart'
+ parser = optparse.OptionParser(usage=usage)
+ parser.add_option('--config-file', dest='config_file', metavar='FILE', default=default_config_file)
+ parser.add_option('--working-directory', dest='working_directory', metavar='DIRECTORY', default=default_working_directory)
+ parser.add_option('--pid-file', dest='pid_file', metavar='FILE', default='/tmp/logsandra.pid')
+ (options, args) = parser.parse_args()
+
+ settings = config.parse(options.config_file)
+ application = Application(options.pid_file)
+ application.settings = settings
+
+ if len(args) == 1:
+ if args[0] == 'start':
+ application.start()
+ elif args[0] == 'stop':
+ application.stop()
+ elif args[0] == 'restart':
+ application.restart()
+ else:
+ print "Unknown command"
+ sys.exit(2)
+
+ sys.exit(0)
+ else:
+ print parser.get_usage()
+ sys.exit(2)
No changes.
View
@@ -0,0 +1,135 @@
+#
+# Created by Sander Marechal for Public Domain use
+#
+
+# Global imports
+import sys
+import os
+import time
+import atexit
+from signal import SIGTERM
+
+class Daemon:
+ """
+ A generic daemon class.
+
+ Usage: subclass the Daemon class and override the run() method
+ """
+ def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
+ self.stdin = stdin
+ self.stdout = stdout
+ self.stderr = stderr
+ self.pidfile = pidfile
+
+ def daemonize(self):
+ """
+ do the UNIX double-fork magic, see Stevens' "Advanced
+ Programming in the UNIX Environment" for details (ISBN 0201563177)
+ http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
+ """
+ try:
+ pid = os.fork()
+ if pid > 0:
+ # exit first parent
+ sys.exit(0)
+ except OSError, e:
+ sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
+ sys.exit(1)
+
+ # decouple from parent environment
+ os.chdir("/")
+ os.setsid()
+ os.umask(0)
+
+ # do second fork
+ try:
+ pid = os.fork()
+ if pid > 0:
+ # exit from second parent
+ sys.exit(0)
+ except OSError, e:
+ sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
+ sys.exit(1)
+
+ # redirect standard file descriptors
+ sys.stdout.flush()
+ sys.stderr.flush()
+ si = file(self.stdin, 'r')
+ so = file(self.stdout, 'a+')
+ se = file(self.stderr, 'a+', 0)
+ os.dup2(si.fileno(), sys.stdin.fileno())
+ os.dup2(so.fileno(), sys.stdout.fileno())
+ os.dup2(se.fileno(), sys.stderr.fileno())
+
+ # write pidfile
+ atexit.register(self.delpid)
+ pid = str(os.getpid())
+ file(self.pidfile,'w+').write("%s\n" % pid)
+
+ def delpid(self):
+ os.remove(self.pidfile)
+
+ def start(self):
+ """
+ Start the daemon
+ """
+ # Check for a pidfile to see if the daemon already runs
+ try:
+ pf = file(self.pidfile,'r')
+ pid = int(pf.read().strip())
+ pf.close()
+ except IOError:
+ pid = None
+
+ if pid:
+ message = "pidfile %s already exist. Daemon already running?\n"
+ sys.stderr.write(message % self.pidfile)
+ sys.exit(1)
+
+ # Start the daemon
+ self.daemonize()
+ self.run()
+
+ def stop(self):
+ """
+ Stop the daemon
+ """
+ # Get the pid from the pidfile
+ try:
+ pf = file(self.pidfile,'r')
+ pid = int(pf.read().strip())
+ pf.close()
+ except IOError:
+ pid = None
+
+ if not pid:
+ message = "pidfile %s does not exist. Daemon not running?\n"
+ sys.stderr.write(message % self.pidfile)
+ return # not an error in a restart
+
+ # Try killing the daemon process
+ try:
+ while 1:
+ os.kill(pid, SIGTERM)
+ time.sleep(0.1)
+ except OSError, err:
+ err = str(err)
+ if err.find("No such process") > 0:
+ if os.path.exists(self.pidfile):
+ os.remove(self.pidfile)
+ else:
+ print str(err)
+ sys.exit(1)
+
+ def restart(self):
+ """
+ Restart the daemon
+ """
+ self.stop()
+ self.start()
+
+ def run(self):
+ """
+ You should override this method when you subclass Daemon. It will be called after the process has been
+ daemonized by start() or restart().
+ """

0 comments on commit 9a09820

Please sign in to comment.