/glances/__init__.py

https://gitlab.com/132nd-etcher/glances · Python · 193 lines · 92 code · 42 blank · 59 comment · 20 complexity · 597b735c8e694821aa51ea2018a6da50 MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Glances.
  4. #
  5. # Copyright (C) 2015 Nicolargo <nicolas@nicolargo.com>
  6. #
  7. # Glances is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU Lesser General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Glances is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. """Init the Glances software."""
  20. __appname__ = 'glances'
  21. __version__ = '2.5.1'
  22. __author__ = 'Nicolas Hennion <nicolas@nicolargo.com>'
  23. __license__ = 'LGPL'
  24. # Import system lib
  25. import locale
  26. import platform
  27. import signal
  28. import sys
  29. # Import psutil
  30. try:
  31. from psutil import __version__ as __psutil_version
  32. except ImportError:
  33. print('PSutil library not found. Glances cannot start.')
  34. sys.exit(1)
  35. # Import Glances libs
  36. # Note: others Glances libs will be imported optionally
  37. from glances.core.glances_logging import logger
  38. from glances.core.glances_main import GlancesMain
  39. try:
  40. locale.setlocale(locale.LC_ALL, '')
  41. except locale.Error:
  42. print("Warning: Unable to set locale. Expect encoding problems.")
  43. # Check Python version
  44. if sys.version_info < (2, 6) or (3, 0) <= sys.version_info < (3, 3):
  45. print('Glances requires at least Python 2.6 or 3.3 to run.')
  46. sys.exit(1)
  47. # Check PSutil version
  48. psutil_min_version = (2, 0, 0)
  49. psutil_version = tuple([int(num) for num in __psutil_version.split('.')])
  50. if psutil_version < psutil_min_version:
  51. print('PSutil 2.0 or higher is needed. Glances cannot start.')
  52. sys.exit(1)
  53. def __signal_handler(signal, frame):
  54. """Callback for CTRL-C."""
  55. end()
  56. def end():
  57. """Stop Glances."""
  58. if core.is_standalone():
  59. # Stop the standalone (CLI)
  60. standalone.end()
  61. logger.info("Stop Glances (with CTRL-C)")
  62. elif core.is_client():
  63. # Stop the client
  64. client.end()
  65. logger.info("Stop Glances client (with CTRL-C)")
  66. elif core.is_server():
  67. # Stop the server
  68. server.end()
  69. logger.info("Stop Glances server (with CTRL-C)")
  70. elif core.is_webserver():
  71. # Stop the Web server
  72. webserver.end()
  73. logger.info("Stop Glances web server(with CTRL-C)")
  74. # The end...
  75. sys.exit(0)
  76. def main():
  77. """Main entry point for Glances.
  78. Select the mode (standalone, client or server)
  79. Run it...
  80. """
  81. # Log Glances and PSutil version
  82. logger.info('Start Glances {0}'.format(__version__))
  83. logger.info('{0} {1} and PSutil {2} detected'.format(
  84. platform.python_implementation(),
  85. platform.python_version(),
  86. __psutil_version))
  87. # Share global var
  88. global core, standalone, client, server, webserver
  89. # Create the Glances main instance
  90. core = GlancesMain()
  91. # Catch the CTRL-C signal
  92. signal.signal(signal.SIGINT, __signal_handler)
  93. # Glances can be ran in standalone, client or server mode
  94. if core.is_standalone():
  95. logger.info("Start standalone mode")
  96. # Import the Glances standalone module
  97. from glances.core.glances_standalone import GlancesStandalone
  98. # Init the standalone mode
  99. standalone = GlancesStandalone(config=core.get_config(),
  100. args=core.get_args())
  101. # Start the standalone (CLI) loop
  102. standalone.serve_forever()
  103. elif core.is_client():
  104. if core.is_client_browser():
  105. logger.info("Start client mode (browser)")
  106. # Import the Glances client browser module
  107. from glances.core.glances_client_browser import GlancesClientBrowser
  108. # Init the client
  109. client = GlancesClientBrowser(config=core.get_config(),
  110. args=core.get_args())
  111. else:
  112. logger.info("Start client mode")
  113. # Import the Glances client module
  114. from glances.core.glances_client import GlancesClient
  115. # Init the client
  116. client = GlancesClient(config=core.get_config(),
  117. args=core.get_args())
  118. # Test if client and server are in the same major version
  119. if not client.login():
  120. logger.critical("The server version is not compatible with the client")
  121. sys.exit(2)
  122. # Start the client loop
  123. client.serve_forever()
  124. # Shutdown the client
  125. client.end()
  126. elif core.is_server():
  127. logger.info("Start server mode")
  128. # Import the Glances server module
  129. from glances.core.glances_server import GlancesServer
  130. args = core.get_args()
  131. server = GlancesServer(cached_time=core.cached_time,
  132. config=core.get_config(),
  133. args=args)
  134. print('Glances server is running on {0}:{1}'.format(args.bind_address, args.port))
  135. # Set the server login/password (if -P/--password tag)
  136. if args.password != "":
  137. server.add_user(args.username, args.password)
  138. # Start the server loop
  139. server.serve_forever()
  140. # Shutdown the server?
  141. server.server_close()
  142. elif core.is_webserver():
  143. logger.info("Start web server mode")
  144. # Import the Glances web server module
  145. from glances.core.glances_webserver import GlancesWebServer
  146. # Init the web server mode
  147. webserver = GlancesWebServer(config=core.get_config(),
  148. args=core.get_args())
  149. # Start the web server loop
  150. webserver.serve_forever()