src/collecty/daemon.py \
src/collecty/errors.py \
src/collecty/i18n.py \
- src/collecty/locales.py \
src/collecty/logger.py \
src/collecty/util.py
+++ /dev/null
-#!/usr/bin/python3
-###############################################################################
-# #
-# collecty - A system statistics collection daemon for IPFire #
-# Copyright (C) 2015 IPFire development team #
-# #
-# This program is free software: you can redistribute it and/or modify #
-# it under the terms of the GNU General Public License as published by #
-# the Free Software Foundation, either version 3 of the License, or #
-# (at your option) any later version. #
-# #
-# This program 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 General Public License for more details. #
-# #
-# You should have received a copy of the GNU General Public License #
-# along with this program. If not, see <http://www.gnu.org/licenses/>. #
-# #
-###############################################################################
-
-import gettext
-import logging
-import os
-
-from .constants import *
-from .i18n import TEXTDOMAIN
-
-log = logging.getLogger("collecty.locale")
-
-DEFAULT_LOCALE = "en_US.utf-8"
-
-class Locale(object):
- def __init__(self, lang):
- self.lang = lang
-
- def translate(self, message, plural_message=None, count=None):
- if plural_message is not None:
- assert count is not None
-
- # Replace the message by the plural message if
- # we are using plural.
- if count > 1:
- message = plural_message
-
- return message
-
-
-class GettextLocale(Locale):
- def __init__(self, lang, translation):
- Locale.__init__(self, lang)
-
- self.translation = translation
-
- # Bind gettext functions
- self.gettext = self.translation.gettext
- self.ngettext = self.translation.ngettext
-
- def translate(self, message, plural_message=None, count=None):
- if plural_message is not None:
- assert count is not None
- return self.ngettext(message, plural_message, count)
-
- return self.gettext(message)
-
-
-def _find_all_locales(domain, directory):
- locales = {
- DEFAULT_LOCALE : Locale(DEFAULT_LOCALE),
- }
-
- for lang in os.listdir(directory):
- if lang.startswith("."):
- continue
-
- filename = os.path.join(directory, lang, "LC_MESSAGES",
- "%s.mo" % domain)
-
- # Check if a translation file exists and go on if not
- if not os.path.exists(filename):
- continue
-
- try:
- translation = gettext.translation(domain,
- directory, languages=[lang])
- except Exception as e:
- log.error("Cound not load translation for %s: %s" \
- % (lang, e))
- continue
-
- locales[lang] = GettextLocale(lang, translation)
-
- log.debug("Loaded translations: %s" % ", ".join(locales.keys()))
-
- return locales
-
-_locales = _find_all_locales(TEXTDOMAIN, "/usr/share/locale")
-
-def get_supported_locales():
- return list(_locales.keys())
-
-def get_closest(*langs):
- for lang in langs:
- if not lang:
- continue
-
- lang = lang.replace("-", "_")
- parts = lang.split("_")
-
- if len(parts) > 2:
- continue
-
- elif len(parts) == 2:
- parts[0] = parts[0].lower()
- parts[1] = parts[1].upper()
- lang = "_".join(parts)
-
- for l in (lang, parts[0]):
- try:
- return _locales[l]
- except KeyError:
- pass
-
-def get(*langs):
- return get_closest(*langs) or _locales.get(DEFAULT_LOCALE, None)
import time
import unicodedata
-from .. import locales
from .. import util
from ..constants import *
from ..i18n import _
Sets the correct environment for rrdtool to create
localised graphs and graphs in the correct timezone.
"""
- def __init__(self, timezone, locale):
+ def __init__(self, timezone="UTC", locale="en_US.utf-8"):
# Build the new environment
self.new_environment = {
- "TZ" : timezone or "UTC",
+ "LANG" : locale,
+ "LC_ALL" : locale,
+ "TZ" : timezone,
}
- for k in ("LANG", "LC_ALL"):
- self.new_environment[k] = locale or "en_US.utf-8"
-
def __enter__(self):
# Save the current environment
self.old_environment = {}
time_start = time.time()
- graph = template.generate_graph(**kwargs)
+ with Environment(timezone=timezone, locale=locale):
+ graph = template.generate_graph(**kwargs)
duration = time.time() - time_start
self.log.debug(_("Generated graph %s in %.1fms") \
self.plugin = plugin
# Save localisation parameters
- self.locale = locales.get(locale)
+ self.locale = locale
self.timezone = timezone
# Get all required RRD objects
for arg in args:
self.log.debug(" %s" % arg)
- with Environment(self.timezone, self.locale.lang):
- graph = rrdtool.graphv("-", *args)
+ graph = rrdtool.graphv("-", *args)
return {
"image" : graph.get("image"),
from ..colours import *
from ..constants import *
+from ..i18n import _
class ConntrackGraphTemplate(base.GraphTemplate):
name = "conntrack"
@property
def rrd_graph(self):
- _ = self.locale.translate
-
return [
"COMMENT:%s" % EMPTY_LABEL,
"COMMENT:%s" % (COLUMN % _("Current")),
@property
def graph_title(self):
- _ = self.locale.translate
-
return _("Connection Tracking Table")
@property
def graph_vertical_label(self):
- _ = self.locale.translate
-
return _("Entries")
from ..colours import *
from ..constants import *
+from ..i18n import _
class GraphTemplateContextSwitches(base.GraphTemplate):
name = "context-switches"
@property
def rrd_graph(self):
- _ = self.locale.translate
-
return [
"COMMENT:%s" % EMPTY_LABEL,
"COMMENT:%s" % (COLUMN % _("Current")),
@property
def graph_title(self):
- _ = self.locale.translate
return _("Context Switches")
@property
def graph_vertical_label(self):
- _ = self.locale.translate
return _("Context Switches/s")
@property
def graph_title(self):
- _ = self.locale.translate
return _("Processor Frequencies")
@property
def graph_vertical_label(self):
- _ = self.locale.translate
return "%s [%s]" % (_("Frequency"), _("Hz"))
processor_colours = [
from ..constants import *
from ..colours import *
+from ..i18n import _
class GraphTemplateDiskUsage(base.GraphTemplate):
name = "disk-usage"
@property
def rrd_graph(self):
- _ = self.locale.translate
-
return [
"COMMENT:%s" % EMPTY_LABEL,
"COMMENT:%s" % (COLUMN % _("Current")),
@property
def graph_title(self):
- _ = self.locale.translate
return _("Disk Usage of %s") % self.object.mountpoint
@property
def graph_vertical_label(self):
- _ = self.locale.translate
return _("Bytes")
@property
def rrd_graph(self):
- _ = self.locale.translate
-
rrd_graph = [
"COMMENT:%s" % EMPTY_LABEL,
"COMMENT:%s" % (COLUMN % _("Current")),
@property
def graph_title(self):
- _ = self.locale.translate
return _("Inode Usage of %s") % self.object.mountpoint
@property
def graph_vertical_label(self):
- _ = self.locale.translate
return _("Inodes")
from ..colours import *
from ..constants import *
+from ..i18n import _
class GraphTemplateDiskBadSectors(base.GraphTemplate):
name = "disk-bad-sectors"
@property
def rrd_graph(self):
- _ = self.locale.translate
-
return [
"COMMENT:%s" % EMPTY_LABEL,
"COMMENT:%s" % (COLUMN % _("Current")),
@property
def graph_title(self):
- _ = self.locale.translate
-
return _("Bad Sectors of %s") % self.object.device_string
@property
def graph_vertical_label(self):
- _ = self.locale.translate
-
return _("Pending/Relocated Sectors")
@property
def rrd_graph(self):
- _ = self.locale.translate
-
rrd_graph = [
"CDEF:read_bytes=read_sectors,512,*",
"CDEF:write_bytes=write_sectors,512,*",
@property
def graph_title(self):
- _ = self.locale.translate
return _("Disk Utilisation of %s") % self.object.device_string
@property
def graph_vertical_label(self):
- _ = self.locale.translate
return _("Byte per Second")
@property
def rrd_graph(self):
- _ = self.locale.translate
-
rrd_graph = [
"LINE1:read_ios%s:%-15s" % (COLOUR_READ, _("Read")),
"GPRINT:read_ios_cur:%12s\:" % _("Current") + " %6.2lf",
@property
def graph_title(self):
- _ = self.locale.translate
return _("Disk IO Operations of %s") % self.object.device_string
@property
def graph_vertical_label(self):
- _ = self.locale.translate
return _("Operations per Second")
@property
def rrd_graph(self):
- _ = self.locale.translate
-
rrd_graph = [
"CDEF:celsius=temperature,273.15,-",
"VDEF:temp_cur=celsius,LAST",
@property
def graph_title(self):
- _ = self.locale.translate
return _("Disk Temperature of %s") % self.object.device_string
@property
def graph_vertical_label(self):
- _ = self.locale.translate
return _("° Celsius")
@property
from ..colours import *
from ..constants import *
+from ..i18n import _
class GraphTemplateInterfaceBase(base.GraphTemplate):
@property
@property
def rrd_graph(self):
- _ = self.locale.translate
-
return [
# Headline
"COMMENT:%s" % EMPTY_LABEL,
@property
def graph_title(self):
- _ = self.locale.translate
-
return _("Bandwidth Usage on %s") % self.interface
@property
def graph_vertical_label(self):
- _ = self.locale.translate
-
return _("Bit/s")
@property
def rrd_graph(self):
- _ = self.locale.translate
-
return [
# Headline
"COMMENT:%s" % EMPTY_LABEL,
@property
def graph_title(self):
- _ = self.locale.translate
-
return _("Transferred Packets on %s") % self.interface
@property
def graph_vertical_label(self):
- _ = self.locale.translate
return _("Packets/s")
@property
def rrd_graph(self):
- _ = self.locale.translate
-
return [
# Headline
"COMMENT:%s" % EMPTY_LABEL,
@property
def graph_title(self):
- _ = self.locale.translate
-
return _("Errors/Dropped Packets on %s") % self.interface
@property
def graph_vertical_label(self):
- _ = self.locale.translate
-
return _("Packets/s")
from ..colours import *
from ..constants import *
+from ..i18n import _
class GraphTemplateInterrupts(base.GraphTemplate):
name = "interrupts"
@property
def rrd_graph(self):
- _ = self.locale.translate
-
return [
# Headline
"COMMENT:%s" % EMPTY_LABEL,
@property
def graph_title(self):
- _ = self.locale.translate
-
if self.object.irq is None:
return _("Interrupts")
@property
def graph_vertical_label(self):
- _ = self.locale.translate
-
return _("Interrupts/s")
from ..colours import *
from ..constants import *
+from ..i18n import _
class GraphTemplateIPv6Fragmentation(base.GraphTemplate):
name = "ipv6-fragmentation"
@property
def rrd_graph(self):
- _ = self.locale.translate
-
return [
"CDEF:ip6_reasm_real_fails=ip6_reasm_fails,ip6_reasm_timeout,-",
@property
def graph_title(self):
- _ = self.locale.translate
-
if self.object.interface:
return _("IPv6 Fragmentation on %s") % self.object.interface
@property
def graph_vertical_label(self):
- _ = self.locale.translate
-
return _("Packets/s")
@property
@property
def rrd_graph(self):
- _ = self.locale.translate
-
return [
"CDEF:ip4_reasm_real_fails=ip4_reasm_fails,ip4_reasm_timeout,-",
@property
def graph_title(self):
- _ = self.locale.translate
-
if self.object.interface:
return _("IPv4 Fragmentation on %s") % self.object.interface
@property
def graph_vertical_label(self):
- _ = self.locale.translate
return _("Packets/s")
@property
import socket
from .. import _collecty
-from ..i18n import _
from . import base
from ..colours import *
from ..constants import *
+from ..i18n import _
PING_HOSTS = [
# gateway is a special name that is automatically
@property
def rrd_graph(self):
- _ = self.locale.translate
-
return [
# Compute the biggest loss and convert into percentage
"CDEF:ploss=loss6,loss4,MAX,100,*",
@property
def graph_title(self):
- _ = self.locale.translate
-
if self.object.hostname == "gateway":
hostname = _("Default Gateway")
else:
@property
def graph_vertical_label(self):
- _ = self.locale.translate
return _("Milliseconds")
@property
from ..colours import *
from ..constants import *
+from ..i18n import _
class GraphTemplateLoadAvg(base.GraphTemplate):
name = "loadavg"
@property
def rrd_graph(self):
- _ = self.locale.translate
-
rrd_graph = [
"LINE2:load15%s:%s" % (
YELLOW, LABEL % _("15 Minutes"),
@property
def graph_title(self):
- _ = self.locale.translate
-
return _("Load Average")
@property
def graph_vertical_label(self):
- _ = self.locale.translate
-
return _("Load")
@property
from ..colours import *
from ..constants import *
+from ..i18n import _
class GraphTemplateMemory(base.GraphTemplate):
name = "memory"
@property
def rrd_graph(self):
- _ = self.locale.translate
-
return [
# Headline
"COMMENT:%s" % EMPTY_LABEL,
@property
def graph_title(self):
- _ = self.locale.translate
-
return _("Memory Usage")
@property
def graph_vertical_label(self):
- _ = self.locale.translate
-
return _("Bytes")
from ..colours import *
from ..constants import *
+from ..i18n import _
class GraphTemplateProcessor(base.GraphTemplate):
name = "processor"
@property
def rrd_graph(self):
- _ = self.locale.translate
-
return [
# Add all used CPU cycles
"CDEF:usage=user,nice,+,sys,+,wait,+,irq,+,sirq,+,steal,+,guest,+,guest_nice,+",
@property
def graph_title(self):
- _ = self.locale.translate
return _("Processor Usage")
@property
def graph_vertical_label(self):
- _ = self.locale.translate
return _("Percent")
from .. import _collecty
from . import base
+from ..i18n import _
class GraphTemplateSensorsTemperature(base.GraphTemplate):
name = "sensors-temperature"
@property
def rrd_graph(self):
- _ = self.locale.translate
-
return [
# Convert everything to Celsius
"CDEF:value_c=value,273.15,-",
@property
def graph_title(self):
- _ = self.locale.translate
return _("Temperature (%s)") % self.object.sensor.name
@property
def graph_vertical_label(self):
- _ = self.locale.translate
return _("° Celsius")
@property
def rrd_graph(self):
- _ = self.locale.translate
rrd_graph = []
counter = 0
@property
def graph_title(self):
- _ = self.locale.translate
return _("Processor")
@property
def graph_vertical_label(self):
- _ = self.locale.translate
return _("Temperature")