Madagascar / projects / autoSMART / scripts / test-smart-collection.pl
Newer Older
f16725e 3 months ago History
132 lines | 3.951kb
Bogdan Timofte authored 3 months ago
1
#!/usr/bin/perl
2

            
3
=head1 NAME
4

            
5
test-smart-collection.pl - Simple SMART data collection test
6

            
7
=head1 DESCRIPTION
8

            
9
Simplified SMART data collection test for autoSMART deployment verification.
10

            
11
=cut
12

            
13
use strict;
14
use warnings;
15
use FindBin qw($Bin);
16
use lib "$Bin/../lib";
17

            
18
use SmartCollector;
19
use DBI;
20
use JSON::XS;
21

            
22
# Configuration from environment
23
my $config = {
24
    db_host => $ENV{AUTOSMART_DB_HOST} || '192.168.2.102',
25
    db_port => $ENV{AUTOSMART_DB_PORT} || '5432',
26
    db_name => $ENV{AUTOSMART_DB_NAME} || 'autosmart',
27
    db_user => $ENV{AUTOSMART_DB_USER} || 'autosmart',
28
    db_pass => $ENV{AUTOSMART_DB_PASS} || 'autoSMART2025!',
29
    node_id => $ENV{AUTOSMART_NODE_ID} || 'ebony',
30
    debug => $ENV{AUTOSMART_DEBUG} || 2,
31
};
32

            
33
print "=== autoSMART SMART Collection Test ===\n\n";
34

            
35
# Test database connection
36
print "Testing database connection...\n";
37
my $dsn = "DBI:Pg:dbname=$config->{db_name};host=$config->{db_host};port=$config->{db_port}";
38
my $dbh = DBI->connect($dsn, $config->{db_user}, $config->{db_pass}, {
39
    RaiseError => 1,
40
    AutoCommit => 1,
41
    PrintError => 0
42
}) or die "Failed to connect to database: $DBI::errstr\n";
43

            
44
print "✓ Database connection successful\n\n";
45

            
46
# Initialize collector
47
print "Initializing SMART collector...\n";
48
my $collector = SmartCollector->new($config);
49
print "✓ Collector initialized\n\n";
50

            
51
# Discover available drives
52
print "Discovering storage devices...\n";
53
my @devices = glob('/dev/sd[a-z]');
54

            
55
for my $device (@devices) {
56
    print "Found device: $device\n";
57

            
58
    # Test SMART data collection
59
    print "  Collecting SMART data...\n";
60
    my $smart_data = $collector->collect_smart_data($device);
61

            
62
    if ($smart_data) {
63
        print "  ✓ SMART data collected successfully\n";
64
        print "    Serial: $smart_data->{serial_number}\n";
65
        print "    Model: $smart_data->{model_name}\n";
66
        print "    Temperature: $smart_data->{temperature}°C\n";
67
        print "    Parameters: " . scalar(keys %{$smart_data->{parameters}}) . "\n";
68

            
69
        # Create drive info structure
70
        my $drive_info = {
71
            device_path => $device,
72
            serial_number => $smart_data->{serial_number},
73
            model_name => $smart_data->{model_name}
74
        };
75

            
76
        # Store in database
77
        print "  Storing in database...\n";
78
        if ($collector->store_smart_data($drive_info, $smart_data)) {
79
            print "  ✓ Data stored successfully\n";
80
        } else {
81
            print "  ✗ Failed to store data\n";
82
        }
83

            
84
    } else {
85
        print "  ✗ Failed to collect SMART data\n";
86
    }
87
    print "\n";
88
}
89

            
90
# Check database contents
91
print "Checking database contents:\n";
92

            
93
my $sth = $dbh->prepare("SELECT COUNT(*) FROM hdd_inventory");
94
$sth->execute();
95
my ($hdd_count) = $sth->fetchrow_array();
96
print "  HDD Inventory: $hdd_count drives\n";
97

            
98
$sth = $dbh->prepare("SELECT COUNT(*) FROM smart_readings");
99
$sth->execute();
100
my ($reading_count) = $sth->fetchrow_array();
101
print "  SMART Readings: $reading_count readings\n";
102

            
103
$sth = $dbh->prepare("SELECT COUNT(*) FROM hdd_migrations");
104
$sth->execute();
105
my ($migration_count) = $sth->fetchrow_array();
106
print "  HDD Migrations: $migration_count migrations\n";
107

            
108
# Show recent readings
109
if ($reading_count > 0) {
110
    print "\nRecent SMART readings:\n";
111
    $sth = $dbh->prepare(q{
112
        SELECT hi.serial_number, hi.model_name, sr.timestamp, sr.temperature, sr.reading_type
113
        FROM smart_readings sr
114
        JOIN hdd_inventory hi ON sr.hdd_id = hi.id
115
        ORDER BY sr.timestamp DESC
116
        LIMIT 5
117
    });
118
    $sth->execute();
119

            
120
    while (my $row = $sth->fetchrow_hashref()) {
121
        printf "  %s (%s) - %s - %d°C - %s\n",
122
               $row->{serial_number},
123
               $row->{model_name},
124
               $row->{timestamp},
125
               $row->{temperature} || 0,
126
               $row->{reading_type};
127
    }
128
}
129

            
130
$dbh->disconnect();
131

            
132
print "\n=== Test Complete ===\n";