source: genesis/nodes/gen2nag.pl@ 6731

Last change on this file since 6731 was 6731, checked in by maarten, 16 years ago

added service to interface section

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