#! /usr/bin/perl # # # aug 2002, jasper\@WirelessLeiden.NL # $now=localtime($unow=time()); $config='/etc/wl/wleiden.conf'; $domain='wLeiden.NET'; $ifconfig='/sbin/ifconfig'; $iwconfig='/sbin/iwconfig'; $ipchains='/sbin/ipchains'; $indexmaker='/usr/bin/indexmaker'; $sroute='/sbin/route'; $OSPFPWD='huppel'; $separator=';'; $author='feb 2003 jasper@WirelessLeiden.NL'; $wicontrol='/usr/sbin/wicontrol'; if ($functions_only) {goto functions;} $source='/etc/wl/wleiden.pl'; do($config) || die; parse_config(); $hostname=`/bin/hostname |cut -d . -f 1`;chomp($hostname); $maxmask= pack("CCCC", 255, 255, 255, 255); if (! $ARGV[0]) {die "use start/stop/startall";} if ($ARGV[1]=~/debug/i) {$DEBUG=1;} if ($ARGV[0]=~/start/i) { ###################################################################### do_it(); open_files(); $nwcfg.="$indexmaker /etc/mrtg/mrtg.cfg> /var/www/html/mrtg/index.html 2> /dev/null\n"; print_files();close_files(); system("chmod 755 /etc/wl/nwcfg.sh;/etc/wl/nwcfg.sh"); if ($DEBUG) {die "DEBUGGING!";} if ($DEBUG) {print "\nDEBUG: making mrtg index.\n";} changeline("/etc/sysconfig/dhcpd","DHCPDARGS","DHCPDARGS=\"$dhcpints\""); #if ($DEBUG) {print "\nDEBUG: reloading firewall.\n";} #$nwcfg.="/usr/local/sbin/firewall\n"; if ($DEBUG) {print "\nDEBUG: restarting daemons.\n";} if ($ARGV[0]=~/startall/) {$restart='restart';} else {$restart='condrestart';} $nwcfg.="/etc/init.d/dhcpd $restart\n"; $nwcfg.="/etc/init.d/zebra $restart\n"; $nwcfg.="/etc/init.d/ospfd $restart\n"; $nwcfg.="/etc/init.d/httpd $restart\n"; $nwcfg.="/etc/init.d/named $restart\n"; ######################################################################## } else # geen start -> stop { $nwcfg.="$ifconfig lo:1 down\n"; foreach $if (keys %config) { $nwcfg.="$ifconfig $if down\n"; } } exit; ######################################################################## functions: $ahum=1; sub do_it { if ($OS=~/FreeBSD/i) {$FreeBSD=1;} elsif ($OS=~/Linux/i) {$Linux=1;} init_files(); #system("/sbin/modprobe ipv6"); #system("/sbin/modprobe ipchains"); $nwcfg.="$ifconfig lo:1 down\n"; #print "ifconfig lo:1 $master_ip\n"; #$nwcfg.="$ifconfig lo:1 $master_ip netmask 255.255.255.255\n"; # altijd up! $last=''; foreach $if (sort {$b <=> $a} keys %config) { if ($TYPE{$if}=~/wireless/) {$last=$if} } if ($last) {iwconfig("$last &> /dev/null");} # Laatste het eerste... $keys=1; foreach $if (sort keys %config) { if ($if=~/^([^:]+):(\d+)/) {$mymain=$1;$mysub=$2;} else {$mymain='';} if ($functions_only<1) {print "Doing interface: $if IP: $IP{$if}\n";} if ($if!~/:\d+/) {$main=1;} else {$main=0;} ##################### DOWN if ($DOWN{$if}=~/yes/) { print "$if configured DOWN.\n"; $nwcfg.="$ifconfig $if 127.0.0.1 down\n"; if ($main) { $zebra1.=sprintf < %08x enne %08x\n",$II,$nwa); } $nwa=itoadr($nwa); if ($AGGREGATE) {$agg=$AGGREGATE;} else {$agg=21;} if (($nwa!~/^172\.16\./) && ($area > 0)) { $ospfarea.="area $area range $nwa/$agg substitute $nwa/$agg\n"; } $ospf21.="! area $area authentication message-digest\n"; } $ospfd.=sprintf < interfaces <================ $ospfd2 ! ================> OSPF router <================ router ospf ospf router-id $master_ip $ospfd21 passive-interface lo0 $ospfd22 !default $ospfd3 $ospfarea ! ================> networks <================ $ospfd4 ! ================> neighbors <================ $ospfd5 log file /var/log/ospf.log EOOL # $rcconf.="\n\n" . $freebsd; } sub get_area { local($IP)=@_; local($range); local($ip,$netmask,$broadcast,$network,$width); local($a,$b,$c,$d)=split(/\./,$IP); local($II)=($a<<24)+($b<<16)+($c<<8)+$d; local($ii,$ip,$nm,$I2); # for ($i=0;$i<=$maxareas;$i++) # { # $ii=$II[$i]; # $nm=$NM[$i]; # $I2=$II & $nm; ##$ospfd.=sprintf "i=$i\t $RANGE[$i]\t ii=$ii\t nm=$nm\t I2=$I2 $II $IP $a,$b,$c,$d\n"; # if ($I2 == $ii) {return($AREA[$i]);} # } # return(12345); $area=1; if ($b eq 16) {$area=0;} elsif ($c < 128) {$area=($b * 10)} else {$area=(1+($b*10));} if ($area > 255) {$area/=10;} return($area); } sub read_areas { $maxareas=0; open(AC,"$home/areas.conf") || die; while () { chomp(); s/#.*$//; if (! $_) {next;} ($area,$range,$desc,$counter,$junk)=split(/\s+/); $AREA[$maxareas]=$area; $RANGE[$maxareas]=$range; $DESC[$maxareas]=$desc; ($ip,$width)=split(/\//,$range); ($a,$b,$c,$d)=split(/\./,$ip); $ii=($a<<24)+($b<<16)+($c<<8)+$d; if ($width eq 32) {$nm=0xffffffff;} else {$nm=0xffffffff-1<<(31-$width);} $II[$maxareas]=$ii; $NM[$maxareas]=$nm; $maxareas++; } close(AC); } sub parse_config { foreach $if (keys %config) { $cfg=$config{$if}; if (! $html) {print "Parsing interface: $if\n";} while ($cfg) { $cfg=~s/^([^\n\r]+)[\r\n]*//m; $line=$1; $line=~s/\s*#.*//; if ((($name,$value)=split(/=/,$line)) eq 2) { if ($name eq 'ESSID') {$value=lc($value);} #print "VAL($name)=$value\n"; $doit="if (exists(\$$name\{\"$if\"\})) {\$$name\{\"$if\"\}.=\"$separator$value\";} else {\$$name\{\"$if\"\}.=\"$value\";}"; #print "DOIT: [$doit]\n"; eval($doit); } #print "CFG:[$cfg]\n"; $cfg=~s/[\r\n]*$//m; } } if ($functions_only<1) {print "Done parsing config.\n";} } sub iwconfig { local($line)=@_; $nwcfg.="$iwconfig $line\n"; } sub ipchains { local($line)=@_; return(system("$ipchains $line")); } #($a,$b,$c,$d) = unpack('C4',$addr[0]); #$IP = pack("CCCC", 152, 2, 128, 184); # create IP address #($var1, $var2, $var3, $var4) = unpack("CCCC", $IP); #inverse of the above sub calc_ip { local($i)=@_; local($a,$b,$c,$d); local($ip,$width)=split(/\//,$i); local($a,$b,$c,$d)=split(/\./,$ip); local($ii)=($a<<24)+($b<<16)+($c<<8)+$d; if ($width eq 32) {$nm=0xffffffff;} else {$nm=0xffffffff-1<<(31-$width);} $nw=($ii & $nm); $br=$nw | (0xffffffff & ~$nm); $netmask=itoadr($nm);; $broadcast=itoadr($br);; $network=itoadr($nw); # $i=itoadr($ii); # printf "ip=$ip|width=$width|nm=%0x|$netmask|br=%0x|$broadcast|$i|\n",$nm,$br; return($ip,$netmask,$broadcast,$network,$width); } sub itoadr { local($ip)=@_; local($a,$b,$c,$d); $a=($ip & 0xff000000) >> 24; $b=($ip & 0x00ff0000) >> 16; $c=($ip & 0x0000ff00) >> 8; $d=($ip & 0x000000ff); return("$a.$b.$c.$d"); } sub changeline { local($file,$old,$new)=@_; open(OLD,$file) || die "Could not open old file: $file\n"; open(NEW,">$file.genesis.$$") || die "Could not create new file: $file.genesis.$$"; while() { if (/$old/) {print NEW "$new\n";} else {print NEW $_;} } close(NEW); close(OLD); rename("$file.genesis.$$",$file) || die "Could not overwrite old file: $file"; } sub open_files { open (SH,">/etc/wireless.conf.sh"); # Voor firewall script. open (NWCFG,">/etc/wl/nwcfg.sh"); # Voor network config open (DHCP,">/etc/dhcpd.conf") || die "Could not create dhcpd.conf"; open (ZEBRA,">/etc/zebra/zebra.conf") || die "Could not create zebra.conf"; open (OSPFD,">/etc/zebra/ospfd.conf") || die "Could not create ospfd.conf"; open (MRTG,">/etc/mrtg/mrtg.cfg") || die "Could not create mrtg.cfg"; open (RCCONF,">/etc/rc.conf") || die "Could not create rc.conf"; open (FREEBSD,">/etc/rc.node.conf") || die "Could not create rc.node.conf"; open (RESOLV,">/etc/resolv.conf") || die "Could not create resolv.conf"; open (NAMED,">/etc/named.conf") || die "Could not create named.conf"; } sub close_files { close (SH); close (NWCFG); close (DHCP); close (ZEBRA); close (OSPFD); close (MRTG); close(RCCONF); close(FREEBSD); close(RESOLV); close(NAMED); } sub print_files { print SH $sh; print NWCFG $nwcfg; print DHCP $dhcp; print ZEBRA $zebra; print OSPFD $ospfd; print MRTG $mrtg; print RCFONG $rcconf; print FREEBSD $freebsd; print RESOLV $resolv; print NAMED $named; } sub init_files { $sh=sprintf <CPU load on ${hostname} Options[${hostname}_load]: transparent, gauge, nopercent Legendi[${hostname}_load]: 5min Legendo[${hostname}_load]: 15min ShortLegend[${hostname}_load]: % YLegend[${hostname}_load]: % WithPeak[${hostname}_load]: wmy Target[${hostname}_mem]: `/usr/local/sbin/memusage.pl` MaxBytes[${hostname}_mem]: 256000000 Title[${hostname}_mem]: Memory usage on ${hostname} PageTop[${hostname}_mem]:

Memory usage on ${hostname}

Options[${hostname}_mem]: transparent, gauge, nopercent Legendi[${hostname}_mem]: free Legendo[${hostname}_mem]: swap ShortLegend[${hostname}_mem]: Bytes YLegend[${hostname}_mem]: Bytes WithPeak[${hostname}_mem]: wmy Target[${hostname}_up]: `/usr/local/sbin/uptime.pl` MaxBytes[${hostname}_up]: 10000 Title[${hostname}_up]: ${hostname} Uptime PageTop[${hostname}_up]:

${hostname} Uptime

Options[${hostname}_up]: transparent, gauge, nopercent Legendi[${hostname}_up]: uptime Legendo[${hostname}_up]: ShortLegend[${hostname}_up]: Days YLegend[${hostname}_up]: Days WithPeak[${hostname}_up]: wmy EOM $rcconf.=sprintf <Traffic on $if $DESC{$if} Options[${if}_traf]: transparent WithPeak[${if}_traf]: wmy EOM2 if ($functions_only<1) { ipchains("-N ${if}-i"); ipchains("-N ${if}-o"); ipchains("-I input -i ${if} -j ${if}-i"); ipchains("-I output -i ${if} -j ${if}-o"); } if ($TYPE{$if}=~/wireless/i) { $mrtg.=sprintf <Quality $if $DESC{$if} Options[${if}_quality]: transparent, gauge, nopercent Legendi[${if}_quality]: Quality Legendo[${if}_quality]: Noise ShortLegend[${if}_quality]: /100 YLegend[${if}_quality]: /100 WithPeak[${if}_quality]: wmy Target[${if}_rate]: `/usr/local/sbin/wlan_bitrate.pl ${if}` MaxBytes1[${if}_rate]: 11 MaxBytes2[${if}_rate]: 11 Title[${if}_rate]: Bitrate $if $SDESC{$if} PageTop[${if}_rate]:

Bitrate $if $DESC{$if}

Options[${if}_rate]: transparent, gauge, nopercent Legendi[${if}_rate]: Bitrate ShortLegend[${if}_rate]: Mb/s YLegend[${if}_rate]: Mb/s WithPeak[${if}_rate]: wmy EOM3 if ($MODE{$if}=~/master/i) { $mrtg.=sprintf <Users on $if $DESC{$if} Options[${if}_users]: transparent, gauge, nopercent Legendi[${if}_users]: Users ShortLegend[${if}_users]: users YLegend[${if}_users]: users WithPeak[${if}_users]: wmy EOM4 } } # wireless } sub mrtg2 { $mrtg.=sprintf <Latency/PktLoss to $p on $if $DESC{$if} Options[${if}_lat]: transparent, gauge, nopercent Legendi[${if}_lat]: PacketLoss% Legendo[${if}_lat]: Max Latency (ms) ShortLegend[${if}_lat]: %/ms YLegend[${if}_lat]: %/ms WithPeak[${if}_lat]: wmy EOM5 } sub ahum { print "AHUM!\n"; } #return(1);