|
10 | 10 | use DBI; |
11 | 11 | use Mail::Sendmail; |
12 | 12 | use Class::Struct; |
| 13 | +use Config::IniFiles; |
13 | 14 |
|
14 | 15 | # Flush I/O frequently |
15 | 16 | $| = 1; |
|
18 | 19 | my $debug; |
19 | 20 | my $debug_no_email; |
20 | 21 |
|
21 | | -#my $to_email_address = "FILL THIS IN"; |
22 | | -#my $from_email_address = "FILL THIS IN"; |
23 | | -my $to_email_address = "mtt-devel-core\@lists.open-mpi.org"; |
24 | | -my $from_email_address = "mtt-devel-core\@lists.open-mpi.org"; |
| 22 | +my $config_filename = "config.ini"; |
| 23 | +my $ini_section; |
| 24 | +my $ini = new Config::IniFiles(-file => $config_filename, |
| 25 | + -nocase => 1, |
| 26 | + -allowcontinue => 1); |
| 27 | +if( !$ini ) { |
| 28 | + print "Error: Failed to read: $config_filename\n"; |
| 29 | + exit 1; |
| 30 | +} |
| 31 | +# Check the contents of the config file |
| 32 | +check_ini_section($ini, "database", ("user", "password", "hostname", "port", "dbname") ); |
| 33 | +check_ini_section($ini, "reporting", ("to_email", "from_email") ); |
| 34 | + |
| 35 | +# Read in config entries |
| 36 | +$ini_section = "database"; |
| 37 | +my $mtt_user = resolve_value($ini, $ini_section, "user");; |
| 38 | +my $mtt_pass = resolve_value($ini, $ini_section, "password"); |
| 39 | +my $mtt_hostname = resolve_value($ini, $ini_section, "hostname"); |
| 40 | +my $mtt_port = resolve_value($ini, $ini_section, "port"); |
| 41 | +my $mtt_dbname = resolve_value($ini, $ini_section, "dbname"); |
| 42 | + |
| 43 | +$ini_section = "reporting"; |
| 44 | +my $to_email_address = resolve_value($ini, $ini_section, "to_email"); |
| 45 | +my $from_email_address = resolve_value($ini, $ini_section, "from_email"); |
| 46 | + |
| 47 | +# Static values |
25 | 48 | my $current_mail_subject = "MTT Database Maintenance: IC Check"; |
26 | 49 | my $current_mail_header = ""; |
27 | 50 | my $current_mail_body = ""; |
|
210 | 233 | } |
211 | 234 |
|
212 | 235 | sub connect_db() { |
213 | | - my $mtt_user = "mtt"; |
214 | 236 | my $stmt; |
215 | 237 |
|
216 | | - $dbh_mtt = DBI->connect("dbi:Pg:dbname=mtt", $mtt_user); |
| 238 | + $dbh_mtt = DBI->connect("dbi:Pg:dbname=".$mtt_dbname.";host=".$mtt_hostname.";port=".$mtt_port, $mtt_user, $mtt_pass); |
217 | 239 |
|
218 | 240 | $stmt = $dbh_mtt->prepare("set sort_mem = '512MB'"); |
219 | 241 | $stmt->execute(); |
|
321 | 343 |
|
322 | 344 | return 0; |
323 | 345 | } |
| 346 | + |
| 347 | +sub resolve_value() { |
| 348 | + my $ini = shift(@_); |
| 349 | + my $section = shift(@_); |
| 350 | + my $key = shift(@_); |
| 351 | + my $value; |
| 352 | + |
| 353 | + $value = $ini->val($section, $key); |
| 354 | + if( !defined($value) ) { |
| 355 | + print "Error: Failed to find \"$key\" in section \"$section\"\n"; |
| 356 | + exit 1; |
| 357 | + } |
| 358 | + $value =~ s/^\"//; |
| 359 | + $value =~ s/\"$//; |
| 360 | + |
| 361 | + if( $value =~ /^run/ ) { |
| 362 | + $value = $'; |
| 363 | + $value =~ s/^\(//; |
| 364 | + $value =~ s/\)$//; |
| 365 | + $value = `$value`; |
| 366 | + chomp($value); |
| 367 | + } |
| 368 | + |
| 369 | + return $value; |
| 370 | +} |
| 371 | + |
| 372 | +sub check_ini_section() { |
| 373 | + my $ini = shift(@_); |
| 374 | + my $section = shift(@_); |
| 375 | + my @keys = @_; |
| 376 | + |
| 377 | + if( !$ini->SectionExists($section) ) { |
| 378 | + print "Error: INI file does not contain a $section field\n"; |
| 379 | + exit 1; |
| 380 | + } |
| 381 | + |
| 382 | + foreach my $key (@keys) { |
| 383 | + if( !$ini->exists($section, $key) ) { |
| 384 | + print "Error: INI file missing $section key named $key\n"; |
| 385 | + exit 1; |
| 386 | + } |
| 387 | + } |
| 388 | + |
| 389 | + return 0; |
| 390 | +} |
0 commit comments