File: momd.in

package info (click to toggle)
mom 0.6.4-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 632 kB
  • sloc: python: 4,090; makefile: 223; sh: 113
file content (134 lines) | stat: -rwxr-xr-x 4,620 bytes parent folder | download
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!@PYTHON@
# Memory Overcommitment Manager
# Copyright (C) 2010 Adam Litke, IBM Corporation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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 St, Fifth Floor, Boston, MA 02110-1301 USA

import sys
import signal
import os
import atexit
from optparse import OptionParser
from six.moves import configparser
import logging
import logging.handlers
import mom

mom_instance = None

def daemonize(pid_file):
    try:
        pid = os.fork()
        if pid > 0:
            sys.exit(0)
    except OSError as e:
        sys.stderr.write("momd: fork failed: %d (%s)\n" % (e.errno, e.strerror))
        sys.exit(1)

    os.chdir("/")
    os.setsid()

    try:
        pid = os.fork()
        if pid > 0:
            sys.exit(0)
    except OSError as e:
        sys.stderr.write("momd: fork failed: %d (%s)\n" % (e.errno, e.strerror))
        sys.exit(1)

    pid = str(os.getpid())
    try:
        open(pid_file,'w+').write("%s\n" % pid)
    except EnvironmentError as e:
        sys.stderr.write("momd: failed to write pid file: %d (%s)\n" %
                         (e.errno, e.strerror))
        sys.exit(1)
    atexit.register(delpid, pid_file)

    sys.stdout.flush()
    sys.stderr.flush()
    si = open('/dev/null', 'r')
    so = open('/dev/null', 'a+')
    se = open('/dev/null', 'a+')
    os.dup2(si.fileno(), sys.stdin.fileno())
    os.dup2(so.fileno(), sys.stdout.fileno())
    os.dup2(se.fileno(), sys.stderr.fileno())

def delpid(pid_file):
    try:
        os.remove(pid_file)
    except OSError as e:
        logger = logging.getLogger('momd')
        logger.error("Unable to remove pid file (%s): %s", pid_file, e.strerror)

def signal_quit(signum, frame):
    global mom_instance
    logger = logging.getLogger('momd')
    logger.info("Caught signal -- shutting down")
    mom_instance.shutdown()

def get_option_overrides(options):
    config = configparser.ConfigParser()
    config.add_section('main')
    config.add_section('logging')
    if options.plot_dir is not None:
        config.set('main', 'plot-dir', options.plot_dir)
    if options.log is not None:
        config.set('logging', 'log', options.log)
    if options.verbosity is not None:
        config.set('logging', 'verbosity', options.verbosity)
    if options.rules_file is not None:
        config.set('main', 'policy', options.rules_file)
    if options.policy_dir is not None:
        config.set('main', 'policy-dir', options.policy_dir)
    return config

def main():
    global mom_instance

    cmdline = OptionParser()
    cmdline.add_option('-c', '--config-file', dest='config_file',
                       help='Load configuration from FILE', metavar='FILE',
                       default='/etc/mom.conf')
    cmdline.add_option('-r', '--rules-file', dest='rules_file',
                       help='Load rules from FILE', metavar='FILE')
    cmdline.add_option('-P', '--policy-dir', dest='policy_dir',
                       help='Load policies from DIR', metavar='DIR')
    cmdline.add_option('-p', '--plot-dir', dest='plot_dir',
                       help='Save data plot files in DIR', metavar='DIR')
    cmdline.add_option('-l', '--log', dest='log', metavar='TARGET',
                       help='Set the log to TARGET (stdout, or <file>')
    cmdline.add_option('-v', '--verbose', dest='verbosity', metavar='LEVEL',
                       help='Set logging verbosity to LEVEL (0-4)')
    cmdline.add_option('-d', '--daemon', action='store_true', dest='daemonize')
    cmdline.add_option('', '--pid-file', dest='pid_file', metavar='FILE',
                       help='When running as a daemon, write pid to FILE',
                       default='/var/run/momd.pid')

    (options, args) = cmdline.parse_args()
    config_overrides = get_option_overrides(options)

    if options.daemonize:
        daemonize(options.pid_file)

    signal.signal(signal.SIGINT, signal_quit)
    signal.signal(signal.SIGTERM, signal_quit)

    mom_instance = mom.MOM(options.config_file, config_overrides)
    mom_instance.run()
    sys.exit(0)

if __name__ == "__main__":
    mom.enable_i8()
    main()