<?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>trevoro.ca &#124; blog &#187; ruby activemq code monitoring sqs</title>
	<atom:link href="http://trevoro.ca/blog/tag/ruby-activemq-code-monitoring-sqs/feed/" rel="self" type="application/rss+xml" />
	<link>http://trevoro.ca/blog</link>
	<description>Elegant Solutions to Complex Problems</description>
	<lastBuildDate>Tue, 12 Jul 2011 21:30:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>How to Monitor Usage Patterns in ActiveMQ</title>
		<link>http://trevoro.ca/blog/2008/11/16/how-to-monitor-usage-patterns-in-activemq/</link>
		<comments>http://trevoro.ca/blog/2008/11/16/how-to-monitor-usage-patterns-in-activemq/#comments</comments>
		<pubDate>Mon, 17 Nov 2008 04:42:05 +0000</pubDate>
		<dc:creator>Trevor Orsztynowicz</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[monitoring]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[ruby activemq code monitoring sqs]]></category>

		<guid isPermaLink="false">http://trevoro.ca/blog/?p=298</guid>
		<description><![CDATA[ActiveMQ is an enterprise message bus that&#8217;s completely open source. It&#8217;s great if you want to tie together a bunch of different services, or act as your own personal Simple Message Queue (SQS). It supports a few interface methods such as JMS, Stomp, XMPP and plain REST. You can learn more about ActiveMQ here. There [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://activemq.apache.org/">ActiveMQ</a> is an enterprise message bus that&#8217;s completely open source. It&#8217;s great if you want to tie together a bunch of different services, or act as your own personal Simple Message Queue (SQS). It supports a few interface methods such as JMS, Stomp, XMPP and plain REST. You can learn more about ActiveMQ <a href="http://activemq.apache.org/">here</a>.</p>
<p>There are a few monitoring solutions for ActiveMQ that will let you know when its broken, but I needed to grab usage patterns over time, so I would be able to automatically spin up more workers. I didn&#8217;t see anything quickly available so I threw this together:</p>
<p>#queuemonitor.rb</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># queries the activeMQ status XML file and returns relevant data.</span>
<span style="color:#008000; font-style:italic;"># I'm sure there's a more elegant way of creating method directly out of xml.</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Rather than have each queuemonitor as a seprate class with one name, we</span>
<span style="color:#008000; font-style:italic;"># might want to be able to parse multiple queues on the same server. So we have</span>
<span style="color:#008000; font-style:italic;"># an array of queue names (@queues) that contain the names of queues we want to monitor.</span>
<span style="color:#008000; font-style:italic;"># if you want to monitor more than one queue then you'd have to create a new QueueMonitor</span>
<span style="color:#008000; font-style:italic;"># instance.</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> QueueMonitor
&nbsp;
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:url</span>, <span style="color:#ff3333; font-weight:bold;">:queues</span>
&nbsp;
  <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'net/http'</span>
  <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rexml/document'</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize
    <span style="color:#0066ff; font-weight:bold;">@url</span>=<span style="color:#996600;">&quot;http://127.0.0.1:8161/admin/xml/queues.jsp&quot;</span>
    <span style="color:#0066ff; font-weight:bold;">@queues</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;">#add a queue to the list of queues that we'll pay attention to</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> addqueue<span style="color:#006600; font-weight:bold;">&#40;</span>queue<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@queues</span>.<span style="color:#9900CC;">push</span><span style="color:#006600; font-weight:bold;">&#40;</span>queue<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#0066ff; font-weight:bold;">@queues</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>queue<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> delqueue<span style="color:#006600; font-weight:bold;">&#40;</span>queue<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@names</span>.<span style="color:#9900CC;">reject</span>!<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>q<span style="color:#006600; font-weight:bold;">|</span> q == queue<span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> query
    results = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    date = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
    xml_data = <span style="color:#6666ff; font-weight:bold;">Net::HTTP</span>.<span style="color:#9900CC;">get_response</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">URI</span>.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span>@url<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">body</span>
    doc = <span style="color:#6666ff; font-weight:bold;">REXML::Document</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>xml_data<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    doc.<span style="color:#9900CC;">elements</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'queues/queue'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>queue<span style="color:#006600; font-weight:bold;">|</span>
      name = queue.<span style="color:#9900CC;">attributes</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;name&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      <span style="color:#008000; font-style:italic;"># only list from Queues listed in the '@names'</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@queues</span>.<span style="color:#9900CC;">member</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>name<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">then</span>
&nbsp;
        queue.<span style="color:#9900CC;">elements</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'stats'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>ele<span style="color:#006600; font-weight:bold;">|</span>
          size = ele.<span style="color:#9900CC;">attributes</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;size&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
          consumers = ele.<span style="color:#9900CC;">attributes</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;consumerCount&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
          enqueue = ele.<span style="color:#9900CC;">attributes</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;enqueueCount&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
          dequeue = ele.<span style="color:#9900CC;">attributes</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;dequeueCount&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
          queue = <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">'name'</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; name,
                    <span style="color:#996600;">'size'</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; size,
                    <span style="color:#996600;">'consumers'</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; consumers,
                    <span style="color:#996600;">'enqueue'</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; enqueue,
                    <span style="color:#996600;">'dequeue'</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; dequeue,
                    <span style="color:#996600;">'date'</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; date
          <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
          results <span style="color:#006600; font-weight:bold;">&amp;</span>lt; <span style="color:#006600; font-weight:bold;">&amp;</span>lt; queue
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> results
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>You can then start monitoring your queue servers with something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/env ruby</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'queuemonitor'</span>
&nbsp;
mfreq = <span style="color:#006666;">30</span>
<span style="color:#0066ff; font-weight:bold;">@monitor</span> = QueueMonitor.<span style="color:#9900CC;">new</span>
<span style="color:#0066ff; font-weight:bold;">@monitor</span>.<span style="color:#9900CC;">url</span> = <span style="color:#996600;">&quot;http://67.202.41.64:8161/admin/xml/queues.jsp&quot;</span>
<span style="color:#0066ff; font-weight:bold;">@monitor</span>.<span style="color:#9900CC;">addqueue</span> <span style="color:#996600;">&quot;pubsub.pings.spider&quot;</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#do something interesting like write to a file</span>
<span style="color:#008000; font-style:italic;">#screen or database</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> query
  date = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
  results = <span style="color:#0066ff; font-weight:bold;">@monitor</span>.<span style="color:#9900CC;">query</span>
  results.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>q<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{date} #{q[&quot;</span>name<span style="color:#996600;">&quot;]} #{q[&quot;</span>size<span style="color:#996600;">&quot;]} #{q[&quot;</span>consumers<span style="color:#996600;">&quot;]}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#You'd want to use daemonize here...</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">loop</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  query
  <span style="color:#CC0066; font-weight:bold;">sleep</span> mfreq
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>It works pretty well. Right now I&#8217;m just writing the results to a file to parse later, and so you can setup other parts of a program that would automatically spin up new workers based on a set of circumstances. The algorithm for that is the hard part and will depend on a bunch of your own rules.</p>
]]></content:encoded>
			<wfw:commentRss>http://trevoro.ca/blog/2008/11/16/how-to-monitor-usage-patterns-in-activemq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

