Show Server Stats in the Today Sidebar with Today-Scripts and Glimpse

Glimpse is a super simple server vital starts service written in bash. There are many much more involved services out there, such as collectd for instance. However, I needed something really simple to show very basic server stats in the today widget on OSX using Today-Scripts, so I made glimpse.

Installing Glimpse

The only prerequisite of glimpse that you may not have on a standard linux system is the socat utility necessary for glimpsed. To install it just run apt-get install socat or yum install socat or similar install command for your system.

After this, just copy glimpse and glimpsed into a folder on your system and start it by running glimpsed.

Using Glimpse

Now that the glimpse server is running, simply go to http://server-ip:5488/ to see the server stats. You can change the port number by editing the glimpsed file.

To look at the server stats from another command line, simply run curl http://server-ip:5488/.

In my case I wanted to use it with the wonderful Today-Scripts. Grab the Today Scripts widget, install it, and add curl http://server-ip:5488/ as one of your scripts. Now you have some basic server stats in the today side bar.

Final Thoughts

Released under MIT License. Enjoy!

Source Code

glimpse

#!/bin/bash

# Uptime
uptime=$(uptime)
uptime=${uptime%,*user*}
uptime=${uptime#*up }

# Users
usercount=$(who | wc -l)

# System Load
load=$(uptime)
load=${load#*\: }

#PS Count
pscount=$(ps -A --no-headers | wc -l)

# Memory Usage
ramtotal=$(free -m | grep Mem | while read a b c; do echo "$b"; done)
ramused=$(free -m | grep buffers/cache | while read a b c d; do echo "$c"; done)
ramfree=$(free -m | grep buffers/cache | while read a b c d e; do echo "$d"; done)

# Disk Space
diskspace=$(df -h | grep /dev/ | while read a b c d e f; do printf "%-9s%s GB Left\n" "${a#*/dev/}:" "${d%G}"; done)

# Output
printf "%-9s%s\n" "Uptime:" "$uptime"
printf "%-9s%s\n" "Users:" "$usercount"
printf "%-9s%s\n" "Load:" "$load"
printf "%-9s%s\n" "Procs:" "$pscount"
printf "%-9s%s MB Free\n" "Memory:" "$ramfree"
printf "$diskspace\n"

# RX and TX Speeds
interface="/sys/class/net/eth0"

rx1=$(cat $interface/statistics/rx_bytes)                                      
tx1=$(cat $interface/statistics/tx_bytes)

sleep 1s

rx2=$(cat $interface/statistics/rx_bytes)                                      
tx2=$(cat $interface/statistics/tx_bytes)

rx="$((($rx2-$rx1)/1024)) Kb ↓"
tx="$((($tx2-$tx1)/1024)) Kb ↑"

printf "%-9s%s / %s\n" "Net:" "$tx" "$rx"

glimpsed

#!/bin/bash

# Location of the Glimpse Script
scriptdir=$(cd "$( dirname "$0" )" && pwd)
glimpse=$scriptdir/glimpse

# Port to Run the Server On
port=5488

# Run the Server
nohup socat TCP4-LISTEN:$port,fork EXEC:$glimpse &