Your IP : 216.73.216.21


Current Path : /pkg/zhost/1.0/bin.old/
Upload File :
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;
}

https://nalibali.org/post-sitemap.xml 2026-11-20T09:00:00+00:00 https://nalibali.org/page-sitemap.xml 2026-11-18T09:24:11+00:00 https://nalibali.org/stories-sitemap.xml 2026-05-05T06:48:38+00:00 https://nalibali.org/stories-sitemap2.xml 2026-05-05T06:48:38+00:00 https://nalibali.org/audio_stories-sitemap.xml 2026-03-27T06:53:13+00:00 https://nalibali.org/story_cards-sitemap.xml 2025-06-06T10:27:48+00:00 https://nalibali.org/story_seeds-sitemap.xml 2025-05-19T07:35:43+00:00 https://nalibali.org/rhymes-sitemap.xml 2025-06-17T07:43:10+00:00 https://nalibali.org/activity_sheets-sitemap.xml 2025-05-16T09:16:43+00:00 https://nalibali.org/printables-sitemap.xml 2025-05-16T09:28:40+00:00 https://nalibali.org/posters_bookmarks-sitemap.xml 2025-05-16T10:20:22+00:00 https://nalibali.org/team_member-sitemap.xml 2025-05-28T09:40:43+00:00 https://nalibali.org/video_stories-sitemap.xml 2025-06-23T08:48:28+00:00 https://nalibali.org/guides-sitemap.xml 2025-06-17T09:38:41+00:00 https://nalibali.org/story_supplements-sitemap.xml 2025-09-16T12:22:20+00:00 https://nalibali.org/vacancies-sitemap.xml 2026-09-17T07:05:01+00:00 https://nalibali.org/category-sitemap.xml 2026-11-20T09:00:00+00:00 https://nalibali.org/post_tag-sitemap.xml 2026-11-06T00:00:00+00:00 https://nalibali.org/test-sitemap.xml 2025-06-13T09:36:28+00:00 https://nalibali.org/story_categories-sitemap.xml 2026-05-05T06:48:38+00:00 https://nalibali.org/activity_sheets_categories-sitemap.xml 2025-05-16T09:16:43+00:00 https://nalibali.org/printables_categories-sitemap.xml 2025-05-16T09:28:40+00:00 https://nalibali.org/posters_bookmarks_categories-sitemap.xml 2025-05-16T10:20:22+00:00 https://nalibali.org/single_audio-sitemap.xml 2026-03-27T06:53:13+00:00 https://nalibali.org/rhyme_categories-sitemap.xml 2025-06-17T07:43:10+00:00 https://nalibali.org/guide_categories-sitemap.xml 2025-06-17T09:38:41+00:00 https://nalibali.org/author-sitemap.xml 2026-01-22T07:26:07+00:00