1 | 1 | simandl | # |
2 | | | # $Id: HotSaNICparser.pm,v 1.47 2004/07/11 22:39:39 bernisys Exp $ |
3 | | | # |
4 | | | |
5 | | | package HotSaNICparser; |
6 | | | |
7 | | | use HotSaNIClog; |
8 | | | |
9 | | | ($VERSION = '$Revision: 1.47 $') =~ s/.*(\d+\.\d+).*/$1/; |
10 | | | |
11 | | | ###################################################################### |
12 | | | # |
13 | | | # evaluate name of module by its subdir |
14 | | | # |
15 | | | # Usage: |
16 | | | # |
17 | | | # $modname=get_module_name(); |
18 | | | # |
19 | | | sub get_module_name { |
20 | | | require Cwd; |
21 | | | my $dir=Cwd::cwd(); |
22 | | | my @array=split /MODULES\//,uc $dir; |
23 | | | my $name=pop @array; |
24 | | | if($name=~ /\//) { $name="UNKNOWN ($dir)"; } |
25 | | | chomp $name; |
26 | | | HotSaNIClog::set_modulename($name); |
27 | | | return $name; |
28 | | | } |
29 | | | |
30 | | | |
31 | | | ###################################################################### |
32 | | | # |
33 | | | # scan given directory for modules |
34 | | | # |
35 | | | # this function automatically weeds out all "." ".." and "CVS" entries |
36 | | | # |
37 | | | # Usage: |
38 | | | # |
39 | | | # @modules=scan_for_modules(<directory>,<debuglevel>,<FILTER>); |
40 | | | # |
41 | | | # <directory> |
42 | | | # |
43 | | | # <debuglevel> 1: function introduces itself |
44 | | | # >1: list all activity |
45 | | | # |
46 | | | # <FILTER> a space-separated list of modules that should be |
47 | | | # inserted if found or "*" for all found modules |
48 | | | # |
49 | | | sub scan_for_modules { |
50 | | | my ($directory,$FILTER)=@_; |
51 | | | undef my @modules; |
52 | | | |
53 | | | if (HotSaNIClog::check_debuglevel("MAIN_SCAN_MODULES")) { HotSaNIClog::info("scanning $directory for modules."); } |
54 | | | |
55 | | | opendir DIR,$directory; |
56 | | | my @mods=sort(readdir DIR); |
57 | | | closedir DIR; |
58 | | | |
59 | | | # weed out non-module entries |
60 | | | foreach (@mods) { |
61 | | | if ($_ eq ".") { next; } |
62 | | | if ($_ eq "..") { next; } |
63 | | | if ($_ eq "CVS") { next; } |
64 | | | if ( -d "$directory/$_") { |
65 | | | if ( ! -e "$directory/$_/rrd" ) { mkdir "$directory/$_/rrd",0755; } |
66 | | | if ($FILTER =~ /\*|(^| )$_( |$)/) { |
67 | | | if (HotSaNIClog::check_debuglevel("MAIN_SCAN_MODULES_VERBOSE")) { HotSaNIClog::info(" found module ".uc $_); } |
68 | | | push @modules,$_; |
69 | | | } |
70 | | | } |
71 | | | } |
72 | | | return @modules; |
73 | | | } |
74 | | | |
75 | | | |
76 | | | ###################################################################### |
77 | | | # |
78 | | | # strip unwanted chars like " and multiple spaces at end / beginning |
79 | | | # of each element and return the processed array. |
80 | | | # |
81 | | | # Usage: |
82 | | | # |
83 | | | # $stripped_string=strip_unwanted($raw_string); |
84 | | | # |
85 | | | sub strip_unwanted { |
86 | | | foreach (@_) { |
87 | | | $_ =~ s/ *= */=/g; # spaces near a = |
88 | | | $_ =~ s/\"//g; # " chars |
89 | | | $_ =~ s/ +/ /g; # multiple spaces to single |
90 | | | $_ =~ s/^ *//g; # spaces at beginning |
91 | | | $_ =~ s/ *$//g; # spaces at end |
92 | | | } |
93 | | | return @_; |
94 | | | } |
95 | | | |
96 | | | |
97 | | | ###################################################################### |
98 | | | # |
99 | | | # Parse a line, strip all unwanted chars, split it by "=" into |
100 | | | # a VAR and a VALUE part and make sure those parts are defined. |
101 | | | # |
102 | | | # Usage: |
103 | | | # |
104 | | | # ($var,$value,$comment)=parse_line($settings_line); |
105 | | | # |
106 | | | sub parse_line { |
107 | | | my ($line)=@_; |
108 | | | chomp $line; |
109 | | | |
110 | | | # remove leading spaces |
111 | | | $line =~ s/^ *//g; |
112 | | | |
113 | | | # weed out comments (i.e. everything following a "#" |
114 | | | my ($important,$comment)=split /#/,$line,2; |
115 | | | if ((! defined $comment) || ( $comment eq "" )) { |
116 | | | if (index($line,"#")>=0) { $comment=" "; } |
117 | | | else { $comment=""; } |
118 | | | } |
119 | | | if ((! defined $important) || ($important eq "")) { return ("","",$comment); } |
120 | | | |
121 | | | # trim the edges ;) |
122 | | | ($important)=strip_unwanted($important); |
123 | | | |
124 | | | # separate the variable name from its assigned value |
125 | | | my ($var,$value)=split /=/,$important,2; |
126 | | | |
127 | | | # avoid undefined variables |
128 | | | $var="" if (! defined $var); |
129 | | | $value="" if (! defined $value); |
130 | | | |
131 | | | # force varnames to be upcase |
132 | | | $var=uc $var; |
133 | | | |
134 | | | return ($var,$value,$comment) |
135 | | | } |
136 | | | |
137 | | | |
138 | | | ###################################################################### |
139 | | | # |
140 | | | # evaluate settings file and check config... |
141 | | | # |
142 | | | # Usage: |
143 | | | # |
144 | | | # %config=get_config($path_to_settings_file); |
145 | | | # |
146 | | | sub get_config { |
147 | | | my ($location)=@_; |
148 | | | my ($var,$value); |
149 | | | my %config; |
150 | | | |
151 | | | foreach (read_settings($location)) { |
152 | | | ($var,$value)=parse_line($_); |
153 | | | $config{$var}=$value; |
154 | | | } |
155 | | | |
156 | | | # sanity check for all config variables |
157 | | | # |
158 | | | $config{"BINPATH"}=check_config_item("BINPATH","path","","","path to \"rrdtool\"",%config); |
159 | | | $config{"DAEMONDIR"}=check_config_item("DAEMONDIR","path","","","path to \"HotSaNIC\"",%config); |
160 | | | $config{"WEBDIR"}=check_config_item("WEBDIR","path","","","path to HotSaNIC's output directory",%config); |
161 | | | $config{"VARDIR"}=check_config_item("VARDIR","path","\$DAEMONDIR/var","","path to HotSaNIC's logfiles",%config); |
162 | | | $config{"LOGDIR"}=check_config_item("LOGDIR","path","\$DAEMONDIR/var/log","","path to HotSaNIC's logfiles",%config); |
163 | | | $config{"PIDFILE"}=check_config_item("PIDFILE","var","\$DAEMONDIR/log/rrdtimer.pid","","path to HotSaNIC's PID file",%config); |
164 | | | $config{"DTIME"}=60*check_config_item("DTIME","var","15","minutes","diagram rebuild time",%config); |
165 | | | $config{"CTIME"}=3600*check_config_item("CTIME","var","12","hours","diagram conversion time",%config); |
166 | | | $config{"STIME"}=check_config_item("STIME","var","120","seconds","module scan time",%config); |
167 | | | $config{"LOGSIZE"}=check_config_item("LOGSIZE","var","500000","bytes","max. logfile size",%config); |
168 | | | $config{"LOGBACKUPS"}=check_config_item("LOGBACKUPS","var","4","","logfiles to keep as backups",%config); |
169 | | | $config{"IMAGEFORMAT"}=check_config_item("IMAGEFORMAT","var","gif","","format of generated pictures",%config); |
170 | | | $config{"ORDER"}=check_config_item("ORDER","var","","","order of modules",%config); |
171 | | | $config{"DEBUGLEVEL"}=check_config_item("DEBUGLEVEL","var","NONE","","debug level",%config); |
172 | | | $config{"SCHEDULE_MIN"}=check_config_item("SCHEDULE_MIN","var","50","ms","minimum scheduling interval",%config); |
173 | | | $config{"SCHEDULE_MAX"}=check_config_item("SCHEDULE_MAX","var","100","ms","maximum scheduling interval",%config); |
174 | | | $config{"AUTOINDEX"}=check_config_item("AUTOINDEX","var","no","","call makindex on startup",%config); |
175 | | | |
176 | | | return %config; |
177 | | | } |
178 | | | |
179 | | | |
180 | | | ###################################################################### |
181 | | | # |
182 | | | # read settings-file and strip comments and empty entries |
183 | | | # |
184 | | | # Usage: |
185 | | | # |
186 | | | # @parsed_config=read_settings($path_to_settings_file); |
187 | | | # |
188 | | | # returns just the lines actually containing data - stripped by comments. |
189 | | | # |
190 | | | sub read_settings { |
191 | | | my $location=shift; |
192 | | | my @config; |
193 | | | if ( (! defined $location) || ($location eq "")) { $location="."; } |
194 | | | if (-e "$location/settings") { |
195 | | | push @config,"settings file exisits. (Dummy added by HotSaNICparser.pm::read_settings)"; |
196 | | | open CONF,"$location/settings"; |
197 | | | while (<CONF>) { |
198 | | | chomp; |
199 | | | next if $_ eq ""; |
200 | | | my ($important,$comment)=split /#/; |
201 | | | if ((defined $important) && ( index($important,"=") >= 0)) { |
202 | | | push @config,$important |
203 | | | } |
204 | | | } |
205 | | | } |
206 | | | close CONF; |
207 | | | |
208 | | | return @config; |
209 | | | } |
210 | | | |
211 | | | |
212 | | | ###################################################################### |
213 | | | # |
214 | | | # read defaults-file and settings-file, into a hash |
215 | | | # |
216 | | | # Usage: |
217 | | | # |
218 | | | # %moduleconfig=get_moduleconfig($path_to_settings_file,%types); |
219 | | | # |
220 | | | # returns a hash with all settings items. |
221 | | | # items beginning with "COLOR_" or "GRAPH_" are strings |
222 | | | # only exception is the "GRAPH_ADDLINE" statement which may be |
223 | | | # stated multiple times. |
224 | | | # other items are (unless defined otherwhise using %types hash) |
225 | | | # arrays containing all instances of this item |
226 | | | # |
227 | | | # Possible types are "array", "var" and "bool". |
228 | | | # Items of the "bool" type will be parsed to "1" for the values |
229 | | | # "1","on","yes","true", and to "0" for everything else. |
230 | | | # |
231 | | | sub get_moduleconfig { |
232 | | | my $location=shift || "."; |
233 | | | my %types=@_; |
234 | | | if ($location eq "") { $location="."; } |
235 | | | |
236 | | | my %config=(GRAPH_FORCE_LEGEND=>[0],GRAPH_RIGID=>[0],GRAPH_STYLE=>["lin"]); |
237 | | | for my $file ("$location/.settings.default","$location/settings") { |
238 | | | if (-e $file) { |
239 | | | open CONF,$file; |
240 | | | while (<CONF>) { |
241 | | | chomp; |
242 | | | next if $_ eq ""; |
243 | | | my ($var,$value,$comment)=parse_line($_); |
244 | | | push @{$config{$var}},$value if ($var ne ""); |
245 | | | } |
246 | | | close CONF; |
247 | | | } |
248 | | | } |
249 | | | |
250 | | | foreach (keys %config) { |
251 | | | my $type="array"; |
252 | | | if (defined $types{$_}) { $type=$types{$_}; } |
253 | | | if ( (index($_,"DEBUGLEVEL") == 0) or (index($_,"GRAPH_") == 0) or (index($_,"COLOR_") == 0) ) { |
254 | | | $type="var"; |
255 | | | if (index ($_,"GRAPH_RIGID") >=0 ) { $type="bool"; } |
256 | | | if (index ($_,"GRAPH_FORCE_LEGEND") >=0 ) { $type="bool"; } |
257 | | | if (index ($_,"GRAPH_ADDLINE") >=0 ) { $type="array"; } |
258 | | | } |
259 | | | if ($type eq "var") { $config{$_}=pop @{$config{$_}}; } |
260 | | | if ($type eq "bool") { |
261 | | | my $value=pop @{$config{$_}}; |
262 | | | if (index("1 on yes true",$value) >=0) { $config{$_}=1, } else { $config{$_}=0; } |
263 | | | } |
264 | | | } |
265 | | | |
266 | | | if (defined $config{GRAPH_FORBID_STYLE_LOG}) { $config{GRAPH_STYLE}="lin"; } |
267 | | | else { |
268 | | | if (! defined $config{GRAPH_STYLE}) { $config{GRAPH_STYLE}="lin"; } |
269 | | | elsif ($config{GRAPH_STYLE} =~ /log/) { $config{GRAPH_STYLE}="log"; } |
270 | | | else { $config{GRAPH_STYLE}="lin"; } |
271 | | | } |
272 | | | |
273 | | | # initialize undefined entries |
274 | | | # |
275 | | | foreach (keys %types) { |
276 | | | if (!defined $config{$_}) { |
277 | | | if ($types{$_} eq "array") { @{$config{$_}}=(); } |
278 | | | if ($types{$_} eq "var") { $config{$_}=""; } |
279 | | | if ($types{$_} eq "bool") { $config{$_}=0; } |
280 | | | } |
281 | | | } |
282 | | | return %config; |
283 | | | } |
284 | | | |
285 | | | |
286 | | | ###################################################################### |
287 | | | # |
288 | | | # sanity check config-item |
289 | | | # |
290 | | | # Usage: |
291 | | | # |
292 | | | # $value=check_config_item($item_name,$type,$default_value,$unit,$description,%config_hash); |
293 | | | # |
294 | | | # $type: |
295 | | | # "path" -> assume that value is a path -> check its existence. |
296 | | | # "var" -> assume that value is a variable -> check if the value is set correctly. |
297 | | | # |
298 | | | # if a fatal config-error is detected, the program dies with an error. |
299 | | | # |
300 | | | sub check_config_item { |
301 | | | my ($name,$type,$default,$unit,$description,%confhash)=@_; |
302 | | | |
303 | | | my $DAEMONDIR=$confhash{"DAEMONDIR"}; |
304 | | | my $var=$confhash{$name}; |
305 | | | |
306 | | | if (defined $var) { $var=~ s/\$DAEMONDIR/$DAEMONDIR/g; } |
307 | | | |
308 | | | if ($type eq "path") { |
309 | | | if ((! defined $var) || ($var eq "")) { |
310 | | | HotSaNIClog::warn("$name ($description) not configured."); |
311 | | | if ($default eq "") { die "no default value available.\n"; } |
312 | | | else { |
313 | | | HotSaNIClog::info("using default value: $default $unit"); |
314 | | | ($var=$default)=~ s/\$DAEMONDIR/$DAEMONDIR/g; |
315 | | | } |
316 | | | } |
317 | | | if ( ! -e $var) { |
318 | | | HotSaNIClog::warn("$name ($description) does not exist."); |
319 | | | HotSaNIClog::info("Trying to create path..."); |
320 | | | mkdir $var,0755; |
321 | | | } |
322 | | | } |
323 | | | |
324 | | | if ($type eq "var") { |
325 | | | if ((! defined $var) || ($var eq "")) { |
326 | | | HotSaNIClog::warn("$name ($description) not configured."); |
327 | | | if ($default eq "") { die "no default value available.\n"; } |
328 | | | else { |
329 | | | HotSaNIClog::info("using default value: $default $unit"); |
330 | | | $var=$default; |
331 | | | } |
332 | | | } |
333 | | | } |
334 | | | return $var; |
335 | | | } |
336 | | | |
337 | | | |
338 | | | ###################################################################### |
339 | | | # |
340 | | | # identify Kernel version |
341 | | | # |
342 | | | # Usage: |
343 | | | # |
344 | | | # $kernel=identify_kernel(); |
345 | | | # |
346 | | | sub identify_kernel { |
347 | | | require POSIX; |
348 | | | my ($ostype, $nodename, $release, $version, $machine) = POSIX::uname(); |
349 | | | my $kernel=2.0; |
350 | | | $kernel=2.2 if $release =~ /^2\.[12]\./; |
351 | | | $kernel=2.4 if $release =~ /^2\.[34]\./; |
352 | | | $kernel=2.6 if $release =~ /^2\.[56]\./; |
353 | | | return $kernel; |
354 | | | } |
355 | | | |
356 | | | |
357 | | | ###################################################################### |
358 | | | # |
359 | | | # identify OS type |
360 | | | # |
361 | | | # Usage: |
362 | | | # |
363 | | | # $ostype=identify_os_type(); |
364 | | | # |
365 | | | sub identify_os_type { |
366 | | | require POSIX; |
367 | | | my ($ostype, undef, undef, undef, undef) = POSIX::uname(); |
368 | | | return $ostype; |
369 | | | } |
370 | | | |
371 | | | |
372 | | | ###################################################################### |
373 | | | # |
374 | | | # convert - converts GB/MB/KB into Bytes |
375 | | | # |
376 | | | # Usage: |
377 | | | # |
378 | | | # @out=convert_units(@in); |
379 | | | # |
380 | | | # all values in @in will be checked against existance of a SI-multiplier |
381 | | | # (K, M or G) and the value stored in @out are multiplied accordingly. |
382 | | | # |
383 | | | sub convert_units { |
384 | | | my @in = @_; |
385 | | | my @out = (); |
386 | | | |
387 | | | foreach $i (@in) { |
388 | | | $i =~ s/(^\d+)K/$1 * 1024/; |
389 | | | $i =~ s/(^\d+)M/$1 * 1024 * 1024/; |
390 | | | $i =~ s/(^\d+)G/$1 * 1024 * 1024 * 1024/; |
391 | | | $bytes = (eval "$i"); |
392 | | | push @out, $bytes; |
393 | | | } |
394 | | | return @out; |
395 | | | } |
396 | | | |
397 | | | |
398 | | | ###################################################################### |
399 | | | # |
400 | | | # returns the PID of the running "read-data" or "rrdtimer" process |
401 | | | # |
402 | | | # Usage: |
403 | | | # $pid = get_pid(<path_to_pidfile>,<name>); |
404 | | | # |
405 | | | # if no parameters are given, the "running.pid" file in the current directory |
406 | | | # will be used as default. |
407 | | | # |
408 | | | # "name" is the name of the program to check for |
409 | | | # |
410 | | | # if no process can be found, "0" will be returned. |
411 | | | # |
412 | | | sub get_pid { |
413 | | | my $pidfile=shift || "./running.pid"; |
414 | | | my $name=shift || "read-data"; |
415 | | | my $PID=0; |
416 | | | |
417 | | | if (-e "$pidfile") { |
418 | | | open FILE,"$pidfile"; |
419 | | | $PID=<FILE> || ""; |
420 | | | close FILE; |
421 | | | chomp $PID; |
422 | | | |
423 | | | my $PROC_USABLE=0; |
424 | | | if ($^O ne "solaris") { |
425 | | | if (-d "/proc") { |
426 | | | opendir DIR,"/proc"; |
427 | | | my @files=grep /^[0-9]/,readdir DIR; |
428 | | | closedir DIR; |
429 | | | if (@files) { $PROC_USABLE="1"; } |
430 | | | } |
431 | | | } |
432 | | | |
433 | | | if ((defined $PID) && ($PID ne "")) { |
434 | | | if ($PROC_USABLE == 0) { |
435 | | | if (HotSaNIClog::check_debuglevel("MAIN_VERBOSE,MODULE_VERBOSE")) { |
436 | | | HotSaNIClog::warn("/proc filesystem not available"); |
437 | | | HotSaNIClog::info("falling back to \"ps\""); |
438 | | | } |
439 | | | my $line=""; |
440 | | | open RESULT,"ps -aef|"; |
441 | | | while (<RESULT>) { $line=$_ if ((/$PID/) && (/$name/)) } |
442 | | | close RESULT; |
443 | | | if ($line eq "") { $PID=0; } |
444 | | | } |
445 | | | else { |
446 | | | if (-d "/proc/$PID") { |
447 | | | $delta=0; |
448 | | | open FILE,"/proc/$PID/cmdline"; |
449 | | | $line=<FILE> || ""; |
450 | | | close FILE; |
451 | | | if (index($line,$name) < 0) { $PID=0; } |
452 | | | } |
453 | | | else { $PID=0; } |
454 | | | } |
455 | | | } |
456 | | | else { $PID=0; } |
457 | | | } |
458 | | | # make sure PID dosn't belong to a vial system process |
459 | | | if ($PID <= 20) { $PID=0; } |
460 | | | return $PID; |
461 | | | } |
462 | | | |
463 | | | |
464 | | | ###################################################################### |
465 | | | # |
466 | | | # make a backup of the given file to the "./backup" directory |
467 | | | # |
468 | | | # Usage: |
469 | | | # |
470 | | | # backup_file($filename,$subdir); |
471 | | | # |
472 | | | # if $subdir is omitted, the current dir "./" will be used. |
473 | | | # |
474 | | | sub backup_file { |
475 | | | my $file=shift; |
476 | | | my $subdir=shift || "."; |
477 | | | mkdir "backup",0755 if ! -e "backup"; |
478 | | | my ($sec,$min,$hour,$mday,$mon,$year,undef,undef,undef) = localtime(time); |
479 | | | $year+=1900; |
480 | | | $mon++; |
481 | | | $NOW=sprintf"%i%02i%02i-%02i%02i%02i",$year,$mon,$mday,$hour,$min,$sec; |
482 | | | rename "$subdir/$file","backup/$file-$NOW"; |
483 | | | HotSaNIClog::info("a backup of \"$subdir/$file\" has been saved as \"./backup/$file-$NOW\""); |
484 | | | } |
485 | | | |
486 | | | |
487 | | | ###################################################################### |
488 | | | # |
489 | | | # calls syscmd: locate and returns a list of files that match the pattern |
490 | | | # |
491 | | | # Usage: |
492 | | | # |
493 | | | # locate_file($pattern); |
494 | | | # |
495 | | | sub locate_files { |
496 | | | my $pattern=shift; |
497 | | | my @results; |
498 | | | open LOCATERESULT,"locate $pattern|" || HotSaNIClog::error("could not run \"locate $pattern\""); |
499 | | | @results=(<LOCATERESULT>); |
500 | | | close LOCATERESULT; |
501 | | | return @results; |
502 | | | } |
503 | | | |
504 | | | |
505 | | | ###################################################################### |
506 | | | # |
507 | | | # return all selected entries found in the given config-array |
508 | | | # if no entry is found, and the default valus is present, create one |
509 | | | # |
510 | | | # the generated string can directly be printed into a settings file |
511 | | | # |
512 | | | # Usage: |
513 | | | # |
514 | | | # $text=get_items($pattern,$default_value,@config); |
515 | | | # |
516 | | | sub get_items { |
517 | | | my $pattern=shift; |
518 | | | my $default=shift; |
519 | | | my @config=@_; |
520 | | | |
521 | | | undef my @results; |
522 | | | foreach (@config) { |
523 | | | push @results,"$_\n" if /^$pattern *=/; |
524 | | | } |
525 | | | if ( (!@results) and ($default ne "") ) { |
526 | | | push @results,"$pattern=\"$default\"\n"; |
527 | | | } |
528 | | | return join ("",@results); |
529 | | | } |
530 | | | |
531 | | | 1; |
532 | | | |