Пример #1
0
 def setLogLevel(self, value):
     try:
         self.__loggingLock.acquire()
         getLogger("bill2").setLevel(
             getattr(logging, value.upper()))
     except AttributeError:
         raise ValueError("Invalid log level")
     else:
         self.__logLevel = value.upper()
     finally:
         self.__loggingLock.release()
Пример #2
0
 def setLogTarget(self, target):
     try:
         self.__loggingLock.acquire()
         # set a format which is simpler for console use
         formatter = logging.Formatter("%(asctime)s %(name)-24s[%(process)d]: %(levelname)-7s %(message)s")
         if target == "SYSLOG":
             # Syslog daemons already add date to the message.
             formatter = logging.Formatter("%(name)s[%(process)d]: %(levelname)s %(message)s")
             facility = logging.handlers.SysLogHandler.LOG_DAEMON
             hdlr = logging.handlers.SysLogHandler("/dev/log", facility=facility)
         elif target == "STDOUT":
             hdlr = logging.StreamHandler(sys.stdout)
         elif target == "STDERR":
             hdlr = logging.StreamHandler(sys.stderr)
         else:
             # Target should be a file
             try:
                 open(target, "a").close()
                 hdlr = logging.handlers.RotatingFileHandler(target)
             except IOError:
                 logSys.error("Unable to log to " + target)
                 logSys.info("Logging to previous target " + self.__logTarget)
                 return False
         # Removes previous handlers -- in reverse order since removeHandler
         # alter the list in-place and that can confuses the iterable
         logger = getLogger("bill2")
         for handler in logger.handlers[::-1]:
             # Remove the handler.
             logger.removeHandler(handler)
             # And try to close -- it might be closed already
             try:
                 handler.flush()
                 handler.close()
             except (ValueError, KeyError):  # pragma: no cover
                 # Is known to be thrown after logging was shutdown once
                 # with older Pythons -- seems to be safe to ignore there
                 # At least it was still failing on 2.6.2-0ubuntu1 (jaunty)
                 if (2, 6, 3) <= sys.version_info < (3,) or \
                                 (3, 2) <= sys.version_info:
                     raise
         # tell the handler to use this format
         hdlr.setFormatter(formatter)
         logger.addHandler(hdlr)
         # Does not display this message at startup.
         if not self.__logTarget is None:
             logSys.info("Changed logging target to %s for Fail2ban v%s" %
                         (target, 1))
         # Sets the logging target.
         self.__logTarget = target
         return True
     finally:
         self.__loggingLock.release()
Пример #3
0
#!/usr/bin/python

__author__ = 'sn'
import getopt, sys, os
from time import sleep

from bill2.server import Server
from bill2.version import version
from util.helpers import getLogger


# Gets the instance of the logger.
logSys = getLogger("bill2")


class Bill2Server:

    def __init__(self):
        self.__server = None
        self.__argv = None
        self.__conf = dict()
        self.__conf["background"] = True
        self.__conf["socket"] = "/var/run/bill2.sock"
        self.__conf["pidfile"] = "/var/run/bill2.pid"

    def dispVersion(self):
        print "Bill2 v %s" % version
        print "Many contributions by Cyril Jaquier <*****@*****.**>."

    def disp_usage(self):
        print "Usage: "+self.__argv[0]+" [OPTIONS]"
Пример #4
0
__author__ = 'sn355_000'
from bill2.nas import Nas, NasComError
from util.RosAPI import Core, MikrotikConnectError
from threading import Lock
from util.helpers import getLogger

sysLog = getLogger(__name__)

class MikroNas(Nas):
    def __init__(self, hosts=None, address='', login='', passwd=''):
        super(MikroNas, self).__init__(hosts)
        self.__addr = address
        self.__login = login
        self.__pass = passwd
        self.__hw_lock = Lock()
        self.__hw_nas = None
        self.__hosts_state = None

    @property
    def _connect(self):
        if not self._nas_connected:
            sysLog.debug('Trying connect to nas')
            with self.__hw_lock:
                try:
                    self.__hw_nas = Core(self.__addr)
                    if self.__hw_nas.ok:
                        self.__hw_nas.login(self.__login, self.__pass)
                        if self.__hw_nas.ok:
                            self.__hosts_state = self.__get_hw_nas_state
                            self._nas_connected = True
                            sysLog.debug('connection to nas success')
Пример #5
0
__author__ = 'sn355_000'
from bill2.nas import Nas, NasComError
from util.RosAPI import Core, MikrotikConnectError
from threading import Lock
from util.helpers import getLogger

sysLog = getLogger(__name__)


class MikroNas(Nas):
    def __init__(self, hosts=None, address='', login='', passwd=''):
        super(MikroNas, self).__init__(hosts)
        self.__addr = address
        self.__login = login
        self.__pass = passwd
        self.__hw_lock = Lock()
        self.__hw_nas = None
        self.__hosts_state = None

    @property
    def _connect(self):
        if not self._nas_connected:
            sysLog.debug('Trying connect to nas')
            with self.__hw_lock:
                try:
                    self.__hw_nas = Core(self.__addr)
                    if self.__hw_nas.ok:
                        self.__hw_nas.login(self.__login, self.__pass)
                        if self.__hw_nas.ok:
                            self.__hosts_state = self.__get_hw_nas_state
                            self._nas_connected = True
Пример #6
0
# coding=utf-8
import re

__author__ = 'sn'

from threading import Lock
from MySQLdb.cursors import Cursor
from MySQLdb.connections import Connection
import datetime

from user import Users, User
from util import net
from util.helpers import getLogger

logSys = getLogger(__name__)


class Host(object):
    def __init__(self, host_ip, lprefix):
        # check ip and mask before create host
        m1 = 32 - lprefix
        ip = host_ip >> m1
        ip <<= m1
        assert ip == host_ip
        #
        self.__ip = host_ip
        self.__lprefix = lprefix
        self.__pool_id = 0
        self.__user = None
        self.__version = 0
        self.__count_in = 0
Пример #7
0
__author__ = 'sn'
from util.helpers import getLogger
from datetime import datetime
import json
import re

logSys = getLogger(__name__)


class TP:
    def __init__(self, tp_core, param):
        """
        :param tp_core:
         :type tp_core : TPCore
        """
        self.__core = tp_core
        self.__param = dict()
        try:
            self.__param = json.loads(param)
        except (ValueError, TypeError):
            pass

    ####################################################

    def fget(self):
        return ('User traf plan - core:',) + self.__core.fget() + ('User traf plan - parameters:',) + \
            tuple('\t%s -> %s' % i for i in self.__param.items()) +\
               ('Current flags: %s' % str(self.get_user_state_for_nas()),)

    ####################################################
Пример #8
0
#!/usr/bin/python

__author__ = 'sn'
import getopt, sys, os
from time import sleep

from bill2.server import Server
from bill2.version import version
from util.helpers import getLogger

# Gets the instance of the logger.
logSys = getLogger("bill2")


class Bill2Server:
    def __init__(self):
        self.__server = None
        self.__argv = None
        self.__conf = dict()
        self.__conf["background"] = True
        self.__conf["socket"] = "/var/run/bill2.sock"
        self.__conf["pidfile"] = "/var/run/bill2.pid"

    def dispVersion(self):
        print "Bill2 v %s" % version
        print "Many contributions by Cyril Jaquier <*****@*****.**>."

    def disp_usage(self):
        print "Usage: " + self.__argv[0] + " [OPTIONS]"
        print
        print "Options:"