[kea-dev] Is there still any interest for a hook embedding python?

Dave Cole davejohncole at gmail.com
Sat Jan 4 01:55:30 UTC 2020


Sorry about the broken formatting.  I forgot mailman does not handle
mime text properly.  Here is (I hope) the example code in slightly
more readable format:

- - %< - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - -
from kea import *
from ipaddress import IPv4Address, IPv4Network
from prometheus_client import start_http_server, Counter

PKT_RECEIVE = Counter('dhcp4_pkt_receive_total', 'Packets received', ['type'])
PKT_SEND = Counter('dhcp4_pkt_send_total', 'Packets sent', ['type'])

class Config:
    def __init__(self, conf):
        dhcp4 = conf['Dhcp4']
        self.options =
[Option(DHO_DHCP_LEASE_TIME).setUint32(dhcp4.get('valid-lifetime',
7200)),

Option(DHO_DHCP_RENEWAL_TIME).setUint32(dhcp4.get('renew-timer',
1800)),

Option(DHO_DHCP_REBINDING_TIME).setUint32(dhcp4.get('rebind-timer',
3600))]
        self.subnet = IPv4Network('10.0.0.0/8')
        self.options.append(Option(DHO_SUBNET_MASK).setBytes(self.subnet.netmask.packed))
        # snip extra code

def load(handle):
    global config, type_to_label
    config = Config(CfgMgr().getStagingCfg().toElement())
    type_to_label = dict([(v, k[4:].lower())
                          for k, v in globals().items()
                          if k.startswith('DHCP')])
    start_http_server(9100)
    return 0

def pkt4_receive(handle):
    query = handle.getArgument('query4')
    PKT_RECEIVE.labels(type=type_to_label.get(query.getType(), 'unknown')).inc()
    # client must request address in Option 82, suboption 1.
    o = query.getOption(DHO_DHCP_AGENT_OPTIONS)
    if not o:
        raise RuntimeError('client must send option %s' %
DHO_DHCP_AGENT_OPTIONS)
    o = o.getOption(1)
    if not o:
        raise RuntimeError('missing suboption 1 in option %s' %
DHO_DHCP_AGENT_OPTIONS)
    handle.setContext('requested-addr', o.getString())
    return 0
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - -


More information about the kea-dev mailing list