| 1 | 1 | simandl | # |
| 2 | | | # $Id: HotSaNICparser.pm,v 1.24 2004/02/08 16:51:55 bernisys Exp $ |
| 3 | | | # |
| 4 | | | |
| 5 | | | package HotSaNICparser; |
| 6 | | | |
| 7 | | | ($VERSION = '$Revision: 1.24 $') =~ s/.*(\d+\.\d+).*/$1/; |
| 8 | | | |
| 9 | | | ###################################################################### |
| 10 | | | # |
| 11 | | | # evaluate name of module by its subdir |
| 12 | | | # |
| 13 | | | # Usage: |
| 14 | | | # |
| 15 | | | # $modname=get_module_name(); |
| 16 | | | # |
| 17 | | | sub get_module_name { |
| 18 | | | require Cwd; |
| 19 | | | my $dir=uc Cwd::cwd(); |
| 20 | | | my @array=split /MODULES\//,$dir; |
| 21 | | | my $name=pop @array; |
| 22 | | | if (!defined $name) { $name="UNKNOWN ($dir)"; } |
| 23 | | | return $name; |
| 24 | | | } |
| 25 | | | |
| 26 | | | |
| 27 | | | ###################################################################### |
| 28 | | | # |
| 29 | | | # scan given directory for modules |
| 30 | | | # |
| 31 | | | # this function automatically weeds out all "." ".." and "CVS" entries |
| 32 | | | # |
| 33 | | | # Usage: |
| 34 | | | # |
| 35 | | | # @modules=scan_for_modules(<directory>,<debuglevel>,<FILTER>); |
| 36 | | | # |
| 37 | | | # <directory> |
| 38 | | | # |
| 39 | | | # <debuglevel> 1: function introduces itself |
| 40 | | | # >1: list all activity |
| 41 | | | # |
| 42 | | | # <FILTER> a space-separated list of modules that should be |
| 43 | | | # inserted if found or "*" for all found modules |
| 44 | | | # |
| 45 | | | sub scan_for_modules { |
| 46 | | | my ($directory,$debuglevel,$FILTER)=@_; |
| 47 | | | undef my @modules; |
| 48 | | | |
| 49 | | | if ($debuglevel > 0) { print time,": scanning $directory for modules.\n"; } |
| 50 | | | |
| 51 | | | opendir DIR,$directory; |
| 52 | | | my @mods=sort(readdir DIR); |
| 53 | | | closedir DIR; |
| 54 | | | |
| 55 | | | # weed out non-module entries |
| 56 | | | foreach (@mods) { |
| 57 | | | if ($_ eq ".") { next; } |
| 58 | | | if ($_ eq "..") { next; } |
| 59 | | | if ($_ eq "CVS") { next; } |
| 60 | | | if ( -d "$directory/$_") { |
| 61 | | | if ( ! -e "$directory/$_/rrd" ) { mkdir "$directory/$_/rrd",0755; } |
| 62 | | | if ($FILTER =~ /\*|(^| )$_( |$)/) { |
| 63 | | | push @modules,$_; |
| 64 | | | if ( $debuglevel > 1 ) {print " ",uc $_;} |
| 65 | | | } |
| 66 | | | } |
| 67 | | | } |
| 68 | | | if ( $debuglevel > 1 ) {print "\n";} |
| 69 | | | return @modules; |
| 70 | | | } |
| 71 | | | |
| 72 | | | |
| 73 | | | ###################################################################### |
| 74 | | | # |
| 75 | | | # strip unwanted chars like " and multiple spaces at end / beginning |
| 76 | | | # of each element and return the processed array. |
| 77 | | | # |
| 78 | | | # Usage: |
| 79 | | | # |
| 80 | | | # $stripped_string=strip_unwanted($raw_string); |
| 81 | | | # |
| 82 | | | sub strip_unwanted { |
| 83 | | | foreach (@_) { |
| 84 | | | $_ =~ s/ *= */=/g; # spaces near a = |
| 85 | | | $_ =~ s/\"//g; # " chars |
| 86 | | | $_ =~ s/ +/ /g; # multiple spaces to single |
| 87 | | | $_ =~ s/^ *//g; # spaces at beginning |
| 88 | | | $_ =~ s/ *$//g; # spaces at end |
| 89 | | | } |
| 90 | | | return @_; |
| 91 | | | } |
| 92 | | | |
| 93 | | | |
| 94 | | | ###################################################################### |
| 95 | | | # |
| 96 | | | # Parse a line, strip all unwanted chars, split it by "=" into |
| 97 | | | # a VAR and a VALUE part and make sure those parts are defined. |
| 98 | | | # |
| 99 | | | # Usage: |
| 100 | | | # |
| 101 | | | # ($var,$value,$comment)=parse_line($settings_line); |
| 102 | | | # |
| 103 | | | sub parse_line { |
| 104 | | | my ($line)=@_; |
| 105 | | | chomp $line; |
| 106 | | | |
| 107 | | | # remove leading spaces |
| 108 | | | $line =~ s/^ *//g; |
| 109 | | | |
| 110 | | | # weed out comments (i.e. everything following a "#" |
| 111 | | | my ($important,$comment)=split /#/,$line,2; |
| 112 | | | if ((! defined $comment) || ( $comment eq "" )) { |
| 113 | | | if (index($line,"#")>=0) { $comment=" "; } |
| 114 | | | else { $comment=""; } |
| 115 | | | } |
| 116 | | | if ((! defined $important) || ($important eq "")) { return ("","",$comment); } |
| 117 | | | |
| 118 | | | # trim the edges ;) |
| 119 | | | ($important)=strip_unwanted($important); |
| 120 | | | |
| 121 | | | # separate the variable name from its assigned value |
| 122 | | | my ($var,$value)=split /=/,$important,2; |
| 123 | | | |
| 124 | | | # avoid undefined variables |
| 125 | | | $var="" if (! defined $var); |
| 126 | | | $value="" if (! defined $value); |
| 127 | | | |
| 128 | | | # force varnames to be upcase |
| 129 | | | $var=uc $var; |
| 130 | | | |
| 131 | | | return ($var,$value,$comment) |
| 132 | | | } |
| 133 | | | |
| 134 | | | |
| 135 | | | ###################################################################### |
| 136 | | | # |
| 137 | | | # evaluate settings file and check config... |
| 138 | | | # |
| 139 | | | # Usage: |
| 140 | | | # |
| 141 | | | # %config=get_config($path_to_settings_file); |
| 142 | | | # |
| 143 | | | sub get_config { |
| 144 | | | my ($location)=@_; |
| 145 | | | my ($var,$value); |
| 146 | | | my %config; |
| 147 | | | |
| 148 | | | my @lines=read_settings($location); |
| 149 | | | foreach (@lines) { |
| 150 | | | ($var,$value)=parse_line($_); |
| 151 | | | $config{$var}=$value; |
| 152 | | | } |
| 153 | | | |
| 154 | | | # sanity check for all config variables |
| 155 | | | # |
| 156 | | | $config{"BINPATH"}=check_config_item("BINPATH","path","","","path to \"rrdtool\"",%config); |
| 157 | | | $config{"DAEMONDIR"}=check_config_item("DAEMONDIR","path","","","path to \"HotSaNIC\"",%config); |
| 158 | | | $config{"WEBDIR"}=check_config_item("WEBDIR","path","","","path to HotSaNIC's output directory",%config); |
| 159 | | | $config{"VARDIR"}=check_config_item("VARDIR","path","\$DAEMONDIR/var","","path to HotSaNIC's logfiles",%config); |
| 160 | | | $config{"LOGDIR"}=check_config_item("LOGDIR","path","\$DAEMONDIR/var/log","","path to HotSaNIC's logfiles",%config); |
| 161 | | | $config{"PIDFILE"}=check_config_item("PIDFILE","var","\$DAEMONDIR/log/rrdtimer.pid","","path to HotSaNIC's PID file",%config); |
| 162 | | | $config{"DTIME"}=60*check_config_item("DTIME","var","15","minutes","diagram rebuild time",%config); |
| 163 | | | $config{"CTIME"}=3600*check_config_item("CTIME","var","12","hours","diagram conversion time",%config); |
| 164 | | | $config{"STIME"}=check_config_item("STIME","var","120","seconds","module scan time",%config); |
| 165 | | | $config{"LOGSIZE"}=check_config_item("LOGSIZE","var","500000","bytes","max. logfile size",%config); |
| 166 | | | $config{"LOGBACKUPS"}=check_config_item("LOGBACKUPS","var","4","","logfiles to keep as backups",%config); |
| 167 | | | $config{"IMAGEFORMAT"}=check_config_item("IMAGEFORMAT","var","gif","","format of generated pictures",%config); |
| 168 | | | $config{"ORDER"}=check_config_item("ORDER","var","","","order of modules",%config); |
| 169 | | | $config{"DEBUGLEVEL"}=check_config_item("DEBUGLEVEL","var","-1","","debug level",%config); |
| 170 | | | $config{"SCHEDULE_MIN"}=check_config_item("SCHEDULE_MIN","var","50","","minimum scheduling interval",%config); |
| 171 | | | $config{"SCHEDULE_MAX"}=check_config_item("SCHEDULE_MAX","var","100","","maximum scheduling interval",%config); |
| 172 | | | |
| 173 | | | return %config; |
| 174 | | | } |
| 175 | | | |
| 176 | | | |
| 177 | | | ###################################################################### |
| 178 | | | # |
| 179 | | | # read settings-file and strip comments and empty entries |
| 180 | | | # |
| 181 | | | # Usage: |
| 182 | | | # |
| 183 | | | # @parsed_config=read_settings($path_to_settings_file); |
| 184 | | | # |
| 185 | | | # returns just the lines actually containing data - stripped by comments. |
| 186 | | | # |
| 187 | | | sub read_settings { |
| 188 | | | my $location=shift; |
| 189 | | | my @config; |
| 190 | | | if ( (! defined $location) || ($location eq "")) { $location="."; } |
| 191 | | | if (-e "$location/settings") { |
| 192 | | | push @config,"settings file exisits. (Dummy added by HotSaNICparser.pm::read_settings)"; |
| 193 | | | open CONF,"$location/settings"; |
| 194 | | | while (<CONF>) { |
| 195 | | | chomp; |
| 196 | | | next if $_ eq ""; |
| 197 | | | my ($important,$comment)=split /#/; |
| 198 | | | if ((defined $important) && ( index($important,"=") >= 0)) { |
| 199 | | | push @config,$important |
| 200 | | | } |
| 201 | | | } |
| 202 | | | } |
| 203 | | | close CONF; |
| 204 | | | |
| 205 | | | return @config; |
| 206 | | | } |
| 207 | | | |
| 208 | | | |
| 209 | | | ###################################################################### |
| 210 | | | # |
| 211 | | | # sanity check config-item |
| 212 | | | # |
| 213 | | | # Usage: |
| 214 | | | # |
| 215 | | | # $value=check_config_item($item_name,$type,$default_value,$unit,$description,%config_hash); |
| 216 | | | # |
| 217 | | | # $type: |
| 218 | | | # "path" -> assume that value is a path -> check its existence. |
| 219 | | | # "var" -> assume that value is a variable -> check if the value is set correctly. |
| 220 | | | # |
| 221 | | | # if a fatal config-error is detected, the program dies with an error. |
| 222 | | | # |
| 223 | | | sub check_config_item { |
| 224 | | | my ($name,$type,$default,$unit,$description,%confhash)=@_; |
| 225 | | | |
| 226 | | | my $DAEMONDIR=$confhash{"DAEMONDIR"}; |
| 227 | | | my $var=$confhash{$name}; |
| 228 | | | |
| 229 | | | if (defined $var) { $var=~ s/\$DAEMONDIR/$DAEMONDIR/g; } |
| 230 | | | |
| 231 | | | if ($type eq "path") { |
| 232 | | | if ((! defined $var) || ($var eq "")) { |
| 233 | | | print "$name ($description) not configured.\n"; |
| 234 | | | if ($default eq "") { die "no default value given.\n"; } |
| 235 | | | else { print "using default value: $default $unit\n"; ($var=$default)=~ s/\$DAEMONDIR/$DAEMONDIR/g; } |
| 236 | | | } |
| 237 | | | if ( ! -e $var) { |
| 238 | | | print "$name ($description) does not exist.\n Trying to create path...\n"; } |
| 239 | | | mkdir $var,0755; |
| 240 | | | } |
| 241 | | | |
| 242 | | | if ($type eq "var") { |
| 243 | | | if ((! defined $var) || ($var eq "")) { |
| 244 | | | print "$name ($description) not configured.\n"; |
| 245 | | | if ($default eq "") { die "no default value given.\n"; } |
| 246 | | | else { print "using default value: $default $unit\n"; $var=$default; } |
| 247 | | | } |
| 248 | | | } |
| 249 | | | return $var; |
| 250 | | | } |
| 251 | | | |
| 252 | | | |
| 253 | | | ###################################################################### |
| 254 | | | # |
| 255 | | | # identify Kernel version |
| 256 | | | # |
| 257 | | | # Usage: |
| 258 | | | # |
| 259 | | | # $kernel=identify_kernel(); |
| 260 | | | # |
| 261 | | | sub identify_kernel { |
| 262 | | | require POSIX; |
| 263 | | | my ($ostype, $nodename, $release, $version, $machine) = POSIX::uname(); |
| 264 | | | my $kernel=2.0; |
| 265 | | | $kernel=2.2 if $release =~ /^2\.[12]\./; |
| 266 | | | $kernel=2.4 if $release =~ /^2\.[34]\./; |
| 267 | | | $kernel=2.6 if $release =~ /^2\.[56]\./; |
| 268 | | | return $kernel; |
| 269 | | | } |
| 270 | | | |
| 271 | | | |
| 272 | | | ###################################################################### |
| 273 | | | # |
| 274 | | | # identify OS type |
| 275 | | | # |
| 276 | | | # Usage: |
| 277 | | | # |
| 278 | | | # $ostype=identify_os_type(); |
| 279 | | | # |
| 280 | | | sub identify_os_type { |
| 281 | | | require POSIX; |
| 282 | | | my ($ostype, undef, undef, undef, undef) = POSIX::uname(); |
| 283 | | | return $ostype; |
| 284 | | | } |
| 285 | | | |
| 286 | | | |
| 287 | | | ###################################################################### |
| 288 | | | # |
| 289 | | | # convert - converts GB/MB/KB into Bytes |
| 290 | | | # |
| 291 | | | # Usage: |
| 292 | | | # |
| 293 | | | # @out=convert_units(@in); |
| 294 | | | # |
| 295 | | | # all values in @in will be checked against existance of a SI-multiplier |
| 296 | | | # (K, M or G) and the value stored in @out are multiplied accordingly. |
| 297 | | | # |
| 298 | | | sub convert_units { |
| 299 | | | my @in = @_; |
| 300 | | | my @out = (); |
| 301 | | | |
| 302 | | | foreach $i (@in) { |
| 303 | | | $i =~ s/(^\d+)K/$1 * 1024/; |
| 304 | | | $i =~ s/(^\d+)M/$1 * 1024 * 1024/; |
| 305 | | | $i =~ s/(^\d+)G/$1 * 1024 * 1024 * 1024/; |
| 306 | | | #print "$i\n"; |
| 307 | | | $bytes = (eval "$i"); |
| 308 | | | #print "$bytes\n"; |
| 309 | | | push @out, $bytes; |
| 310 | | | } |
| 311 | | | return @out; |
| 312 | | | } |
| 313 | | | |
| 314 | | | |
| 315 | | | ###################################################################### |
| 316 | | | # |
| 317 | | | # returns the PID of the running "read-data" or "rrdtimer" process |
| 318 | | | # |
| 319 | | | # Usage: |
| 320 | | | # $pid = get_pid(<debuglevel>,<path_to_pidfile>); |
| 321 | | | # |
| 322 | | | # |
| 323 | | | # if no parameters are given, the "running.pid" file in the current directory |
| 324 | | | # will be used as default. |
| 325 | | | # |
| 326 | | | # if no process can be found, "0" will be returned. |
| 327 | | | # |
| 328 | | | sub get_pid { |
| 329 | | | my $debug=shift || -1; |
| 330 | | | my $pidfile=shift || "./running.pid"; |
| 331 | | | my $what=shift || "read-data"; |
| 332 | | | my $PID=0; |
| 333 | | | |
| 334 | | | if (-e "$pidfile") { |
| 335 | | | open FILE,"$pidfile"; |
| 336 | | | $PID=<FILE> || ""; |
| 337 | | | close FILE; |
| 338 | | | chomp $PID; |
| 339 | | | |
| 340 | | | if ((defined $PID) && ($PID ne "")) { |
| 341 | | | if ($^O eq "solaris") { |
| 342 | | | # @lines=grep /$PID/,grep /$what/,`ps -aef`; |
| 343 | | | my $line=""; |
| 344 | | | open RESULT,"ps -aef|"; |
| 345 | | | while (<RESULT>) { $line=$_ if ((/$PID/) && (/$what/)) } |
| 346 | | | close RESULT; |
| 347 | | | if ($line eq "") { $PID=0; } |
| 348 | | | } |
| 349 | | | else { |
| 350 | | | if (-e "/proc/$PID") { |
| 351 | | | $delta=0; |
| 352 | | | open FILE,"/proc/$PID/cmdline"; |
| 353 | | | $line=<FILE> || ""; |
| 354 | | | close FILE; |
| 355 | | | if (index($line,$what) < 0) { $PID=0; } |
| 356 | | | } |
| 357 | | | else { $PID=0; } |
| 358 | | | } |
| 359 | | | } |
| 360 | | | else { $PID=0; } |
| 361 | | | } |
| 362 | | | # make sure PID dosn't belong to a vial system process |
| 363 | | | if ($PID <= 20) { $PID=0; } |
| 364 | | | return $PID; |
| 365 | | | } |
| 366 | | | |
| 367 | | | |
| 368 | | | ###################################################################### |
| 369 | | | # |
| 370 | | | # make a backup of the given file to the "./backup" directory |
| 371 | | | # |
| 372 | | | # Usage: |
| 373 | | | # |
| 374 | | | # backup_file($filename,$subdir); |
| 375 | | | # |
| 376 | | | # if $subdir is omitted, the current dir "./" will be used. |
| 377 | | | # |
| 378 | | | sub backup_file { |
| 379 | | | my $file=shift; |
| 380 | | | my $subdir=shift || "."; |
| 381 | | | mkdir "backup",0755 if ! -e "backup"; |
| 382 | | | my ($sec,$min,$hour,$mday,$mon,$year,undef,undef,undef) = localtime(time); |
| 383 | | | $year+=1900; |
| 384 | | | $mon++; |
| 385 | | | $NOW=sprintf"%i%02i%02i-%02i%02i%02i",$year,$mon,$mday,$hour,$min,$sec; |
| 386 | | | rename "$subdir/$file","backup/$file-$NOW"; |
| 387 | | | print "a backup of \"$subdir/$file\" has been saved as \"./backup/$file-$NOW\".\n\n"; |
| 388 | | | } |
| 389 | | | |
| 390 | | | |
| 391 | | | ###################################################################### |
| 392 | | | # |
| 393 | | | # calls syscmd: locate and returns a list of files that match the pattern |
| 394 | | | # |
| 395 | | | # Usage: |
| 396 | | | # |
| 397 | | | # locate_file($pattern); |
| 398 | | | # |
| 399 | | | sub locate_files { |
| 400 | | | my $pattern=shift; |
| 401 | | | my @results; |
| 402 | | | open LOCATERESULT,"locate $pattern|" || print "could not run \"locate $pattern\n\""; |
| 403 | | | @results=(<LOCATERESULT>); |
| 404 | | | close LOCATERESULT; |
| 405 | | | return @results; |
| 406 | | | } |
| 407 | | | |
| 408 | | | 1; |
| 409 | | | |