__init__.py 5.8 KB
Newer Older
N
Nicolas Hennion 已提交
1 2
# -*- coding: utf-8 -*-
#
3
# This file is part of Glances.
N
Nicolas Hennion 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#
# Copyright (C) 2014 Nicolargo <nicolas@nicolargo.com>
#
# Glances is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Glances 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
A
Alessio Sergi 已提交
19

A
PEP 257  
Alessio Sergi 已提交
20
"""Init the Glances software."""
N
Nicolas Hennion 已提交
21

A
Alessio Sergi 已提交
22
__appname__ = 'glances'
N
Nicolas Hennion 已提交
23
__version__ = '2.2_BETA'
A
Alessio Sergi 已提交
24 25 26
__author__ = 'Nicolas Hennion <nicolas@nicolargo.com>'
__license__ = 'LGPL'

27
# Import system lib
28 29
import gettext
import locale
30
import signal
A
Alessio Sergi 已提交
31
import sys
N
Nicolargo 已提交
32
import platform
33

A
Alessio Sergi 已提交
34 35 36 37 38 39 40
# Import psutil
try:
    from psutil import __version__ as __psutil_version
except ImportError:
    print('psutil library not found. Glances cannot start.')
    sys.exit(1)

N
Nicolas Hennion 已提交
41
# Import Glances libs
42
# Note: others Glances libs will be imported optionally
N
Nicolas Hennion 已提交
43
from glances.core.glances_globals import gettext_domain, locale_dir, logger
44
from glances.core.glances_main import GlancesMain
N
Nicolas Hennion 已提交
45

N
Nicolas Hennion 已提交
46 47 48 49 50
# Get PSutil version
psutil_min_version = (2, 0, 0)
psutil_version = tuple([int(num) for num in __psutil_version.split('.')])

# First log with Glances and PSUtil version
N
Nicolargo 已提交
51
logger.info('Start Glances {0}'.format(__version__))
52
logger.info('{0} {1} and PSutil {2} detected'.format(platform.python_implementation(),
53 54
                                                     platform.python_version(),
                                                     __psutil_version))
N
Nicolas Hennion 已提交
55 56

# Check PSutil version
57
if psutil_version < psutil_min_version:
N
Nicolas Hennion 已提交
58 59 60
    logger.critical('PSutil 2.0 or higher is needed. Glances cannot start.')
    sys.exit(1)

A
Alessio Sergi 已提交
61

62
def __signal_handler(signal, frame):
A
PEP 257  
Alessio Sergi 已提交
63
    """Callback for CTRL-C."""
64 65 66 67
    end()


def end():
A
PEP 257  
Alessio Sergi 已提交
68
    """Stop Glances."""
69
    if core.is_standalone():
70 71
        # Stop the standalone (CLI)
        standalone.end()
N
Nicolas Hennion 已提交
72
        logger.info("Stop Glances (with CTRL-C)")
73
    elif core.is_client():
74 75
        # Stop the client
        client.end()
N
Nicolas Hennion 已提交
76
        logger.info("Stop Glances client (with CTRL-C)")
77
    elif core.is_server():
78 79
        # Stop the server
        server.end()
N
Nicolas Hennion 已提交
80 81 82 83 84
        logger.info("Stop Glances server (with CTRL-C)")
    elif core.is_webserver():
        # Stop the Web server
        webserver.end()
        logger.info("Stop Glances web server(with CTRL-C)")
85 86 87 88 89

    # The end...
    sys.exit(0)


N
Nicolas Hennion 已提交
90
def main():
A
PEP 257  
Alessio Sergi 已提交
91
    """Main entry point for Glances.
92

93 94 95
    Select the mode (standalone, client or server)
    Run it...
    """
96 97 98
    # Setup translations
    locale.setlocale(locale.LC_ALL, '')
    gettext.install(gettext_domain, locale_dir)
99 100

    # Share global var
N
Nicolas Hennion 已提交
101
    global core, standalone, client, server, webserver
102

103 104
    # Create the Glances main instance
    core = GlancesMain()
N
Nicolas Hennion 已提交
105

106 107 108
    # Catch the CTRL-C signal
    signal.signal(signal.SIGINT, __signal_handler)

N
Nicolas Hennion 已提交
109
    # Glances can be ran in standalone, client or server mode
110
    if core.is_standalone():
N
Nicolargo 已提交
111
        logger.info("Start standalone mode")
N
Nicolas Hennion 已提交
112

113
        # Import the Glances standalone module
114
        from glances.core.glances_standalone import GlancesStandalone
115 116

        # Init the standalone mode
N
Nicolas Hennion 已提交
117
        standalone = GlancesStandalone(config=core.get_config(),
N
Nicolas Hennion 已提交
118
                                       args=core.get_args())
119 120 121 122

        # Start the standalone (CLI) loop
        standalone.serve_forever()

123
    elif core.is_client():
124
        if core.is_client_browser():
N
Nicolas Hennion 已提交
125
            logger.info("Start client mode (browser)")
N
Nicolas Hennion 已提交
126

N
Nicolas Hennion 已提交
127 128
            # Import the Glances client browser module
            from glances.core.glances_client_browser import GlancesClientBrowser
N
Nicolas Hennion 已提交
129

N
Nicolas Hennion 已提交
130 131 132
            # Init the client
            client = GlancesClientBrowser(config=core.get_config(),
                                          args=core.get_args())
133 134

        else:
N
Nicolas Hennion 已提交
135 136 137 138 139 140 141 142 143
            logger.info("Start client mode")

            # Import the Glances client module
            from glances.core.glances_client import GlancesClient

            # Init the client
            client = GlancesClient(config=core.get_config(),
                                   args=core.get_args())

144 145
            # Test if client and server are in the same major version
            if not client.login():
A
Alessio Sergi 已提交
146
                logger.critical("The server version is not compatible with the client")
147
                sys.exit(2)
N
Nicolas Hennion 已提交
148

N
Nicolas Hennion 已提交
149 150
        # Start the client loop
        client.serve_forever()
N
Nicolas Hennion 已提交
151 152

        # Shutdown the client
N
Nicolargo 已提交
153
        client.end()
N
Nicolas Hennion 已提交
154

155
    elif core.is_server():
N
Nicolargo 已提交
156
        logger.info("Start server mode")
N
Nicolas Hennion 已提交
157

N
Nicolas Hennion 已提交
158
        # Import the Glances server module
159
        from glances.core.glances_server import GlancesServer
N
Nicolas Hennion 已提交
160

N
Nicolas Hennion 已提交
161 162 163 164 165
        args = core.get_args()

        server = GlancesServer(cached_time=core.cached_time,
                               config=core.get_config(),
                               args=args)
A
Alessio Sergi 已提交
166
        print(_("Glances server is running on {0}:{1}").format(args.bind_address, args.port))
N
Nicolas Hennion 已提交
167 168

        # Set the server login/password (if -P/--password tag)
169
        if args.password != "":
N
Nicolas Hennion 已提交
170
            server.add_user(args.username, args.password)
N
Nicolas Hennion 已提交
171

N
Nicolas Hennion 已提交
172 173
        # Start the server loop
        server.serve_forever()
N
Nicolas Hennion 已提交
174

175
        # Shutdown the server?
N
Nicolas Hennion 已提交
176
        server.server_close()
177

178
    elif core.is_webserver():
N
Nicolargo 已提交
179
        logger.info("Start web server mode")
180 181 182 183 184 185 186 187 188 189

        # Import the Glances web server module
        from glances.core.glances_webserver import GlancesWebServer

        # Init the web server mode
        webserver = GlancesWebServer(config=core.get_config(),
                                     args=core.get_args())

        # Start the web server loop
        webserver.serve_forever()