Exemplo n.º 1
0
def update_config(cfg):
    try:
        config.smtp_host, config.smtp_port = parse_host_port(
            cfg.get('mailer', 'smtp'), default_port)
    except:
        config.smtp_host = 'localhost'
        config.smtp_port = default_port
    logging.debug('mailer.smtp = %s:%u' % (config.smtp_host, config.smtp_port))
    try:
        config.sender = cfg.get('mailer', 'from')
    except:
        config.sender = 'eva@' + platform.node()
    logging.debug('mailer.from = %s' % config.sender)
    try:
        config.default_rcp = list(
            filter(None, [
                x.strip() for x in cfg.get('mailer', 'default_rcp').split(',')
            ]))
    except:
        config.default_rcp = ['root']
    logging.debug('mailer.default_rcp = %s' % ', '.join(config.default_rcp))
    return True
Exemplo n.º 2
0
 def __init__(self, **kwargs):
     host, port = parse_host_port(self.phi_cfg.get('host'), 161)
     community = self.phi_cfg.get('community')
     if not community:
         community = 'public'
     if not host:
         self.log_error('Host not specified')
         self.ready = False
         return
     try:
         retries = int(self.phi_get('retries'))
     except:
         retries = 0
     self.t_data = {}
     self.t_lock = threading.Lock()
     self.snmp_get = partial(snmp.get,
                             host=host,
                             port=port,
                             community=community,
                             retries=retries)
     self.host = host
     self.port = port
     self.community = community
     self.retries = retries
Exemplo n.º 3
0
 def __init__(self, **kwargs):
     self.snmp_host, self.snmp_port = parse_host_port(
         self.phi_cfg.get('host'), 161)
     self.sensor_port = self.phi_cfg.get('sp')
     if self.sensor_port and not isinstance(self.sensor_port, list):
         self.sensor_port = [self.sensor_port]
     self.sensor_port = self.sensor_port.copy()
     try:
         for i in range(len(self.sensor_port)):
             self.sensor_port[i] = int(self.sensor_port[i]) - 1
             if self.sensor_port[i] < 0:
                 raise Exception('Sensor port can not be less than 1')
             self.sensor_port[i] = str(self.sensor_port[i])
     except:
         self.sensor_port = None
     if not self.snmp_host:
         self.log_error('no host specified')
         self.ready = False
     if not self.sensor_port:
         self.log_error('no sensor port specified')
         self.ready = False
     else:
         if len(self.sensor_port) > 1:
             self.__lpi_default = 'sensor'
Exemplo n.º 4
0
 def create_notifier(self, params):
     n = self.get_notifier(params['i'], pass_errors=True)
     if n:
         self.print_err('notifier %s already exists' % params['i'])
         return self.local_func_result_failed
     notifier_id = params['i']
     p = params['p'].split(':')
     space = params.get('s')
     timeout = params.get('t')
     if len(p) < 2: return self.local_func_result_failed
     if p[0] == 'json':
         u = (':'.join(p[1:])).split('/')
         if len(u) < 3: return self.local_func_result_failed
         if u[2].find('@') != -1:
             try:
                 notify_key, u[2] = u[2].split('@')
             except:
                 return self.local_func_result_failed
         else:
             notify_key = None
         uri = '/'.join(u)
         if uri.find('#') != -1:
             uri, method = uri.split('#', 1)
         else:
             method = None
         n = eva.notify.HTTP_JSONNotifier(notifier_id=notifier_id,
                                          uri=uri,
                                          method=method,
                                          notify_key=notify_key,
                                          space=space,
                                          timeout=timeout)
     elif p[0] == 'influxdb':
         u = (':'.join(p[1:])).split('/')
         if len(u) < 3: return self.local_func_result_failed
         uri = '/'.join(u)
         if uri.find('#') != -1:
             uri, db = uri.split('#', 1)
         else:
             self.print_err('database not specified')
             return self.local_func_result_failed
         n = eva.notify.InfluxDB_Notifier(notifier_id=notifier_id,
                                          uri=uri,
                                          db=db,
                                          space=space,
                                          timeout=timeout)
     elif p[0] == 'mqtt':
         _p = ':'.join(p[1:])
         if _p.find('@') != -1:
             auth = _p.split('@')[0]
             host = _p.split('@')[1]
             username = auth.split(':')[0]
             try:
                 password = auth.split(':')[1]
             except:
                 password = None
         else:
             username = None
             password = None
             host = _p
         from eva.tools import parse_host_port
         host, port = parse_host_port(host)
         n = eva.notify.MQTTNotifier(notifier_id=notifier_id,
                                     host=host,
                                     port=port,
                                     username=username,
                                     password=password,
                                     space=space,
                                     timeout=timeout)
     elif p[0] == 'db':
         db_uri = ':'.join(p[1:])
         n = eva.notify.SQLANotifier(notifier_id=notifier_id,
                                     db_uri=db_uri,
                                     keep=None,
                                     space=space)
     elif p[0] == 'gcpiot':
         try:
             project, region, registry = ':'.join(p[1:]).split('/')
         except:
             return self.local_func_result_failed
         n = eva.notify.GCP_IoT(notifier_id=notifier_id,
                                project=project,
                                region=region,
                                registry=registry,
                                timeout=timeout)
     elif p[0] == 'prometheus':
         n = eva.notify.PrometheusNotifier(notifier_id=notifier_id)
     else:
         self.print_err('notifier type unknown %s' % p[0])
         return self.local_func_result_failed
     n.enabled = True if params.get('y') else False
     eva.notify.append_notifier(n)
     eva.notify.save_notifier(notifier_id)
     return self.local_func_result_ok
Exemplo n.º 5
0
 def set(self, port=None, data=None, cfg=None, timeout=0):
     if cfg:
         host, snmp_port = parse_host_port(cfg.get('host'), 161)
         community = cfg.get('community')
         if not community: community = cfg.get('write_community')
         tries = cfg.get('retries')
         try:
             tries = int(tries)
         except:
             tries = None
     else:
         host = None
         community = None
         tries = None
     if not host:
         host = self.snmp_host
         snmp_port = self.snmp_port
     if not community:
         community = self.snmp_write_community
     if tries is None: tries = self.snmp_tries
     if not host or not community: return False
     _timeout = timeout / self.snmp_tries
     if isinstance(port, list) and netsnmp:
         try:
             sess = netsnmp.Session(
                 Version=2,
                 DestHost=host,
                 RemotePort=snmp_port,
                 Community=community,
                 Timeout=int(_timeout * 1000000),
                 Retries=self.snmp_tries - 1)
             vts = ()
             for p, v in zip(port, data):
                 try:
                     port = int(p)
                     val = int(v)
                     if port < 1 or \
                             port > self.port_max or val < 0 or val > 1:
                         raise Exception('port out of range')
                 except Exception as e:
                     self.log_error(e)
                     log_traceback()
                     return False
                 vts += (netsnmp.Varbind(
                     '%s.%u' % (self.oid_work, port + self.port_shift), '',
                     str(v).encode(), 'INTEGER'),)
             return True if sess.set(netsnmp.VarList(*vts)) else False
         except:
             log_traceback()
             return False
     else:
         try:
             port = int(port)
             val = int(data)
         except:
             return False
         if port < 1 or port > self.port_max or val < 0 or val > 1:
             return False
         return snmp.set('%s.%u' % (self.oid_work, port + self.port_shift),
                         rfc1902.Integer(val), host, snmp_port, community,
                         _timeout, tries - 1)
Exemplo n.º 6
0
 def get(self, port=None, cfg=None, timeout=0):
     if cfg:
         host, snmp_port = parse_host_port(cfg.get('host'), 161)
         community = cfg.get('community')
         if not community: community = cfg.get('read_community')
         tries = cfg.get('retries')
         try:
             tries = int(tries) + 1
         except:
             tries = None
     else:
         host = None
         community = None
         tries = None
     if not host:
         host = self.snmp_host
         snmp_port = self.snmp_port
     if not community:
         community = self.snmp_read_community
     if tries is None: tries = self.snmp_tries
     if not host or not community: return None
     _timeout = timeout / tries
     if not port and netsnmp:
         try:
             sess = netsnmp.Session(
                 Version=2,
                 DestHost=host,
                 RemotePort=snmp_port,
                 Community=community,
                 Timeout=int(_timeout * 1000000),
                 Retries=self.snmp_tries - 1)
             oid = netsnmp.VarList(
                 '%s.%u' % (self.oid_work, self.port_shift + 1))
             sess.getbulk(0, self.port_max, oid)
             result = {}
             for i, v in enumerate(oid):
                 result[str(i + 1)] = v.val.decode()
             return result
         except Exception as e:
             self.log_error(e)
             log_traceback()
             return None
     else:
         try:
             port = int(port)
         except:
             return None
         if port < 1 or port > self.port_max: return None
         if netsnmp:
             try:
                 sess = netsnmp.Session(
                     Version=2,
                     DestHost=host,
                     RemotePort=snmp_port,
                     Community=community,
                     Timeout=int(_timeout * 1000000),
                     Retries=self.snmp_tries - 1)
                 oid = netsnmp.VarList(
                     '%s.%u' % (self.oid_work, port + self.port_shift))
                 return sess.get(oid)[0].decode()
             except Exception as e:
                 self.log_error(e)
                 log_traceback()
                 return None
         else:
             return snmp.get(
                 '%s.%u' % (self.oid_work, port + self.port_shift),
                 host,
                 snmp_port,
                 community,
                 _timeout,
                 tries - 1,
                 rf=int)
Exemplo n.º 7
0
 def get(self, port=None, cfg=None, timeout=0):
     if cfg:
         host, snmp_port = parse_host_port(cfg.get('host'), 161)
         community = cfg.get('community')
         tries = cfg.get('retries')
         try:
             tries = int(tries) + 1
         except:
             tries = None
     else:
         host = None
         community = None
         tries = None
     if not host:
         host = self.snmp_host
         snmp_port = self.snmp_port
     if not community:
         community = self.community
     if tries is None: tries = self.snmp_tries
     if not host or not community: return None
     _timeout = timeout / tries
     port = str(port)
     if port.startswith('a'):
         oid = self.oid_ain
         port_max = 8
         port = port[1:]
         ret = 1
     elif port.startswith('t'):
         oid = self.oid_temp
         port_max = 8
         port = port[1:]
         ret = 2
     else:
         oid = self.oid_din
         port_max = 16
         ret = 0
     try:
         port = int(port)
     except:
         return None
     if port < 1 or port > port_max: return None
     if netsnmp:
         try:
             sess = netsnmp.Session(Version=2,
                                    DestHost=host,
                                    RemotePort=snmp_port,
                                    Community=community,
                                    Timeout=int(_timeout * 1000000),
                                    Retries=self.snmp_tries - 1)
             o = netsnmp.VarList('%s.%u' % (oid, port - 1))
             result = sess.get(o)[0].decode()
         except Exception as e:
             self.log_error(e)
             log_traceback()
             return None
     else:
         result = snmp.get('%s.%u' % (oid, port - 1),
                           host,
                           snmp_port,
                           community,
                           _timeout,
                           tries - 1,
                           rf=int)
     if ret == 0:
         return result
     elif ret == 1:
         return int(result) / 100
     elif ret == 2:
         return None if result == '---' else result
Exemplo n.º 8
0
 def __init__(self, **kwargs):
     self.port_max = 8
     self.relay_host, self.relay_port = parse_host_port(
         self.phi_cfg.get('host'), 6722)