Ministat

From James Dooley's Wiki
Jump to: navigation, search

Overview

Down and dirty mini status viewer. Makes it easy to monitor several different sections of a server at the same time.

Useful for monitoring a server that is having problems.

The main concept is to minify the output of several sections in to a small terminal window, useful for monitoring a server when it is having problems or could at some point.

Auto adjusts output based on size of the terminal.

Script

#!/bin/bash
function checkmysql {
	cmdmysql=`mysql --batch -e 'show processlist;' | grep -v "Id" | cut -f6,5,4,8 | sed -e 's/\t/ /g' | sort -nr --key=3 | sed -e 's/ /\|/' -e 's/ /\|/' -e 's/ /\|/' | awk -v termwidth="$termwidth" 'BEGIN {FS="|"}; {termwidth=termwidth - 39; query=$4; query=substr("'"${query}"'",1,termwidth) }; { printf "%-3s%-17s%7-s%-" termwidth "s%-1s",$3,$1,$2,$4,query; print ""; }' | sed '/^$/d' `
	if [[ $cmdmysql != "" ]]
	then
		wcmysql=`echo "$cmdmysql" | wc -l`
	else
		wcmysql=0
	fi
	return
}
function checkapache {
	if [ -d "/var/cpanel" ]
	then
		cmdapache=`service httpd status 2>&1 | sed -n '/requests\/sec/,/Scoreboard/p' | sed 'N;$!P;$!D;$d' | grep "requests/sec" -A 999`
	else
		cmdapache=`lynx --dump http://localhost/server-status 2>&1 | sed -n '/requests\/sec/,/Scoreboard/p' | sed 'N;$!P;$!D;$d' | grep "requests/sec" -A 999`
	fi
	if [[ $cmdapache != "" ]]
	then
		wcapache=`echo "$cmdapache" | wc -l`
	else
		wcapache=0
	fi
	return
}
function calclines {
	nline=`tput lines | awk '{print int(($1 - 14) / 3)}'`
	aline=`echo $nline 3 | awk '{print $1 + $2}'`
	if [ $nline -gt $wcmysql ]
	then
		nline=`echo $nline $wcmysql | awk '{print $1 * 2 - $2}'`
	fi
	return
}
function addseperator {
	printf "%$(tput cols)s\n"|tr ' ' '='
	return
}

termwidth=`tput cols`
cat /proc/loadavg
addseperator

checkmysql
checkapache
calclines

top -c -b -n 1 | grep -v "grep" | grep "PID" -A $nline
addseperator

free | grep -v "cache:"

if [ $wcmysql -gt 0 ]
then
	addseperator
	echo "$cmdmysql" | head -$nline
fi
if [ $wcapache -gt 0 ]
then
	addseperator
	echo "$cmdapache" | head -$aline
fi

Usage

Direct download and execution of script (auto mode) from ent.int server:

[bash,n]wget http://scripts.ent.liquidweb.com/ministat -O ministat; watch sh ministat; rm -f ministat

Direct download and execution of script (auto mode) from this server:

[bash,n]wget http://svn.jamesdooley.us/svn/JDooley/Ministat/ministat.sh -O ministat; watch sh ministat; rm -f ministat

What to change