<?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>Coffee Powered &#187; monitoring</title>
	<atom:link href="http://www.coffeepowered.net/tag/monitoring/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.coffeepowered.net</link>
	<description>code and content</description>
	<lastBuildDate>Sun, 05 Sep 2010 20:38:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Monitoring Rails: Getting instant monitoring alerts</title>
		<link>http://www.coffeepowered.net/2009/03/13/monitoring-rails-getting-instant-monitoring-alerts/</link>
		<comments>http://www.coffeepowered.net/2009/03/13/monitoring-rails-getting-instant-monitoring-alerts/#comments</comments>
		<pubDate>Fri, 13 Mar 2009 08:58:31 +0000</pubDate>
		<dc:creator>Chris Heald</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[jabberish]]></category>
		<category><![CDATA[monitoring]]></category>
		<category><![CDATA[uptime]]></category>

		<guid isPermaLink="false">http://www.coffeepowered.net/?p=145</guid>
		<description><![CDATA[Monitoring is big. Having an automated daemon watch your stuff and make sure it&#8217;s running properly can let you sleep at night, knowing that if something blows up, there&#8217;s an ever-watchful guardian ready to wake you up so you can fix it. There are a number of monitoring solutions that are popular these days, such [...]]]></description>
			<content:encoded><![CDATA[<p>Monitoring is big. Having an automated daemon watch your stuff and make sure it&#8217;s running properly can let you sleep at night, knowing that if something blows up, there&#8217;s an ever-watchful guardian ready to wake you up so you can fix it.</p>
<p>There are a number of monitoring solutions that are popular these days, such as <a href="http://mmonit.com/monit/">monit</a>, <a href="http://god.rubyforge.org/">god</a>, and <a href="http://www.nagios.org/">Nagios</a>. They&#8217;re all fantastic, but sometimes you just want something simple and to-the-point, right?</p>
<p><span id="more-145"></span></p>
<p>With <a href="http://github.com/cheald/jabberish/tree/master/">jabberish</a> in your project, this becomes a no-brainer. I&#8217;m already using Jabberish in my project, so I whipped up a little script that checks system load, available memory, and any changes in swap usage and shoots me an IM under certain conditions. My monitoring still handles automated maintenance in the case of a runaway process or whatnot, but this keeps me instantly informed of any problems that my system might be running in to.</p>
<pre class="brush: ruby;">
require 'rubygems'
require 'drb'
require 'daemons'

MAX_MEMORY = 95
MAX_LOAD = 4.0
DELIVER_TO = &quot;cheald@gmail.com&quot;
JABBERISH_SERVER = &quot;druby://localhost:35505&quot;

$warned = {}
$hostname = `hostname`.strip

def im
	$im_service ||= DRbObject.new(nil, JABBERISH_SERVER)
end

def deliver(msg)
	im.deliver DELIVER_TO, &quot;[#{$hostname}] #{msg}&quot;
end

def check_stats
	meminfo = open(&quot;/proc/meminfo&quot;).read

	mtotal = meminfo.match(/MemTotal:\s+(\d+)/)[1].to_i
	mfree = meminfo.match(/MemFree:\s+(\d+)/)[1].to_i
	mused = mtotal - mfree

	stotal = meminfo.match(/SwapTotal:\s+(\d+)/)[1].to_i
	sfree = meminfo.match(/SwapFree:\s+(\d+)/)[1].to_i
	sused = stotal - sfree

	begin
		if $warned[:swap] and sused &gt; $warned[:swap] then
			deliver &quot;WARNING: Swap has increased from #{$warned[:swap]} to #{sused}&quot;
		end
		$warned[:swap] = sused

		pct = mused / mtotal.to_f * 100.0
		if pct &gt; MAX_MEMORY then
			unless $warned[:memory]
				deliver sprintf(&quot;ALERT: Memory free: %2.2fmb (%2.2f%% used)&quot;, mfree / 1024.0, pct)
				$warned[:memory] = true
			end
		else
			$warned[:memory] = false
		end

		load = open(&quot;/proc/loadavg&quot;).read.split(&quot; &quot;).first
		if load &gt; MAX_LOAD then
			unless $warned[:load]
				deliver sprintf(&quot;WARNING: Load average is %s&quot;, load)
				$warned[:load] = true
			end
		else
			$warned[:load] = false
		end
	rescue
		puts &quot;Error: #{$!}&quot;
	end
end

Daemons.daemonize(:backtrace =&gt; true)

loop {
	check_stats
	sleep(10)
}
</pre>
<p>Not too bad, huh? This is written for a CentOS installation, so you may need to change things like the meminfo regexes depending on your system. It could probably use a YAML config file to be truly correct &#8211; configuration options in constants works, but is a little ugly.</p>
<p>Now I get alerts like these via instant message:</p>
<pre class="brush: ruby;">
[iceman.tagteam] WARNING: Load average is 4.44
[iceman.tagteam] ALERT: Memory free: 99.82mb (93.38% used)
[polaris.tagteam] ALERT: Memory free: 72.20mb (95.14% used)
</pre>
<p>This lets me respond to changing system conditions extremely rapidly, and serves as a high-level alert log when when I&#8217;m not at the keyboard &#8211; when I get back, I check my messages from blippr, and can see when and how often certain marginal conditions are being met.</p>
<p>Hope it&#8217;s useful!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coffeepowered.net/2009/03/13/monitoring-rails-getting-instant-monitoring-alerts/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using memcached
Page Caching using memcached
Database Caching 10/18 queries in 0.010 seconds using memcached

Served from: www.coffeepowered.net @ 2010-09-07 06:38:31 -->