Start the server

This commit is contained in:
Nicolas Hennion 2014-01-19 22:20:09 +01:00
parent 8da175d5fa
commit bb8d379797
6 changed files with 239 additions and 45 deletions

View File

@ -18,6 +18,8 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import Glances libs
# Note: others Glances libs will be imported optionnaly
from .core.glances_core import GlancesCore
def main(argv=None):
@ -45,10 +47,8 @@ def main(argv=None):
if (core.password != ""):
server.add_user(core.username, core.password)
# Init stats
# !!! Uncomment
# stats = GlancesStatsServer()
# stats.update({})
# Start the server loop
server.serve_forever()
# Shutdown the server
# !!! How to close the server with CTRL-C

View File

@ -21,9 +21,13 @@
# Import system libs
import sys
import socket
import json
# Import Glances libs
from ..core.glances_stats import GlancesStatsServer
from ..core.glances_timer import Timer
# Other imports
try:
# Python 2
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
@ -127,6 +131,12 @@ class GlancesInstance():
"""
def __init__(self, cached_time=1):
# Init stats
self.stats = GlancesStatsServer()
# Initial update
self.stats.update({})
# cached_time is the minimum time interval between stats updates
# i.e. XML/RPC calls will not retrieve updated info until the time
# since last update is passed (will retrieve old cached info instead)
@ -136,7 +146,7 @@ class GlancesInstance():
def __update__(self):
# Never update more than 1 time per cached_time
if self.timer.finished():
stats.update()
self.stats.update()
self.timer = Timer(self.cached_time)
def init(self):
@ -146,7 +156,7 @@ class GlancesInstance():
def getAll(self):
# Update and return all the stats
self.__update__()
return json.dumps(stats.getAll())
return json.dumps(self.stats.getAll())
def getAllLimits(self):
# Return all the limits
@ -160,82 +170,82 @@ class GlancesInstance():
# Return operating system info
# No need to update...
#~ self.__update__()
return json.dumps(stats.getSystem())
return json.dumps(self.stats.getSystem())
def getCore(self):
# Update and return number of Core
self.__update__()
return json.dumps(stats.getCore())
return json.dumps(self.stats.getCore())
def getCpu(self):
# Update and return CPU stats
self.__update__()
return json.dumps(stats.getCpu())
return json.dumps(self.stats.getCpu())
def getLoad(self):
# Update and return LOAD stats
self.__update__()
return json.dumps(stats.getLoad())
return json.dumps(self.stats.getLoad())
def getMem(self):
# Update and return MEM stats
self.__update__()
return json.dumps(stats.getMem())
return json.dumps(self.stats.getMem())
def getMemSwap(self):
# Update and return MEMSWAP stats
self.__update__()
return json.dumps(stats.getMemSwap())
return json.dumps(self.stats.getMemSwap())
def getSensors(self):
# Update and return SENSORS stats
self.__update__()
return json.dumps(stats.getSensors())
return json.dumps(self.stats.getSensors())
def getHDDTemp(self):
# Update and return HDDTEMP stats
self.__update__()
return json.dumps(stats.getHDDTemp())
return json.dumps(self.stats.getHDDTemp())
def getNetwork(self):
# Update and return NET stats
self.__update__()
return json.dumps(stats.getNetwork())
return json.dumps(self.stats.getNetwork())
def getDiskIO(self):
# Update and return DISK IO stats
self.__update__()
return json.dumps(stats.getDiskIO())
return json.dumps(self.stats.getDiskIO())
def getFs(self):
# Update and return FS stats
self.__update__()
return json.dumps(stats.getFs())
return json.dumps(self.stats.getFs())
def getProcessCount(self):
# Update and return ProcessCount stats
self.__update__()
return json.dumps(stats.getProcessCount())
return json.dumps(self.stats.getProcessCount())
def getProcessList(self):
# Update and return ProcessList stats
self.__update__()
return json.dumps(stats.getProcessList())
return json.dumps(self.stats.getProcessList())
def getBatPercent(self):
# Update and return total batteries percent stats
self.__update__()
return json.dumps(stats.getBatPercent())
return json.dumps(self.stats.getBatPercent())
def getNow(self):
# Update and return current date/hour
self.__update__()
return json.dumps(stats.getNow().strftime(_("%Y-%m-%d %H:%M:%S")))
return json.dumps(self.stats.getNow().strftime(_("%Y-%m-%d %H:%M:%S")))
def getUptime(self):
# Update and return system uptime
self.__update__()
return json.dumps(stats.getUptime().strftime(_("%Y-%m-%d %H:%M:%S")))
return json.dumps(self.stats.getUptime().strftime(_("%Y-%m-%d %H:%M:%S")))
def __getTimeSinceLastUpdate(self, IOType):
assert(IOType in ['net', 'disk', 'process_disk'])
@ -270,6 +280,7 @@ class GlancesServer():
# By default, no auth is needed
self.server.user_dict = {}
self.server.isAuth = False
# Register functions
self.server.register_introspection_functions()
self.server.register_instance(GlancesInstance(cached_time))

View File

@ -18,6 +18,10 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import system libs
# Check for PSUtil already done in the glances_core script
from psutil import cpu_times
# from ..plugins.glances_plugin import GlancesPlugin
from glances_plugin import GlancesPlugin
@ -30,14 +34,60 @@ class Plugin(GlancesPlugin):
def __init__(self):
GlancesPlugin.__init__(self)
self.update()
def update(self):
# !!! Example
self.stats = { 'user': 15, 'iowait': 10 }
"""
Update CPU stats
"""
def __str__(self):
ret = "CPU\n"
for k in self.stats:
ret += "{0} {1}\n".format(k, self.stats[k])
return ret
# Grab CPU using the PSUtil cpu_times method
cputime = cpu_times(percpu=False)
cputime_total = cputime.user + cputime.system + cputime.idle
# Only available on some OS
if hasattr(cputime, 'nice'):
cputime_total += cputime.nice
if hasattr(cputime, 'iowait'):
cputime_total += cputime.iowait
if hasattr(cputime, 'irq'):
cputime_total += cputime.irq
if hasattr(cputime, 'softirq'):
cputime_total += cputime.softirq
if hasattr(cputime, 'steal'):
cputime_total += cputime.steal
if not hasattr(self, 'cputime_old'):
self.cputime_old = cputime
self.cputime_total_old = cputime_total
self.stats = {}
else:
self.cputime_new = cputime
self.cputime_total_new = cputime_total
try:
percent = 100 / (self.cputime_total_new -
self.cputime_total_old)
self.stats = {'user': (self.cputime_new.user -
self.cputime_old.user) * percent,
'system': (self.cputime_new.system -
self.cputime_old.system) * percent,
'idle': (self.cputime_new.idle -
self.cputime_old.idle) * percent}
if hasattr(self.cputime_new, 'nice'):
self.stats['nice'] = (self.cputime_new.nice -
self.cputime_old.nice) * percent
if hasattr(self.cputime_new, 'iowait'):
self.stats['iowait'] = (self.cputime_new.iowait -
self.cputime_old.iowait) * percent
if hasattr(self.cputime_new, 'irq'):
self.stats['irq'] = (self.cputime_new.irq -
self.cputime_old.irq) * percent
if hasattr(self.cputime_new, 'softirq'):
self.stats['softirq'] = (self.cputime_new.softirq -
self.cputime_old.softirq) * percent
if hasattr(self.cputime_new, 'steal'):
self.stats['steal'] = (self.cputime_new.steal -
self.cputime_old.steal) * percent
self.cputime_old = self.cputime_new
self.cputime_total_old = self.cputime_total_new
except Exception, err:
self.stats = {}

View File

@ -18,6 +18,10 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import system libs
import os
import platform
# from ..plugins.glances_plugin import GlancesPlugin
from glances_plugin import GlancesPlugin
@ -25,19 +29,33 @@ class Plugin(GlancesPlugin):
"""
Glances' Host Plugin
stats is a ?
stats is a dict
"""
def __init__(self):
GlancesPlugin.__init__(self)
self.update()
# self.update()
def update(self):
# !!! Example
self.stats = { 'os': 'linux' }
self.stats = {}
self.stats['os_name'] = platform.system()
self.stats['hostname'] = platform.node()
self.stats['platform'] = platform.architecture()[0]
is_archlinux = os.path.exists(os.path.join("/", "etc", "arch-release"))
if self.stats['os_name'] == "Linux":
if is_archlinux:
self.stats['linux_distro'] = "Arch Linux"
else:
linux_distro = platform.linux_distribution()
self.stats['linux_distro'] = ' '.join(linux_distro[:2])
self.stats['os_version'] = platform.release()
elif self.stats['os_name'] == "FreeBSD":
self.stats['os_version'] = platform.release()
elif self.stats['os_name'] == "Darwin":
self.stats['os_version'] = platform.mac_ver()[0]
elif self.stats['os_name'] == "Windows":
os_version = platform.win32_ver()
self.stats['os_version'] = ' '.join(os_version[::2])
else:
self.stats['os_version'] = ""
def __str__(self):
ret = "Host\n"
for k in self.stats:
ret += "{0} {1}\n".format(k, self.stats[k])
return ret

View File

@ -0,0 +1,107 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Glances - An eye on your system
#
# Copyright (C) 2014 Nicolargo <nicolas@nicolargo.com>
#
# Glances is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Glances is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import system libs
# Check for PSUtil already done in the glances_core script
from psutil import cpu_times
# from ..plugins.glances_plugin import GlancesPlugin
from glances_plugin import GlancesPlugin
class Plugin(GlancesPlugin):
"""
Glances' PerCpu Plugin
stats is a list
"""
def __init__(self):
GlancesPlugin.__init__(self)
def update(self):
"""
Update Per CPU stats
"""
# Grab CPU using the PSUtil cpu_times method
# Per-CPU
percputime = cpu_times(percpu=True)
percputime_total = []
for i in range(len(percputime)):
percputime_total.append(percputime[i].user +
percputime[i].system +
percputime[i].idle)
# Only available on some OS
for i in range(len(percputime)):
if hasattr(percputime[i], 'nice'):
percputime_total[i] += percputime[i].nice
for i in range(len(percputime)):
if hasattr(percputime[i], 'iowait'):
percputime_total[i] += percputime[i].iowait
for i in range(len(percputime)):
if hasattr(percputime[i], 'irq'):
percputime_total[i] += percputime[i].irq
for i in range(len(percputime)):
if hasattr(percputime[i], 'softirq'):
percputime_total[i] += percputime[i].softirq
for i in range(len(percputime)):
if hasattr(percputime[i], 'steal'):
percputime_total[i] += percputime[i].steal
if not hasattr(self, 'percputime_old'):
self.percputime_old = percputime
self.percputime_total_old = percputime_total
self.stats = []
else:
self.percputime_new = percputime
self.percputime_total_new = percputime_total
perpercent = []
self.stats = []
try:
for i in range(len(self.percputime_new)):
perpercent.append(100 / (self.percputime_total_new[i] -
self.percputime_total_old[i]))
cpu = {'user': (self.percputime_new[i].user -
self.percputime_old[i].user) * perpercent[i],
'system': (self.percputime_new[i].system -
self.percputime_old[i].system) * perpercent[i],
'idle': (self.percputime_new[i].idle -
self.percputime_old[i].idle) * perpercent[i]}
if hasattr(self.percputime_new[i], 'nice'):
cpu['nice'] = (self.percputime_new[i].nice -
self.percputime_old[i].nice) * perpercent[i]
if hasattr(self.percputime_new[i], 'iowait'):
cpu['iowait'] = (self.percputime_new[i].iowait -
self.percputime_old[i].iowait) * perpercent[i]
if hasattr(self.percputime_new[i], 'irq'):
cpu['irq'] = (self.percputime_new[i].irq -
self.percputime_old[i].irq) * perpercent[i]
if hasattr(self.percputime_new[i], 'softirq'):
cpu['softirq'] = (self.percputime_new[i].softirq -
self.percputime_old[i].softirq) * perpercent[i]
if hasattr(self.percputime_new[i], 'steal'):
cpu['steal'] = (self.percputime_new[i].steal -
self.percputime_old[i].steal) * perpercent[i]
self.stats.append(cpu)
self.percputime_old = self.percputime_new
self.percputime_total_old = self.percputime_total_new
except Exception, err:
self.stats = []

View File

@ -26,3 +26,11 @@ class GlancesPlugin(object):
def __init__(self):
# Init the stat list
self.stats = None
def __str__(self):
# Return the human-readable stats
return str(self.stats)
def get_stats(self):
# Return the stats object
return self.stats