Difference between revisions of "Ministat"

From James Dooley's Wiki
Jump to: navigation, search
(Overview)
 
(10 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
cmdtop=`top -c -b -n 1 | grep "PID" -A999`
+
function checkmysql {
cmdmysql=`mysqladmin proc stat | grep "|" | grep -v "Id" | awk 'BEGIN{FS="|"};{print $7,$6,$9}' | sort -n`
+
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
 +
}
  
wctop=`echo $cmdtop | wc -l`
+
termwidth=`tput cols`
wcmysql=`echo $cmdmysql | wc -l`
+
cat /proc/loadavg
 +
addseperator
 +
 
 +
checkmysql
 +
checkapache
 +
calclines
 +
 
 +
top -c -b -n 1 | grep -v "grep" | grep "PID" -A $nline
 +
addseperator
 +
 
 +
free | grep -v "cache:"
  
nline=`tput lines | awk '{print int(($1 - 18) / 2)}'`
+
if [ $wcmysql -gt 0 ]
if [ $nline -gt $wcmysql ]  
+
then
 +
addseperator
 +
echo "$cmdmysql" | head -$nline
 +
fi
 +
if [ $wcapache -gt 0 ]
 
then
 
then
  nline=`echo $nline $wcmysql | awk '{print $1 * 2 - $2}'`
+
addseperator
 +
echo "$cmdapache" | head -$aline
 
fi
 
fi
 +
</source>
 +
 +
==Usage==
  
#Display Junk
+
Direct download and execution of script (auto mode) from ent.int server:
cat /proc/loadavg
 
printf "%$(tput cols)s\n"|tr ' ' '='
 
top -c -b -n 1 | grep "PID" -A999 | head -$nline
 
printf "%$(tput cols)s\n"|tr ' ' '='
 
echo $cmdmysql | head -$nline
 
printf "%$(tput cols)s\n"|tr ' ' '='
 
free | grep -v "cache:"
 
printf "%$(tput cols)s\n"|tr ' ' '='
 
httpd status | grep "requests/sec" -A6
 
</code>
 
  
==Usage==
+
<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)
+
Direct download and execution of script (auto mode) from this 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://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