Difference between revisions of "Ministat"

From James Dooley's Wiki
Jump to: navigation, search
(Script)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
[[Category:Scripts]]
 
==Overview==
 
==Overview==
 
Down and dirty mini status viewer. Makes it easy to monitor several different sections of a server at the same time.
 
Down and dirty mini status viewer. Makes it easy to monitor several different sections of a server at the same time.
Line 10: Line 11:
 
==Script==
 
==Script==
  
<code>[bash,n]
+
<source lang='bash'>
 
#!/bin/bash
 
#!/bin/bash
 
function checkmysql {
 
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/ /\|/' -e 's/ /\|/' | awk 'BEGIN {FS="|"}; { printf "%-4s%-15s%-8s%-30s",$3,$1,$2,$4; print "" }'`
+
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 != "" ]]
 
if [[ $cmdmysql != "" ]]
 
then
 
then
Line 25: Line 26:
 
if [ -d "/var/cpanel" ]
 
if [ -d "/var/cpanel" ]
 
then
 
then
cmdapache=`httpd status 2>&1 | sed -n '/requests\/sec/,/Scoreboard/p' | sed 'N;$!P;$!D;$d' | grep "requests/sec" -A 999`
+
cmdapache=`service httpd status 2>&1 | sed -n '/requests\/sec/,/Scoreboard/p' | sed 'N;$!P;$!D;$d' | grep "requests/sec" -A 999`
 
else
 
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`
 
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`
Line 51: Line 52:
 
}
 
}
  
 +
termwidth=`tput cols`
 
cat /proc/loadavg
 
cat /proc/loadavg
 
addseperator
 
addseperator
Line 73: Line 75:
 
echo "$cmdapache" | head -$aline
 
echo "$cmdapache" | head -$aline
 
fi
 
fi
</code>
+
</source>
  
 
==Usage==
 
==Usage==
  
Direct download and execution of script (auto mode)
+
Direct download and execution of script (auto mode) from ent.int server:
  
<code>[bash,n]wget http://wiki.jamesdooley.us/scripts/ministat -O ministat; watch -n 10 sh ministat; rm -f ministat</code>
+
<code>[bash,n]wget http://scripts.ent.liquidweb.com/ministat -O ministat; watch sh ministat; rm -f ministat</code>
 +
 
 +
Direct download and execution of script (auto mode) from this server:
 +
 
 +
<code>[bash,n]wget http://svn.jamesdooley.us/svn/JDooley/Ministat/ministat.sh -O ministat; watch sh ministat; rm -f ministat</code>
  
 
==What to change==
 
==What to change==

Latest revision as of 11:41, 9 August 2014

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