提交 fddca794 编写于 作者: N Nicolas Hennion

Correct automatic sort

上级 6554c875
......@@ -79,14 +79,10 @@ gettext.install(gettext_domain, locale_dir)
# Instances shared between all Glances's scripts
#===============================================
# The global instance for the configuration file
# from ..core.glances_config import Config as glancesConfig
# glances_config = glancesConfig()
# Processcount and processlist plugins
from ..core.glances_processes import glancesProcesses
# glances_processes for processcount and processlist plugins
from glances.core.glances_processes import glancesProcesses
glances_processes = glancesProcesses()
# Default auto process sort is 'cpu_percent'
process_auto_by = 'cpu_percent'
# The global instance for the logs
from ..core.glances_logs import glancesLogs
from glances.core.glances_logs import glancesLogs
glances_logs = glancesLogs()
......@@ -23,7 +23,7 @@ import time
from datetime import datetime
# Import Glances libs
from glances.core.glances_globals import process_auto_by
from glances.core.glances_globals import glances_processes
class glancesLogs:
......@@ -55,24 +55,18 @@ class glancesLogs:
# Init the logs list
self.logs_list = []
# Automaticaly define the sort to apply on the processes list
self.sort_process_by = 'none'
def get(self):
"""
Return the logs list (RAW)
"""
return self.logs_list
def len(self):
"""
Return the number of item in the log list
"""
return self.logs_list.__len__()
def __itemexist__(self, item_type):
"""
An item exist in the list if:
......@@ -86,33 +80,52 @@ class glancesLogs:
return i
return -1
def add(self, item_state, item_type, item_value, proc_list=[], proc_desc=""):
def set_process_sort(self, item_type):
"""
If item is a 'new one':
Add the new item at the beginning of the logs list
Else:
Update the existing item
Define the process auto sort key from the alert type
"""
# Add Top process sort depending on alert type
process_auto_by = 'cpu_percent'
# Process sort depending on alert type
if (item_type.startswith("MEM")):
# Sort TOP process by memory_percent
process_auto_by = 'memory_percent'
elif (item_type.startswith("CPU IO")):
elif (item_type.startswith("CPU_IOWAIT")):
# Sort TOP process by io_counters (only for Linux OS)
process_auto_by = 'io_counters'
elif (item_type.startswith("MON")):
# !!! Never in v2 because MON are not logged...
# Do no sort process for monitored prcesses list
self.sort_process_by = 'none'
else:
# Default sort is...
process_auto_by = 'cpu_percent'
glances_processes.setsortkey(process_auto_by)
return process_auto_by
def reset_process_sort(self):
"""
Reset the process_auto_by variable
"""
# Default sort is...
process_auto_by = 'cpu_percent'
glances_processes.setsortkey(process_auto_by)
return process_auto_by
def add(self, item_state, item_type, item_value, proc_list=[], proc_desc=""):
"""
If item is a 'new one':
Add the new item at the beginning of the logs list
Else:
Update the existing item
"""
# Add or update the log
item_index = self.__itemexist__(item_type)
if (item_index < 0):
# Item did not exist, add if WARNING or CRITICAL
if ((item_state == "WARNING") or (item_state == "CRITICAL")):
# Define the automatic process sort key
self.set_process_sort(item_type)
# Create the new log item
# Time is stored in Epoch format
# Epoch -> DMYHMS = datetime.fromtimestamp(epoch)
item = []
......@@ -126,23 +139,27 @@ class glancesLogs:
item.append(item_value) # MIN
item.append(item_value) # SUM
item.append(1) # COUNT
# Process list is sorted automaticaly
# Process list is sorted automaticaly
# Overwrite the user choise
topprocess = sorted(proc_list, key=lambda process: process[process_auto_by],
reverse=True)
item.append(topprocess[0:3]) # TOP 3 PROCESS LIST
# topprocess = sorted(proc_list, key=lambda process: process[process_auto_by],
# reverse=True)
# item.append(topprocess[0:3]) # TOP 3 PROCESS LIST
item.append([]) # TOP 3 PROCESS LIST
item.append(proc_desc) # MONITORED PROCESSES DESC
# Add the item to the list
self.logs_list.insert(0, item)
if self.len() > self.logs_max:
self.logs_list.pop()
else:
# Item exist, update
if ((item_state == "OK") or (item_state == "CAREFUL")):
# Reset the automatic process sort key
self.reset_process_sort()
# Close the item
self.logs_list[item_index][1] = time.mktime(
datetime.now().timetuple())
# TOP PROCESS LIST
self.logs_list[item_index][9] = []
else:
# Update the item
# State
......@@ -160,18 +177,19 @@ class glancesLogs:
self.logs_list[item_index][8] += 1
self.logs_list[item_index][5] = (self.logs_list[item_index][7] /
self.logs_list[item_index][8])
# Process list is sorted automaticaly
# Overwrite the user choise
topprocess = sorted(proc_list, key=lambda process: process[process_auto_by],
reverse=True)
# TOP PROCESS LIST
self.logs_list[item_index][9] = topprocess[0:3]
# # Process list is sorted automaticaly
# # Overwrite the user choise
# topprocess = sorted(proc_list, key=lambda process: process[process_auto_by],
# reverse=True)
# # TOP PROCESS LIST
# self.logs_list[item_index][9] = topprocess[0:3]
self.logs_list[item_index][9] = []
# MONITORED PROCESSES DESC
self.logs_list[item_index][10] = proc_desc
return self.len()
def clean(self, critical=False):
"""
Clean the log list by deleting finished item
......
......@@ -47,6 +47,7 @@ class glancesProcesses:
# value = [ read_bytes_old, write_bytes_old ]
self.io_old = {}
# Init
self.processsort = 'cpu_percent'
self.processlist = []
self.processcount = {'total': 0, 'running': 0, 'sleeping': 0, 'thread': 0}
......@@ -193,6 +194,19 @@ class glancesProcesses:
"""
return self.processlist
def getsortkey(self):
"""
Return the current sort key for automatic sort
"""
return self.processsort
def setsortkey(self, sortedby):
"""
Return the current sort key for automatic sort
"""
self.processsort = sortedby
return self.processsort
def getsortlist(self, sortedby=None):
"""
Return the processlist
......
......@@ -20,7 +20,7 @@
# Import Glances libs
from glances.plugins.glances_plugin import GlancesPlugin
from glances.core.glances_globals import glances_processes, process_auto_by
from glances.core.glances_globals import glances_processes
class Plugin(GlancesPlugin):
......@@ -95,7 +95,7 @@ class Plugin(GlancesPlugin):
if (args.process_sorted_by == 'auto'):
msg = "{0}".format(_("sorted automatically"))
ret.append(self.curse_add_line(msg))
msg = " {0} {1}".format(_("by"), process_auto_by)
msg = " {0} {1}".format(_("by"), glances_processes.getsortkey())
ret.append(self.curse_add_line(msg))
else:
msg = "{0} {1}".format(_("sorted by"), args.process_sorted_by)
......
......@@ -23,7 +23,7 @@ from datetime import timedelta
# Import Glances libs
from glances.plugins.glances_plugin import GlancesPlugin
from glances.core.glances_globals import glances_processes, process_auto_by
from glances.core.glances_globals import glances_processes
class Plugin(GlancesPlugin):
......@@ -69,7 +69,7 @@ class Plugin(GlancesPlugin):
# Compute the sort key
if (args.process_sorted_by == 'auto'):
process_sort_key = process_auto_by
process_sort_key = glances_processes.getsortkey()
else:
process_sort_key = args.process_sorted_by
sort_style = 'BOLD'
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册