Edit file File name : cgroup_cleaner.pl Content :#!/usr/bin/env perl use strict; use warnings; use feature 'say'; use Getopt::Long; use Data::Dumper; my ($dryrun) = (0, 0); my $cgdel_bin = '/usr/bin/cgdelete'; my $cgget_bin = '/usr/bin/cgget'; GetOptions( "d|dryrun" => \$dryrun, "h|help" => sub{ usage() }, ); sub usage { say << "EOS"; [About] Script will remove cgroups from users that don't exist on the server anymore [Usage] usage: $0 [--dryrun] [Flags] -d, --dryrun - dryrun, doesnt execute removal -h, --help - help! my hand is stuck in the toaster [Example] Standard Usage: $0 EOS exit 1; } # Load users from passwd sub GetMachineUsers { my %users; my $users_cmd = q(awk -F ":" '{if ($3>=1000) print $1, $3 }' /etc/passwd); my $dh_users = `$users_cmd`; foreach (split /\n/, $dh_users) { my ($user, $uid) = split / /, $_; next if $user =~ /root|mysql|Gdnscache|Gdnslog|ansible|nobody/; $users{$uid} = $user; } return %users; } # find created cgroups sub GetUserCgroups { my @users; my $users_cmd = q(find /sys/fs/cgroup/customer/cpu_*/limits/* -type d | cut -d / -f8); my $dh_user = `$users_cmd`; foreach (split /\n/, $dh_user) { push @users, $_; } return @users; } my %users = GetMachineUsers(); my @users = GetUserCgroups(); for my $user (@users) { next if grep { $users{$_} eq $user } keys %users; say "User $user no longer exists on the server, need to remove the cgroup for them."; my $find_cgroups = `find /sys/fs/cgroup/customer/cpu_*/limits/ -iname $user`; if ($find_cgroups) { $find_cgroups =~ s/\/sys\/fs\/cgroup\///; chomp($find_cgroups); say $find_cgroups if $find_cgroups; my $controllers = `$cgget_bin -n -v -r cgroup.controllers $find_cgroups`; chomp($controllers); my @active_controllers = split / /, $controllers; # this may error out on other control groups as the first cgroup delete may # delete the rest of the control cgroups in that pass foreach (@active_controllers) { say "$cgdel_bin -g $_:$find_cgroups"; `$cgdel_bin -g $_:$find_cgroups` unless $dryrun; } } else { say "couldnt find cgroup for $user"; } } say "done"; exit 0; Save