-
Notifications
You must be signed in to change notification settings - Fork 1
/
cgtool
executable file
·131 lines (117 loc) · 2.36 KB
/
cgtool
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
#!/bin/bash
#write by shuaili
while getopts ":p:f:x:d:r:n:mhv" optname
do
case "$optname" in
"p")
cgroup_arg=$OPTARG
;;
"f")
filename=$OPTARG
;;
"m")
rmpid=true
;;
"x")
exec_str=$OPTARG
;;
"d")
attach_pid=$OPTARG
;;
"r")
rm_null=true
;;
"n")
groupname=$OPTARG
;;
"h")
echo "help:$0 -p \"arg1=v1 arg2=v2 ..\" -f argfilename -x execstr -d pids"
;;
"v")
debuglabel=true
;;
"?")
echo "Unknown option $OPTARG"
;;
":")
echo "No argument value for option $OPTARG"
;;
*)
echo "Unknown error while processing options"
;;
esac
done
error_message()
{
echo $1
exit $2
}
tmpfile=$(mktemp)
if [[ $cgroup_arg && $filename ]]
then
error_message "-p and -f is error" 23
fi
if [[ $exec_str && $attach_pid ]]
then
error_message "-x and -d is error" 24
fi
if [[ $cgroup_arg ]]
then
echo $cgroup_arg | tr " " "\n" | sort >$tmpfile
fi
if [[ $filename ]]
then
cat $filename | tr " " "\n" | sort >$tmpfile
fi
if [[ $exec_str || $attach_pid ]]
then
if [[ $groupname ]]
then
gro=$groupname
else
uuid >/dev/null || yum install -y uuid ||apt-get install -y uuid
gro=$(uuid |awk -F '-' '{print $1}')
fi
for i in $(cat $tmpfile)
do
value=${i##*=}
j=${i%%=*}
subsys=${j%%.*}
pro=${j#*.}
cgget -g $subsys / |grep -q "$subsys.$pro:" || error_message "$subsys.$pro is not found in $subsys" 25
done
for i in $(cat $tmpfile)
do
value=${i##*=}
j=${i%%=*}
subsys=${j%%.*}
pro=${j#*.}
#echo subsys=$subsys pro=$pro value=$value
cgcreate -g $subsys:$gro
cgset -r $subsys.$pro=$value /$gro
done
arg=$(cat $tmpfile | awk -F '.' '{print $1}'|uniq|awk -v gro="$gro" -F '.' '{print "-g "$1":"gro}')
fi
if [[ $rmpid ]]
then
for i in `lscgroup |awk -F ':' '{print $1}'|sort |uniq`;do echo "-g $i:/ " ;done >$tmpfile
arg=$(cat $tmpfile)
fi
if [[ $exec_str ]]
then
cgexec $arg --sticky $exec_str
rm $tmpfile
fi
if [[ $attach_pid ]]
then
pidlist=`ps -eLf |awk -v pid=$attach_pid '{if($2==pid){print $4}}'`
cgclassify $arg --sticky $attach_pid $pidlist
rm $tmpfile
fi
if [[ $rm_null ]]
then
for i in `lscgroup`
do
grep -q $i /proc/*/cgroup || cgdelete $i
done
fi