Example #1
0
 def create(self):
     cur_nd = netdevice(
         device=NDStruct.device,
         devname=self.dev_name,
         netdevice_speed=NDStruct.default_nds,
         routing=False,
         penalty=1,
         dhcp_device=False,
         is_bridge=True if self.br_dict else False,
     )
     cur_nd.save()
     self.nd = cur_nd
     self.log("created netdevice")
     if self.br_dict:
         self.dev_name, self.br_dict.get("interfaces", [])
     if "ether" in self.in_dict.get("links", {}):
         _ether = self.in_dict["links"]["ether"]
         _mac = _ether[0].split()[0]
         cur_nd.macaddr = _mac
         cur_nd.save()
         self.log("set macaddr to '{}'".format(cur_nd.macaddr))
     for _inet in self.in_dict.get("inet", []):
         cur_ip_nw = _inet.split()[0]
         cur_ip = cur_ip_nw.split("/")[0]
         new_ip = net_ip(
             netdevice=cur_nd,
             ip=cur_ip,
             domain_tree_node=self.device.domain_tree_node,
         )
         new_ip.save()
         self.log("added IP {} (network {})".format(new_ip.ip,
                                                    str(new_ip.network)))
Example #2
0
def create_device(args, name, group, domain_tree_node, netdevices, bootserver):
    print("Creating device {} with {} netdevices".format(name, len(netdevices)))
    dev = device(
        name=name,
        device_group=group,
        domain_tree_node=domain_tree_node,
        bootserver=bootserver,
        dhcp_write=(bootserver is not None)
    )
    if args.write_to_db:
        dev.save()
    nds = []
    for nd_data in netdevices:
        nd_inst = netdevice(
            device=dev,
            macaddr=nd_data.mac,
            devname=nd_data.name,
            netdevice_speed=nd_data.speed,
            snmp_network_type=nd_data.snmp_network_type,
        )
        nds.append(nd_inst)
        if args.write_to_db:
            nd_inst.save()
        for nd_ip_data in nd_data.iplist:
            nd_ip_inst = net_ip(
                ip=nd_ip_data.ip,
                network=nd_ip_data.network,
                domain_tree_node=nd_ip_data.domain_tree_node,
                netdevice=nd_inst,
            )
            if args.write_to_db:
                nd_ip_inst.save()
    return dev, nds
Example #3
0
    def update(self, dev, scheme, result_dict, oid_list, flags):
        # ip dict
        _ip_dict = {}
        # import pprint
        # pprint.pprint(result_dict)
        # pprint.pprint(simplify_dict(result_dict["1.3.6.1.2.1.4.22"], (1,)))
        if IP_NET_TO_MEDIA_TABLE in result_dict:
            for key, struct in simplify_dict(
                    result_dict[IP_NET_TO_MEDIA_TABLE], (1, )).iteritems():
                # check for static entries
                if 4 in struct and struct[4] == 4:
                    # build snmp_ip struct
                    _ip = ipvx_tools.ipv4(".".join(
                        ["{:d}".format(_entry) for _entry in key[1:]]))
                    _networks = _ip.find_matching_network(
                        network.objects.all())
                    if _networks:
                        self.log("found {} for {}: {}".format(
                            logging_tools.get_plural("matching network",
                                                     len(_networks)),
                            unicode(_ip),
                            ", ".join([unicode(_net) for _net in _networks]),
                        ))
                        _nw = _networks[0]
                        _dict = {
                            2:
                            key[0],
                            1:
                            struct[3],
                            3:
                            "".join([
                                chr(int(_value))
                                for _value in _nw[1].netmask.split(".")
                            ]),
                        }
                        try:
                            _ip = ifSNMPIP(_dict)
                        except:
                            self.log(
                                "error interpreting {} as IP: {}".format(
                                    str(struct),
                                    process_tools.get_except_info()),
                                logging_tools.LOG_LEVEL_ERROR,
                            )
                        else:
                            _ip_dict[key[0]] = _ip
                    else:
                        self.log(
                            "found no matching network for IP {}".format(
                                unicode(_ip)), logging_tools.LOG_LEVEL_ERROR)
        else:
            self.log(
                "table {} not found in result".format(IP_NET_TO_MEDIA_TABLE),
                logging_tools.LOG_LEVEL_ERROR)
        if IP_ADDR_TABLE in result_dict:
            for key, value in simplify_dict(result_dict[IP_ADDR_TABLE],
                                            (1, )).iteritems():
                try:
                    _ip = ifSNMPIP(value)
                except:
                    self.log(
                        "error interpreting {} as IP: {}".format(
                            str(value), process_tools.get_except_info()),
                        logging_tools.LOG_LEVEL_ERROR,
                    )
                else:
                    _ip_dict[key] = _ip
        else:
            self.log("table {} not found in result".format(IP_ADDR_TABLE, ),
                     logging_tools.LOG_LEVEL_ERROR)
        if any([
                unicode(_value.address_ipv4) == "0.0.0.0"
                for _value in _ip_dict.itervalues()
        ]):
            self.log("ignoring zero IP address", logging_tools.LOG_LEVEL_WARN)
            _ip_dict = {
                key: value
                for key, value in _ip_dict.iteritems()
                if unicode(value.address_ipv4) != "0.0.0.0"
            }
        if dev.domain_tree_node_id:
            _tln = dev.domain_tree_node
        else:
            _tln = domain_tree_node.objects.get(Q(depth=0))
        if_lut = {
            _dev_nd.snmp_idx: _dev_nd
            for _dev_nd in netdevice.objects.filter(
                Q(snmp_idx__gt=0) & Q(device=dev))
        }
        # handle IPs
        _found_ip_ids = set()
        _added = 0
        for ip_struct in _ip_dict.itervalues():
            if ip_struct.if_idx in if_lut:
                _dev_nd = if_lut[ip_struct.if_idx]
                # check for network
                _network_addr = ip_struct.address_ipv4 & ip_struct.netmask_ipv4

                cur_nw = network.objects.get_or_create_network(
                    network_addr=_network_addr,
                    netmask=ip_struct.netmask_ipv4,
                    context="SNMP",
                )
                # check for existing IP
                try:
                    _ip = net_ip.objects.get(
                        Q(netdevice__device=dev) & Q(ip=ip_struct.address))
                except net_ip.DoesNotExist:
                    _added += 1
                    _ip = net_ip(ip=ip_struct.address, )
                _ip.domain_tree_node = _tln
                _ip.network = cur_nw
                _ip.netdevice = _dev_nd
                _ip.save()
                _found_ip_ids.add(_ip.idx)
        if flags["strict"]:
            stale_ips = net_ip.objects.exclude(Q(pk__in=_found_ip_ids)).filter(
                Q(netdevice__device=dev))
            if stale_ips.count():
                stale_ips.delete()
        if _added:
            return ResultNode(ok="updated IPs (added: {:d})".format(_added))
        else:
            return ResultNode()
Example #4
0
    def post(self, request):
        _post = request.POST
        # domain name tree
        dnt = domain_name_tree()
        device_data = json.loads(_post["device_data"])
        try:
            cur_dg = device_group.objects.get(
                Q(name=device_data["device_group"]))
        except device_group.DoesNotExist:
            try:
                cur_dg = device_group.objects.create(
                    name=device_data["device_group"],
                    domain_tree_node=dnt.get_domain_tree_node(""),
                    description="auto created device group {}".format(
                        device_data["device_group"]),
                )
            except:
                request.xml_response.error(
                    u"cannot create new device group: {}".format(
                        process_tools.get_except_info()),
                    logger=logger)
                cur_dg = None
            else:
                request.xml_response.info(
                    u"created new device group '{}'".format(unicode(cur_dg)),
                    logger=logger)
        else:
            if cur_dg.cluster_device_group:
                request.xml_response.error(
                    u"no devices allowed in system (cluster) group",
                    logger=logger)
                cur_dg = None
        _create_ok = True
        if cur_dg is not None:
            if device_data["full_name"].count("."):
                short_name, domain_name = device_data["full_name"].split(
                    ".", 1)
                dnt_node = dnt.add_domain(domain_name)
            else:
                short_name = device_data["full_name"]
                # top level node
                dnt_node = dnt.get_domain_tree_node("")
            try:
                cur_dev = device.objects.get(
                    Q(name=short_name) & Q(domain_tree_node=dnt_node))
            except device.DoesNotExist:
                # check image
                if device_data["icon_name"].strip():
                    try:
                        cur_img = mon_ext_host.objects.get(
                            Q(name=device_data["icon_name"]))
                    except mon_ext_host.DoesNotExist:
                        cur_img = None
                    else:
                        pass
                try:
                    cur_dev = device.objects.create(
                        device_group=cur_dg,
                        is_meta_device=False,
                        domain_tree_node=dnt_node,
                        name=short_name,
                        mon_resolve_name=device_data["resolve_via_ip"],
                        comment=device_data["comment"],
                        mon_ext_host=cur_img,
                    )
                except:
                    request.xml_response.error(
                        u"cannot create new device: {}".format(
                            process_tools.get_except_info()),
                        logger=logger)
                    cur_dev = None
                else:
                    request.xml_response["device_pk"] = cur_dev.idx
            else:
                request.xml_response.warn(u"device {} already exists".format(
                    unicode(cur_dev)),
                                          logger=logger)
                cur_dev = None

            if cur_dev is not None:
                try:
                    cur_nd = netdevice.objects.get(
                        Q(device=cur_dev) & Q(devname='eth0'))
                except netdevice.DoesNotExist:
                    try:
                        cur_nd = netdevice.objects.create(
                            devname="eth0",
                            device=cur_dev,
                            routing=device_data["routing_capable"],
                        )
                        if device_data["peer"]:
                            peer_information.objects.create(
                                s_netdevice=cur_nd,
                                d_netdevice=netdevice.objects.get(
                                    Q(pk=device_data["peer"])),
                                penalty=1,
                            )
                    except:
                        request.xml_response.error("cannot create netdevice")
                        _create_ok = False
                try:
                    net_ip.objects.get(
                        Q(netdevice=cur_nd) & Q(ip=device_data["ip"]))
                except net_ip.DoesNotExist:
                    cur_ip = net_ip(
                        netdevice=cur_nd,
                        ip=device_data["ip"],
                        domain_tree_node=dnt_node,
                    )
                    try:
                        cur_ip.save()
                    except:
                        request.xml_response.error(
                            u"cannot create IP: {}".format(
                                process_tools.get_except_info()),
                            logger=logger)
                        _create_ok = False
        if cur_dev is not None:
            if _create_ok:
                request.xml_response.info(u"created new device '{}'".format(
                    unicode(cur_dev)),
                                          logger=logger)
            else:
                # creation not ok, deleting device
                cur_dev.delete()
Example #5
0
    def check_ext_com(self):
        if all(ext_com.finished() is not None
               for ext_com in self._ext_coms.itervalues()):

            outputs = {
                ext_com_key: ext_com.communicate()
                for ext_com_key, ext_com in self._ext_coms.iteritems()
            }

            any_err = False
            for ext_com_key, ext_com in self._ext_coms.iteritems():
                if ext_com.result != 0:
                    any_err = True
                    self.log("Error querying {}, output:".format(ext_com_key),
                             logging_tools.LOG_LEVEL_ERROR)
                    self.log("Stdout: {}".format(outputs[ext_com_key][0]),
                             logging_tools.LOG_LEVEL_ERROR)
                    self.log("Stderr: {}".format(outputs[ext_com_key][1]),
                             logging_tools.LOG_LEVEL_ERROR)

                if outputs[ext_com_key][1]:
                    self.log(
                        "Query for {} wrote to stderr: {}".format(
                            ext_com_key, outputs[ext_com_key][1]),
                        logging_tools.LOG_LEVEL_WARN)

            if not any_err:
                network_adapter_data = WmiUtils.parse_wmic_output(
                    outputs[self.NETWORK_ADAPTER_MODEL][0])
                network_adapter_configuration_data = WmiUtils.parse_wmic_output(
                    outputs[self.NETWORK_ADAPTER_CONFIGURATION_MODEL][0])

                ND_SPEED_LUT = netdevice_speed.build_lut()
                updated_nds, created_nds, created_ips, existing_ips = [], [], [], []

                # iterate by adapter since only adapters are filtered
                for adapter in network_adapter_data:
                    adapter_index = int(adapter['Index'])
                    adapter_name = adapter['Name']
                    # corresponding adapter and adapter_configuration have same index according to some sources
                    # http://blogs.technet.com/b/heyscriptingguy/archive/2011/10/07/use-powershell-to-identify-your-real-network-adapter.aspx
                    # http://blogs.technet.com/b/heyscriptingguy/archive/2005/06/14/how-can-i-associate-a-network-connection-with-an-ip-address.aspx
                    adapter_configuration = next(
                        c for c in network_adapter_configuration_data
                        if int(c['Index']) == adapter_index)

                    device_netdevices = netdevice.objects.filter(
                        device=self.device)

                    # find existing dev by idx or else by name
                    present_nds = [
                        nd for nd in device_netdevices
                        if nd.wmi_interface_index == adapter_index
                    ]
                    if not present_nds:
                        present_nds = [
                            nd for nd in device_netdevices
                            if nd.devname == adapter_name
                        ]

                    if len(present_nds) > 1:
                        self.log(
                            "Error: Found multiple netdevices matching specification:"
                            + "Index: {}; Name: {}; Net devices: {}".format(
                                adapter['Index'], adapter['Name'], present_nds)
                        )
                    else:
                        if present_nds:  # only one
                            nd = present_nds[0]
                            updated_nds.append(nd)
                        else:
                            nd = netdevice(
                                device=self.device,
                                wmi_interface_index=adapter_index,
                                force_network_device_type_match=False,
                            )
                            created_nds.append(nd)

                        nd.devname = adapter_name
                        nd.macaddr = adapter[
                            'MACAddress'] or ""  # must not be None
                        nd.mtu = adapter_configuration['MTU']
                        nd.speed = int(adapter['Speed'])
                        nd.netdevice_speed = ND_SPEED_LUT.get(
                            int(adapter['Speed']), ND_SPEED_LUT.get(0))
                        nd.save()

                        for ip_found in WmiUtils.WmiList.handle(
                                adapter_configuration['IPAddress']):
                            try:
                                ip_found_struct = ipvx_tools.ipv4(ip_found)
                            except ValueError:
                                self.log(
                                    "Found IP which is not supported: {}".
                                    format(ip_found),
                                    logging_tools.LOG_LEVEL_WARN)
                            else:
                                # find ipv4 subnet
                                netmasks_found = []
                                for _nm in WmiUtils.WmiList.handle(
                                        adapter_configuration["IPSubnet"]):
                                    try:
                                        netmasks_found.append(
                                            ipvx_tools.ipv4(_nm))
                                    except ValueError:
                                        pass

                                if not netmasks_found:
                                    self.log(
                                        "No netmask found among: {}".format(
                                            adapter['IPSubnet']))
                                else:
                                    netmask_found_struct = netmasks_found[0]

                                    _gws = []
                                    for _gw in WmiUtils.WmiList.handle(
                                            adapter_configuration[
                                                "DefaultIPGateway"]):
                                        try:
                                            _gws.append(ipvx_tools.ipv4(_gw))
                                        except ValueError:
                                            pass

                                    gw_found_struct = _gws[0] if _gws else None

                                    cur_nw = network.objects.get_or_create_network(
                                        network_addr=ip_found_struct
                                        & netmask_found_struct,
                                        netmask=netmask_found_struct,
                                        gateway=gw_found_struct,
                                        context="WMI",
                                    )

                                    try:
                                        nip = net_ip.objects.get(netdevice=nd,
                                                                 ip=ip_found)
                                        existing_ips.append(nip)
                                    except net_ip.DoesNotExist:
                                        try:
                                            nip = net_ip(
                                                netdevice=nd,
                                                ip=ip_found,
                                                network=cur_nw,
                                            )
                                            nip.save()
                                            created_ips.append(nip)
                                        except ValidationError as e:
                                            self.log(
                                                "Failed to create ip {} for netdevice {}: {}"
                                                .format(ip_found, nd, e),
                                                logging_tools.LOG_LEVEL_ERROR)
                                            self.log(traceback.format_exc(e))

                self.log("Created {}, updated {}, created {}, found {}".format(
                    logging_tools.get_plural("net device", len(created_ips)),
                    logging_tools.get_plural("net device", len(updated_nds)),
                    logging_tools.get_plural("ip", len(created_ips)),
                    logging_tools.get_plural("existing ip", len(existing_ips)),
                ))

            self.finish()