summaryrefslogtreecommitdiffstats
path: root/logactio
blob: 0f682cdccd1ca621b9c8ac377441795d1dd76bd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/python3
#
#   logactio  -  simple framework for doing configured action on certain
#                log file events
#
#   Copyright 2012 - 2020   David Sommerseth <dazo@eurephia.org>
#
#   This program is free software; you can redistribute it and/or
#   modify it under the terms of the GNU General Public License
#   as published by the Free Software Foundation, version 2
#   of the License.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
#   For the avoidance of doubt the "preferred form" of this code is one which
#   is in an open unpatent encumbered format. Where cryptographic key signing
#   forms part of the process of creating an executable the information
#   including keys needed to generate an equivalently functional executable
#   are deemed to be part of the source code.
#

import sys, optparse
from LogActio import LogActio, Logger


if __name__ == '__main__':
    parser = optparse.OptionParser()

    parser.add_option("-d", "--daemon", action="store_true", default=False,
                      dest="daemon",
                      help="Run as a daemon")
    parser.add_option("-p", "--pid-file",
                      dest="pidfile", metavar="PID-FILE", default=None,
                      help="Put pid file of logactio in this file")
    parser.add_option("--stdout-redir",
                      dest="stdoutredir", metavar="FILE", default="/dev/null",
                      help="Redirect all stdout data to this file (only active when running as daemon)")
    parser.add_option("-c", "--config",
                      dest="cfg", metavar="FILE", default="/etc/logactio.cfg",
                      help="Configuration file for logactio (Default: %default)")
    parser.add_option("-v", "--verbose", action="count", default=0,
                      dest="verb",
                      help="Increase the log verbosity")
    parser.add_option("-L", "--log-type", type="choice", choices=["syslog","file", "stdout"],
                      dest="logtype", default="syslog",
                      help="Should logging go to file or syslog? (default: %default)")
    parser.add_option("-l", "--log-file",
                      dest="logfile", metavar="LOG-FILE",
                      help="Filename of the log file to use")
    parser.add_option("-s", "--syslog-id",
                      dest="syslogid", metavar="SYSLOG-NAME", default="logactio",
                      help="syslog ident to use for syslog events")
    parser.add_option("--trace", action="store_true",
                      dest="trace", default=False,
                      help="On errors, show a backtrace")
    (opts, args) = parser.parse_args()

    try:
        if opts.logtype == "syslog":
            logger = Logger.Logger(Logger.LOGTYPE_SYSLOG, opts.syslogid, opts.verb)
        elif opts.logtype == "file":
            logger = Logger.Logger(Logger.LOGTYPE_FILE, opts.logfile, opts.verb)
        elif opts.logtype == "stdout":
            logger = Logger.Logger(Logger.LOGTYPE_STDOUT, opts.logfile, opts.verb)
        else:
            raise Exception("Unknown log type '%s'" % opts.logtype)

        main = LogActio(opts.cfg, opts.daemon, opts.pidfile, logger.Log, opts.stdoutredir)
        main.Run()
        sys.exit(0)
    except Exception as e:
        if opts.trace:
            import traceback
            traceback.print_exc(file=sys.stdout)
        else:
            print("** ERROR ** %s" % str(e))