<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Saiweb &#187; Bash Script</title>
	<atom:link href="http://www.saiweb.co.uk/category/bash-script/feed" rel="self" type="application/rss+xml" />
	<link>http://www.saiweb.co.uk</link>
	<description>Ramblings of a Sys admin</description>
	<lastBuildDate>Mon, 06 Feb 2012 14:57:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Linux &#8211; Generating file manifests and then checking them</title>
		<link>http://www.saiweb.co.uk/linux/linux-generating-file-manifests-and-then-checking-them</link>
		<comments>http://www.saiweb.co.uk/linux/linux-generating-file-manifests-and-then-checking-them#comments</comments>
		<pubDate>Mon, 07 Sep 2009 10:50:26 +0000</pubDate>
		<dc:creator>Buzz</dc:creator>
				<category><![CDATA[Bash Script]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://www.saiweb.co.uk/?p=766</guid>
		<description><![CDATA[This issue has come about whilst having to migrate a positively huge number of files, and have to check the integrity of the transfer. Build the manifest 1find /path/to/folder -type f -print0 &#124; xargs --null md5sum &#62; /path/to/manifest -type f : This flag tells find to only return files -print0: This flag tells find to [...]]]></description>
			<content:encoded><![CDATA[<p>This issue has come about whilst having to migrate a positively huge number of files, and have to check the integrity of the transfer.</p>
<p><strong>Build the manifest</strong></p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>folder <span style="color: #660033;">-type</span> f <span style="color: #660033;">-print0</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">--null</span> md5sum <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>manifest</div></td></tr></tbody></table></div>
<ul>
<li>-type f : This flag tells find to only return files</li>
<li>-print0: This flag tells find to null terminate strings, this allows us to take files with spaces</li>
<li>&#8211;null: This flag tells xargs to accept null terminated strings</li>
<li><strong>NOTE: PUT THE MANIFEST OUTSIDE THE FOLDER YOU ARE INDEXING!</strong></li>
</ul>
<p><strong>Checking the manifest</strong></p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">md5sum <span style="color: #660033;">--check</span> <span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>manifest <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> FAILED</div></td></tr></tbody></table></div>
<p>The above will return all failed checks, if you want a simple count (maybe for automated reporting) just add  | wc -l</p>
<p><strong>FAQ</strong></p>
<p><span style="text-decoration: underline;">How big is the manifest?</span></p>
<p>This depends entirely on the length of your filepaths, taking UTF-8 as an encoding example each char is 8bits or 1byte, each manifest line consists of the md5hash, a space and the filepath as the filepath length varies there is no exact way to estimate the filesize of the manifest.</p>
<p>However each line is always 32 + 1 + len(path) bytes.</p>
<p>The more sub directories you have the larger the manifest size will be.</p>
<p><span style="text-decoration: underline;">How long does the manifest take to build?</span></p>
<p>This depends on the number of files you have to index, along with any other factors such as network shares, in test runs 2819 files indexed in 1.493 seconds.<span style="float: left;" ><a class="twitter-share-button"  data-via="Saiweb" data-count="horizontal" data-related="Saiweb:David Busby" data-lang="en" data-url="http://www.saiweb.co.uk/linux/linux-generating-file-manifests-and-then-checking-them" data-text="Linux &#8211; Generating file manifests and then checking them" href="http://twitter.com/share?via=Saiweb&#038;count=horizontal&#038;related=Saiweb%3ADavid%20Busby&#038;lang=en&#038;url=http%3A%2F%2Fwww.saiweb.co.uk%2Flinux%2Flinux-generating-file-manifests-and-then-checking-them&#038;text=Linux%20%26%238211%3B%20Generating%20file%20manifests%20and%20then%20checking%20them" >Tweet</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.saiweb.co.uk/linux/linux-generating-file-manifests-and-then-checking-them/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>mySQL bash backup script</title>
		<link>http://www.saiweb.co.uk/mysql/mysql-bash-backup-script</link>
		<comments>http://www.saiweb.co.uk/mysql/mysql-bash-backup-script#comments</comments>
		<pubDate>Tue, 31 Mar 2009 09:06:33 +0000</pubDate>
		<dc:creator>Buzz</dc:creator>
				<category><![CDATA[Bash Script]]></category>
		<category><![CDATA[mySQL]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[gzip]]></category>

		<guid isPermaLink="false">http://www.saiweb.co.uk/?p=622</guid>
		<description><![CDATA[In on of those &#8220;oh ffs&#8221; moments I found myself writing a BASH script to quickly dump all database on a mySQL server. 12345678910111213141516#!/bin/bash MYSQL=`which mysql`; MYSQLDUMP=`which mysqldump`; GZIP=`which gzip`; DEST=&#34;/path/to/dump/folder&#34; USER=&#34;root&#34;; PWD=&#34;XXXXXX&#34;; DBS=&#40;`$MYSQL &#160;-u $USER -p$PWD &#160;-Bse 'show databases'`&#41;; for db in ${DBS[@]}; do &#160; &#160; &#160; &#160; `$MYSQLDUMP --default-character-set=utf8 --set-charset -u $USER -p$PWD [...]]]></description>
			<content:encoded><![CDATA[<p>In on of those &#8220;oh ffs&#8221; moments I found myself writing a BASH script to quickly dump all database on a mySQL server.</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">#!/bin/bash</span><br />
<span style="color: #007800;">MYSQL</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">which</span> mysql<span style="color: #000000; font-weight: bold;">`</span>;<br />
<span style="color: #007800;">MYSQLDUMP</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">which</span> mysqldump<span style="color: #000000; font-weight: bold;">`</span>;<br />
<span style="color: #007800;">GZIP</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">which</span> <span style="color: #c20cb9; font-weight: bold;">gzip</span><span style="color: #000000; font-weight: bold;">`</span>;<br />
<span style="color: #007800;">DEST</span>=<span style="color: #ff0000;">&quot;/path/to/dump/folder&quot;</span><br />
<br />
<span style="color: #007800;">USER</span>=<span style="color: #ff0000;">&quot;root&quot;</span>;<br />
<span style="color: #007800;">PWD</span>=<span style="color: #ff0000;">&quot;XXXXXX&quot;</span>;<br />
<br />
<span style="color: #007800;">DBS</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #007800;">$MYSQL</span> &nbsp;<span style="color: #660033;">-u</span> <span style="color: #007800;">$USER</span> -p<span style="color: #007800;">$PWD</span> &nbsp;<span style="color: #660033;">-Bse</span> <span style="color: #ff0000;">'show databases'</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;<br />
<br />
<span style="color: #000000; font-weight: bold;">for</span> db <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #800000;">${DBS[@]}</span>;<br />
<span style="color: #000000; font-weight: bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">`</span><span style="color: #007800;">$MYSQLDUMP</span> <span style="color: #660033;">--default-character-set</span>=utf8 <span style="color: #660033;">--set-charset</span> <span style="color: #660033;">-u</span> <span style="color: #007800;">$USER</span> -p<span style="color: #007800;">$PWD</span> <span style="color: #007800;">$db</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #007800;">$GZIP</span> <span style="color: #660033;">-9</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$DEST</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$db</span>.sql.gz<span style="color: #000000; font-weight: bold;">`</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$db</span> - DONE&quot;</span>;<br />
<span style="color: #000000; font-weight: bold;">done</span>;</div></td></tr></tbody></table></div>
<p>This script gets a list of all databases, dumps them out with UTF8 encoding, and gzip compresses the SQL file into the given &#8220;DEST&#8221; folder.</p>
<p>If you want to skip over certain databases i.e. &#8220;mysql&#8221;</p>
<p>Change this line:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #007800;">DBS</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #007800;">$MYSQL</span> &nbsp;<span style="color: #660033;">-u</span> <span style="color: #007800;">$USER</span> -p<span style="color: #007800;">$PWD</span> &nbsp;<span style="color: #660033;">-Bse</span> <span style="color: #ff0000;">'show databases'</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>To:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #007800;">DBS</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #007800;">$MYSQL</span> &nbsp;<span style="color: #660033;">-u</span> <span style="color: #007800;">$USER</span> -p<span style="color: #007800;">$PWD</span> &nbsp;<span style="color: #660033;">-Bse</span> <span style="color: #ff0000;">'show databases'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> <span style="color: #ff0000;">&quot;database_to_exclude&quot;</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>Or for multiple exclusions</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #007800;">DBS</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #007800;">$MYSQL</span> &nbsp;<span style="color: #660033;">-u</span> <span style="color: #007800;">$USER</span> -p<span style="color: #007800;">$PWD</span> &nbsp;<span style="color: #660033;">-Bse</span> <span style="color: #ff0000;">'show databases'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> <span style="color: #ff0000;">&quot;database_to_exclude&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> <span style="color: #ff0000;">&quot;another_database_to_exclude&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> <span style="color: #ff0000;">&quot;etc&quot;</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>I may re-write this in Python, if I get time.</p>
<p><span style="float: left;" ><a class="twitter-share-button"  data-via="Saiweb" data-count="horizontal" data-related="Saiweb:David Busby" data-lang="en" data-url="http://www.saiweb.co.uk/mysql/mysql-bash-backup-script" data-text="mySQL bash backup script" href="http://twitter.com/share?via=Saiweb&#038;count=horizontal&#038;related=Saiweb%3ADavid%20Busby&#038;lang=en&#038;url=http%3A%2F%2Fwww.saiweb.co.uk%2Fmysql%2Fmysql-bash-backup-script&#038;text=mySQL%20bash%20backup%20script" >Tweet</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.saiweb.co.uk/mysql/mysql-bash-backup-script/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Bash Hello World in 60 seconds</title>
		<link>http://www.saiweb.co.uk/linux/bash-hello-world-in-60-seconds</link>
		<comments>http://www.saiweb.co.uk/linux/bash-hello-world-in-60-seconds#comments</comments>
		<pubDate>Mon, 19 Jan 2009 11:49:08 +0000</pubDate>
		<dc:creator>Buzz</dc:creator>
				<category><![CDATA[Bash Script]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[hello world]]></category>

		<guid isPermaLink="false">http://www.saiweb.co.uk/linux/bash-hello-world-in-60-seconds</guid>
		<description><![CDATA[How to write a bash &#8216;hello world&#8217; script in 60 seconds, admitedly it could of been faster &#8230; damn typos Also the first line you can add as an alias, if your going to be writing a lot of bash scripts. WPFP(document).ready(function() { //load player $f("saiweb_a616f55dc1da68a06c734da061cca981", "/wp-content/plugins/wordpress-flowplayer/flowplayer/gpl/flowplayer-3.1.5.swf", { plugins: { controls: { sliderGradient: 'none', progressGradient: [...]]]></description>
			<content:encoded><![CDATA[<p>How to write a bash &#8216;hello world&#8217; script in 60 seconds, admitedly it could of been faster &#8230; damn typos</p>
<p>Also the first line you can add as an alias, if your going to be writing a lot of bash scripts.</p>
<p><div id="saiweb_5ba4989a36bc543b38f335a303458a6f" style="width:600px; height:200px;"></div><script language="Javascript" type="text/javascript">
	WPFP(document).ready(function() {
		//load player
		$f("saiweb_5ba4989a36bc543b38f335a303458a6f", "/wp-content/plugins/wordpress-flowplayer/flowplayer/gpl/flowplayer-3.1.5.swf", {
				plugins: {
  					 controls: {    					
      					
      					
      					
      					sliderGradient: 'none',
      					progressGradient: 'medium',
      					
      					
      					backgroundColor: '#141648',
      					
      					
      					backgroundGradient: 'none',
      					bufferGradient: 'none',
   						opacity:1.0
   						}
				},
			clip: {
					url:'bash_hello_world.mp4',
					autoPlay: false,
       				autoBuffering: false
				},
				canvas: {
					backgroundColor:''
				}})
			});</script>
				</p>
<p>Or you can copy paste and have it done in about 5 seconds <img src='http://cdn.saiweb.co.uk/wp-includes/images/smilies/icon_razz.gif' alt=':-P' class='wp-smiley' /> </p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">BPATH=`which bash`; echo &quot;#! $BPATH&quot; | awk '{print $1$2}' &gt; script.sh</div></td></tr></tbody></table></div>
<p>The reason for the echo and awk is when trying to do echo &#8220;#!$BPATH&#8221; > script.sh my shell wouldn&#8217;t cooperate so all the awk does is take out the space <img src='http://cdn.saiweb.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
<p><span style="float: left;" ><a class="twitter-share-button"  data-via="Saiweb" data-count="horizontal" data-related="Saiweb:David Busby" data-lang="en" data-url="http://www.saiweb.co.uk/linux/bash-hello-world-in-60-seconds" data-text="Bash Hello World in 60 seconds" href="http://twitter.com/share?via=Saiweb&#038;count=horizontal&#038;related=Saiweb%3ADavid%20Busby&#038;lang=en&#038;url=http%3A%2F%2Fwww.saiweb.co.uk%2Flinux%2Fbash-hello-world-in-60-seconds&#038;text=Bash%20Hello%20World%20in%2060%20seconds" >Tweet</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.saiweb.co.uk/linux/bash-hello-world-in-60-seconds/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linux &#8211; The Sysadmin Script &#8211; Part 4</title>
		<link>http://www.saiweb.co.uk/linux/linux-the-sysadmin-script-part-4</link>
		<comments>http://www.saiweb.co.uk/linux/linux-the-sysadmin-script-part-4#comments</comments>
		<pubDate>Fri, 14 Nov 2008 09:31:23 +0000</pubDate>
		<dc:creator>Buzz</dc:creator>
				<category><![CDATA[Bash Script]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[sysadmin]]></category>

		<guid isPermaLink="false">http://www.saiweb.co.uk/uncategorized/linux-the-sysadmin-script-part-4</guid>
		<description><![CDATA[In part 4, I am going to cover more of an improvement than anything else to part 3 Part 3 itself is not incorrect, it correctly takes a memory footprint for each process running, the same as VIRT in top &#8230; However in processes such as APACHE the VIRT memory is the size of all [...]]]></description>
			<content:encoded><![CDATA[<p>In part 4, I am going to cover more of an improvement than anything else to <a href="http://www.saiweb.co.uk/linux/linux-the-sysadmin-script-part-3">part 3</a></p>
<p>Part 3 itself is not incorrect, it correctly takes a memory footprint for each process running, the same as VIRT in top &#8230;</p>
<p>However in processes such as APACHE the VIRT memory is the size of all shared libraries, as correctly shown by pmap &#8230;</p>
<p>So what does this mean realy?</p>
<p>The memory usage is infact the following VIRT + RSS, where RSS is the resident set size, the RSS is a representation of the memory in use by the PID, and VIRT is shared between the child processes.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">[buzz@buzz_srv ~]# ps aux | grep httpd | grep -v 'grep'<br />
root &nbsp; &nbsp; 16378 &nbsp;0.0 &nbsp;0.1 148640 &nbsp;3024 ? &nbsp; &nbsp; &nbsp; &nbsp;Ss &nbsp; Nov13 &nbsp; 0:00 /usr/sbin/httpd<br />
apache &nbsp; 20088 &nbsp;0.0 &nbsp;0.1 148640 &nbsp;3304 ? &nbsp; &nbsp; &nbsp; &nbsp;S &nbsp; &nbsp;Nov13 &nbsp; 0:00 /usr/sbin/httpd<br />
apache &nbsp; 20101 &nbsp;0.0 &nbsp;0.1 148640 &nbsp;3304 ? &nbsp; &nbsp; &nbsp; &nbsp;S &nbsp; &nbsp;Nov13 &nbsp; 0:00 /usr/sbin/httpd<br />
apache &nbsp; 20756 &nbsp;0.0 &nbsp;0.1 148640 &nbsp;3312 ? &nbsp; &nbsp; &nbsp; &nbsp;S &nbsp; &nbsp;Nov13 &nbsp; 0:00 /usr/sbin/httpd<br />
apache &nbsp; 20759 &nbsp;0.0 &nbsp;0.1 148640 &nbsp;3300 ? &nbsp; &nbsp; &nbsp; &nbsp;S &nbsp; &nbsp;Nov13 &nbsp; 0:00 /usr/sbin/httpd<br />
apache &nbsp; 20790 &nbsp;0.0 &nbsp;0.1 148640 &nbsp;3284 ? &nbsp; &nbsp; &nbsp; &nbsp;S &nbsp; &nbsp;Nov13 &nbsp; 0:00 /usr/sbin/httpd<br />
apache &nbsp; 20792 &nbsp;0.0 &nbsp;0.1 148640 &nbsp;3312 ? &nbsp; &nbsp; &nbsp; &nbsp;S &nbsp; &nbsp;Nov13 &nbsp; 0:00 /usr/sbin/httpd<br />
apache &nbsp; 20798 &nbsp;0.0 &nbsp;0.1 148640 &nbsp;3308 ? &nbsp; &nbsp; &nbsp; &nbsp;S &nbsp; &nbsp;Nov13 &nbsp; 0:00 /usr/sbin/httpd<br />
apache &nbsp; 20804 &nbsp;0.0 &nbsp;0.1 148640 &nbsp;3308 ? &nbsp; &nbsp; &nbsp; &nbsp;S &nbsp; &nbsp;Nov13 &nbsp; 0:00 /usr/sbin/httpd<br />
apache &nbsp; 20886 &nbsp;0.0 &nbsp;0.1 148640 &nbsp;3304 ? &nbsp; &nbsp; &nbsp; &nbsp;S &nbsp; &nbsp;Nov13 &nbsp; 0:00 /usr/sbin/httpd<br />
apache &nbsp; 20906 &nbsp;0.0 &nbsp;0.1 148640 &nbsp;3300 ? &nbsp; &nbsp; &nbsp; &nbsp;S &nbsp; &nbsp;Nov13 &nbsp; 0:00 /usr/sbin/httpd<br />
apache &nbsp; 20907 &nbsp;0.0 &nbsp;0.1 148640 &nbsp;3308 ? &nbsp; &nbsp; &nbsp; &nbsp;S &nbsp; &nbsp;Nov13 &nbsp; 0:00 /usr/sbin/httpd<br />
apache &nbsp; 20912 &nbsp;0.0 &nbsp;0.1 148640 &nbsp;3304 ? &nbsp; &nbsp; &nbsp; &nbsp;S &nbsp; &nbsp;Nov13 &nbsp; 0:00 /usr/sbin/httpd<br />
apache &nbsp; 20915 &nbsp;0.0 &nbsp;0.1 148640 &nbsp;3312 ? &nbsp; &nbsp; &nbsp; &nbsp;S &nbsp; &nbsp;Nov13 &nbsp; 0:00 /usr/sbin/httpd<br />
apache &nbsp; 20959 &nbsp;0.0 &nbsp;0.1 148640 &nbsp;3304 ? &nbsp; &nbsp; &nbsp; &nbsp;S &nbsp; &nbsp;Nov13 &nbsp; 0:00 /usr/sbin/httpd<br />
apache &nbsp; 20969 &nbsp;0.0 &nbsp;0.1 148640 &nbsp;3300 ? &nbsp; &nbsp; &nbsp; &nbsp;S &nbsp; &nbsp;Nov13 &nbsp; 0:00 /usr/sbin/httpd<br />
apache &nbsp; 20994 &nbsp;0.0 &nbsp;0.1 148640 &nbsp;3320 ? &nbsp; &nbsp; &nbsp; &nbsp;S &nbsp; &nbsp;Nov13 &nbsp; 0:00 /usr/sbin/httpd<br />
apache &nbsp; 20995 &nbsp;0.0 &nbsp;0.1 148640 &nbsp;3288 ? &nbsp; &nbsp; &nbsp; &nbsp;S &nbsp; &nbsp;Nov13 &nbsp; 0:00 /usr/sbin/httpd<br />
apache &nbsp; 20996 &nbsp;0.0 &nbsp;0.1 148640 &nbsp;3320 ? &nbsp; &nbsp; &nbsp; &nbsp;S &nbsp; &nbsp;Nov13 &nbsp; 0:00 /usr/sbin/httpd<br />
apache &nbsp; 20997 &nbsp;0.0 &nbsp;0.1 148640 &nbsp;3320 ? &nbsp; &nbsp; &nbsp; &nbsp;S &nbsp; &nbsp;Nov13 &nbsp; 0:00 /usr/sbin/httpd<br />
apache &nbsp; 20999 &nbsp;0.0 &nbsp;0.1 148640 &nbsp;3296 ? &nbsp; &nbsp; &nbsp; &nbsp;S &nbsp; &nbsp;Nov13 &nbsp; 0:00 /usr/sbin/httpd</div></td></tr></tbody></table></div>
<p>As can be seen above the &#8216;VIRT&#8217; does not change between the child processes, where as the RSS does dependant on what the thread is doing at that time.</p>
<p>So below is an improved appmem function to allow for this:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">function</span> appmem <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Usage: sysadmin appmem app_name i.e. (sysadmin appmem apache)&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007800;">RRES</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">ps</span> aux <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> <span style="color: #ff0000;">'grep'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> <span style="color: #ff0000;">&quot;$0&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $6}'</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007800;">VRES</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">ps</span> aux <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> <span style="color: #ff0000;">'grep'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> <span style="color: #ff0000;">&quot;$0&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $5}'</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007800;">COUNT</span>=<span style="color: #000000;">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007800;">VMEM</span>=<span style="color: #000000;">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007800;">RMEM</span>=<span style="color: #000000;">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> RSS <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #800000;">${RRES[@]}</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007800;">RMEM</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$RSS</span>+<span style="color: #007800;">$RMEM</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">done</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> VIRT <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #800000;">${VRES[@]}</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007800;">VMEM</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$VIRT</span>+<span style="color: #007800;">$VMEM</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007800;">COUNT</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$COUNT</span>+<span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">done</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007800;">VMEM</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$VMEM</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$COUNT</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007800;">VMEM</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$VMEM</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1024</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007800;">RMEM</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$RMEM</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1024</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$YELLOW</span> ----- MEMORY USAGE REPORT FOR '$1' ----- <span style="color: #007800;">$CLEAR</span>&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;PID Count: <span style="color: #007800;">$COUNT</span>&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Shared Mem usage: <span style="color: #007800;">$VMEM</span> MB&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Total Resident Set Size: <span style="color: #007800;">$RMEM</span> MB&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Mem/PID: <span style="color: #007800;">$(($RMEM/$COUNT)</span>) MB&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">fi</span><br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span></div></td></tr></tbody></table></div>
<p>Example output:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;----- MEMORY USAGE REPORT FOR 'httpd' ----- <br />
PID Count: 41<br />
Shared Mem usage: 140 MB<br />
Total Resident Set Site: 95 MB<br />
Mem/PID: 2 MB</div></td></tr></tbody></table></div>
<p><span style="float: left;" ><a class="twitter-share-button"  data-via="Saiweb" data-count="horizontal" data-related="Saiweb:David Busby" data-lang="en" data-url="http://www.saiweb.co.uk/linux/linux-the-sysadmin-script-part-4" data-text="Linux &#8211; The Sysadmin Script &#8211; Part 4" href="http://twitter.com/share?via=Saiweb&#038;count=horizontal&#038;related=Saiweb%3ADavid%20Busby&#038;lang=en&#038;url=http%3A%2F%2Fwww.saiweb.co.uk%2Flinux%2Flinux-the-sysadmin-script-part-4&#038;text=Linux%20%26%238211%3B%20The%20Sysadmin%20Script%20%26%238211%3B%20Part%204" >Tweet</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.saiweb.co.uk/linux/linux-the-sysadmin-script-part-4/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Linux &#8211; The Sysadmin script &#8211; Part 3</title>
		<link>http://www.saiweb.co.uk/linux/linux-the-sysadmin-script-part-3</link>
		<comments>http://www.saiweb.co.uk/linux/linux-the-sysadmin-script-part-3#comments</comments>
		<pubDate>Wed, 12 Nov 2008 12:25:22 +0000</pubDate>
		<dc:creator>Buzz</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Bash Script]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[sysadmin]]></category>

		<guid isPermaLink="false">http://www.saiweb.co.uk/linux/linux-the-sysadmin-script-part-3</guid>
		<description><![CDATA[PART 3 IS INACCURATE, THE BELOW SCRIPT IS FOR REFERENCE ONLY, IT HAS BEEN REPLACED IN PART 4 In part 3, I am going to cover a bash function that will allow you to profile the memory usage of any application by name. By adding the function below into your script you can execute a [...]]]></description>
			<content:encoded><![CDATA[<p><strong>PART 3 IS INACCURATE, THE BELOW SCRIPT IS FOR REFERENCE ONLY, IT HAS BEEN REPLACED IN <a href="http://www.saiweb.co.uk/uncategorized/linux-the-sysadmin-script-part-4">PART 4</a></strong></p>
<p>In part 3, I am going to cover a bash function that will allow you to profile the memory usage of any application by name.</p>
<p>By adding the function below into your script you can execute a command such as: sysadmin appmem apache</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">function appmem {<br />
if [ -z &quot;$1&quot; ]; then<br />
echo &quot;Usage: sysadmin appmem app_name i.e. (sysadmin appmem apache)&quot;;<br />
else<br />
if [ -x '/usr/bin/pmap' ]; then<br />
APID=(`ps aux | grep &quot;$1&quot; | grep -v 'grep' | grep -v &quot;$0&quot; | awk '{print $2}'`);<br />
COUNT=0;<br />
AMEM=0<br />
for PID in ${APID[@]}<br />
do<br />
TMP=$((`pmap -x $PID | grep &quot;total&quot; | awk '{print $3}'`));<br />
AMEM=$(($AMEM+$TMP));<br />
COUNT=$(($COUNT+1));<br />
done<br />
AMEM=$(($AMEM/1024));<br />
echo -e &quot;$YELLOW ----- MEMORY USAGE REPORT FOR '$1' ----- $CLEAR&quot;;<br />
echo &quot;PID Count: $COUNT&quot;;<br />
echo &quot;Mem usage: $AMEM MB&quot;;<br />
echo &quot;Mem/PID: $(($AMEM/$COUNT)) MB&quot;;<br />
echo -e &quot;$RED&quot;<br />
echo -e &quot;For more information run: pmap -x $PID $CLEAR&quot;;<br />
else<br />
echo 'Could not execute /usr/bin/pmap ... aborting';<br />
exit;<br />
fi<br />
fi<br />
}</div></td></tr></tbody></table></div>
<p>Sample output:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;span style=&quot;color: #ffcc00;&quot;&gt;----- MEMORY USAGE REPORT FOR 'apache' -----&lt;/span&gt;<br />
PID Count: 6<br />
Mem usage: 1134 MB<br />
Mem/PID: 189 MB<br />
&lt;span style=&quot;color: #ff0000;&quot;&gt;<br />
For more information run: pmap -x 123456&lt;/span&gt;</div></td></tr></tbody></table></div>
<p>You can of course replace &#8216;apache&#8217; with the application or daemon name you want to profile the memory usage of.</p>
<p>This script does require that pmap is installed, if the script can not find it, it will abort.</p>
<p>As always any problems, post a comment.</p>
<p>UPDATE: Apparently I need to point out that if you haven&#8217;t read <a href="http://www.saiweb.co.uk/linux/linux-the-sysadmin-script-part-2">PART 2</a>! then the colored output will not work &#8230; That&#8217;s why this entry is titled part 3, it does assume a degree of competence on your part in realizing part&#8217;s 1 and 2 may just be required reading &#8230;</p>
<p><strong>NOTE: The above provides a complete memory footprint of the indvidual PID, the same as VIRT in top.</strong></p>
<p><strong> VIRT &#8212; Virtual Image (kb)<br />
* The total amount of virtual memory used by the task. It includes all code, data and shared libraries plus pages that have been swapped out.<br />
* VIRT = SWAP + RES </strong><span style="float: left;" ><a class="twitter-share-button"  data-via="Saiweb" data-count="horizontal" data-related="Saiweb:David Busby" data-lang="en" data-url="http://www.saiweb.co.uk/linux/linux-the-sysadmin-script-part-3" data-text="Linux &#8211; The Sysadmin script &#8211; Part 3" href="http://twitter.com/share?via=Saiweb&#038;count=horizontal&#038;related=Saiweb%3ADavid%20Busby&#038;lang=en&#038;url=http%3A%2F%2Fwww.saiweb.co.uk%2Flinux%2Flinux-the-sysadmin-script-part-3&#038;text=Linux%20%26%238211%3B%20The%20Sysadmin%20script%20%26%238211%3B%20Part%203" >Tweet</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.saiweb.co.uk/linux/linux-the-sysadmin-script-part-3/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Linux &#8211; The Sysadmin script &#8211; Part 2</title>
		<link>http://www.saiweb.co.uk/linux/linux-the-sysadmin-script-part-2</link>
		<comments>http://www.saiweb.co.uk/linux/linux-the-sysadmin-script-part-2#comments</comments>
		<pubDate>Tue, 14 Oct 2008 08:40:55 +0000</pubDate>
		<dc:creator>Buzz</dc:creator>
				<category><![CDATA[Bash Script]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[bofh]]></category>
		<category><![CDATA[sysadmin]]></category>

		<guid isPermaLink="false">http://www.saiweb.co.uk/linux/linux-the-sysadmin-script-part-2</guid>
		<description><![CDATA[Part 2 has finally arrived &#8230;. don&#8217;t all cheer at once now &#8230; In part two I will cover how to run an IP range scan using bash script, and if the host can be pinged retrieve the MAC address of the connected host. Now bare in mind this script was written to run from [...]]]></description>
			<content:encoded><![CDATA[<p>Part 2 has finally arrived &#8230;. don&#8217;t all cheer at once now &#8230;</p>
<p>In part two I will cover how to run an IP range scan using bash script, and if the host can be pinged retrieve the MAC address of the connected host.</p>
<p>Now bare in mind this script was written to run from a MAC running OSX Leopard.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">#!/bin/bash<br />
#colours<br />
function colours {<br />
CLEAR='\e[00m';<br />
GREEN='\e[0;32m';<br />
RED='\e[0;31m';<br />
YELLOW='\e[1;33m';<br />
}<br />
#ipscan<br />
function ipscan {<br />
IPS_START=1;<br />
IPS_END=254;<br />
IPS_RANGE=192.168.1.<br />
echo &quot;Now running IPSCAN $IPS_RANGE$IPS_START - $IPS_RANGE$IPS_END&quot;<br />
for ((i=$IPS_START;i&amp;lt;=$IPS_END;i+=1)); do<br />
RESULT=`ping -c 1 -t 1 $IPS_RANGE$i | grep &quot;bytes from&quot;`;<br />
if [ -z &quot;$RESULT&quot; ]; then<br />
echo -e &quot;$IPS_RANGE$i:$RED DEAD $CLEAR&quot;;<br />
# If you comment out the above to report just the alive hosts, bash gets a bit funny about not processing anything here, so uncomment the below to keep it happy<br />
#holder=$i;<br />
else<br />
MAC=`arp $IPS_RANGE$i | awk '{ print $4 }';`;<br />
echo -e &quot;$IPS_RANGE$i:$GREEN ALIVE $CLEAR ($MAC)&quot;;<br />
fi<br />
done<br />
}<br />
colours;<br />
$1 $2</div></td></tr></tbody></table></div>
<p>To make this work on your Linux distro replace -t in the ping command with -W and check the awk entry for the arp output, a display of (no) means that no ARP entries could be found for the host, and change the IP range to that of your network.</p>
<p>That&#8217;s it for this part, dump this is a file and chmod +x as useual and run with ./script.sh ipscan.<span style="float: left;" ><a class="twitter-share-button"  data-via="Saiweb" data-count="horizontal" data-related="Saiweb:David Busby" data-lang="en" data-url="http://www.saiweb.co.uk/linux/linux-the-sysadmin-script-part-2" data-text="Linux &#8211; The Sysadmin script &#8211; Part 2" href="http://twitter.com/share?via=Saiweb&#038;count=horizontal&#038;related=Saiweb%3ADavid%20Busby&#038;lang=en&#038;url=http%3A%2F%2Fwww.saiweb.co.uk%2Flinux%2Flinux-the-sysadmin-script-part-2&#038;text=Linux%20%26%238211%3B%20The%20Sysadmin%20script%20%26%238211%3B%20Part%202" >Tweet</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.saiweb.co.uk/linux/linux-the-sysadmin-script-part-2/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Linux &#8211; The sysadmin script &#8211; Part 1</title>
		<link>http://www.saiweb.co.uk/linux/linux-the-sysadmin-script-part-1</link>
		<comments>http://www.saiweb.co.uk/linux/linux-the-sysadmin-script-part-1#comments</comments>
		<pubDate>Wed, 09 Jul 2008 16:10:47 +0000</pubDate>
		<dc:creator>Buzz</dc:creator>
				<category><![CDATA[Bash Script]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[bofh]]></category>
		<category><![CDATA[sysadmin]]></category>

		<guid isPermaLink="false">http://www.saiweb.co.uk/uncategorized/linux-the-sysadmin-script-part-1</guid>
		<description><![CDATA[Prompted by the following remarks today &#8230; Kerm: &#8220;;) there is always an abbreviation in the CLI as all sysadmins are lazy feckers&#8221; Kerm: &#8220;Someone might think you actually do work occasionally, god forbid!&#8221; Sysadmins are NOT inherently lazy, we just know how to save time, and are quite adept at doing so &#8230;ok? You [...]]]></description>
			<content:encoded><![CDATA[<p>Prompted by the following remarks today &#8230;</p>
<p><a href="http://www.absolutech.co.uk">Kerm</a>: <em>&#8220;;) there is always an abbreviation in the CLI as all sysadmins are lazy feckers&#8221;</em></p>
<p><em><a href="http://www.absolutech.co.uk">Kerm</a>: &#8220;Someone might think you actually do work occasionally, god forbid!&#8221;</em></p>
<p>Sysadmins are <strong><span style="text-decoration: underline;">NOT</span></strong> inherently lazy, we just know how to save time, and are quite adept at doing so &#8230;ok?</p>
<p><strong>You cheeky sods!</strong></p>
<p>So let me clear up one instance in which I take a lot of information, and make it quickly and easily accessible using a &#8220;<em>Lazy feckers</em>&#8221; abbreviation &#8230;</p>
<p><span style="text-decoration: underline;"><strong>Be warned this is a very jaded write up, read on at your own peril.</strong></span></p>
<p>Right then, onto the point of this post, the sysadmin script part 1, this is going to cover how to check how many connections to a specific port you have on your server.</p>
<p>Trust me this becomes very useful when you have exhausted all other options when trying to figure out why your web server is running like a dog with no legs &#8230;</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">netstat -ant</div></td></tr></tbody></table></div>
<p>After running the above on your SSH session you will see lines, and lines &#8230; and yet more lines of network connection information, especially if you just run this on a busy server.</p>
<p>Example (colours added):</p>
<p><span style="color: #ff0000;">tcp</span> <span style="color: #009900;">0</span> <span style="color: #ffcc00;">0</span> <span style="color: #00cccc;">***.***.***.***:25</span> <span style="color: #6633ff;">***.***.***.***:32794</span> <span style="color: #666666;">ESTABLISHED</span></p>
<p>Key:</p>
<p><span style="color: #ff0000;">PROTOCOL</span> <span style="color: #009900;">Tx</span> <span style="color: #ffcc00;">Rx</span> <span style="color: #00cccc;">LOCALHOST:PORT</span> <span style="color: #6633ff;">FOREIGN_HOST:PORT </span> <span style="color: #666666;">CONNECTION STATE</span></p>
<p>From this information it&#8217;s pretty easy to spot this is an inbound SMTP connection.</p>
<p>(If you can&#8217;t see why, don&#8217;t worry it&#8217;s ok maybe it&#8217;s <a href="http://www.theregister.co.uk/2008/07/04/bofh_2008_episode_24/">genetic</a>)</p>
<p>Now this may be handy, but other than taking all this information and dumping it into a spreadsheet (god knows you love those spreadsheets !!! ), how are you going to figure out how many connections are occurring from that external host?</p>
<p>How infact are you going to be able to easily see how many total connections to that port you have ?!?!</p>
<p>Bash script, now for some history, Bash is the Bourne Again Shell, or as I like to think of it, it is the verb for what I will do to your head if you ask me what BASH / SSH / Shell is again &#8230;</p>
<p>Now create a directory:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">mkdir ~/.sysadmin<br />
cd ~/.sysadmin</div></td></tr></tbody></table></div>
<p>Note the prefixing dot, this will create a &#8220;hidden&#8221; directory in your home directory (~), the reason for this is so you don&#8217;t have system admin script sat in your home directoy, as if you are like me, all sorts of crap moves in an out of that directory on a daily basis, and the last thing you want to do is to have to rummage through backups trying to find &#8220;<em>that script you wrote to diagnose connection problems a year ago</em>&#8220;.</p>
<p>The point is these scripts will become part of your workflow, once written they will rarely need updating, and should never be called directly, (I mean we&#8217;re lazy right? WTH do we want to be typing the full script path for? &#8230; oh yeh it saves time!).</p>
<p>In this case:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">vi ~/.sysadmin/buzz.sh</div></td></tr></tbody></table></div>
<p>You can of course call your script whatever you want, and use any text editor you want, if you don&#8217;t like / know vi &#8230;</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">#!/bin/bash<br />
# Sysadmin script PART 1 http://www.saiweb.co.uk<br />
# Provided under the MIT license (http://www.opensource.org/licenses/mit-license.php)<br />
# © D.Busby<br />
function usage {<br />
echo &quot;Usage: portcon port&quot;;<br />
echo &quot;i.e. portcon 80&quot;;<br />
}<br />
function portcon {<br />
echo &quot;----- Active Connections For Port $1 -----&quot;;<br />
netstat -ant | grep &quot;ABC.DEF.HIJ.KLM:$1 &quot; | wc -l<br />
netstat -ant | grep &quot;ABC.DEF.HIJ.KLM:$1 &quot; | awk '{ print $5 }' &nbsp;| awk -F \: '{ print $1 &nbsp;}' | sort | uniq -c &nbsp;| sort -n<br />
}<br />
if [ -z &quot;$1&quot; ]; then<br />
usage;<br />
exit<br />
fi<br />
$1 $2</div></td></tr></tbody></table></div>
<p>Ok so the above code is provided with two functions usage and portcon.</p>
<p><strong>MAKE SURE YOU REPLACE &#8220;ABC.DEF.HIJ.KLM&#8221; WITH YOUR <span style="color: #00cccc;">LOCAL IP ADDRESS </span></strong></p>
<p>CHMOD this file to allow execution.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">chmod +x ~/.sysadmin/buzz.sh</div></td></tr></tbody></table></div>
<p>Now edit your bashrc file.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">vi ~/.bashrc</div></td></tr></tbody></table></div>
<p>And add the following:</p>
<p>alias buzz=&#8217;~/.sysadmin/buzz.sh&#8217;</p>
<p>Now exit (logout) your SSH session and log back in (or SU root &gt; SU your_user for testing).</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">[buzz@buzz_srv ~]$ buzz<br />
Usage: portcon port<br />
i.e. portcon 80<br />
[buzz@buzz_srv ~]$</div></td></tr></tbody></table></div>
<p>Now run the portcon check &#8230;</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">[buzz@buzz_srv ~]$ buzz portcon 80<br />
----- Active Connections For Port 80 -----<br />
505<br />
1 ***.***.***.***<br />
3 ***.***.***.***<br />
3 ***.***.***.***<br />
4 ***.***.***.***<br />
4 ***.***.***.***<br />
5 ***.***.***.***<br />
11 ***.***.***.***<br />
14 ***.***.***.***<br />
16 ***.***.***.***<br />
76 ***.***.***.***<br />
373 ***.***.***.***</div></td></tr></tbody></table></div>
<p>(Yes before you ask ***.***.***.*** does display the correct IP address, I have purposely removed them for security).</p>
<p>So, I have taken something that would of resulted in netstat output &gt; spreadsheet to formulas &gt; at a estimate 30mins a time analysis to something that now takes less than 5 seconds to type, and get the relevant output, for roughly the same initial effort (30 mins scripting time).</p>
<p>You could argue you can keep a spreadsheet pre-setup with the right formulas / pivot tables and just dump the data each time, well yes you could but that&#8217;s no where near as quick as this &#8230;</p>
<p>And no trying to convince me it is as quick and better than the script above, for</p>
<ol>
<li> You have to wait for excel to open the spreadsheet</li>
<li> You have to copy paste the data</li>
<li> You have to wait for excel to process the formulas</li>
</ol>
<p>If you have a machine that can do that in time equal to or less than the time it takes the script above to output the data, the only thing I have to say is, <strong>stop spending such a budget on desktops and get a better server.<br />
</strong></p>
<p>Final Thoughts:</p>
<p>This write up is in jest, and is intended to be read as such, the code and methods provided above are factual. etc &#8230;<span style="float: left;" ><a class="twitter-share-button"  data-via="Saiweb" data-count="horizontal" data-related="Saiweb:David Busby" data-lang="en" data-url="http://www.saiweb.co.uk/linux/linux-the-sysadmin-script-part-1" data-text="Linux &#8211; The sysadmin script &#8211; Part 1" href="http://twitter.com/share?via=Saiweb&#038;count=horizontal&#038;related=Saiweb%3ADavid%20Busby&#038;lang=en&#038;url=http%3A%2F%2Fwww.saiweb.co.uk%2Flinux%2Flinux-the-sysadmin-script-part-1&#038;text=Linux%20%26%238211%3B%20The%20sysadmin%20script%20%26%238211%3B%20Part%201" >Tweet</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.saiweb.co.uk/linux/linux-the-sysadmin-script-part-1/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using apc
Database Caching 16/53 queries in 0.038 seconds using apc
Object Caching 1851/1940 objects using apc
Content Delivery Network via Rackspace Cloud Files: cdn.saiweb.co.uk

Served from: www.saiweb.co.uk @ 2012-02-08 17:37:49 -->
