PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/glances/core/glances_stats.py

https://gitlab.com/132nd-etcher/glances
Python | 407 lines | 354 code | 14 blank | 39 comment | 4 complexity | c59eec99b0f9450ed3cd1e2980dfc0cf 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. """The stats manager."""
  20. import collections
  21. import os
  22. import re
  23. import sys
  24. import threading
  25. from glances.core.glances_globals import exports_path, plugins_path, sys_path
  26. from glances.core.glances_logging import logger
  27. # SNMP OID regexp pattern to short system name dict
  28. oid_to_short_system_name = {'.*Linux.*': 'linux',
  29. '.*Darwin.*': 'mac',
  30. '.*BSD.*': 'bsd',
  31. '.*Windows.*': 'windows',
  32. '.*Cisco.*': 'cisco',
  33. '.*VMware ESXi.*': 'esxi',
  34. '.*NetApp.*': 'netapp'}
  35. class GlancesStats(object):
  36. """This class stores, updates and gives stats."""
  37. def __init__(self, config=None, args=None):
  38. # Set the argument instance
  39. self.args = args
  40. # Set the config instance
  41. self.config = config
  42. # Init the plugin list dict
  43. self._plugins = collections.defaultdict(dict)
  44. # Load the plugins
  45. self.load_plugins(args=args)
  46. # Init the export modules list dict
  47. self._exports = collections.defaultdict(dict)
  48. # Load the plugins
  49. self.load_exports(args=args)
  50. # Load the limits
  51. self.load_limits(config)
  52. def __getattr__(self, item):
  53. """Overwrite the getattr method in case of attribute is not found.
  54. The goal is to dynamically generate the following methods:
  55. - getPlugname(): return Plugname stat in JSON format
  56. """
  57. # Check if the attribute starts with 'get'
  58. if item.startswith('get'):
  59. # Get the plugin name
  60. plugname = item[len('get'):].lower()
  61. # Get the plugin instance
  62. plugin = self._plugins[plugname]
  63. if hasattr(plugin, 'get_stats'):
  64. # The method get_stats exist, return it
  65. return getattr(plugin, 'get_stats')
  66. else:
  67. # The method get_stats is not found for the plugin
  68. raise AttributeError(item)
  69. else:
  70. # Default behavior
  71. raise AttributeError(item)
  72. def load_plugins(self, args=None):
  73. """Load all plugins in the 'plugins' folder."""
  74. header = "glances_"
  75. for item in os.listdir(plugins_path):
  76. if (item.startswith(header) and
  77. item.endswith(".py") and
  78. item != (header + "plugin.py")):
  79. # Import the plugin
  80. plugin = __import__(os.path.basename(item)[:-3])
  81. # Add the plugin to the dictionary
  82. # The key is the plugin name
  83. # for example, the file glances_xxx.py
  84. # generate self._plugins_list["xxx"] = ...
  85. plugin_name = os.path.basename(item)[len(header):-3].lower()
  86. if plugin_name == 'help':
  87. self._plugins[plugin_name] = plugin.Plugin(args=args, config=self.config)
  88. else:
  89. self._plugins[plugin_name] = plugin.Plugin(args=args)
  90. # Log plugins list
  91. logger.debug("Available plugins list: {0}".format(self.getAllPlugins()))
  92. def load_exports(self, args=None):
  93. """Load all exports module in the 'exports' folder."""
  94. if args is None:
  95. return False
  96. header = "glances_"
  97. # Transform the arguments list into a dict
  98. # The aim is to chec if the export module should be loaded
  99. args_var = vars(locals()['args'])
  100. for item in os.listdir(exports_path):
  101. export_name = os.path.basename(item)[len(header):-3].lower()
  102. if (item.startswith(header) and
  103. item.endswith(".py") and
  104. item != (header + "export.py") and
  105. item != (header + "history.py") and
  106. args_var['export_' + export_name] is not None and
  107. args_var['export_' + export_name] is not False):
  108. # Import the export module
  109. export_module = __import__(os.path.basename(item)[:-3])
  110. # Add the export to the dictionary
  111. # The key is the module name
  112. # for example, the file glances_xxx.py
  113. # generate self._exports_list["xxx"] = ...
  114. self._exports[export_name] = export_module.Export(args=args, config=self.config)
  115. # Log plugins list
  116. logger.debug("Available exports modules list: {0}".format(self.getExportList()))
  117. return True
  118. def getAllPlugins(self):
  119. """Return the plugins list."""
  120. return [p for p in self._plugins]
  121. def getExportList(self):
  122. """Return the exports modules list."""
  123. return [p for p in self._exports]
  124. def load_limits(self, config=None):
  125. """Load the stats limits."""
  126. # For each plugins, call the init_limits method
  127. for p in self._plugins:
  128. # logger.debug("Load limits for %s" % p)
  129. self._plugins[p].load_limits(config)
  130. def update(self):
  131. """Wrapper method to update the stats."""
  132. # For standalone and server modes
  133. # For each plugins, call the update method
  134. for p in self._plugins:
  135. # logger.debug("Update %s stats" % p)
  136. self._plugins[p].update()
  137. def export(self, input_stats=None):
  138. """Export all the stats.
  139. Each export module is ran in a dedicated thread.
  140. """
  141. # threads = []
  142. input_stats = input_stats or {}
  143. for e in self._exports:
  144. logger.debug("Export stats using the %s module" % e)
  145. thread = threading.Thread(target=self._exports[e].update,
  146. args=(input_stats,))
  147. # threads.append(thread)
  148. thread.start()
  149. def getAll(self):
  150. """Return all the stats (list)."""
  151. return [self._plugins[p].get_raw() for p in self._plugins]
  152. def getAllExports(self):
  153. """
  154. Return all the stats to be exported (list).
  155. Default behavor is to export all the stat
  156. """
  157. return [self._plugins[p].get_export() for p in self._plugins]
  158. def getAllAsDict(self):
  159. """Return all the stats (dict)."""
  160. # Python > 2.6
  161. # {p: self._plugins[p].get_raw() for p in self._plugins}
  162. ret = {}
  163. for p in self._plugins:
  164. ret[p] = self._plugins[p].get_raw()
  165. return ret
  166. def getAllLimits(self):
  167. """Return the plugins limits list."""
  168. return [self._plugins[p].limits for p in self._plugins]
  169. def getAllLimitsAsDict(self):
  170. """Return all the stats limits (dict)."""
  171. ret = {}
  172. for p in self._plugins:
  173. ret[p] = self._plugins[p].limits
  174. return ret
  175. def getAllViews(self):
  176. """Return the plugins views."""
  177. return [self._plugins[p].get_views() for p in self._plugins]
  178. def getAllViewsAsDict(self):
  179. """Return all the stats views (dict)."""
  180. ret = {}
  181. for p in self._plugins:
  182. ret[p] = self._plugins[p].get_views()
  183. return ret
  184. def get_plugin_list(self):
  185. """Return the plugin list."""
  186. return self._plugins
  187. def get_plugin(self, plugin_name):
  188. """Return the plugin name."""
  189. if plugin_name in self._plugins:
  190. return self._plugins[plugin_name]
  191. else:
  192. return None
  193. def end(self):
  194. """End of the Glances stats."""
  195. # Close export modules
  196. for e in self._exports:
  197. self._exports[e].exit()
  198. # Close plugins
  199. for p in self._plugins:
  200. self._plugins[p].exit()
  201. class GlancesStatsServer(GlancesStats):
  202. """This class stores, updates and gives stats for the server."""
  203. def __init__(self, config=None):
  204. # Init the stats
  205. GlancesStats.__init__(self, config)
  206. # Init the all_stats dict used by the server
  207. # all_stats is a dict of dicts filled by the server
  208. self.all_stats = collections.defaultdict(dict)
  209. def update(self, input_stats=None):
  210. """Update the stats."""
  211. input_stats = input_stats or {}
  212. # Force update of all the stats
  213. GlancesStats.update(self)
  214. # Build all_stats variable (concatenation of all the stats)
  215. self.all_stats = self._set_stats(input_stats)
  216. def _set_stats(self, input_stats):
  217. """Set the stats to the input_stats one."""
  218. # Build the all_stats with the get_raw() method of the plugins
  219. ret = collections.defaultdict(dict)
  220. for p in self._plugins:
  221. ret[p] = self._plugins[p].get_raw()
  222. return ret
  223. def getAll(self):
  224. """Return the stats as a list."""
  225. return self.all_stats
  226. def getAllAsDict(self):
  227. """Return the stats as a dict."""
  228. # Python > 2.6
  229. # return {p: self.all_stats[p] for p in self._plugins}
  230. ret = {}
  231. for p in self._plugins:
  232. ret[p] = self.all_stats[p]
  233. return ret
  234. class GlancesStatsClient(GlancesStats):
  235. """This class stores, updates and gives stats for the client."""
  236. def __init__(self, config=None, args=None):
  237. """Init the GlancesStatsClient class."""
  238. # Init the plugin list dict
  239. self._plugins = collections.defaultdict(dict)
  240. # Init the configuration
  241. self.config = config
  242. # Init the arguments
  243. self.args = args
  244. # Init the export modules list dict
  245. self._exports = collections.defaultdict(dict)
  246. # Load the plugins
  247. self.load_exports(args=args)
  248. def set_plugins(self, input_plugins):
  249. """Set the plugin list according to the Glances server."""
  250. header = "glances_"
  251. for item in input_plugins:
  252. # Import the plugin
  253. plugin = __import__(header + item)
  254. # Add the plugin to the dictionary
  255. # The key is the plugin name
  256. # for example, the file glances_xxx.py
  257. # generate self._plugins_list["xxx"] = ...
  258. logger.debug("Server uses {0} plugin".format(item))
  259. self._plugins[item] = plugin.Plugin()
  260. # Restoring system path
  261. sys.path = sys_path
  262. def update(self, input_stats):
  263. """Update all the stats."""
  264. # For Glances client mode
  265. for p in input_stats:
  266. # Update plugin stats with items sent by the server
  267. self._plugins[p].set_stats(input_stats[p])
  268. # Update the views for the updated stats
  269. self._plugins[p].update_views()
  270. class GlancesStatsClientSNMP(GlancesStats):
  271. """This class stores, updates and gives stats for the SNMP client."""
  272. def __init__(self, config=None, args=None):
  273. # Init the plugin list dict
  274. self._plugins = collections.defaultdict(dict)
  275. # Init the configuration
  276. self.config = config
  277. # Init the arguments
  278. self.args = args
  279. # OS name is used because OID is differents between system
  280. self.os_name = None
  281. # Load plugins
  282. self.load_plugins(args=self.args)
  283. # Init the export modules list dict
  284. self._exports = collections.defaultdict(dict)
  285. # Load the plugins
  286. self.load_exports(args=args)
  287. def check_snmp(self):
  288. """Chek if SNMP is available on the server."""
  289. # Import the SNMP client class
  290. from glances.core.glances_snmp import GlancesSNMPClient
  291. # Create an instance of the SNMP client
  292. clientsnmp = GlancesSNMPClient(host=self.args.client,
  293. port=self.args.snmp_port,
  294. version=self.args.snmp_version,
  295. community=self.args.snmp_community,
  296. user=self.args.snmp_user,
  297. auth=self.args.snmp_auth)
  298. # If we cannot grab the hostname, then exit...
  299. ret = clientsnmp.get_by_oid("1.3.6.1.2.1.1.5.0") != {}
  300. if ret:
  301. # Get the OS name (need to grab the good OID...)
  302. oid_os_name = clientsnmp.get_by_oid("1.3.6.1.2.1.1.1.0")
  303. try:
  304. self.system_name = self.get_system_name(oid_os_name['1.3.6.1.2.1.1.1.0'])
  305. logger.info("SNMP system name detected: {0}".format(self.system_name))
  306. except KeyError:
  307. self.system_name = None
  308. logger.warning("Cannot detect SNMP system name")
  309. return ret
  310. def get_system_name(self, oid_system_name):
  311. """Get the short os name from the OS name OID string."""
  312. short_system_name = None
  313. if oid_system_name == '':
  314. return short_system_name
  315. # Find the short name in the oid_to_short_os_name dict
  316. try:
  317. iteritems = oid_to_short_system_name.iteritems()
  318. except AttributeError:
  319. # Correct issue #386
  320. iteritems = oid_to_short_system_name.items()
  321. for r, v in iteritems:
  322. if re.search(r, oid_system_name):
  323. short_system_name = v
  324. break
  325. return short_system_name
  326. def update(self):
  327. """Update the stats using SNMP."""
  328. # For each plugins, call the update method
  329. for p in self._plugins:
  330. # Set the input method to SNMP
  331. self._plugins[p].input_method = 'snmp'
  332. self._plugins[p].short_system_name = self.system_name
  333. try:
  334. self._plugins[p].update()
  335. except Exception as e:
  336. logger.error("Update {0} failed: {1}".format(p, e))