aboutsummaryrefslogtreecommitdiffstats
path: root/hardware-model-summary
blob: 383ac2959cd8dba5364cbbe2dca5cb59fbe4284a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/perl

use strict;
use warnings;

use SiteSummary;
use Getopt::Std;

my %vendors;
my %models;
my %hostmap;
my %modelmap;
my %opts;

sub usage {
    my $retval = shift;
    print <<EOF;
Usage: $0 [-l]
   -l   list hosts with the given vendor/model
EOF
    exit $retval;
}

getopt("l", \%opts) || usage(1);

for_all_hosts(\&handle_host);

print_summary();

sub handle_host {
    my $hostid = shift;
    #print "$hostid\n";
    my ($vendor, $model, undef) = get_hardware_info($hostid);
    $vendor = "[no-dmi-info]" unless defined $vendor;
    $vendors{$vendor}++;
    if ($model) {
        $models{$vendor}{$model}++;
        if (exists $modelmap{$vendor} && exists $modelmap{$vendor}{$model}) {
            push @{$modelmap{$vendor}{$model}}, $hostid ;
        } else {
            $modelmap{$vendor}{$model} = [$hostid];
        }
    } else {
        if (exists $hostmap{$vendor}) {
            push @{$hostmap{$vendor}}, $hostid ;
        } else {
            $hostmap{$vendor} = [$hostid];
        }
    }
}

sub print_summary {
    printf("  %-25s %5s\n", "vendor", "count");
    for my $vendor (sort keys %vendors) {
        printf("  %-25s %5d\n", $vendor, $vendors{$vendor});
        if (exists $opts{l}) {
            if (exists $hostmap{$vendor}) {
                for my $hostid (sort @{$hostmap{$vendor}}) {
                    my $hostname = get_hostname($hostid);
                    my ($vendor, $model, undef) = get_hardware_info($hostid);
                    $vendor = "[unknown]" unless defined $vendor;
                    $model = "" unless defined $model;
                    printf "    %s %s/%s %s\n", $hostname, $vendor, $model, $hostid;
                }
            }
        }
        for my $model (sort keys %{$models{$vendor}}) {
            printf("    %-23s %5d\n", $model, $models{$vendor}{$model});
            if (exists $opts{l}) {
                for my $hostid (sort @{$modelmap{$vendor}{$model}}) {
                    my $hostname = get_hostname($hostid);
                    my ($vendor, $model, undef) = get_hardware_info($hostid);
                    printf "      %s %s/%s %s\n", $hostname, $vendor, $model, $hostid;
                }
            }
        }
    }
}
_prefix_v4 = ObjectVar( description="IPv4 Prefix to assign a management IP Address from", model=Prefix, query_params={ 'family': 4, 'vlan_id': '$mgmt_vlan' } ) mgmt_prefix_v6 = ObjectVar( description="IPv6 Prefix to assign a management IP Address from", model=Prefix, query_params={ 'family': 6, 'vlan_id': '$mgmt_vlan' } ) tags = MultiObjectVar( description="Tags to be sent to Gondul. These are used for templating, so be sure what they do.", model=Tag, ) def run(self, data, commit): mgmt_vlan = data['mgmt_vlan'] # Create the new switch switch = Device( name=data['switch_name'], device_type=data['device_type'], device_role=data['role'], site=data['site'], ) switch.save() self.log_success(f"Created new switch: <a href=\"{switch.get_absolute_url()}\">{switch}</a>") vlan_group = data['vlan_group'] vid = vlan_group.get_next_available_vid() # use provided vid if specified. if data['vlan_id']: vid = data['vlan_id'] vlan = VLAN.objects.create( name=switch.name, group=vlan_group, vid=vid ) vlan.save() mgmt_vlan_interface = Interface.objects.create( device=switch, name=f"vlan.{mgmt_vlan.vid}", type=InterfaceTypeChoices.TYPE_VIRTUAL, mode=InterfaceModeChoices.MODE_TAGGED, ) mgmt_vlan_interface.tagged_vlans.add(mgmt_vlan.id) uplink_ae = Interface.objects.create( device=switch, name="ae0", description=data['destination_device'].name, type=InterfaceTypeChoices.TYPE_LAG, mode=InterfaceModeChoices.MODE_TAGGED, ) uplink_ae.tagged_vlans.add(mgmt_vlan.id) uplink_vlan = Interface.objects.create( device=switch, name="ae0.0", description=data['destination_device'].name, type=InterfaceTypeChoices.TYPE_VIRTUAL, parent=uplink_ae, ) destination_ae = Interface.objects.create( device=data['destination_device'], name=f"ae{vlan.vid}", description=switch.name, type=InterfaceTypeChoices.TYPE_LAG, mode=InterfaceModeChoices.MODE_TAGGED, ) destination_ae.tagged_vlans.add(mgmt_vlan.id) destination_vlan = Interface.objects.create( device=data['destination_device'], name=f"vlan.{vid}", description=switch.name, type=InterfaceTypeChoices.TYPE_VIRTUAL, parent=destination_ae, ) self.log_success("Created AE and VLAN interfaces for both ends") v4_mgmt_addr = IPAddress.objects.create( address=data['mgmt_prefix_v4'].get_first_available_ip(), ) v6_mgmt_addr = IPAddress.objects.create( address=data['mgmt_prefix_v6'].get_first_available_ip(), ) mgmt_vlan_interface.ip_addresses.add(v4_mgmt_addr) mgmt_vlan_interface.ip_addresses.add(v6_mgmt_addr) switch.primary_ip4 = v4_mgmt_addr switch.primary_ip6 = v6_mgmt_addr switch.save() num_uplinks = len(data['destination_interfaces']) interfaces = list(Interface.objects.filter(device=switch).exclude(type=InterfaceTypeChoices.TYPE_VIRTUAL).exclude(type=InterfaceTypeChoices.TYPE_LAG)) interface_type = ContentType.objects.get_for_model(Interface) for uplink_num in range(0, num_uplinks): # mark last ports as uplinks a_interface = data['destination_interfaces'][::-1][uplink_num] b_interface = interfaces[(uplink_num * -1) -1] # Configure uplink as AE0 b_interface.lag = uplink_ae b_interface.save() # Configure downlink on destination a_interface.lag = destination_ae a_interface.save() cable = Cable.objects.create() a = CableTermination.objects.create( cable=cable, cable_end='A', termination_id=a_interface.id, termination_type=interface_type, ) b = CableTermination.objects.create( cable_end='B', cable=cable, termination_id=b_interface.id, termination_type=interface_type, ) cable = Cable.objects.get(id=cable.id) # https://github.com/netbox-community/netbox/discussions/10199 cable._terminations_modified = True cable.save() self.log_success(f"Cabled {data['destination_device']} {a_interface} to {switch} {b_interface}")