You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
5.5 KiB

4 years ago
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Glances.
  4. #
  5. # Copyright (C) 2018 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. """Manage the Glances standalone session."""
  20. import sys
  21. import time
  22. from glances.globals import WINDOWS
  23. from glances.logger import logger
  24. from glances.processes import glances_processes
  25. from glances.stats import GlancesStats
  26. from glances.outputs.glances_curses import GlancesCursesStandalone
  27. from glances.outputs.glances_stdout import GlancesStdout
  28. from glances.outdated import Outdated
  29. from glances.timer import Counter
  30. class GlancesStandalone(object):
  31. """This class creates and manages the Glances standalone session."""
  32. def __init__(self, config=None, args=None):
  33. self.config = config
  34. self.args = args
  35. # Quiet mode
  36. self._quiet = args.quiet
  37. self.refresh_time = args.time
  38. # Init stats
  39. self.stats = GlancesStats(config=config, args=args)
  40. # Modules (plugins and exporters) are loaded at this point
  41. # Glances can display the list if asked...
  42. if args.modules_list:
  43. self.display_modules_list()
  44. sys.exit(0)
  45. # If process extended stats is disabled by user
  46. if not args.enable_process_extended:
  47. logger.debug("Extended stats for top process are disabled")
  48. glances_processes.disable_extended()
  49. else:
  50. logger.debug("Extended stats for top process are enabled")
  51. glances_processes.enable_extended()
  52. # Manage optionnal process filter
  53. if args.process_filter is not None:
  54. glances_processes.process_filter = args.process_filter
  55. if (not WINDOWS) and args.no_kernel_threads:
  56. # Ignore kernel threads in process list
  57. glances_processes.disable_kernel_threads()
  58. # Initial system informations update
  59. self.stats.update()
  60. if self.quiet:
  61. logger.info("Quiet mode is ON, nothing will be displayed")
  62. # In quiet mode, nothing is displayed
  63. glances_processes.max_processes = 0
  64. elif args.stdout:
  65. logger.info("Stdout mode is ON, following stats will be displayed: {}".format(args.stdout))
  66. # Init screen
  67. self.screen = GlancesStdout(config=config, args=args)
  68. else:
  69. # Default number of processes to displayed is set to 50
  70. glances_processes.max_processes = 50
  71. # Init screen
  72. self.screen = GlancesCursesStandalone(config=config, args=args)
  73. # Check the latest Glances version
  74. self.outdated = Outdated(config=config, args=args)
  75. @property
  76. def quiet(self):
  77. return self._quiet
  78. def display_modules_list(self):
  79. """Display modules list"""
  80. print("Plugins list: {}".format(
  81. ', '.join(sorted(self.stats.getPluginsList(enable=False)))))
  82. print("Exporters list: {}".format(
  83. ', '.join(sorted(self.stats.getExportsList(enable=False)))))
  84. def __serve_forever(self):
  85. """Main loop for the CLI.
  86. return True if we should continue (no exit key has been pressed)
  87. """
  88. # Start a counter used to compute the time needed for
  89. # update and export the stats
  90. counter = Counter()
  91. # Update stats
  92. self.stats.update()
  93. logger.debug('Stats updated in {} seconds'.format(counter.get()))
  94. # Export stats
  95. counter_export = Counter()
  96. self.stats.export(self.stats)
  97. logger.debug('Stats exported in {} seconds'.format(counter_export.get()))
  98. # Patch for issue1326 to avoid < 0 refresh
  99. adapted_refresh = self.refresh_time - counter.get()
  100. adapted_refresh = adapted_refresh if adapted_refresh > 0 else 0
  101. # Display stats
  102. # and wait refresh_time - counter
  103. if not self.quiet:
  104. # The update function return True if an exit key 'q' or 'ESC'
  105. # has been pressed.
  106. ret = not self.screen.update(self.stats, duration=adapted_refresh)
  107. else:
  108. # Nothing is displayed
  109. # Break should be done via a signal (CTRL-C)
  110. time.sleep(adapted_refresh)
  111. ret = True
  112. return ret
  113. def serve_forever(self):
  114. """Wrapper to the serve_forever function."""
  115. loop = True
  116. while loop:
  117. loop = self.__serve_forever()
  118. self.end()
  119. def end(self):
  120. """End of the standalone CLI."""
  121. if not self.quiet:
  122. self.screen.end()
  123. # Exit from export modules
  124. self.stats.end()
  125. # Check Glances version versus PyPI one
  126. if self.outdated.is_outdated():
  127. print("You are using Glances version {}, however version {} is available.".format(
  128. self.outdated.installed_version(), self.outdated.latest_version()))
  129. print("You should consider upgrading using: pip install --upgrade glances")