Difference between revisions of "Ministat"

From James Dooley's Wiki
Jump to: navigation, search
(Created page with "==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 p...")
 
 
(18 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.
  
 
Useful for monitoring a server that is having problems.
 
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==
 
==Script==
  
<code>[bash,n]
+
<source lang='bash'>
 +
#!/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
 
cat /proc/loadavg
printf "%$(tput cols)s\n"|tr ' ' '='
+
addseperator
top -c -b -n 1 | head -12 | tail -5
+
 
printf "%$(tput cols)s\n"|tr ' ' '='
+
checkmysql
mysqladmin proc stat | grep "Query" | cut -d "|" -f7,9 | sed "s/ //g" | sort -n | head
+
checkapache
printf "%$(tput cols)s\n"|tr ' ' '='
+
calclines
free
+
 
printf "%$(tput cols)s\n"|tr ' ' '='
+
top -c -b -n 1 | grep -v "grep" | grep "PID" -A $nline
httpd status | head -23 | tail -7
+
addseperator
</code>
+
 
 +
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
 +
</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://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://wiki.jamesdooley.us/scripts/ministat -O ministat; watch 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