#!/usr/bin/env python
#
# Glances is a simple CLI monitoring tool based on libstatgrab
#
# Pre-requisites: python-statgrab 0.5 or >
#
# Copyright (C) Nicolargo 2011 <nicolas@nicolargo.com>
#
# 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 os
import getopt
import sys
import signal
import time
import datetime
import multiprocessing
import gettext
#i18n
#====
application = 'glances'
gettext.install(application)
try:
import statgrab
except:
print 'Statgrab initialization failed, Glances cannot start.'
print ''
sys.exit(1)
try:
import curses
import curses.panel
except:
print _('Textmode GUI initialization failed, Glances cannot start.')
print
sys.exit(1)
# Globals variables
#==================
# The glances version id
__version__ = "1.3.5"
# Class
#======
class Timer():
"""
The timer class
"""
def __init__(self, duration):
self.target = time.time() + duration
def finished(self):
return time.time() > self.target
class glancesGrabFs():
"""
Get FS stats: idem as structure http://www.i-scream.org/libstatgrab/docs/sg_get_fs_stats.3.html
"""
def __init__(self):
self.__update__()
def __update__(self):
"""
Update the stats
"""
# Reset the list
self.fs_list = []
# Ignore the following fs
ignore_fsname = ('none', 'gvfs-fuse-daemon', 'fusectl', 'cgroup')
ignore_fstype = ('binfmt_misc', 'devpts', 'iso9660', 'none', 'proc', 'sysfs', 'usbfs')
# Open the current mounted FS
mtab = open("/etc/mtab", "r")
for line in mtab.readlines():
if line.split()[0] in ignore_fsname: continue
if line.split()[2] in ignore_fstype: continue
# Get FS stats
fs_current = {}
fs_name = self.__getmount__(line.split()[1])
fs_stats = os.statvfs(fs_name)
# Build the list
fs_current['device_name'] = str(line.split()[0])
fs_current['fs_type'] = str(line.split()[2])
fs_current['mnt_point'] = str(fs_name)
fs_current['size'] = float(fs_stats.f_blocks) * long(fs_stats.f_frsize)
fs_current['used'] = float(fs_stats.f_blocks - fs_stats.f_bfree) * long(fs_stats.f_frsize)
fs_current['avail'] = float(fs_stats.f_bfree) * long(fs_stats.f_frsize)
self.fs_list.append(fs_current)
mtab.close()
def __getmount__(self, path):
"""
Return the real root path of a file
Exemple: /home/nicolargo can return /home or /
"""
path = os.path.realpath(os.path.abspath(path))
while path != os.path.sep:
if os.path.ismount(path):
return path
path = os.path.abspath(os.path.join(path, os.pardir))
return path
def get(self):
self.__update__()
return self.fs_list
class glancesStats():
"""
This class store, update and give the libstatgrab stats
"""
def __init__(self):
"""
Init the libstatgrab and process to the first update
"""
# Init libstatgrab
if not statgrab.sg_init():
print _("Error: Can not init the libstatgrab library.\n")
# Init the fs stats
try:
self.glancesgrabfs = glancesGrabFs()
except:
self.glancesgrabfs = {}
# Do the first update
self.__update__()
def __update__(self):
"""
Update the stats
"""
# Get informations from libstatgrab and others...
try:
self.host = statgrab.sg_get_host_info()
except:
self.host = {}
self.system = self.host
try:
self.cpu = statgrab.sg_get_cpu_percents()
except:
self.cpu = {}
try:
self.load = statgrab.sg_get_load_stats()
except:
self.load = {}
try:
self.mem = statgrab.sg_get_mem_stats()
except:
self.mem = {}
try:
self.memswap = statgrab.sg_get_swap_stats()
except:
self.memswap = {}
try:
self.networkinterface = statgrab.sg_get_network_iface_stats()
except:
self.networkinterface = {}
try:
self.network = statgrab.sg_get_network_io_stats_diff()
except:
self.network = {}
try:
self.diskio = statgrab.sg_get_disk_io_stats_diff()
except:
self.diskio = {}
try:
# Replace the bugged self.fs = statgrab.sg_get_fs_stats()
self.fs = self.glancesgrabfs.get()
except:
self.fs = {}
try:
self.processcount = statgrab.sg_get_process_count()
except:
self.processcount = {}
try:
self.process = statgrab.sg_get_process_stats()
except:
self.process = {}
# Get the current date/time
self.now = datetime.datetime.now()