-
Notifications
You must be signed in to change notification settings - Fork 0
/
random-data.pl
executable file
·151 lines (112 loc) · 3.06 KB
/
random-data.pl
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env perl
#
# Perl common chunks
#
use strict ;
# use Data::Dumper;
# $Data::Dumper::Sortkeys = 1 ;
use feature qw(say state switch);
# SSL/LWP checks?
# $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;
#### work out where i am
# these variables can then be used to find config files
# and keep the code portable
use FindBin qw($Bin $Script);
my ($BASE,$NAME)=($Bin,$Script) ;
use lib "lib" ;
use lib "$FindBin::Bin" ;
use lib "$FindBin::Bin/lib" ;
my $DEVCOUNT = 150 ;
my $SWITCHCOUNT = 20 ;
my $MAXLINK = 4 ;
my $FLOWCOUNT = $DEVCOUNT * 2 ;
my @TYPES = qw( http https voip dbs exchange lbs dns dhcp fw vpn );
# pre-generate the switch ID's
my @SIDS = map { sprintf( "s%03d" , $_ ) } ( 1 .. $SWITCHCOUNT );
# pre-generate the host ID's
my @HIDS = map { sprintf( "%04d" , $_ ) } ( 1 .. $DEVCOUNT );
# create the hosts
my @hlines ;
foreach my $hid ( @HIDS ) {
my $type = $TYPES[ rand @TYPES ];
my $sid = $SIDS[ rand @SIDS ];
my $hname = "$type-$hid";
my $hline = ' { "id": "'.$hid.'", '
. '"name": "'.$hname.'", '
. '"type": "'.$type.'", '
. '"ovs": "'.$sid.'"}'
;
push @hlines , $hline ;
}
say '{
"hosts": [' ;
say join ( ",\n" , @hlines );
say ' ],' ;
# switches
my @slines ;
foreach my $sid ( @SIDS ) {
my $sline = ' { "id": "'.$sid.'", '
. '"name": "switch_'.$sid.'", '
. '"type": "ovs"}'
;
push @slines , $sline ;
}
say ' "switches": [' ;
say join ( ",\n" , @slines );
say ' ],' ;
# random generate flows form the hosts
my @flines ;
foreach my $fid ( 1 .. $FLOWCOUNT ) {
my $src = $HIDS[ rand @HIDS ];
my $dst = $HIDS[ rand @HIDS ];
next if $src == $dst ;
my $fl = ' { "src": "'.$src.'", "dst": "'.$dst.'" }';
push @flines , $fl ;
}
say ' "flows": [' ;
say join ( ",\n" , @flines );
say ' ],' ;
# lastly we need to create uplinks, but keep it linear, we can't loop
# so we keep plucking off lists until we get concensus
say ' "uplinks": [' ;
# we have unattached switches in @SIDS
# we have linked edge switches in @linked, which can still take conections
# we have $MAXLINK uplinks per switch
# seed the first device, pull it off the list
my $fs = splice ( @SIDS , rand @SIDS , 1 ) ;
my @uplinks ;
my @edges ;
# now eat through the list of switches, kinda recursing
while ( @SIDS ) {
linkEdges();
}
say join ( ",\n" , @uplinks );
say ' ]' ;
say '}' ;
exit ;
sub linkEdges {
return unless @SIDS ;
# get some random endpoints
my @nextBatch = getEdges();
# link from here to these endpoints
foreach my $ds ( @nextBatch ) {
my $ul = ' { "src": "'.$fs.'", "dst": "'.$ds.'" }';
push @uplinks , $ul ;
# say "$ul";
}
# say "$fs , @edges , @nextBatch";
# add these new edges to the list
push @edges , @nextBatch ;
# pop to the new edge
# $fs = pop @edges ;
$fs = shift @edges ;
# repeat
linkEdges();
}
sub getEdges {
my @edges ;
foreach my $lc ( 1 .. 2 + rand $MAXLINK ) {
push @edges , splice ( @SIDS , rand @SIDS , 1 ) ;
}
return @edges ;
}