source: genesis/nodes/gen2nag.pl@ 6635

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

small update in gen2nag.pl count

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