Project

General

Profile

Download (5.12 KB) Statistics
| Branch: | Revision:
1
#!/usr/bin/perl
2

    
3
# All rights reserved and Copyright (c) 2020 Origo Systems ApS.
4
# This file is provided with no warranty, and is subject to the terms and conditions defined in the license file LICENSE.md.
5
# The license file is part of this source code package and its content is also available at:
6
# https://www.origo.io/info/stabiledocs/licensing/stabile-open-source-license
7

    
8
# Monitor disk space usage
9
#
10
# Arguments are:
11
#
12
# "Node IP address" "image path" [kBfree|free%] ["1:2:3"|"all"]
13
#
14
# This script will exit with value 1 if any partition has less than
15
# "kBfree" kilobytes, or less than "free" percent available.
16
#
17
# The first output line is a list of the paths which failed, and
18
# how much space is free, in megabytes.
19

    
20
use URI::Escape;
21
use Tie::DBI;
22
use Data::Dumper;
23
use ConfigReader::Simple;
24

    
25
$ENV{PATH} = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin';
26
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
27

    
28
#my $macip = shift;
29
#$macip =~ /(.+)/; $macip = $1; # Untaint
30
#my $path = shift;
31
#$path =~ /(.+)/; $path = uri_unescape($1); # Untaint
32
#$path =~ s/([ ])/\\$1/g;
33

    
34
my @argv = @ARGV;
35
pop @argv if ($#ARGV>0); # mon sends IP as last param, which we don't need
36

    
37
my $serveruuid = $argv[0];
38
$serveruuid =~ /(.+)/; $serveruuid = $1; # Untaint
39

    
40
my $minavailp = $argv[1];
41
$minavailp = '10' unless ($minavailp);
42
$minavailp =~ /(.+)/; $minavailp = $1; # Untaint
43

    
44
my $parts = $argv[2];
45
$parts = 'all' unless ($parts);
46
$parts =~ /(.+)/; $parts = $1; # Untaint
47

    
48
my $res;
49
my $part = 1;
50
my @failures;
51
my @successes;
52
my $START_TIME = time;
53
my $END_TIME;
54

    
55
my $config = ConfigReader::Simple->new("/etc/stabile/config.cfg",
56
    [qw(DBI_USER DBI_PASSWD)]);
57

    
58
$dbiuser =  $config->get('DBI_USER') || "irigo";
59
$dbipasswd = $config->get('DBI_PASSWD') || "";
60

    
61
unless (tie %domreg,'Tie::DBI', {
62
    db=>'mysql:steamregister',
63
    table=>'domains',
64
    key=>'uuid',
65
    autocommit=>0,
66
    CLOBBER=>3,
67
    user=>$dbiuser,
68
    password=>$dbipasswd}) {push (@failures,  "Error connecting to DB")};
69

    
70
my $domstatus = $domreg{$serveruuid}->{'status'};
71
my $macip = $domreg{$serveruuid}->{'macip'};
72
$macip =~ /(.+)/; $macip = $1; # Untaint
73
#my $virtname = $domreg{$serveruuid}->{'name'} . substr($serveruuid,0,8);
74
#my $path = $domreg{$serveruuid}->{'image'};
75
#my $path2 = $domreg{$serveruuid}->{'image2'};
76
#$path2 = '' if ($path2 eq '--');
77

    
78
unless (tie %nodereg,'Tie::DBI', {
79
    db=>'mysql:steamregister',
80
    table=>'nodes',
81
    key=>'mac',
82
    autocommit=>0,
83
    CLOBBER=>3,
84
    user=>$dbiuser,
85
    password=>$dbipasswd}) {push (@failures,  "Error connecting to DB")};
86

    
87
my $hypervisor = $nodereg{$domreg{$serveruuid}->{'mac'}}->{'identity'};
88

    
89
untie %nodereg;
90
untie %domreg;
91
$hypervisor = 'qemu' if (!$hypervisor || $hypervisor eq 'kvm');
92

    
93
if ($macip && $macip ne '' && $serveruuid) {
94

    
95
    if (!$domstatus || $domstatus eq 'inactive' || $domstatus eq 'shutoff' || $domstatus eq 'paused') {
96
        push (@failures, "Error - server not running, no disk activity to monitor!");
97
    } else {
98
        my $sshcmd = "/usr/bin/ssh -q -l irigo -i /var/www/.ssh/id_rsa_mon -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no";
99
        my $sudo = ($hypervisor eq 'vbox'?'sudo virt-df':'virt-df');
100
        my $cmd = qq{$sshcmd $macip "$sudo --csv -c $hypervisor:///system -d $serveruuid"};
101
        #print "$cmd\n";
102
        $res .= `$cmd`;
103
        #$res .= `$sshcmd $macip "virt-df --csv -c qemu:///system -d $serveruuid"`;
104
        #$res .= `$sshcmd $macip "virt-df --csv -a $path2"` if ($path2);
105
        foreach $line (split(/\n/, $res)) {
106
            if ($line =~ /^(\S+),(\d+),(\d+),(\d+),/) {
107

    
108
                my ($img, $dev, $size, $used, $avail, $usepercent) = split(/,/,$line,6);
109
                $usepercent = int(100*$usepercent+0.5)/100;
110
                my $availg = int(1000 * $avail / 1024 / 1024 + 0.5)/1000;
111
                my $totalg = int(1000 * ($avail+$used) / 1024 / 1024 + 0.5)/1000;
112

    
113
                #if (!($dev =~ /sdc/) && !($dev =~ /sdb/)) { # Exclude cdroms
114
#                if (100*$avail/($used + $avail) < $minavailp && ($parts eq 'all' || $parts =~ /$part/)) {
115
                    if (100*$avail/$size < $minavailp && ($parts eq 'all' || $parts =~ /$part/)) {
116
                        push (@failures, sprintf ("[*$part] %s: %1.2fGB free (%1.0f\%), total %1.2fGB",
117
                        $dev, $availg, 100-$usepercent, $totalg));
118
                    } else {
119
                        push (@successes, sprintf ("[$part] %s: %1.2fGB free (%1.0f\%), total %1.2fGB",
120
                        $dev, $availg, 100-$usepercent, $totalg));
121
                    }
122
                    $part++;
123
                #}
124
            }
125
        }
126
        push (@failures, "Error - no partitions found for $serveruuid on $macip!") if ($part==1);
127
    }
128

    
129
} else {
130
    push (@failures, "Error - Unable to find IP address and path for $serveruuid!");
131
}
132

    
133
$END_TIME = time;
134

    
135
print "\nstart time: " . localtime ($START_TIME) . "\n";
136
print "end time  : " . localtime ($END_TIME) . "\n";
137
print "duration  : " . ($END_TIME - $START_TIME) . " seconds\n";
138
print "min.free %: $minavailp\n\n";
139

    
140
if (@failures) {
141
    print join("\n", @failures), "\n", join("\n", @successes), "\n";
142
    exit 1;
143
} else {
144
    print join("\n", @successes), "\n";
145
    exit 0;
146
}
(23-23/27)