jablonka.czprosek.czf

hotsanic

Subversion Repositories:
[/] [trunk/] [setup.pl] - Blame information for rev 22

 

Line No. Rev Author Line
11simandl#!/usr/bin/env perl
2use warnings;
3use diagnostics;
4 
5use strict;
6use FindBin;
7use lib "./lib";
8use lib $FindBin::RealBin;
9use HotSaNICparser;
10use HotSaNICshellio;
11use Cwd;
12 
13$|=1;
14 
15my $ALLYES;
16 
17my $THISDIR=Cwd::cwd();
18my $args=lc join "",@ARGV;
19 
20if ( index($args,"y") >=0 ) { $ALLYES="y"; } else { $ALLYES=""; }
21if ( index($args,"?") >=0 ) {
22 print "Usage:
23 
24 setup.pl [m[y]][s][g][?]
25 
26 when run without switches, the default value of \"msg\" will be used.
27 
28 m configure modules individually
29 
30 y (in combination with \"m\")
31 configure all modules
32 automatically answers \"Yes\" when asked if module shall be
33 used or displayed. This has nothing to do with the modules'
34 own configuration, it just affects the main setup behaviour.
35 
36 s generate main settings
37 note: if you want to add or remove modules using the \"m\" switch,
38 the \"s\" switch has also to be set to modify the main
39 settings accordingly. In this case the \"y\" swich will
40 automatically activate all available modules!
41 
42 g generate \"rrdgraph\" start/stop scipt
43 
44 ? display this help text.\n\n";
45 exit;
46 }
47 
48if ( (index ($args,"g")<0) && (index ($args,"s")<0) && (index ($args,"m")<0) ) { $args=$args."gsm"; }
49 
50my $RUN="";
51my $SHOW="";
52 
53if ( ! -e "var" ) { mkdir "var",0755; }
54if ( ! -e "var/settings" ) { mkdir "var/settings",0755; }
55if ( ! -e "var/log" ) { mkdir "var/log",0755; }
56if ( ! -e "var/run" ) { mkdir "var/run",0755; }
57if ( ! -e "var/modules" ) { mkdir "var/modules",0755; }
58 
59if ( index($args,"m") >=0 ) { configure_modules(); }
60if ( index($args,"s") >=0 ) { configure_main(); }
61if ( index($args,"g") >=0 ) { write_rrdgraph_script(); }
62 
63if ( -e "settings.new" ) {
64 HotSaNICparser::backup_file("settings");
65 system "mv -f settings.new settings";
66 }
67system "ln -sf ../../settings var/settings/main";
68 
69# build array with all settings files in the "modules" dir
70#
71my @allsettings;
72require File::Find;
73$File::Find::name=""; # suppress stupid warning...
74File::Find::find( {wanted => sub { /^settings\z/s && push @allsettings,$File::Find::name; } }, 'modules');
75 
76for my $item (@allsettings) {
77 chomp $item;
78 my $name;
79 ($name=$item) =~ s/ules\/(.*)\/.*/_$1/g;
80 system "ln -sf ../../$item var/settings/$name\n";
81 }
82 
83print "\nNow adapt all settings files to satisfy your needs.\nThey are all linked to the directory <HotSaNIC_installdir>/var/settings .\n\n";
84exit 0;
85 
86 
87 
88sub write_rrdgraph_script {
89 
90 print "\nWriting start/stop script \"rrdgraph\" ...";
91 
92 open OUT,">rrdgraph";
93 if ( -e "/bin/bash" ) { print OUT "#!/bin/bash\n"; } else { print OUT "#!/usr/bin/env sh\n"; }
94 print OUT "# chkconfig: 12345 55 45
95# description: System statistics
96 
97cd \"$THISDIR\"
98 
99. ./settings
100 
101case \$1 in
102 start)
103 echo \"Starting rrdtimer...\"
104 ./rrdtimer.pl Dp
105 ;;
106 stop)
107 echo \"Stopping rrdtimer...\"
108 if [ -e \"\$PIDFILE\" ]; then
109 PID=`cat \"\$PIDFILE\"`
110 for nn in \$PID ; do kill \$nn; done
111 else
112 echo \"pid-file not found, rrdtimer not running?!\"
113 fi
114 ./clearall CLEAR_COUNTERS
115 ;;
116 status)
117 if [ -e \$PIDFILE ]; then
118 echo \"main process on PID \`cat \$PIDFILE\` (according to PID-file)\"
119 else
120 echo \"pid-file not found, rrdtimer not running?!\"
121 fi
122 ;;
123 restart)
124 ./rrdgraph stop
125 ./rrdgraph start
126 ;;
127 *)
128 echo \"usage: rrdgraph [start|stop|status|restart]\"
129 ;;
130 esac
131";
132 close OUT;
133 chmod 0755,"rrdgraph";
134 
135 print" Ok.\n";
136 }
137 
138 
139sub configure_modules {
140 
141 print "Configuring modules:\n";
142 
143 my @mods=HotSaNICparser::scan_for_modules( "./modules", 0, "*" );
144 my @run;
145 my @show;
146 foreach my $dir (@mods) {
147 if ( ($dir eq ".") || ($dir eq "..") || ($dir eq "CVS") ) { next; }
148 my $modname=uc $dir;
149 my $input=$ALLYES;
150 if ($input eq "") {
151 print "\n\nModule found: $modname\n\nDescription:\n";
152 if ( -e "modules/$dir/.description") {
153 open FILE,"modules/$dir/.description";
154 while (<FILE>) { print " $_"; }
155 close FILE;
156 }
157 else { print "(not available)"; }
158 $input=HotSaNICshellio::askyesno("Do you want to use this module","y");
159 }
160 
161 if ($input eq "y") {
162 push @run,lc $modname;
163 $input=$ALLYES;
164 if ($input eq "") {
165 $input=HotSaNICshellio::askyesno("Do you want to show this module's graphs on the webpage","y");
166 }
167 if ($input eq "y") { push @show,lc $modname; }
168 }
169 print "\n----------------------------------------\n";
170 }
171 foreach my $dir (@run) {
172 print "\nsetting up $dir ...\n";
173 if (-e "modules/$dir/setup.pl") { system "cd modules/$dir; ./setup.pl; cd .."; }
174 else { system "cd modules/$dir; ./setup; cd .."; }
175 }
176 $RUN=join " ",@run;
177 $SHOW=join " ",@show;
178 print" Ok.\n";
179 }
180 
181 
182 
183sub configure_main {
184 
185 print "Writing main settings ...\n";
186 
187 my $convertpath;
188 my %VARS;
189 
190 my $file=".settings.default";
191 my $newfile="settings";
192 if ( -e "settings" ) {
193 $newfile="settings.new";
194 my @CONF=HotSaNICparser::read_settings(".");
195 foreach (@CONF) {
196 my ($var,$val)=HotSaNICparser::parse_line($_);
197 $VARS{$var}=$val;
198 }
199 }
200 open FILE,$file;
201 open NEWFILE,">$newfile";
202 
203 while (<FILE>) {
204 chomp;
205 
206 my ($var,$value,$comment)=HotSaNICparser::parse_line($_);
207 
208 if (defined $VARS{$var}) { $value=$VARS{$var}; }
209 if ($var eq "RUN") { if ($RUN ne "") { ($value=$RUN) =~ s/^ *//; } }
210 if ($var eq "SHOW") { if ($SHOW ne "") { ($value=$SHOW) =~ s/^ *//; } }
211 
212 if ( ($value eq "" ) || (lc $value eq "not configured") ) {
213 
214 if ($var eq "DAEMONDIR") {
215 print " checking path to \"rrdtimer.pl\" (current: '$value')\n";
216 if ( -e "$THISDIR/rrdtimer.pl" ) { $value=$THISDIR; }
217 }
218 
219 if ($var eq "BINPATH") {
220 print " checking path to \"rrdtool\" (current: '$value')\n";
221 my @found=HotSaNICparser::locate_files("bin\/rrdtool");
222 if (@found) {
223 foreach my $nn (@found) { $nn=~ s/\/rrdtool$//g; }
224 $value=HotSaNICshellio::choose($value,"rrdtool could not be found. If you already installed it, please edit the settings file.",@found);
225 }
226 }
227 
228 if ($var eq "CONVERTMETHOD") {
229 print " guessing convert method...\n";
230 print " checking for Image::Magick perl module...";
231 eval {
232 require Image::Magick;
233 };
234 if ( $@ ) {
235 print " not found\n";
236 }
237 else {
238 print " found\n";
239 $value="I::M";
240 }
241 
242 print " checking path to \"convert\" from ImageMagick (current: '$value')\n";
243 my @found=HotSaNICparser::locate_files("bin\/convert|grep convert\$");
244 if (@found) {
245 $convertpath=HotSaNICshellio::choose($value,"convert could not be found. If ImageMagick package is already installed, please edit the settings file.",@found);
246 $value="ImgMgck" if ( $value ne "I::M" );
247 }
248 $value="HTML" if ( $value eq "not configured" );
249 }
250 
251 if ($var eq "CONVERTPATH") {
252 $value=$convertpath;
253 }
254 
255 if ($var eq "SNMPWALK") {
256 print " checking path to \"snmpwalk\" (current: '$value')\n";
257 my @found=HotSaNICparser::locate_files("bin\/snmpwalk");
258 if (@found) {
259 $value=HotSaNICshellio::choose($value,"snmpwalk could not be found. If you already installed it, please edit the settings file.",@found);
260 }
261 }
262 
263 if ($var eq "SNMPGET") {
264 print " checking path to \"snmpget\" (current: '$value')\n";
265 my @found=grep /snmpget$/,HotSaNICparser::locate_files("bin\/snmpget");
266 if (@found) {
267 $value=HotSaNICshellio::choose($value,"snmpget could not be found. If you already installed it, please edit the settings file.",@found);
268 }
269 }
270 
271 if ($var eq "SNMPBULKWALK") {
272 print " checking path to \"snmpbulkwalk\" (current: '$value')\n";
273 my @found=grep /snmpbulkwalk$/,HotSaNICparser::locate_files("bin\/snmpbulkwalk");
274 if (@found) {
275 $value=HotSaNICshellio::choose($value,"snmpbulkwalk could not be found. If you already installed it, please edit the settings file.", @found);
276 }
277 }
278 }
279 
280 if ($var ne "") { print NEWFILE "$var=\"$value\""; print "$var=\"$value\"\n"; }
281 if ($comment ne "") { $comment="" if $comment eq " "; print NEWFILE "#$comment"; }
282 print NEWFILE "\n";
283 
284# if ( ($value ne " ") && ($var !~ /^#/) ) { print "$var=\"$value\"\n"; }
285# } else { $var=""; }
286# if ($value ne " ") { print NEWFILE "$var=\"$value\"\n"; }
287# else { print NEWFILE $var,"\n"; }
288 }
289 close FILE;
290 close NEWFILE;
291 
292 print"\n --- Main settings generated. ---\n";
293 }
294 

Powered by WebSVN 2.2.1