| 68 | rodolico | 1 | #!/usr/bin/env perl
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | use strict;
 | 
        
           |  |  | 4 | use warnings;
 | 
        
           |  |  | 5 |   | 
        
           |  |  | 6 | #
 | 
        
           |  |  | 7 | # v0.8.1 20160218 RWR
 | 
        
           |  |  | 8 | # removed print statement to STDOUT (just commented it out)
 | 
        
           |  |  | 9 | #
 | 
        
           |  |  | 10 | # v0.8.2 20160407 RWR
 | 
        
           |  |  | 11 | # Added checking for missing sysinfo reports
 | 
        
           |  |  | 12 | # if a report is not shown in current day, but has been seen in the 
 | 
        
           |  |  | 13 | # past five days, an entry is made on the report
 | 
        
           |  |  | 14 |   | 
        
           |  |  | 15 | use DBI;
 | 
        
           |  |  | 16 | use Cwd 'abs_path';
 | 
        
           |  |  | 17 | #use Data::Dumper;
 | 
        
           |  |  | 18 | use File::Basename;
 | 
        
           |  |  | 19 | use YAML::XS;
 | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 |   | 
        
           |  |  | 22 | my $VERSION = '0.8.1';
 | 
        
           |  |  | 23 | my $MY_DIRECTORY = abs_path(dirname(__FILE__) );
 | 
        
           |  |  | 24 | my $query = qq/select 
 | 
        
           |  |  | 25 |    hyper.name DOM0,
 | 
        
           |  |  | 26 |    virt.name DOMU
 | 
        
           |  |  | 27 | from 
 | 
        
           |  |  | 28 |    device hyper join device virt
 | 
        
           |  |  | 29 |    join device_type on (virt.device_type_id = device_type.device_type_id)
 | 
        
           |  |  | 30 | where 
 | 
        
           |  |  | 31 |    device_type.show_as_system = "Y"
 | 
        
           |  |  | 32 |    and hyper.device_id = virt.part_of 
 | 
        
           |  |  | 33 |    and hyper.removed_date is null 
 | 
        
           |  |  | 34 |    and virt.removed_date is null
 | 
        
           |  |  | 35 | order by 
 | 
        
           |  |  | 36 |    hyper.name,
 | 
        
           |  |  | 37 |    virt.name/;
 | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 |   | 
        
           |  |  | 40 | # following are used to find the configuration file
 | 
        
           |  |  | 41 | my $confFileName = "sysinfoRead.conf.yaml";
 | 
        
           |  |  | 42 | my @searchPaths = ( '/etc/camp', '/opt/camp', '/opt/camp/sysinfo', $MY_DIRECTORY );
 | 
        
           |  |  | 43 |   | 
        
           |  |  | 44 |   | 
        
           |  |  | 45 | sub loadConfig {
 | 
        
           |  |  | 46 |    my ( $confFileName, @searchPaths ) = @_;
 | 
        
           |  |  | 47 |    my $configuration;
 | 
        
           |  |  | 48 |    for ( my $i = 0; $i < @searchPaths; $i++ ) {
 | 
        
           |  |  | 49 |       my $filename = $searchPaths[$i] . '/' . $confFileName;
 | 
        
           |  |  | 50 |       if ( -e $filename ) {
 | 
        
           |  |  | 51 |          #print "Found $filename\n";
 | 
        
           |  |  | 52 |          open CONF, "<$filename" or warn "Could not read $filename: $!\n";
 | 
        
           |  |  | 53 |          $configuration = Load( join( '', <CONF> ) );
 | 
        
           |  |  | 54 |          close CONF;
 | 
        
           |  |  | 55 |          last; # exit out of the loop; we don't try to load it more than once
 | 
        
           |  |  | 56 |       } # if
 | 
        
           |  |  | 57 |    } # foreach
 | 
        
           |  |  | 58 |    return $configuration;
 | 
        
           |  |  | 59 | } # sub loadConfig
 | 
        
           |  |  | 60 |   | 
        
           |  |  | 61 | my $configuration = &loadConfig( $confFileName, @searchPaths );
 | 
        
           |  |  | 62 | die "Could not find configuration file $confFileName in " . join( ', ', @searchPaths) . "\n" unless $configuration;
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 | my $dbh = DBI->connect('DBI:mysql:' . $$configuration{'database'}{'database'},
 | 
        
           |  |  | 65 |                        $$configuration{'database'}{'databaseUsername'}, 
 | 
        
           |  |  | 66 |                        $$configuration{'database'}{'databasePassword'} )
 | 
        
           |  |  | 67 |          || die "Could not connect to database: $DBI::errstr";
 | 
        
           |  |  | 68 |   | 
        
           |  |  | 69 | my $sth = $dbh->prepare( $query );
 | 
        
           |  |  | 70 | $sth->execute();
 | 
        
           |  |  | 71 |   | 
        
           |  |  | 72 | open REPORT, ">$$configuration{'domuOutput'}" or die "Could not open $$configuration{'domuOutput'} for DOMU Report: $!\n";
 | 
        
           | 74 | rodolico | 73 | print REPORT "DOM0\tDOMU\n";
 | 
        
           | 68 | rodolico | 74 | while ( my $row =  $sth->fetchrow_hashref() ) {
 | 
        
           | 74 | rodolico | 75 |    print REPORT $row->{"DOM0"} . "\t" . $row->{"DOMU"} . "\n";
 | 
        
           | 68 | rodolico | 76 | }
 | 
        
           |  |  | 77 | close REPORT;
 | 
        
           | 74 | rodolico | 78 | `chown www-data:www-data $$configuration{"domuOutput"}`;
 | 
        
           | 68 | rodolico | 79 | 1;
 |