| 1 | #!/usr/bin/perl | 
|---|
| 2 | # | 
|---|
| 3 | # Parsen van nagios config uit Genesis | 
|---|
| 4 | # by maarten@wirelessleiden.nl | 
|---|
| 5 | # | 
|---|
| 6 | # v0.7 - Notes directive points to the remote log | 
|---|
| 7 | # v0.6 - Traffic interface check. checking via SNMP | 
|---|
| 8 | # v0.5 - added args support for location output and genesis and added count to parser | 
|---|
| 9 | # v0.4 - handeling proxies | 
|---|
| 10 | # v0.3 - added hostgroups on postcode | 
|---|
| 11 | # v0.2 - append host ext info and xy for statusmap | 
|---|
| 12 | # v0.1 - loops trough all nodes and interfaces | 
|---|
| 13 | # | 
|---|
| 14 | # Inspired by: | 
|---|
| 15 | # - configcleaner.pl by Rick van der Zwet (rick@wirelessleiden.nl) | 
|---|
| 16 | # - conf2inc.pl | 
|---|
| 17 |  | 
|---|
| 18 | #maak een mooi systeem met hash aanroepen om snel gegevens te verwerken | 
|---|
| 19 | sub parse_config { | 
|---|
| 20 | foreach my $id (sort keys %config) { | 
|---|
| 21 | my $rawData = $config{$id}; | 
|---|
| 22 | $rawData =~ s/#.*\n/\n/g;                  #wegslopen comments | 
|---|
| 23 | $rawData =~ s/\s*[\r,\n]+\s*/:::/g;        #spaties+enter+spaties vervangen door ::: | 
|---|
| 24 | $rawData =~ s/^::://;                      #::: aan het begin weghalen | 
|---|
| 25 | $rawData =~ s/:::$//;                      #::: aan het eind weghalen | 
|---|
| 26 | my @configArray = split( /:::/, $rawData); #array er van maken | 
|---|
| 27 | foreach $line (@configArray) { | 
|---|
| 28 | (my $name, my $value) = split( /\=/, $line); | 
|---|
| 29 | $$name{$id} = $value; | 
|---|
| 30 | } | 
|---|
| 31 | } | 
|---|
| 32 | } | 
|---|
| 33 |  | 
|---|
| 34 | sub generateConfig { | 
|---|
| 35 | my $configfile = $_[0]; | 
|---|
| 36 | open( CONF, "> $configfile"); | 
|---|
| 37 |  | 
|---|
| 38 | ### Pre render variables | 
|---|
| 39 | # make genesis configname needed for naming convention proxy | 
|---|
| 40 | my $configname = $nodetype.$nodename; | 
|---|
| 41 | if ($nodetype eq Proxy) { $configname = lc($nodename)}; | 
|---|
| 42 | # get current date | 
|---|
| 43 | $DATE = localtime(time()); | 
|---|
| 44 |  | 
|---|
| 45 | # get all WL IP's for host alive check and exclude aliases | 
|---|
| 46 | my $IPS = $master_ip; | 
|---|
| 47 | foreach my $key (sort keys %config) { | 
|---|
| 48 | if (($key !~ m/(:\d)$/) && ($IP{$key} =~/^172\../)) { | 
|---|
| 49 | $IPS = join(' ',$IPS, substr($IP{$key}, 0, -3)); | 
|---|
| 50 | } | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | # sanitize edugis coords for statusmap | 
|---|
| 54 | # X=0 uiterst links zwaluwak edugis x,y 90739,463249 | 
|---|
| 55 | # Y=0 uiterst boven LMkempers1 edugis x,y 107030,471206 | 
|---|
| 56 | # X=800 uiterst rechts LMkempers1 edugis x,y 107030,471206 | 
|---|
| 57 | # Y=600 uiterst onder PPFortis edugis x,y 104920,458730 | 
|---|
| 58 | $X = int(($X-90739)/((107030-90739)/800)); | 
|---|
| 59 | $Y = int((471206-$Y)/((471206-458730)/600)); | 
|---|
| 60 |  | 
|---|
| 61 | # Create hostgroups on location postcode | 
|---|
| 62 | my @Alphen = (2445, 2400..2409, 2470, 2471, 2770, 2771, 2420, 2421, 2440, 2441, 2430..2432, 2460, 2461, 1428, 3651..3653, 2435); | 
|---|
| 63 | my @Rijnwoude = (2730, 2731, 2390, 2391, 2394, 2396); | 
|---|
| 64 | my @Oegstgeest = (2223,2340..2343); | 
|---|
| 65 | my @Voorschoten = (2250..2254); | 
|---|
| 66 | my @Jacobswoude = (2452, 2355, 2450, 2451, 2465, 2480, 2481, 1430..1432); | 
|---|
| 67 | my @Leiden = (2159, 2376, 2374, 2377, 2375, 2370, 2371, 2300..2334, 2350..2353, 2200..2204, 2190, 2191, 2210, 2211, 2240..2245, 2735, 2380..2382); | 
|---|
| 68 | $group = "Rest"; | 
|---|
| 69 | if ($location =~ m/(\d\d\d\d)/) { | 
|---|
| 70 | for (@Alphen) {if ($_ eq $1) {$group = "Alphen"}} | 
|---|
| 71 | for (@Rijnwoude) {if ($_ eq $1) {$group = "Rijnwoude"}} | 
|---|
| 72 | for (@Oegstgeest) {if ($_ eq $1) {$group = "Oegstgeest"}} | 
|---|
| 73 | for (@Voorschoten) {if ($_ eq $1) {$group = "Voorschoten"}} | 
|---|
| 74 | for (@Jacobswoude) {if ($_ eq $1) {$group = "Jacobswoude"}} | 
|---|
| 75 | for (@Leiden) {if ($_ eq $1) {$group = "Leiden"}} | 
|---|
| 76 | if ($group eq "Rest") {$group = "Onbekend"} | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | # Create hostgroup for proxies | 
|---|
| 80 | if ($nodetype eq "Proxy") { $group = "Proxy"} | 
|---|
| 81 |  | 
|---|
| 82 | ### begin config file | 
|---|
| 83 |  | 
|---|
| 84 | print CONF <<EOP; | 
|---|
| 85 | # Config for $nodetype $nodename | 
|---|
| 86 | # Generated $DATE from Genesis (by gen2nag.pl) | 
|---|
| 87 | # | 
|---|
| 88 | # host definitions can be done in hosts.cfg | 
|---|
| 89 | # service definitions can be done in services.cfg | 
|---|
| 90 |  | 
|---|
| 91 | define host { | 
|---|
| 92 | host_name       $nodename | 
|---|
| 93 | alias           $nodetype$nodename.wleiden.net | 
|---|
| 94 | use             nodes | 
|---|
| 95 | address         $master_ip | 
|---|
| 96 | check_command   check_alive!$IPS | 
|---|
| 97 | hostgroups      $group | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | # Host extra info | 
|---|
| 101 | define hostextinfo{ | 
|---|
| 102 | host_name       $nodename | 
|---|
| 103 | notes           This node is generated $DATE from genesis | 
|---|
| 104 | notes_url       http://watch.wirelessleiden.nl/logs/$configname | 
|---|
| 105 | icon_image      $nodetype.png | 
|---|
| 106 | icon_image_alt  $nodetype $nodename | 
|---|
| 107 | statusmap_image $nodetype.gd2 | 
|---|
| 108 | 2d_coords       $X,$Y | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | #Service definitions for interfaces | 
|---|
| 112 | EOP | 
|---|
| 113 | # loop trough all interfaces for service checks | 
|---|
| 114 | foreach my $key (sort keys %config) { | 
|---|
| 115 |  | 
|---|
| 116 | ### Prerender | 
|---|
| 117 | #Strip subnet | 
|---|
| 118 | $IP{$key} = substr($IP{$key}, 0, -3); | 
|---|
| 119 |  | 
|---|
| 120 | # Service checks for all WL interfaces and exclude aliases | 
|---|
| 121 | if (($key !~ m/(:\d)$/) && ($IP{$key} =~/^172\../)) { | 
|---|
| 122 | print CONF <<EOP; | 
|---|
| 123 | define service { | 
|---|
| 124 | host_name               $nodename | 
|---|
| 125 | use                     check-if | 
|---|
| 126 | service_description     Ping $key | 
|---|
| 127 | check_command           check_if_ip!$IP{$key} | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | define service { | 
|---|
| 131 | host_name               $nodename | 
|---|
| 132 | use                     check-if | 
|---|
| 133 | service_description     Traffic $key | 
|---|
| 134 | check_command           check_if_snmp!$key | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | EOP | 
|---|
| 138 | # Service definition for wireless interfaces only | 
|---|
| 139 | if ( lc($TYPE{$key}) eq "wireless" && 1==0 ) {  # disabled with && 1==0 , not used yet | 
|---|
| 140 | print CONF <<EOP; | 
|---|
| 141 | # Service definition for wireless interface $key | 
|---|
| 142 | EOP | 
|---|
| 143 | } | 
|---|
| 144 | } | 
|---|
| 145 | } | 
|---|
| 146 | # close the config file | 
|---|
| 147 | close ( CONF ); | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | # check if any args | 
|---|
| 151 | if ($#ARGV < 0 ) { | 
|---|
| 152 | print "Usage: gen2nag.pl outputdir/nagios/config [genesis/localtion(default=.)]\n"; | 
|---|
| 153 | exit; | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | # Set the output dir | 
|---|
| 157 | my $OUTPUTDIR=$ARGV[0]; # location for nagios object files | 
|---|
| 158 |  | 
|---|
| 159 | # Set the config dir / The location of genesis | 
|---|
| 160 | my $CONFDIR='.'; | 
|---|
| 161 | if ($#ARGV == 1) { | 
|---|
| 162 | $CONFDIR=$ARGV[1]; | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | opendir(DIR,$CONFDIR) or die $!; | 
|---|
| 166 |  | 
|---|
| 167 | my $count=0; | 
|---|
| 168 | foreach(readdir(DIR)) { | 
|---|
| 169 | next unless m/([CH]Node[\w+i]|proxy\d+)/; | 
|---|
| 170 | my $n = $_; | 
|---|
| 171 | foreach ($n){ | 
|---|
| 172 | my $file = $CONFDIR.'/'.$n.'/wleiden.conf'; | 
|---|
| 173 | next unless -r $file; | 
|---|
| 174 | my $file_output = $OUTPUTDIR.'/'.$n.'.cfg'; | 
|---|
| 175 | print "Working on $n...\n"; | 
|---|
| 176 | $$_ = "" foreach qw(location master_ip gw_open nodetype nodename | 
|---|
| 177 | OS status X Y N E OS DISK AGGREGATE); | 
|---|
| 178 | %$_ = () foreach qw(config TYPE IP DESC SDESC SPEED DHCP DHCP_STATIC | 
|---|
| 179 | OSPF_BROADCAST OSPF_NEIGHBORS MODE ESSID CHANNEL | 
|---|
| 180 | POLAR ANTENNA GAIN DIRECTION BEAMWIDTH CABLE | 
|---|
| 181 | HEIGHT ROUTE); | 
|---|
| 182 | do($file) || die $!;#("Can't open file $file"); | 
|---|
| 183 | print "\t Parsing config..."; | 
|---|
| 184 | parse_config; | 
|---|
| 185 | print "DONE\n"; | 
|---|
| 186 | print "\t Generating nagios objectfile..."; | 
|---|
| 187 | generateConfig($file_output); | 
|---|
| 188 | print "DONE\n"; | 
|---|
| 189 | $count+=1; | 
|---|
| 190 | } | 
|---|
| 191 | } | 
|---|
| 192 | print "Created nagios configs for $count nodes in directory $OUTPUTDIR\n"; | 
|---|