__init__.py 3.4 KB
Newer Older
N
Nicolas Hennion 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Glances - An eye on your system
#
# 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/>.
N
Nicolas Hennion 已提交
20 21 22
"""
Init the Glances software
"""
N
Nicolas Hennion 已提交
23

24 25 26
# Import system lib
import sys

N
Nicolas Hennion 已提交
27 28
# Import Glances libs
# Note: others Glances libs will be imported optionnaly
29
from glances.core.glances_main import GlancesMain
N
Nicolas Hennion 已提交
30

A
Alessio Sergi 已提交
31

N
Nicolas Hennion 已提交
32
def main():
33 34
    # Create the Glances main instance
    core = GlancesMain()
N
Nicolas Hennion 已提交
35 36 37

    # Glances can be ran in standalone, client or server mode
    if (core.is_standalone()):
N
Nicolas Hennion 已提交
38

39
        # Import the Glances standalone module
40
        from glances.core.glances_standalone import GlancesStandalone
41 42

        # Init the standalone mode
N
Nicolas Hennion 已提交
43 44
        standalone = GlancesStandalone(config=core.get_config(),
                                       args=core.get_args(),
45 46 47 48 49 50
                                       refresh_time=core.refresh_time,
                                       use_bold=core.use_bold)

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

N
Nicolas Hennion 已提交
51
    elif (core.is_client()):
N
Nicolas Hennion 已提交
52 53

        # Import the Glances client module
54
        from glances.core.glances_client import GlancesClient
N
Nicolas Hennion 已提交
55 56

        # Init the client
A
Alessio Sergi 已提交
57 58
        client = GlancesClient(args=core.get_args(),
                               server_address=core.server_ip, server_port=int(core.server_port),
59
                               username=core.username, password=core.password, config=core.get_config())
N
Nicolas Hennion 已提交
60 61 62

        # Test if client and server are in the same major version
        if (not client.login()):
63
            print(_("Error: The server version is not compatible with the client"))
N
Nicolas Hennion 已提交
64 65 66 67 68 69 70 71
            sys.exit(2)

        # Start the client loop
        client.serve_forever()

        # Shutdown the client
        # !!! How to close the server with CTRL-C
        # !!! Call core.end() with parameters ?
72
        client.close()
N
Nicolas Hennion 已提交
73

N
Nicolas Hennion 已提交
74
    elif (core.is_server()):
N
Nicolas Hennion 已提交
75

N
Nicolas Hennion 已提交
76
        # Import the Glances server module
77
        from glances.core.glances_server import GlancesServer
N
Nicolas Hennion 已提交
78 79

        # Init the server
A
Alessio Sergi 已提交
80 81
        server = GlancesServer(bind_address=core.bind_ip,
                               bind_port=int(core.server_port),
82
                               cached_time=core.cached_time,
N
Nicolas Hennion 已提交
83
                               config=core.get_config())
84 85
        # print(_("DEBUG: Glances server is running on %s:%s with config file %s") % (core.bind_ip, core.server_port, core.config.get_config_path()))
        print("{} {}:{}".format(_("Glances server is running on"), core.bind_ip, core.server_port))
N
Nicolas Hennion 已提交
86 87 88 89 90

        # Set the server login/password (if -P/--password tag)
        if (core.password != ""):
            server.add_user(core.username, core.password)

N
Nicolas Hennion 已提交
91 92
        # Start the server loop
        server.serve_forever()
N
Nicolas Hennion 已提交
93 94 95 96 97

        # Shutdown the server
        # !!! How to close the server with CTRL-C
        # !!! Call core.end() with parameters ?
        server.server_close()