2009-11-05 19:26:54 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
###############################################################################
|
|
|
|
#
|
|
|
|
# Copyright (c) 2009 Ramon Novoa <rnovoa@artica.es>
|
|
|
|
# Copyright (c) 2009 Artica Soluciones Tecnologicas S.L.
|
|
|
|
#
|
|
|
|
# pandora_df Retrieve filesystem disk usage. By default information for all
|
|
|
|
# filesystems is returned, but one or more filesystems may be
|
2014-05-21 09:53:15 +02:00
|
|
|
# specified as command line parameters. If parameter start with
|
|
|
|
# a '-', the filesystem is excluded from results.
|
2009-11-05 19:26:54 +01:00
|
|
|
#
|
|
|
|
# Sample usage: ./pandora_df tmpfs /dev/sda1
|
|
|
|
#
|
2016-08-18 13:29:56 +02:00
|
|
|
# Set the first parameter as "-r" to use regular expressions in name match
|
|
|
|
#
|
2009-11-05 19:26:54 +01:00
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; version 2 of the License.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
use strict;
|
2018-10-30 17:53:57 +01:00
|
|
|
use warnings;
|
2009-11-05 19:26:54 +01:00
|
|
|
|
|
|
|
# Retrieve information from all filesystems
|
|
|
|
my $all_filesystems = 0;
|
|
|
|
|
2016-08-18 13:29:56 +02:00
|
|
|
# Regex flag
|
|
|
|
my $regex_mode = 0;
|
2018-10-30 17:53:57 +01:00
|
|
|
my $inode_mode = 0;
|
|
|
|
|
|
|
|
# Exclusion
|
|
|
|
my @exclude_fs = ();
|
|
|
|
my $cmd = undef;
|
|
|
|
|
|
|
|
|
|
|
|
sub in_array {
|
|
|
|
my ($array, $value) = @_;
|
|
|
|
|
|
|
|
if (!defined($value)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
my %params = map { $_ => 1 } @{$array};
|
|
|
|
if (exists($params{$value})) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2016-08-18 13:29:56 +02:00
|
|
|
|
|
|
|
sub check_re($$){
|
|
|
|
my $item = shift;
|
|
|
|
my $array = shift;
|
|
|
|
|
|
|
|
foreach my $check (@{$array}){
|
|
|
|
if ($item =~ /$check/){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub check_in($$){
|
|
|
|
my $filesystems = shift;
|
|
|
|
my $fs = shift;
|
|
|
|
|
|
|
|
if(($regex_mode == 0) || ( !defined ($regex_mode))) {
|
|
|
|
return defined ($filesystems->{$fs});
|
|
|
|
}
|
|
|
|
# check if matches any key
|
|
|
|
my @keys = keys %{$filesystems};
|
|
|
|
return check_re($fs, \@keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# MAIN
|
|
|
|
#
|
|
|
|
|
2009-11-05 19:26:54 +01:00
|
|
|
# Check command line parameters
|
|
|
|
if ($#ARGV < 0) {
|
|
|
|
$all_filesystems = 1;
|
|
|
|
}
|
2016-08-18 13:29:56 +02:00
|
|
|
|
2018-10-30 17:53:57 +01:00
|
|
|
while ($#ARGV >= 0) {
|
|
|
|
my $param = shift @ARGV;
|
|
|
|
if ($param eq '-r') {
|
|
|
|
$regex_mode = 1;
|
|
|
|
shift @ARGV;
|
|
|
|
}
|
|
|
|
elsif ($param eq '-i') {
|
|
|
|
$inode_mode = 1;
|
|
|
|
}
|
|
|
|
elsif ($param eq '-exclude_fs') {
|
|
|
|
my $_tmp = shift @ARGV;
|
|
|
|
chomp ($_tmp);
|
|
|
|
@exclude_fs = split /,/, $_tmp;
|
|
|
|
}
|
|
|
|
elsif ($param eq '-custom_cmd') {
|
|
|
|
$cmd = shift @ARGV;
|
|
|
|
chomp ($cmd);
|
|
|
|
}
|
2016-08-18 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
2009-11-05 19:26:54 +01:00
|
|
|
# Parse command line parameters
|
|
|
|
my %filesystems;
|
2014-05-21 09:53:15 +02:00
|
|
|
my %excluded_filesystems;
|
|
|
|
my $onlyexclude = 1;
|
2009-11-05 19:26:54 +01:00
|
|
|
foreach my $fs (@ARGV) {
|
2014-05-21 09:53:15 +02:00
|
|
|
if (substr($fs,0,1) eq '-') {
|
|
|
|
$excluded_filesystems{substr($fs,1,)} = '-1%';
|
|
|
|
} else {
|
|
|
|
$filesystems{$fs} = '-1%';
|
|
|
|
$onlyexclude = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($onlyexclude) {
|
|
|
|
$all_filesystems = 1;
|
2009-11-05 19:26:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Retrieve filesystem information
|
|
|
|
# -P use the POSIX output format for portability
|
2018-10-30 17:53:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
$cmd = 'df -PT' unless defined($cmd);
|
|
|
|
|
|
|
|
if ($inode_mode > 0) {
|
|
|
|
$cmd = 'df -PTi';
|
|
|
|
}
|
|
|
|
|
|
|
|
my @df = `$cmd`;
|
2009-11-05 19:26:54 +01:00
|
|
|
shift (@df);
|
|
|
|
|
|
|
|
# No filesystems? Something went wrong.
|
|
|
|
if ($#df < 0) {
|
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
|
2016-08-18 13:29:56 +02:00
|
|
|
my %out;
|
2009-11-05 19:26:54 +01:00
|
|
|
# Parse filesystem usage
|
|
|
|
foreach my $row (@df) {
|
|
|
|
my @columns = split (' ', $row);
|
2018-10-30 17:53:57 +01:00
|
|
|
exit 1 if ($#columns < 5);
|
|
|
|
|
|
|
|
next if (in_array(\@exclude_fs, $columns[1]) > 0);
|
|
|
|
|
|
|
|
if (check_in (\%filesystems,$columns[0])
|
|
|
|
|| ($all_filesystems == 1 && !check_in(\%excluded_filesystems,$columns[0])) ){
|
|
|
|
$out{$columns[0]} = $columns[5] ;
|
2014-05-21 09:53:15 +02:00
|
|
|
}
|
2009-11-05 19:26:54 +01:00
|
|
|
}
|
|
|
|
|
2016-08-18 13:29:56 +02:00
|
|
|
while (my ($filesystem, $use) = each (%out)) {
|
2009-11-05 19:26:54 +01:00
|
|
|
|
|
|
|
# Remove the trailing %
|
|
|
|
chop ($use);
|
|
|
|
|
|
|
|
# Print module output
|
|
|
|
print "<module>\n";
|
2018-10-30 17:53:57 +01:00
|
|
|
print "<name><![CDATA[" . ($inode_mode > 0 ? 'Inodes:' : '') . $filesystem . "]]></name>\n";
|
2009-11-05 19:26:54 +01:00
|
|
|
print "<type><![CDATA[generic_data]]></type>\n";
|
|
|
|
print "<data><![CDATA[" . $use . "]]></data>\n";
|
2015-07-07 12:43:34 +02:00
|
|
|
print "<description><![CDATA[% of usage in this volume]]></description>\n";
|
2009-11-05 19:26:54 +01:00
|
|
|
print "</module>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
exit 0;
|