使用BerryClip监控Raspberry Pi CPU使用情况

3

berryclip_with_pi_2013_03_#16在网上漫步时,我发现了一篇文章 Raspberry Pi:使用GPIO监视CPU使用率 由chteuchteu。这说明了如何在GPIO接头连接的7个LED上显示当前的CPU使用情况。

我认为这将是一个很棒的应用程序,可以与BerryClip 6 LED 蜂鸣器板一起使用。因此,我修改了原始代码以使用6个LED并调整了一些功能。除了LED计数之外,我还更改了脚本以使用GPIO引用数组,因此如果使用不同的引脚,则更新起来会更加容易。

我从全新安装最新的Raspbian SD卡映像开始。

#!/bin/bash
#--------------------------------------
#    ___                   ________
#   / _ )___ __________ __/ ___/ (_)__
#  / _  / -_) __/ __/ // / /__/ / / _ \
# /____/\__/_/ /_/  \_, /\___/_/_/ .__/
#                  /___/        /_/
#
#       浆果夹 - 6  LED  Board
#
# This script uses 6  LED s to  显示  the
# current CPU usage.
#
# Author :  马特  Hawkins
# Date   : 12/05/2013
#
# //www.ytguangda.com/
#
# Original script created by Chteuchteu
# http://www.chteuchteu.com/
#
# Modified by  马特  Hawkins to use the 6  LED s
#  上  the 浆果夹 addon board.
#
# 浆果夹  LED  reference :
#  LED  1  - Pin 7  -  通用输入输出 4
#  LED  2  - Pin 11 -  通用输入输出 17
#  LED  3  - Pin 15 -  通用输入输出 22
#  LED  4  - Pin 19 -  通用输入输出 10
#  LED  5  - Pin 21 -  通用输入输出 9
#  LED  6  - Pin 23 -  通用输入输出 11
#
#--------------------------------------

# Create array of  通用输入输出  references
led[1]="11"
led[2]="9"
led[3]="10"
led[4]="22"
led[5]="17"
led[6]="4"

# Configure  通用输入输出 s to be outputs
echo ${led[1]} > /sys/class/gpio/export 2> /dev/null
echo ${led[2]} > /sys/class/gpio/export 2> /dev/null
echo ${led[3]} > /sys/class/gpio/export 2> /dev/null
echo ${led[4]} > /sys/class/gpio/export 2> /dev/null
echo ${led[5]} > /sys/class/gpio/export 2> /dev/null
echo ${led[6]} > /sys/class/gpio/export 2> /dev/null
echo "out" > /sys/class/gpio/gpio${led[1]}/direction
echo "out" > /sys/class/gpio/gpio${led[2]}/direction
echo "out" > /sys/class/gpio/gpio${led[3]}/direction
echo "out" > /sys/class/gpio/gpio${led[4]}/direction
echo "out" > /sys/class/gpio/gpio${led[5]}/direction
echo "out" > /sys/class/gpio/gpio${led[6]}/direction

#################################################
#                   FUNCTIONS                   #
#################################################

number=0
gpio=0

function  LED ON {
  # Turn ON the  LED  "number"

  gpio=${led[number]}

  echo "1" > /sys/class/gpio/gpio$gpio/value
  number=0
  gpio=0
}
function  LED OFF {
  # Allow you to turn off the del (the number of the del is contained in "number")
  # number  pin is done at the begining of the function

  gpio=${led[number]}

  echo "0" > /sys/class/gpio/gpio$gpio/value
  number=0
  gpio=0
}

function allON
{
  # Turn ON all the  LED s
  for i in {1..6}
  do
     number=i ;  LED ON
  done

}
function allOFF
{
  # Turn OFF all the  LED s
  for i in {1..6}
  do
     number=i ;  LED OFF
  done

}
function flash
{
  counter=0
  while [ $counter -le $1 ]
  do
    allON ; sleep 0.2
    allOFF ; sleep 0.2
    counter=$(( $counter + 1 ))
  done

}

#################################################
#                   MAIN CODE                   #
#################################################

# Turn ON all  LED s in sequence
number=1 ;  LED ON ; sleep 0.2
number=2 ;  LED ON ; sleep 0.2
number=3 ;  LED ON ; sleep 0.2
number=4 ;  LED ON ; sleep 0.2
number=5 ;  LED ON ; sleep 0.2
number=6 ;  LED ON ; sleep 1

# Turn all  LED s OFF
allOFF

# Flash the  LED s 5 times
flash 5

# Start main loop
while true ; do

  # Obtain the current CPU usage percentage
  idle=`vmstat 2 3 | tail -n1 | sed "s/\ \ */\ /g" | cut -d' ' -f 16`
  cpu=$(( 100 - idle ))

  # Switch the  LED s ON or OFF the "cpu" value
  allOFF
  if [ ${cpu} -lt 5 ] ; then
    allOFF
  elif [ ${cpu} -lt 23 ] ; then
    number=1   ;  LED ON
  elif [ ${cpu} -lt 41 ] ; then
    number=1   ;  LED ON
    number=2   ;  LED ON
  elif [ ${cpu} -lt 59 ] ; then
    number=1   ;  LED ON
    number=2   ;  LED ON
    number=3   ;  LED ON
  elif [ ${cpu} -lt 77 ] ; then
    number=1   ;  LED ON
    number=2   ;  LED ON
    number=3   ;  LED ON
    number=4   ;  LED ON
  elif [ ${cpu} -lt 95 ] ; then
    number=1   ;  LED ON
    number=2   ;  LED ON
    number=3   ;  LED ON
    number=4   ;  LED ON
    number=5   ;  LED ON
  else
    number=1   ;  LED ON
    number=2   ;  LED ON
    number=3   ;  LED ON
    number=4   ;  LED ON
    number=5   ;  LED ON
    number=6   ;  LED ON
  fi

done

# Turn all  LED s OFF
allOFF

exit 0

可以使用以下命令将该脚本直接下载到您的Pi:

wget //www.ytguangda.com/berryclip/6_led/berryclip_cpu_01.sh

必须使脚本可执行:

chmod +x 浆果夹_cpu_01.sh

然后可以使用以下命令从命令行运行:

sudo ./berryclip_cpu_01.sh &

行末尾的与号是运行脚本作为后台进程并返回命令行的必需项。运行脚本时显示的数字是进程ID。您可以通过键入以下命令来检查进程是否仍在运行:

ps aux | grep 浆果夹

您应该看到具有相同ID的进程。如果要关闭该过程,可以键入:

sudo kill 1234

其中1234是您的进程ID。

LED 将根据CPU利用率而亮起。每个LED点亮所需的百分比如下所示:

None   less than 5%
1  LED   5% or greater
2  LED  23% or greater
3  LED  41% or greater
4  LED  59% or greater
5  LED  77% or greater
6  LED  95% or greater

如果现在运行“startx”并使用某些应用程序(例如,Midori),您将看到CPU监视器栩栩如生!如果你不这样做’与本示例中使用的百分比一样,它们很容易通过调整脚本中的适当数字来进行更改。

这是一些使用中的显示器的照片:

分享。

3条留言

发表评论

该网站使用Akismet减少垃圾邮件。 了解如何处理您的评论数据.