[RT17143] Syntax errors fixed
[sitka/sitka-tools.git] / data_cleanup / date1 / date1_cleanup.pl
CommitLineData
69ed3052
LW
1#!/usr/bin/perl
2# vim:et:ts=4:
3use strict;
4use warnings;
5
6use DBI;
7use OpenSRF::Utils::Config;
8use Getopt::Long;
9use Net::Domain;
0f46c84e
LW
10use XML::LibXML;
11use XML::LibXML::XPathContext;
12
13my $output = '';
69ed3052
LW
14
15my ($gather, $hostname, $core_config, $tmpdir) =
16 (0, Net::Domain::hostfqdn(), '/srv/openils/conf/opensrf_core.xml', '/tmp/');
17
18GetOptions(
19 'gather' => \$gather,
20 'hostname=s' => \$hostname,
21 'config_file=s' => \$core_config,
22 'tempdir=s' => \$tmpdir,
23);
24
25(my $conf_dir = $core_config) =~ s#(.*)/.*#$1#;
26OpenSRF::Utils::Config->load(config_file => $core_config);
27my $conf = OpenSRF::Utils::Config->current;
28my $settings_config = $conf->bootstrap->settings_config;
29
30my $xmlparser = XML::LibXML->new();
31my $confxml = $xmlparser->parse_file($core_config);
32my $confxpc = XML::LibXML::XPathContext->new($confxml);
33my $osrfxml = $xmlparser->parse_file($settings_config);
34
0f46c84e
LW
35check_all_database_connections();
36
69ed3052
LW
37sub check_all_database_connections {
38 print "\nChecking database connections\n";
39 # Check database connections
40 my @databases = $osrfxml->findnodes('//database');
41
42 # If we have no database connections, this is probably the OpenSRF version
43 # of opensrf.xml
44 if (!@databases) {
45 my $de = "* WARNING: There are no database connections defined in " .
46 "opensrf.xml. These are defined in services such as " .
47 "open-ils.cstore and open-ils.reporter. Please ensure that " .
48 "your opensrf_core.xml and opensrf.xml configuration files " .
49 "are based on the examples shipped with Evergreen instead of " .
50 "OpenSRF.\n";
51 $output .= $de;
52 warn $de;
53 }
54
55 foreach my $database (@databases) {
0f46c84e 56 unless ($database->parentNode->parentNode->localname eq 'open-ils.cstore') {
69ed3052
LW
57 next;
58 }
59
60 my $db_name = $database->findvalue("./db");
61 if (!$db_name) {
62 $db_name = $database->findvalue("./name");
63 }
64 my $db_host = $database->findvalue("./host");
65 my $db_port = $database->findvalue("./port");
66 my $db_user = $database->findvalue("./user");
67 my $db_pw = $database->findvalue("./pw");
68
69 my $osrf_xpath;
70 foreach my $node ($database->findnodes("ancestor::node()")) {
71 next unless $node->nodeType == XML::LibXML::XML_ELEMENT_NODE;
72 $osrf_xpath .= "/" . $node->nodeName;
73 }
74 $output .= test_db_connect($db_name, $db_host, $db_port, $db_user, $db_pw, $osrf_xpath);
75 }
76}
77
78sub test_db_connect {
79 my ($db_name, $db_host, $db_port, $db_user, $db_pw, $osrf_xpath) = @_;
80
81 my $dsn = "dbi:Pg:dbname=$db_name;host=$db_host;port=$db_port";
82 my $de = undef;
83 my ($dbh, $encoding, $langs);
84 $dbh = DBI->connect($dsn, $db_user, $db_pw);
85
86 # Short-circuit if we didn't connect successfully
87 unless($dbh) {
88 $de = "* $osrf_xpath :: Unable to connect to database $dsn, user=$db_user, password=$db_pw\n";
89 warn "* $osrf_xpath :: Unable to connect to database $dsn, user=$db_user, password=$db_pw\n";
90 return $de;
91 }
92
93 $dbh->disconnect;
94 print "* $osrf_xpath :: Successfully connected to database $dsn\n" unless ($de);
95}