<?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>Untitled &#187; bash</title>
	<atom:link href="http://ammonlauritzen.com/blog/tag/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://ammonlauritzen.com/blog</link>
	<description>and still for good reason.</description>
	<lastBuildDate>Wed, 05 May 2010 18:43:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>report-storage.sh</title>
		<link>http://ammonlauritzen.com/blog/2009/10/14/report-storagesh/</link>
		<comments>http://ammonlauritzen.com/blog/2009/10/14/report-storagesh/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 20:55:13 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/?p=552</guid>
		<description><![CDATA[In the grand tradition of my publishing little building-block shell scripts of interest, here goes another one. This is a simple cron job that I run daily on a number of hosts to generate storage usage growth. (This is in addition to Cacti and Nagios which poll some of this data already but for different [...]]]></description>
			<content:encoded><![CDATA[<p>In the grand tradition of my publishing little building-block shell scripts of interest, here goes another one. This is a simple cron job that I run daily on a number of hosts to generate storage usage growth. (This is in addition to Cacti and Nagios which poll some of this data already but for different reasons and with different granularity).</p>
<p>The FILES variable should be populated with a whitespace separated list of files, directories, and block devices to track.</p>
<p>The DB_ABCD variables should be populated with appropriate credentials to talk to a mysql server.</p>
<p>The actual script looks something like this:</p>
<pre class="brush: bash;">
#!/bin/bash

FILES='/var/lib/mysql/ibdata1 /var/lib/mysql/db/table.ibd /dev/sda1 /var/log/mysql'

LOCAL=`hostname -s`
DB_HOST='aaa'
DB_USER='bbb'
DB_PASS='ccc'

function insert {
    FILE=$1
    SIZE=$2
    QUERY=&quot;replace into metrics.storage_usage values( now(), '$LOCAL', '$FILE', $SIZE )&quot;
    mysql --host=${DB_HOST} --user=${DB_USER} --password=&quot;${DB_PASS}&quot; -e &quot;${QUERY}&quot;
}

for FILE in $FILES
do
    if [ -d $FILE ]; then
        BASE=$FILE
        SIZE=`du -ks $FILE/ | awk '{print $1}'`
    elif [ -b $FILE ]; then
        TMP=`df -k -P $FILE | tail -n1 | awk '{print $3 &quot; &quot; $6}'`
        SIZE=`echo $TMP | awk '{print $1}'`
        BASE=`echo $TMP | awk '{print $2}'`
    else
        BASE=`basename $FILE`
        SIZE=`du -k $FILE | awk '{print $1}'`
    fi

    echo &quot;$BASE = $SIZE&quot;
    insert $BASE $SIZE
done
</pre>
<p>I am putting my data into a table called &#8220;storage_usage&#8221; in a database called &#8220;metrics&#8221;:</p>
<pre class="brush: sql;">
CREATE TABLE `storage_usage` (
  `ts` date NOT NULL,
  `host` varchar(25) NOT NULL,
  `file` varchar(64) NOT NULL,
  `size` int(10) unsigned NOT NULL COMMENT 'in kbytes',
  PRIMARY KEY (`ts`,`host`,`file`)
)
</pre>
<p>Obviously, this could be tweaked in any different number of ways, based on your needs. One tweak you might want to consider if you&#8217;re running it in a daily cron is to remove the echo so you don&#8217;t get an email report of every run. Also, if you might want to record more than one snapshot per file per host per day &#8211; in the which case you probably need to change the type of the timestamp column to a datetime. Or there might be cases where you want to change the replace to an insert or&#8230; whatever <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2009/10/14/report-storagesh/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>init.d template</title>
		<link>http://ammonlauritzen.com/blog/2009/06/18/initd-template/</link>
		<comments>http://ammonlauritzen.com/blog/2009/06/18/initd-template/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 17:29:31 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[init.d]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/?p=512</guid>
		<description><![CDATA[This is a rudimentary template that I&#8217;ve been using for very quick and dirty /etc/init.d scripts recently.
It works under the assumption that your server daemon has a unique name and only ever runs a single instance &#8211; this also means that the binary and the init.d script cannot share a name &#8211; otherwise strange things [...]]]></description>
			<content:encoded><![CDATA[<p>This is a rudimentary template that I&#8217;ve been using for very quick and dirty /etc/init.d scripts recently.</p>
<p>It works under the assumption that your server daemon has a unique name and only ever runs a single instance &#8211; this also means that the binary and the init.d script cannot share a name &#8211; otherwise strange things happen <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Actual invocation logic may need to be updated on a per-service basis and chkconfig style headers would have to be added manually, but it works well for what it is.</p>
<pre class="brush: bash;">
#!/bin/bash

DIR=''	# path to the daemon executable
CMD=''	# name of the command itself
ARG=''	# optional. any arguments to pass when starting
NAM=''	# descriptive name of the daemon so it shows up pretty

function get_ps {
	ps --no-header -C${CMD}
}

function do_start {
	echo -n &quot;Starting ${NAM}... &quot;
	cd ${DIR}
	nohup ./${CMD} ${ARG} &amp;
	SUCC=`get_ps | wc -l`
	if [ &quot;1&quot; == &quot;$SUCC&quot; ]; then
		echo &quot;[SUCCESS]&quot;
	else
		echo &quot;[FAILURE]&quot;
	fi
}

function do_stop {
	echo -n &quot;Stopping ${NAM}... &quot;
	PID=`get_ps | awk '{print $1}'`
	kill $PID
	SUCC=`get_ps | wc -l`
	if [ &quot;0&quot; == &quot;$SUCC&quot; ]; then
		echo &quot;[SUCCESS]&quot;
	else
		echo &quot;[FAILURE]&quot;
	fi
}

case &quot;${1:-''}&quot; in
	'start')
		do_start
		;;
	'stop')
		do_stop
		;;
	'restart')
		do_stop
		do_start
		;;
	*)
		#echo &quot;Usage: $SELF start|stop|restart|reload|force-reload|status&quot;
		echo &quot;Usage: $SELF start|stop|restart&quot;
		exit 1
		;;
esac
</pre>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2009/06/18/initd-template/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>svn get revision</title>
		<link>http://ammonlauritzen.com/blog/2008/05/27/svn-get-revision/</link>
		<comments>http://ammonlauritzen.com/blog/2008/05/27/svn-get-revision/#comments</comments>
		<pubDate>Tue, 27 May 2008 19:57:04 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[subversion]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/?p=388</guid>
		<description><![CDATA[One of the more annoying things about svn is that (to my knowledge), there exists no single simple command to retrieve the revision number from a shell.
What I want:

ammon@hermes:~/repo$ svn info --get-revision .
1234

But of course, nothing like this exists.
Thankfully, svn info&#8217;s output IS easy enough to parse. You just have to do it your self.

ammon@hermes:~/repo$ [...]]]></description>
			<content:encoded><![CDATA[<p>One of the more annoying things about svn is that (to my knowledge), there exists no single simple command to retrieve the revision number from a shell.</p>
<p>What I want:</p>
<pre class="brush: plain;">
ammon@hermes:~/repo$ svn info --get-revision .
1234
</pre>
<p>But of course, nothing like this exists.</p>
<p>Thankfully, svn info&#8217;s output IS easy enough to parse. You just have to do it your self.</p>
<pre class="brush: plain;">
ammon@hermes:~/repo$ svn info | grep Revision | awk -- '{print $2}'
1234
</pre>
<p>Will give you the revision of your current checkout without the network hit of a call to svn log.</p>
<p>To get the current version of the repo itself (hits the network), add &#8220;-r HEAD&#8221; to the svn info call:</p>
<pre class="brush: plain;">
ammon@hermes:~/repo$ svn info -r HEAD | grep Revision | awk -- '{print $2}'
1280
</pre>
<p>Of course, svn info also supports outputting info as xml, so you could use that to parse things in a more advanced environment but one where you&#8217;re still not using the svn api bindings.</p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2008/05/27/svn-get-revision/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>svn keyword magic</title>
		<link>http://ammonlauritzen.com/blog/2006/12/19/svn-keyword-magic/</link>
		<comments>http://ammonlauritzen.com/blog/2006/12/19/svn-keyword-magic/#comments</comments>
		<pubDate>Tue, 19 Dec 2006 22:07:11 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[subversion]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/2006/12/19/svn-keyword-magic/</guid>
		<description><![CDATA[Subversion is officially the coolest thing ever  
And now on a related note&#8230;
Yesterday during a code review, the svn:keywords property was discussed, and the idea proposed to start using it to embed magical versioning information into our builds (for bug reporting reasons and such). Well, I decided to jump on the idea this afternoon. [...]]]></description>
			<content:encoded><![CDATA[<p><a href='http://subversion.tigris.org'>Subversion</a> is officially the coolest thing ever <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>And now on a related note&#8230;</p>
<p>Yesterday during a code review, the <b>svn:keywords</b> property was discussed, and the idea proposed to start using it to embed magical versioning information into our builds (for bug reporting reasons and such). Well, I decided to jump on the idea this afternoon. After a bit of fiddling, we now have a little component that can be used to display the revision number of the current application build &#8211; all w/o anyone having to remember to do it.</p>
<p>The SVN Book explains <a href='http://svnbook.red-bean.com/nightly/en/svn.advanced.props.html#svn.advanced.props.special.keywords'>how to use the property</a> in greater detail.</p>
<p>I created a simple xml file (it could be any text file, really), that I then dropped the svn keyword tags into it.</p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;version&gt;
	&lt;date&gt;$Date$&lt;/date&gt;
	&lt;rev&gt;$Rev$&lt;/rev&gt;
&lt;/version&gt;
</pre>
<p>I then set the svn:keywords property on the file to &#8220;Date Rev&#8221; (ie, the names of the keywords i want replaced &#8211; see the full docs for a list of valid keywords).</p>
<p>Upon commit, the file is updated on the server to now look more like this:</p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;version&gt;
	&lt;date&gt;$Date: 2006-12-19 14:30:02 -0700 (Tue, 19 Dec 2006) $&lt;/date&gt;
	&lt;rev&gt;$Rev: 376 $&lt;/rev&gt;
&lt;/version&gt;
</pre>
<p>Unfortunately&#8230; there&#8217;s a major hitch in what I wanted to do. These labels actually only update when the file in question is updated. Ie, it displays the last time the source file was changed, not the last time the entire repository was changed. So, this trick is useful for updating header comments or perhaps an internal version constant string (if you&#8217;re doing server-side <a href='http://svnbook.red-bean.com/nightly/en/svn.reposadmin.create.html#svn.reposadmin.create.hooks'>post-commit</a> builds or something). But&#8230; it&#8217;s not what I want in this case <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>In the end, I wound up having to do a bit of old fashioned <a href='http://www.gnu.org/software/sed/manual/html_chapter/sed_toc.html'>sed</a> manipulation on the server side post-commit&#8230; but that doesn&#8217;t change the fact that subversion still allows me to do this kind of magic. It&#8217;s just not quite as magical as I&#8217;d originally hoped it could be.</p>
<p>My post-commit hook now looks something like this:</p>
<pre class="brush: bash;">
#!/bin/bash
REPO=$1
REV=$2
DATE=`date`

cd /var/www/$REPO
svn up .
sed -i &quot;s/\\\\\\\$Rev.*\\\\\\\$/\\\\\\\$Rev: $REV \\\\\\\$/g&quot; version.xml
sed -i &quot;s/\\\\\\\$Date.*\\\\\\\$/\\\\\\\$Date: $DATE \\\\\\\$/g&quot; version.xml
ant
svn revert version.xml
</pre>
<p>I&#8217;m keeping updated copies of a part of the repository in a web accessible directory, and am using <a href='http://ant.apache.org'>Ant</a> to build deployable zip files and such. The sed commands are performing a search and replace on the svn keywords before the build and I then revert the changes (just in case somebody actually changes the real contents of version.xml &#8211; which would result in a conflict if we kept these local changes around).</p>
<p>The file I&#8217;m producing now looks like this:</p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;cmykversion&gt;
	&lt;date&gt;$Date: Tue Dec 19 15:43:07 MST 2006 $&lt;/date&gt;
	&lt;rev&gt;$Rev: 378 $&lt;/rev&gt;
&lt;/cmykversion&gt;
</pre>
<p>If I really wanted to, I could generate the same exact date format as SVN uses, but I prefer this one for its brevity.</p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2006/12/19/svn-keyword-magic/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>wallpaper yoinkage</title>
		<link>http://ammonlauritzen.com/blog/2006/02/15/wallpaper_yoinkage/</link>
		<comments>http://ammonlauritzen.com/blog/2006/02/15/wallpaper_yoinkage/#comments</comments>
		<pubDate>Wed, 15 Feb 2006 11:37:15 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[bash]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Ok, so it&#8217;s 4am and I&#8217;m awake. Nothing new there. I was scrounging sites and looking for some bit of information obscure enough that I&#8217;d actually forgotten what my original goal was. Thus, it was only natural that I get sidetracked by whatever shiny object floated past me as I clicked URL&#8217;s at random.
I landed [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, so it&#8217;s 4am and I&#8217;m awake. Nothing new there. I was scrounging sites and looking for some bit of information obscure enough that I&#8217;d actually forgotten what my original goal was. Thus, it was only natural that I get sidetracked by whatever shiny object floated past me as I clicked URL&#8217;s at random.</p>
<p>I landed on a French anime wallpaper site. I&#8217;d not been there before, and I started looking at a decently sized collection of images that I&#8217;d also never before encountered. Naturally, I decided to acquire them &#8211; but the sheer scope of the site lended a non-trivial amount of difficulty to this. At least, it did until I realized that their images were stored in sequentially numbered files in sequentially numbered directories. No zero padding or anything.</p>
<p>This led to the following script, which is currently quite happily churning its way through the 36th directory &#8211; whatever show that may actually happen to be <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>I am making one assumption here, that they haven&#8217;t divided things up into more than 500 directories. I could just as easily have done the sort of while loop as I did on the inside on the outside, but this is also meant as demonstrative code for future reference.</p>
<blockquote><pre class="brush: bash;">
#!/usr/bin/bash
for DIR in `seq 0 500`
do
        mkdir $DIR
        cd $DIR
        IMG = -1
        while [ $? != 1 ]
        do
                ((IMG += 1))
                wget -c host/$DIR/$IMG.jpg
        done
        cd ..
        rmdir $DIR

        if [ $? == 0 ]
        then
                echo &quot;removed $DIR, assuming we're done&quot;
                exit
        fi
done
</pre>
</blockquote>
<p>Why not just use wget&#8217;s spider functionality? I have in the past, but not for something this fun. Besides, this way I don&#8217;t have any cleanup to do (other than identifying shows and renaming their folders when all is done).</p>
<p>It&#8217;s working on directory number 45 now <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2006/02/15/wallpaper_yoinkage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
