File: zz-disk_space

package info (click to toggle)
logwatch 7.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,572 kB
  • sloc: perl: 8,290; sh: 354; makefile: 38
file content (225 lines) | stat: -rw-r--r-- 6,689 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225

########################################################
# Please file all bug reports, patches, and feature
# requests under:
#      https://sourceforge.net/p/logwatch/_list/tickets
# Help requests and discusion can be filed under:
#      https://sourceforge.net/p/logwatch/discussion/
########################################################

#######################################################
## Copyright (c) 2008 Kirk Bauer
## Covered under the included MIT/X-Consortium License:
##    http://www.opensource.org/licenses/mit-license.php
## All modifications and contributions by other persons to
## this script are assumed to have been donated to the
## Logwatch project and thus assume the above copyright
## and licensing terms.  If you want to make contributions
## under your own copyright or a different license this
## must be explicitly stated in the contribution an the
## Logwatch project reserves the right to not accept such
## contributions.  If you have made significant
## contributions to this script and want to claim
## copyright please contact logwatch-devel@lists.sourceforge.net.
#########################################################

$| = 1;

use POSIX qw(uname);
use strict;

my (%Config);

my $Debug = $ENV{'LOGWATCH_DEBUG'} || 0;
my $Detail = $ENV{'LOGWATCH_DETAIL_LEVEL'} || 0;
my $logwatch_hostname = $ENV{'LOGWATCH_ONLY_HOSTNAME'};

my $show_home_dir_sizes = $ENV{'show_home_dir_sizes'} || 0;
my $home_dir = $ENV{'home_dir'} || 0;
my $show_mail_dir_sizes = $ENV{'show_mail_dir_sizes'} || 0;
my $mail_dir = $ENV{'mail_dir'} || 0;
my $show_disk_usage = $ENV{'show_disk_usage'} || 0;
my $diskfull_threshold = $ENV{'diskfull_threshold'} || 90;
my $diskfull_exclude_dirs = $ENV{'diskfull_exclude_dirs'} || '$';
my $local_disks_only = $ENV{'local_disks_only'} || 0;
my $suppress_listing = $ENV{'suppress_listing'} || 0;

my $df_options;
my $disk_cmd;
my $DebugCounter=0;

my ($OSname, $hostname, $release, $version, $machine) = POSIX::uname();
$hostname =~ s/\..*//;
exit (0) if ($ENV{'LOGWATCH_ONLY_HOSTNAME'} and ($logwatch_hostname ne $hostname));

if ( $Debug >= 5 ) {
	print STDERR "\n\nDEBUG: Inside zz-disk-space Filter \n\n";
	$DebugCounter = 1;
}

sub DirUsage {
	my $Dir = $_[0];
    if ( !-d $Dir ) {
        print STDERR "Directory $Dir not found\n";
        return
    };
	$Dir = $Dir . "/*"; # Add /* to the end of the directory name for globbing -mgt
	if ($OSname eq "Linux") {
		system("du -s --block-size=1048576 -h $Dir | sort -h -r -k 1");
	} elsif ($OSname eq "Darwin") {
		system("du -s --block-size=1048576 -h $Dir | sort -h -r -k 1");
	} elsif ($OSname eq "SunOS") {
		if ( ($release eq "5.10") || ($release eq "5.9") || ($release eq "5.11") ) {
		   system("/usr/xpg4/bin/du -s -h $Dir | sort -n -r -k 1");
      } else {
         system("du -s $Dir | sort -n -r -k 1");
      }
	} elsif ($OSname eq "HP-UX") {
		system("du -s -k $Dir | sort -n -r -k 1");
	} elsif ($OSname eq "AIX") {
	 	system("du -s -k $Dir | sort -n -r -k 1");
	} else {
	  	system("du -s -h $Dir | sort -n -r -k 1");
	}
}

sub DirectorySizes
{
	my $Dir = $_[0];
	DirUsage($Dir); #Do not add a glob here -mgt
}

sub HomeDirectorySizes
{
	my $HomeDir = $_[0];
	print "\n\n------------- Home Directory Sizes ---------------\n\n";
	print "Size    Location\n";
	DirectorySizes($HomeDir);
	print "\n\n------------- Home Directory Sizes ---------------\n\n";
}

sub MailDirectorySizes
{
	my $MailDir = $_[0];
	print "\n\n------------- Mail Directory Sizes ---------------\n\n";
	print "Size    Location\n";
	DirectorySizes($MailDir);
	print "\n\n------------- Mail Directory Sizes ---------------\n\n";
}

sub DiskUsage ()
{
	my $Dir = $_[0];
	print "\n\n------------- Directory Sizes ---------------\n\n";
	print "Size    Location\n";

	if ($OSname eq "Linux") {
	 	DirUsage("/var/log");
	 	DirUsage("/usr");
	} elsif ($OSname eq "Darwin") {
		DirUsage("/var/log");
		DirUsage("/usr");
	} elsif ($OSname eq "SunOS") {
		DirUsage("/var/log");
		DirUsage("/usr");
		DirUsage("/opt");
	} elsif ($OSname eq "HP-UX") {
		DirUsage("/var/adm");
		DirUsage("/usr");
		DirUsage("/opt");
	} else {
		DirUsage("/var/log");
		DirUsage("/usr");
		DirUsage("/opt");
	}

	print "\n\n------------- Directory Sizes ---------------\n\n";

}

sub DiskSpace ()
{
	system($disk_cmd);
	print "\n";
}

sub DiskFull {
    my @rows = split('\n', `$disk_cmd`);
    # Remove header
    shift @rows;
    foreach my $row (@rows) {
        my ($source,$used,$target) = ($row =~ /^(.*?)(?:\s+[\d\.]+[KMGTP]?){3}\s+(\d+)%\s+(.*)$/);
        if (($used > $diskfull_threshold) &&
                    ($source !~ /\/dev\/scd/ ) &&
                    ($source !~ /\/dev\/sr/ ) &&
                    ($source !~ /\/dev\/loop./) &&
                    ($target !~ /^$diskfull_exclude_dirs/i)) {
            print "$target ($source) => $used% Used. Warning: Disk Filling up.\n";
        }
    }
}

#####################
#Main

if ($OSname eq "Linux") {
	$df_options = "-h -x tmpfs -x devtmpfs -x udf -x iso9660 -x squashfs -x overlay -x nsfs";
	if ($local_disks_only) { $df_options .= " -l"; }
} elsif ($OSname eq "Darwin") {
	$df_options = "-h -T nodevfs,autofs";
	if ($local_disks_only) { $df_options .= " -l"; }
} elsif ($OSname eq "SunOS") {
	if ( ($release eq "5.10") || ($release eq "5.9") || ($release eq "5.11") ) {
		$df_options = "-h";
	}
	if ($local_disks_only) { $df_options .= " -l"; }
} elsif ($OSname eq "AIX") {
	$df_options = "-P";
	if ($local_disks_only) { $df_options .= " -T local"; }
} elsif ($OSname eq "GNU/kFreeBSD") {
	$df_options = "-h -x tmpfs -x devtmpfs -x udf -x iso9660 -x devfs -x linprocfs -x sysfs -x fdescfs";
	if ($local_disks_only) { $df_options .= " -l"; }
} else {
	$df_options = "";
	if ($local_disks_only) { $df_options .= " -l"; }
}

if ( $ENV{'df_options'} ) {
	$df_options = $ENV{'df_options'};
};

if ($OSname eq "SunOS") {
	$disk_cmd = "/usr/xpg4/bin/df $df_options";
} elsif ($OSname eq "HP-UX") {
	$disk_cmd = "bdf $df_options";
} else {
	$disk_cmd = "df $df_options";
}

if ( $ENV{'disk_cmd'} ) {
	$disk_cmd = $ENV{'disk_cmd'};
};

#Only show disk space "df" by default -mgt
DiskSpace() if ( !$suppress_listing and (($ENV{PRINTING} eq 'y') or $Detail));

if ( $show_disk_usage == 1 ) { DiskUsage(); }; #Turn on in zz-disk_space.conf

if (($Detail > 10) || ($show_home_dir_sizes == 1)) { #set in zz-disk_space.conf -mgt
	HomeDirectorySizes($home_dir);
}

if (($Detail > 10) || ($show_mail_dir_sizes == 1)) { #set in zz-disk_space.conf -mgt
	MailDirectorySizes($mail_dir);
}

if ($diskfull_threshold > 0) {
	DiskFull();
}

# vi: shiftwidth=3 tabstop=3 syntax=perl et
# Local Variables:
# mode: perl
# perl-indent-level: 3
# indent-tabs-mode: nil
# End: