| Current Path : /pkg/zhost/1.0/bin.old/ |
| Current File : //pkg/zhost/1.0/bin.old/zz-web-quota-scan.old |
#!/cmd/perl
# customer panel
#
# settings (webmasters)
# system (quota,memory,traffic)
# files (elfinder)
# logs (tail)
# database (mariadb)
use Text::Template;
use Quota;
# main routine ========================================================
# TODO:
# notify help desk if no quota is configured
# notify help desk rather than only fail
# track rate of change of quota for further warnings
my $BASE = '/srv/web';
my $CONF = '/etc/zoot/web';
my $STORAGE = read_item("$CONF/storage");
if (!defined($STORAGE)) {
error('fail', "$CONF/storage read error")
}
my $WEBMASTERS = read_list("$CONF/webmasters");
if (!defined($WEBMASTERS)) {
error('warn', "$CONF/webmasters read error")
}
my @webs = ();
opendir(DIR, $BASE)
or error('fail', "cannot read $BASE");
while (readdir(BASE) {
my $web = $_;
if (-f /etc/nginx/vhosts/$web || -f /etc/nginx/vhosts.hold/$web) {
if (-d "$BASE/$web/.svc") {
push (@webs, $web);
}
else {
error('warn', "$BASE/$web/.svc missing");
}
}
}
closedir(DIR);
foreach my $web (@webs) {
my $conf = "$BASE/$web/etc"
my ($quota, $limit, $percent) = read_quota($web);
#my $webmasters_file = read_list("$conf/notify/webmasters"); # array ref
#my $triggers_file = read_list("$conf/quota/triggers"); # array ref
print "$web $quota $limit $percent $min-$max";
#my @triggers = ();
#@triggers = read_list("$file", '^\d$') or do {
#@triggers = read_list("$CONF/triggers", '^\d$') or do {
#}
#read $web/etc/quota/triggers
#write $web/tmp/quota/trigger_bands
#write $web/tmp/quota/last_trigger_band if !exists
#my $max = 100;
#foreach my $trigger (@triggers) {
#my $min = $trigger;
#if ($percent > $min && $percent < $max) {
#if (new_trigger_range($web, $min, $max)) {
#print " TRIGGER";
##notify($webmasters, $web, $quota);
#}
#last;
#}
#$max = $trigger;
#}
#print "\n";
}
# subroutines =========================================================
sub build_path {
my $ref = shift;
my %path = %$ref;
chdir($path{base});
my @dirs = split('/', $path{dirs});
my @users = split('/', $path{users});
my @groups = split('/', $path{groups});
my @modes = split('/', $path{modes});
foreach $dir (@dirs) {
mkdir($dir) unless (-d $dir);
my $user = shift @users;
my $group = shift @groups;
my $mode = shift @modes;
#print "$dir = $user:$group ($mode)\n";
my $uid = getpwnam $user;
my $gid = getgrnam $group;
chown($uid, $gid, $dir);
chmod(oct($mode), $dir);
chdir($dir);
}
}
sub read_quota {
my $web = shift;
my $uid = getpwnam($web);
my $mnt = Quota::getqcarg("$BASE/$web");
my ($used, $soft, $hard) = Quota::query($mnt, $uid);
my $percent = sprintf("%.0f", ($used/$hard)*100);
my %quota = (used => $used, limit => $hard, percent => $percent);
return (\%quota);
}
sub init_quota {
my $web = shift;
my $default_quota = 0.25;
my $result = read_item("$BASE/$web/sys/storage");
unless ($result) {
build_path( { base => "$BASE/$web",
dirs => 'sys',
users => 'root',
groups => "$web",
modes => '0750' } );
write_item($default_quota, "$BASE/$web/sys/storage");
}
}
sub read_item {
my $file = shift;
open(F, "<$file") or return undef;
my $value = <F>;
chomp($value);
close(F) or return undef;
return "$value";
}
sub write_item {
my $item = shift;
my $file = shift;
open(F, ">$file") or return 0;
print F "$item";
close(F) or return 0;
return 1;
}
sub read_list {
my $file = shift;
my $regex = shift;
my @list = ();
open(F, "<$file") or return undef;
while (my $line = <F>) {
chomp($line);
return undef unless ($line =~ m/$regex/);
push(@list, $line);
}
close(F) or return undef;
return @list;
}
sub new_trigger_range {
my $web = shift;
my $min = shift;
my $max = shift;
my $this_range = "$min-$max";
my $range_file = "$BASE/$web/tmp/quota/trigger_range";
if (-f $range_file) {
my $last_range = read_item($range_file);
if ($this_range eq $last_range) {
return 0;
}
}
return 1;
}