예제 #1
0
    def __driver__(self):
        Flag = True
        count = 0
        ip_mac = None
        while True:
            while Flag:
                leases = IscDhcpLeases('/var/lib/dhcp/dhcpd.leases')
                get_lease_object = leases.get()

                ip_mac = list(
                    set([(x.ip, x.ethernet) for x in get_lease_object]))
                time.sleep(10)
                count += 1
                if count == 6:
                    Flag = False

            for x in ip_mac:
                self.queue.put(x)

            for x in self.active_thread:
                x.join()

            for x in self.workers:
                make_worker = Thread(target=self.__arp__, name=x)
                make_worker.start()
                self.active_thread.append(make_worker)

            print(bcolors.OKGREEN + str(self.active) + bcolors.ENDC + "\n")
            print(bcolors.OKGREEN + str(self.blacklist) + bcolors.ENDC + "\n")
            self.__print__()
            time.sleep(20 * 60)
예제 #2
0
def lookup(ip):
	leases = IscDhcpLeases('/var/lib/dhcp/dhcpd.leases')
	leaseList = leases.get()
	for l in leaseList:
		if l.ip == ip:
			return l.ethernet
	return None
예제 #3
0
class DhcpdCollector(object):
    def __init__(self, leasefile='/var/lib/dhcp/dhcpd.leases'):
        print(leasefile)
        netbox = 'https://netbox.minserver.dk/ipam/prefixes/?status=1&parent=&family=&q=&vrf=npflan&mask_length=&export'
        data = urllib.request.urlopen(netbox).read()
        reader = csv.reader(io.StringIO(data.decode()),
                            delimiter=',',
                            quotechar='|')
        subnets = []

        for row in reader:
            if row[7].lower() == "Access".lower() or row[7].lower(
            ) == "Wireless".lower() or row[9].lower() == "AP-MGMT".lower():
                if row[9].lower() == 'Wireless Networks'.lower():
                    continue
                # Add networks to array
                subnets.append(IPv4Network(row[0]))
        self.subnets = subnets
        self.leases = IscDhcpLeases(leasefile)
        print("dhcpd_exporter started!")

    def collect(self):
        scope_size = GaugeMetricFamily('size_per_scope',
                                       'Size of scope',
                                       labels=['scope'])
        for network in self.subnets:
            scope_size.add_metric([str(network)], network.num_addresses - 2)
        yield scope_size

        scope_usage = GaugeMetricFamily('usage_per_scope',
                                        'Currently in use leases per scope',
                                        labels=['scope'])
        # TODO: Very bad from a performance perspective, needs optimization
        uses = defaultdict(lambda: 0)
        cur_leases = 0
        total_leases = 0
        for lease in self.leases.get():
            total_leases = total_leases + 1
            if lease.valid and lease.active:
                cur_leases = cur_leases + 1
                parsed = IPv4Address(lease.ip)
                for network in self.subnets:
                    if parsed in network:
                        uses[network] = uses[network] + 1
                        break
        for network, used_ips in uses.items():
            scope_usage.add_metric([str(network)], used_ips)
        yield scope_usage

        yield GaugeMetricFamily('total_current',
                                'Total amount of current valid leases',
                                value=cur_leases)
        yield GaugeMetricFamily('total_leases',
                                'Total amount of leases valid and invalid',
                                value=total_leases)
예제 #4
0
파일: parseLeases.py 프로젝트: raspiclu/K3S
def parse():
    leases = IscDhcpLeases('/var/lib/dhcp/dhcpd.leases')
    allLeases = leases.get()

    for lease in allLeases:
        # print('Lease ist: '+ lease.ip + ', Hostname ist:'  + lease.hostname)
        allIps.append(lease.ip)

    for ip in allIps:
        hostname = 'worker1-' + ip[8:]
        allHostnames.append(hostname)
        print(hostname)
예제 #5
0
파일: parser.py 프로젝트: raspiclu/K3S
def parse():
    all_ips = []
    all_hostnames = []

    leases = IscDhcpLeases('/var/lib/dhcp/dhcpd.leases')
    all_leases = leases.get()

    for lease in all_leases:
        all_ips.append(lease.ip)

    for ip in all_ips:
        hostname = 'worker1-' + ip[8:]
        all_hostnames.append(hostname)

    return all_ips
예제 #6
0
def proxy():
    leases = IscDhcpLeases('/var/lib/dhcp/dhcpd.leases')

    ips = [lease for lease in leases.get()]

    with futures.ThreadPoolExecutor(max_workers=255) as executor:
        futs = [(host, executor.submit(functools.partial(ping, host)))
                for host in ips]

    js = [{
        "ip": ip.ip,
        "mac": ip.ethernet,
        "hostname": ip.hostname
    } for ip, f in futs if f.result()]

    return Response(json.dumps(js), mimetype='application/json')
예제 #7
0
def dhcpLeases(filter='active'):
    leases = IscDhcpLeases('/var/db/dhcpd/dhcpd.leases')
    entries = {}
    rows = []
    now = datetime.utcnow()
    for l in sorted(leases.get(), key=lambda l: inet_aton(l.ip)):
        age = now - l.start
        age = str(age).split('.')[0]
        state = l.binding_state
        if state == 'active' and l.end and l.end < now:
            state = 'expired'
        name = reverse(l.ip)
        vendor = macParser.get_manuf_long(l.ethernet)
        if filters[filter](l):
            leaserow = LeaseRow(active=l.active,
                                age=age,
                                dns=name,
                                end=l.end,
                                ip=l.ip,
                                mac=l.ethernet,
                                name=l.hostname,
                                start=l.start,
                                state=state,
                                valid=l.valid,
                                vendor=vendor,
                                color=color(l.ip))
            entries[leaserow.mac] = leaserow
            rows.append(leaserow)
    for l in arpentries():
        name = reverse(l.ip)
        vendor = macParser.get_manuf_long(l.mac)
        if l.mac not in entries:
            rows.append(
                LeaseRow(active=True,
                         age=0,
                         dns=name,
                         end=None,
                         ip=l.ip,
                         mac=l.mac,
                         name='',
                         start=None,
                         state='arp',
                         valid=True,
                         vendor=vendor,
                         color=color(l.ip)))
    return dict(extended=False,
                leases=sorted(rows, key=lambda l: inet_aton(l.ip)))
예제 #8
0
        subnets.append(IPv4Network(row[0]))


def generate_per_scope():
    used_ips = 0
    for network in subnets:
        for lease in leases.get():
            if lease.valid and lease.active:
                if IPv4Address(lease.ip) in network:
                    used_ips = used_ips + 1
        USAGE_PER_SCOPE.labels(network).set(used_ips)
        SIZE_PER_SCOPE.labels(network).set(network.num_addresses - 2)
        used_ips = 0


TOTAL_LEASES.set_function(lambda: len(leases.get()))
TOTAL_CURRENT.set_function(lambda: len(leases.get_current().keys()))

# Start HTTP server
start_http_server(8000)

while True:
    # Instantiate and parse DHCPD leases file
    leases = IscDhcpLeases(dhcpd_leases)

    TOTAL_LEASES.set_function(lambda: len(leases.get()))
    TOTAL_CURRENT.set_function(lambda: len(leases.get_current()))

    generate_per_scope()

    time.sleep(5)
예제 #9
0
파일: inventory.py 프로젝트: hikinit/mum18
def get_lease():
    leases = IscDhcpLeases('/var/lib/dhcp/dhcpd.leases')
    return leases.get()
예제 #10
0
def main():
    fastd_sockets = (
        ('0', '/run/fastd-ffda-vpn.sock'),
        ('1', '/run/fastd-ffda-vpn1.sock'),
    )

    device_name_mapping = {
        'freifunk': 'ffda-br',
        'bat0': 'ffda-bat',
        'mesh-vpn': 'ffda-vpn'
    }
    device_whitelist = [
        'eth0',
        'tun-ffrl-ber',
        'tun-ffrl-dus',
        'tun-ffrl-fra',
        'tun-ffda-gw01',
        'tun-ffda-gw02',
        'tun-ffda-gw03',
        'tun-ffda-gw04',
        'ffda-vpn',
        'ffda-bat',
        'ffda-br',
        'icvpn',
        'ffda-transport',
        'services',
    ]

    fields = [
        'bytes', 'packets', 'errs', 'drop', 'fifo',
        'frame', 'compressed', 'multicast',
    ]
    field_format = '(?P<{direction}_{field}>\d+)'
    
    pattern = re.compile(
        '^\s*(?P<device_name>[\w-]+):\s+' + '\s+'.join(
            itertools.chain.from_iterable((field_format.format(direction=direction, field=field)
                                           for field in fields) for direction in ['rx', 'tx'])
        )
    )

    update = {}
    with open('/proc/net/dev') as fh:
        lines = fh.readlines()
        for line in lines:
            m = pattern.match(line)
            if m:
                groupdict = m.groupdict()
                device_name = groupdict.pop('device_name')
                device_name = device_name_mapping.get(device_name, device_name)
                if device_name in device_whitelist or device_name.endswith('-vpn') or \
                        device_name.endswith('-bat') or \
                        device_name.endswith('-br') or \
                        device_name.endswith('-transport'):
                    for key, value in groupdict.items():
                        direction, metric = key.split('_')
                        update['%s.%s.%s' % (device_name, direction, metric)] = value

    with open('/proc/loadavg', 'r') as fh:
        line = fh.read()
        values = line.split(' ', 3)
        update['load.15'] = values[0]
        update['load.5'] = values[1]
        update['load.1'] = values[2]

    for key in  ['count', 'max']:
        try:
            with open('/proc/sys/net/netfilter/nf_conntrack_%s' % key, 'r') as fh:
                update['netfilter.%s' % key] = fh.read().strip()
        except IOError as e:
            pass

    with open('/proc/net/snmp6', 'r') as fh:
        for line in fh.readlines():
            key, value = line.split(' ', 1)
            value = value.strip()
            update['ipv6.%s' % key] = value

    with open('/proc/net/snmp', 'r') as fh:
        for heading, values in pairwise(fh.readlines()):
            section, headings = heading.split(':')
            headings = headings.strip().split(' ')
            _, values = values.split(':')
            values = values.strip().split(' ')
            for key, value in zip(headings, values):
                update['ipv4.%s.%s' % (section, key)] = value

    with open('/proc/stat', 'r') as fh:
        for line in fh.readlines():
            key, value = line.split(' ', 1)
            if key == 'ctxt':
                update['context_switches'] = value.strip()
                break

    for name, filename in fastd_sockets:
        if not os.path.exists(filename):
            continue
 
        data = read_from_fastd_socket(filename)
        if len(data) > 0:
            update.update({'fastd.%s.%s' % (name, key): value for (key, value) in data.items()})

    fastd_drops = get_fastd_process_stats()
    if fastd_drops:
        update['fastd.drops'] = fastd_drops

    if IscDhcpLeases:
        try:
            isc_leases = IscDhcpLeases('/var/lib/dhcp/dhcpd.leases')

            update['dhcp_leases'] = len(isc_leases.get())
            update['dhcp_leases_valid'] = len(isc_leases.get_current())
            update['dhcp_leases_active'] = len([1 for lease in isc_leases.get() if lease.valid])

        except Exception:
            pass

    #pprint.pprint(update)
    write_to_graphite(update)