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
|
#!/bin/bash
#
# Copyright (C) 2006-2008 Holger Levsen and Micah Anderson
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2 dated June,
# 1991.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Graph Vserver cpu usage stats
#
# Configuration variables
# vservers - specify the vservers to include in the graph (default: all)
#
# NOTE: If no configuration variable is set, the default will be used
#
# see vserver_resources for example uses of configuration files
#
# or links to define what to monitor:
# vserver_cpu_ -> monitor cpu usage of all vservers on all cpus
# vserver_hold_ -> monitor hold on all vservers on all cpus
# vserver_hold_0 -> monitor hold on all vservers on cpu0
# vserver_hold_1 -> monitor hold on all vservers on cpu1
# vserver_hold_foo -> monitor hold on all cpus on vserver named foo
# vserver_sys_foo -> monitor cpu usage on all cpus on vserver named foo
# Changelog
# version 0.2 - 2006 October 02 Holger Levsen <debian@layer-acht.org>
# - label fixed: we measure jiffies not seconds
# - Fix error that results if NodeName is set to include a domain name
# - Fix hypens in NodeNames, replace them with underscores
# - whitespace cleanup
# version 0.3 - 2006 October 07 Holger Levsen <debian@layer-acht.org>
# - rewrite of vserver_usercpu
# - smp-aware
# - can display hold too (third value in the cpu line(s) of /proc/virtual/<xid>/sched)
# - no seperation between user and system cpu anymore
# - handle identical vserver-names by using the vserver-id internally
# version 0.4 - 2007, October 07
# Micah Anderson <micah@riseup.net>
# - fixed variable name (thanks pietro)
# version 0.5 - 2008, July 07
# Micah Anderson <micah@riseup.net>
# - fixed number of CPU regexp to be more accurate
# - added $NAMELOC - fixes plugin so it works with VCI_SPACES (> 2.6.19) as well as older version
# TODO:
# - comment the code or go mad
# - add info how many jiffies per second are available on a machine
# - user and system cpu are always added to each other, make it optional to split them?
# - use /proc less often (100 times more overhead than talking to the kernel directly)
# i.e. use something like pagesize=`perl -MPOSIX -e 'print POSIX::sysconf(_SC_PAGESIZE), "\n";'`
VSERVERS="$vservers"
# FIXME: using arrays is a bashism
INFO=(`sed 's/.*:\t//' /proc/virtual/info 2>/dev/null || echo '<none>'`)
KCIN="$[ 16#${INFO[2]} ]";
# If this is 1, then VCI_SPACES is present in the kernel (new in 2.6.19)
if [ $[ (KCIN >> 10) & 1 ] -eq 1 ]
then
NAMELOC="nsproxy"
else
NAMELOC="cvirt"
fi
if [ -z "$VSERVERS" ] ; then
XIDS=`find /proc/virtual/* -type d -exec basename {} \;`
else
# it's really more performant to specify vservers by ids or by linking but not in the configuration-file by name
XIDS=""
for i in $VSERVERS ; do
if [ -d /proc/virtual/$i ] ; then
XIDS="${XIDS}${i} "
else
for j in `find /proc/virtual/* -type d -exec basename {} \;` ; do
if [ "$i" = "`cat /proc/virtual/$j/$NAMELOC |grep NodeName |cut -f2`" ] ; then
XIDS="${XIDS}${j} "
fi
done
fi
done
fi
BASEPARAM=`basename $0 | sed 's/^vserver_//'`
MODE=`echo $BASEPARAM | sed 's/^hold.*//'`
#debug=true
if [ -z "$MODE" ] ; then
MODE=hold
TARGET=`echo $BASEPARAM | sed 's/^hold_//'`
else
MODE=cpu
TARGET=`echo $BASEPARAM | sed 's/^cpu_//'`
fi
CPU1=0
if [ -n "$TARGET" ] ; then
if [ "${#TARGET}" == 1 ] ; then
if [ $debug ] ; then echo $MODE, only on cpu $TARGET, for all vservers ; fi
WHAT=ALLVSERVER
CPU1=$TARGET
else
if [ $debug ] ; then echo $MODE on all cpus together, only for vserver $TARGET ; fi
WHAT=VSERVER
fi
else
if [ $debug ] ; then echo $MODE for all cpus, for all vservers ; fi
WHAT=ALLVSERVER
fi
CPUS=$[ `grep ^processor /proc/cpuinfo|wc -l` -1 ]
CPUS=`seq $CPU1 $CPUS`
if [ $debug ] ; then
echo cpus= $CPUS
echo baseparam= $BASEPARAM
echo mode= $MODE
echo target= $TARGET
echo what= $WHAT
fi
if [ "$1" = "config" ]; then
echo 'graph_category vserver'
echo 'graph_args --base 1000'
if [ "$MODE" == "cpu" ] ; then
echo 'graph_title Vserver cpu usage'
echo 'graph_vlabel jiffies used per cpu per ${graph_period}'
echo 'graph_info Shows jiffies used per cpu on each vserver.'
else
echo 'graph_title Vserver cpu on hold'
echo 'graph_vlabel jiffies on hold per cpu per ${graph_period}'
echo 'graph_info Shows jiffies on hold used per cpu on each vserver.'
fi
for j in $CPUS ; do
A=0
for i in $XIDS ; do
LABEL=`cat /proc/virtual/$i/$NAMELOC |grep NodeName |cut -f2`
if [ "$WHAT" == "ALLVSERVER" ] || [ "$TARGET" == "$LABEL" ] ; then
NAME=`echo $LABEL | cut -d. -f1 | tr '-' '_'`
if [ "$MODE" == "cpu" ] ; then
echo "${NAME}_$j.label cpu usage for cpu $j on $LABEL"
echo "${NAME}_$j.info cpu usage for cpu $j on $LABEL."
else
echo "${NAME}_$j.label on hold for cpu $j on $LABEL"
echo "${NAME}_$j.info on hold for cpu $j on $LABEL."
fi
echo "${NAME}_$j.type COUNTER"
if [ "$A" == 0 ] ; then
echo "${NAME}_$j.draw AREA"
A=1
else
echo "${NAME}_$j.draw STACK"
fi
fi
done
done
exit 0
fi
for j in $CPUS ; do
for i in $XIDS ; do
LABEL=`cat /proc/virtual/$i/$NAMELOC |grep NodeName |cut -f2`
if [ "$WHAT" == "ALLVSERVER" ] || [ "$TARGET" == "$LABEL" ] ; then
NAME=`echo $LABEL | cut -d. -f1 | tr '-' '_'`
echo -n "${NAME}_$j.value "
if [ "$MODE" == "cpu" ] ; then
USERCPU=`cat /proc/virtual/$i/sched |grep "cpu $j"| cut -d' ' -f3`
SYSCPU=`cat /proc/virtual/$i/sched |grep "cpu $j"| cut -d' ' -f4`
echo $[$USERCPU + $SYSCPU]
else
cat /proc/virtual/$i/sched |grep "cpu $j"| cut -d' ' -f5
fi
fi
done
done
|