source: genesis/tools/genesis-to-yaml.pl@ 8674

Last change on this file since 8674 was 8644, checked in by richardvm, 14 years ago

nagios config generator

  • Property svn:executable set to *
File size: 4.4 KB
Line 
1#!/usr/bin/env perl
2use File::Basename;
3
4eval {
5 require Geo::Coordinates::RDNAP;
6};
7if (! $@) {
8 Geo::Coordinates::RDNAP->import(qw/from_rd to_rd dms/);
9} else {
10 sub from_rd {
11 return (0,0,0);
12 }
13}
14
15# RD X,Y -> RD Noorderbreedte, Oosterbreedte
16# http://web.inter.nl.net/users/F.Kissels/gps/conversie.html
17
18# Little perl to convert genesis files into python config files,
19# Which gonna be used for more logic stuff ;-)
20
21$argc = $#ARGV + 1;
22if ($argc == 0) {
23 print STDERR "Usage $_ <wleiden.conf> ...\n";
24 exit 1;
25# Hack to easy support multiple files without remembering any variable in between
26} elsif ($argc > 1) {
27 foreach $file (@ARGV) {
28 $output = $file;
29 $output =~ s/conf$/yaml/;
30 print "# Processing $file\n";
31 $CMD=dirname(__FILE__) . "/genesis-to-yaml.pl";
32 `$CMD $file > $output`;
33 }
34 exit 0;
35}
36
37
38do($ARGV[0]) || die;
39
40my %status_labels = ( up => 'up', down => 'dw', planned => 'pl' );
41
42$interfaces = [];
43foreach $if (keys %config) {
44 push(@interfaces, $if) if ($if =~ m/^[a-z0-9]+$/g);
45}
46$interfaces = join(",",@interfaces);
47
48if ( ($X =~ /\d+/) and ($Y =~ /\d+/) and ($X > 10) and ($Y > 10)) {
49 ($lat, $lon, $h) = from_rd( $X, $Y, 0);
50} else {
51 $lat = $lon = $h = 0;
52}
53
54$status = $status_labels{$status} || 'up'; # ensure reporting a correct status
55$comment = $comment || 'None';
56
57print <<EOF;
58# Genesis config yaml style
59# vim:ts=2:et:sw=2:ai
60#
61comment : $comment
62interfaces: $interfaces
63latitude : $lat
64location : $location
65longitude : $lon
66masterip : $master_ip
67release : $release
68nodename : $nodename
69nodetype : $nodetype
70rdnap_x : $X
71rdnap_y : $Y
72status : $status
73EOF
74
75if ($ileiden =~ /yes/) {
76 print "ileiden : yes\n";
77} else {
78 print "ileiden : no\n";
79}
80
81foreach $if (keys %config) {
82 ($iflabel, $alias) = split(':',$if);
83 $iflabel .= "_alias${alias}" if ($alias =~ m/[0-9]/);
84
85 print <<EOF;
86iface_$iflabel:
87EOF
88 %tmp = (
89 'dhcp' => 'False',
90 'compass' => 'None',
91 'interface' => $if,
92 );
93 foreach $line (split('\n',$config{$if})) {
94 ($key, $value) = split(/=/,$line);
95 # Remove trailing and leading white spaces
96 $value =~ s/^\s+//;
97 $value =~ s/\s+$//;
98 if ($key and (lc($key) =~ /^[a-z].*/)) {
99 #print "$key : $value\n";
100 $value = lc($value) if not lc($key) =~ m/(desc|comment)/;
101 if ($key =~ /^type$/i) {
102 if ($value eq 'ethernet') {
103 $value = 'eth';
104 } else {
105 $value = '11b';
106 }
107 } elsif ($key =~ /^status$/i) {
108 if ($value eq 'up') {
109 $value = 'up';
110 } else {
111 $value = 'down';
112 }
113 } elsif ($key =~ /^mode$/i) {
114 if ($value eq 'master') {
115 $value = 'ap-wds';
116 } else { # managed
117 $value = 'station-wds';
118 }
119 } elsif ($key =~ /^polar$/i) {
120 if ($value eq 'hor') {
121 $value = 'hr';
122 } else {
123 $value = 'vr';
124 }
125 } elsif ($key =~ /^essid$/i) {
126 $key = 'ssid';
127
128 # Dirty hack to fetch compass direction of essid
129 $value =~ /[a-z]+-([a-z]+)\..*/;
130 $direction = $1;
131 # Translate into English
132 if ($direction ne "omni") {
133 $direction =~ tr/oz/es/;
134 }
135 # node AMwtrt1 and 'test' directions
136 if ($direction eq "wtrtr" or $direction eq "test") {
137 $direction = 'None';
138 }
139 if ($direction) {
140 $tmp{'compass'} = $direction;
141 } else {
142 $tmp{'compass'} = 'None';
143 }
144 } elsif ($key =~ /^gain$/i) {
145 $value =~ s/dbi//;
146 }
147 # Als key niet bestaat, Dan key toevoegen
148 $tmp{lc($key)} = $value;
149 }
150 }
151# Status
152 if ( ! $tmp{status} ) {
153 $tmp{status} = 'up';
154 }
155# ethernet2wifi bridges
156 if ( $tmp{extra_type} eq 'eth2wifibridge' ) {
157 $tmp{type} = 'eth';
158 }
159# 11a
160 if ( $tmp{type} eq '11b' && $tmp{channel} > 13 ) {
161 $tmp{type} = '11a';
162 }
163
164 foreach $key (sort keys %tmp) {
165 printf " %-11s: '$tmp{$key}'\n", $key;
166 }
167 print "\n";
168}
169
Note: See TracBrowser for help on using the repository browser.