Tải bản đầy đủ (.pdf) (7 trang)

Colorful proc Reporting

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (63.1 KB, 7 trang )

223
■ ■ ■
CHAPTER 35
Colorful /proc Reporting
T
he proc file system is found on many UNIX and Linux variants and gives a virtual view
into the running system. Traditionally the proc file system is attached to the /proc mount
point, but proc is not really a file system and mostly contains “files” of zero size. Even
though these objects look as if they are empty when they are listed, they actually contain
quite a lot of information about the running system. When viewed, they show system
information from the kernel’s perspective: their contents are based on system resources
and characteristics, such as memory, CPU, kernel, and network utilization.
There are several kinds of files in the /proc directory tree on a Linux machine. The
first type is a series of directories that have numbers as names, each containing a group
of files that hold specific information about the running system. Each numbered direc-
tory corresponds to a running process ID. The files in these directories relate to the
command that was invoked, the execution environment, parameters passed to the com-
mand, memory usage, and other valuable pieces of process information. The other file
types and directories found in /proc contain items such as current resource usage, sys-
tem settings, hardware information, and network usage.
The man page for proc contains far more detail than this chapter, but please note
that implementations of the proc file system on different operating systems are by no
means identical. I have worked on older versions of Solaris whose /proc contained only
numbered directories for running processes, and those directories held files that were
different from their Linux counterparts. With this state of affairs, you are certain to
profit from exploring the documentation for your specific system.
The script we are about to explore has its origins in a utility that creates a brief
system-status report. The script returned a single line of key performance indicators;
the information was derived from files located in /proc directories. It displayed all the
process-performance values for each system being monitored on a few lines all con-
tained in a single window, using colors to indicate load level, memory, and swap usage


(green for normal usage and red for high). The main purpose of this tool was to permit a
quick visual check of whether certain machines were overloaded.
Our code extends the functionality of the original script from providing just one line of
output per system to displaying a more comprehensive report about the processes run-
ning on a single machine. The code itself is not very complex but it does demonstrate
some of the /proc system usage data. You will also learn how to add color to your output.
224
CHAPTER 35

COLORFUL /PROC REPORTING
The first function sets up the colors that the script will use. These are standard ANSI
color definitions, and each one begins with the string \033. This is the plain-text ASCII
code for an escape character. The numerical notation used here is easier to read in text
form than the actual escape character. (The escape character happens to be ^[, and if you
want to type it in, you must use your editor’s mechanism for handling this character—for
example, pressing Ctrl+v and then ESC in vi.) Note that these color definitions may not
work on all terminal types. I have had no problems with using these values in a simple
xterm window.
#!/bin/sh
def_colors () {
# Attributes
normal='\033[0m'; bold='\033[1m'; dim='\033[2m'; under='\033[4m'
italic='\033[3m'; noitalic='\033[23m'; blink='\033[5m';
reverse='\033[7m'; conceal='\033[8m' nobold='\033[22m';
nounder='\033[24m'; noblink='\033[25m'
# Foreground
black='\033[30m'; red='\033[31m'; green='\033[32m'; yellow='\033[33m'
blue='\033[34m'; magenta='\033[35m'; cyan='\033[36m'; white='\033[37m'
# Background
bblack='\033[40m'; bred='\033[41m'

bgreen='\033[42m'; byellow='\033[43m'
bblue='\033[44m'; bmagenta='\033[45m'
bcyan='\033[46m'; bwhite='\033[47m'
}
def_colors
I did not use all of the colors or attributes defined in the function, but I included them
for the sake of completeness. This function would be an excellent addition to a standard
library, which is discussed in Chapter 2.
Now that the colors are defined, the script clears the display and then obtains the name
of the machine from the /proc/hostname file. Then it displays a header for the report.
clear
hostname=`cat /proc/sys/kernel/hostname`
echo
echo -e "System Report for $white$hostname$normal on `date`"
echo
There are a few noteworthy items in the command that displays the header line: first,
echo is used with the -e switch, which translates escaped three-digit number sequences
contained in text into the corresponding ASCII character; in our case it translates the ANSI
escape-character sequence into the escape character. Other escape values can be used to
represent other special characters, such as the carriage return, tab, and backspace.
The second item is the hostname variable, which is surrounded by variables that control
color output. The first variable changes the output from the default color to white. Once
CHAPTER 35

COLORFUL /PROC REPORTING
225
the hostname variable’s value has been presented, the color is reset to the default. Changes
to the text color can be accomplished together with and at the same time as modifications
to the foreground and background color. Other attributes, such as blinking text, can be
changed as well by using the appropriate variable.

ANSI graphics were common on old dial-up bulletin-board systems prior to wide-
spread Internet access. Note that when the blink attribute is used, the action of blinking
utilizes network resources when displaying in remote terminals. The bandwidth used is
probably minimal, but you should be aware of it. The blink attribute poses more of an
issue when it is used over a serial connection with a modem.
The next section of code grabs more data from various files in /proc to get a few more
items of information, such as processor type and NIS domain name. The script then
determines if the processor is manufactured by Intel or AMD and changes the output
color depending on the vendor before printing this portion of the report to the screen.
processor=`grep 'model name' /proc/cpuinfo | cut -d: -f2 | cut -c2-`
nisdomain=`cat /proc/sys/kernel/domainname`
cache=`grep 'cache size' /proc/cpuinfo | awk '{print $4,$5}'`
bogomips=`grep 'bogomips' /proc/cpuinfo | awk '{print $3}'`
vendor=`grep 'vendor_id' /proc/cpuinfo`
echo -e "Hostname: $white$hostname$normal NIS Domain: $white$nisdomain$normal"
if [ "`echo $vendor | grep -i intel`" ]
then
cpu_color=$blue
elif [ "`echo $vendor | grep -i amd`" ]
then
cpu_color=$green
fi
echo -e "Processor: $cpu_color$processor$normal"
echo -e " Running at $white$bogomips$normal bogomips with\
$white$cache$normal cache"
echo
Now the script gathers information about the operating system and kernel, including
their versions and release numbers. It also obtains the system uptime and outputs col-
orized text reporting the results.
ostype=`cat /proc/sys/kernel/ostype`

osrelease=`cat /proc/sys/kernel/osrelease`
rev=`cat /proc/sys/kernel/version | awk '{print $1}'`
da_date=`cat /proc/sys/kernel/version | cut -d\ -f2-`
upsec=`awk '{print $1}' /proc/uptime`
uptime=`echo "scale=2;$upsec/86400" | bc`
226
CHAPTER 35

COLORFUL /PROC REPORTING
echo -e "OS Type: $white$ostype$normal Kernel:\
$white$osrelease$normal"
echo -e " Kernel Compile $white$rev$normal on\
$white$da_date$normal"
echo -e "Uptime: $magenta$uptime$normal days"
These lines gather system memory and swap information and calculate the proportion
of these system resources currently used by the running processes. One interesting bit is
the use of the set command; it uses the result of greping through /proc/meminfo to assign
values to positional parameters. This is a convenient way of assigning each item in a space-
separated sequence of items to its own variable.
set `grep MemTotal /proc/meminfo`
tot_mem=$2 ; tot_mem_unit=$3
set `grep MemFree /proc/meminfo`
free_mem=$2 ; fre_mem_unit=$3
perc_mem_used=$((100-(100*free_mem/tot_mem)))
set `grep SwapTotal /proc/meminfo`
tot_swap=$2 ; tot_swap_unit=$3
set `grep SwapFree /proc/meminfo`
free_swap=$2 ; fre_swap_unit=$3
perc_swap_used=$((100-(100*free_swap/tot_swap)))
Once the script has calculated percentages, the color of the output is set depending on

the usage value that is being reported. Messages reporting usage levels of less than 80 per-
cent appear in green, values between 80 and 90 percent appear in yellow, and 90 percent
utilization or greater values appear in red. I have chosen the percentages somewhat arbi-
trarily. One improvement to the script would be to replace the percentage values with
variables that are initialized using either a command-line switch or a configuration file.
if [ $perc_mem_used -lt 80 ]
then
mem_color=$green
elif [ $perc_mem_used -ge 80 -a $perc_mem_used -lt 90 ]
then
mem_color=$yellow
else
mem_color=$red
fi
if [ $perc_swap_used -lt 80 ]
then
swap_color=$green
elif [ $perc_swap_used -ge 80 -a $perc_swap_used -lt 90 ]
then
swap_color=$yellow
CHAPTER 35

COLORFUL /PROC REPORTING
227
else
swap_color=$red
fi
echo -e "Memory: $white$tot_mem$normal $tot_mem_unit Free: $white$free_mem$normal \
$fre_mem_unit %Used: $mem_color$perc_mem_used$normal"
echo -e "Swap: $white$tot_swap$normal $tot_swap_unit Free: $white$free_swap$normal \

$fre_swap_unit %Used: $swap_color$perc_swap_used$normal"
echo
In addition to memory and swap utilization, the script determines the system-load
averages, again using the set command to assign values to the positional variables. The
/proc/loadavg file contains a single line of space-separated fields, where the first three
fields are the 1-minute, 5-minute, and 15-minute load averages of the running system.
set `cat /proc/loadavg`
one_min=$1
five_min=$2
fifteen_min=$3
echo -n "Load Average:"
for ave in $one_min $five_min $fifteen_min
do
int_ave=`echo $ave | cut -d. -f1`
if [ $int_ave -lt 1 ]
then
echo -en " $green$ave$normal"
elif [ $int_ave -ge 1 -a $int_ave -lt 5 ]
then
echo -en " $yellow$ave$normal"
else
echo -en " $red$ave$normal"
fi
done
echo
When the file’s output is applied to the set command, the first three fields become the
values of the positional parameter variables $1, $2, and $3. Once these values have been
obtained, the script adjusts the color of the text. A load average below 1 will be reported in
green, between 1 and 5 in yellow, and any value 5 or greater in red. Keep in mind that the
colors for the load averages are assumed to be for single-CPU systems.

The final operation counts the running processes and determines their states. Once the
total number of processes is calculated, the script displays the process count. It also gives
a breakdown of processes by status type, telling the user how many processes are run-
ning, sleeping, stopped, or zombified.

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×