Sunday, October 20, 2013

Raspberry system information

Sometimes it is useful to see some basic information when logging onto your PI. In my case I wanted to see if some processes are running, so I created simple script, that writes those and few additional information.

First a screenshot how it looks:


In the picture is motd and afterwards my script prints system information: CPU temperature, processes check, network interfaces and uptime. This script is triggered via .bashrc.

And here's the script:
#!/bin/bash
# krisko 2014
# script to check services and interfaces state

# define services to check and network interface
SERVICES=('nginx' 'transmission' 'php-fpm' 'mysqld' 'nfsd' 'fail2ban-server')
INTERFACES=('eth0' 'wlan0')

# print RaspberryPI CPU temp, record needs to be in sudoers
printf '\n%s%*s%s\n' "CPU Temperature:" 31 "$(sudo /opt/vc/bin/vcgencmd measure_temp | cut -d= -f2)"

# set IFS to handle spaces in names
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

# set colors
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
RESET=$(tput sgr0)

# check processes
printf '%s\n' "Running processes:"
for PROC in ${SERVICES[*]}; do
        let col=40-${#PROC}
        if [ $(pgrep $PROC 1>/dev/null; echo $?) -eq 0 ]; then
                printf '  %s%*s%s%s%s%s%s\n' "$PROC" $col "[" "$GREEN" " OK " "$RESET" "]"
        else
                printf '  %s%*s%s%s%s%s%s\n' "$PROC" $col "[" "$RED" "FAIL" "$RESET" "]"
        fi
done

IFS=$SAVEIFS

# check network interfaces
printf '\n%s\n' "Network interfaces:"
for INT in ${INTERFACES[*]}; do
        let col=45-${#INT}
        IP=$(ifconfig | grep -A1 $INT | grep inet | awk '{print $2}')
        if [ ! -z ${IP} ]; then
                printf '  %s%*s%s\n' "$INT" $col "$IP"
        else
                printf '  %s%*s%s%s%s%s%s\n' "$INT" $(($col-5)) "[" "$RED" "DOWN" "$RESET" "]"
        fi
done

# print uptime
printf '\n%s\n %s\n\n' "Uptime:" "$(uptime)"

exit 0

##################
# ChangeLog/TODO #
##################
# 1.0.0 2014-01-01 Genesis
# 1.0.1 2014-03-12 Removed unused/commented lines
##################

No comments:

Post a Comment