Skip to content

Instantly share code, notes, and snippets.

@dongweiming
Last active December 20, 2015 10:49
Show Gist options
  • Save dongweiming/6118859 to your computer and use it in GitHub Desktop.
Save dongweiming/6118859 to your computer and use it in GitHub Desktop.
def __getattr__(self, name):
"""
According to invoke methods to obtain data
"""
base_type = ['Careful', 'Warning', 'Critical']
base_module = ['CPU', 'LOAD', 'Process', 'STD', 'MEM', 'SWAP', 'TEMP', 'HDDTEMP', 'FS']
get_type = ['get'+n for n in base_type]
get_module = ['get'+m+t for m in base_module\
for t in base_type]
if name in get_module:
for index, t in enumerate(base_type):
if name.endswith(t):
module_name = name.replace('get','').replace(t,'')
if module_name == 'CPU':
return partial(self.get_CPUStat, index)
elif module_name == 'LOAD':
return partial(self.get_LOADStat, index)
elif module_name == 'Process':
return partial(self.get_ProcessStat, index)
return partial(self.get_Stat, index, module_name)
elif name in get_type:
return partial(self.get_Stat, get_type.index(name))
def get_Stat(self, index, stat):
return self.__limits_list[stat][index]
def get_CPUStat(self, index, stat):
return self.get_Stat(index, 'CPU_'+stat.upper())
def get_LOADStat(self, index, core=1):
return self.get_Stat(index, 'LOAD') * core
def get_ProcessStat(self, index, stat='', core=1):
if stat.upper() != 'CPU':
# Use core only for CPU
core = 1
return self.get_Stat(index, 'PROCESS_' + stat.upper()) * core
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment