<?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>Matejunkie &#187; python</title>
	<atom:link href="http://www.matejunkie.com/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.matejunkie.com</link>
	<description>"Look behind you, a Three-Headed Monkey!"</description>
	<lastBuildDate>Thu, 07 Jan 2010 14:26:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to kick off Hadoop&#8217;s rack awareness</title>
		<link>http://www.matejunkie.com/how-to-kick-off-hadoops-rack-awareness/</link>
		<comments>http://www.matejunkie.com/how-to-kick-off-hadoops-rack-awareness/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 22:31:39 +0000</pubDate>
		<dc:creator>Mike Adolphs</dc:creator>
				<category><![CDATA[Binary Talks]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[distributed computing]]></category>
		<category><![CDATA[distributed data storage]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[hadoop]]></category>
		<category><![CDATA[HDFS]]></category>
		<category><![CDATA[namenode]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[rack awareness]]></category>

		<guid isPermaLink="false">http://www.matejunkie.com/?p=1198</guid>
		<description><![CDATA[Hadoop, an Open Source framework for reliable, scalable, distributed computing and data storage, has a nice feature called rack awareness. This means nothing more than that you&#8217;re able to widely spread your Hadoop cluster over multiple machines within different racks and even different data centers that are worlds apart from each other. Sadly this isn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://hadoop.apache.org/" title="Hadoop">Hadoop</a>, an Open Source framework for reliable, scalable, distributed computing and data storage, has a nice feature called rack awareness. This means nothing more than that you&#8217;re able to widely spread your Hadoop cluster over multiple machines within different racks and even different data centers that are worlds apart from each other. Sadly this isn&#8217;t well documented as almost anything regarding Hadoop since it&#8217;s under heavy development and because of the few people that are actually working with Hadoop compared to other huge Open Source projects.</p>
<p style="text-align: left;"><a href="http://www.matejunkie.com/wp-content/uploads/2009/07/hadoop_rack_awareness_example_01.png"><img class="aligncenter size-full wp-image-1199" title="Hadoop - Rack Awareness" src="http://www.matejunkie.com/wp-content/uploads/2009/07/hadoop_rack_awareness_example_01.png" alt="Hadoop - Rack Awareness" width="500" height="337" /></a></p>
<p>Anyway, kicking off Hadoop&#8217;s rack awareness is no big deal in general. Here&#8217;s how to achieve this goal:</p>
<p>Put a small script in whatever language you prefer to a location of your choice which is accessible by the local Hadoop user on the namenode. The only requirement is that the script is able to print a record to stdout. In this example I&#8217;m using a small Python script written <a href="http://www.nabble.com/Re%3A-Hadoop-topology.script.file.name-Form-p22588620.html" title="Nabble - Hadoop Core Users">by Vadim Zaliva</a> stored in the Hadoop user&#8217;s home directory under /home/hadoop:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
&nbsp;
<span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
This script used by hadoop to determine network/rack topology.  It
should be specified in hadoop-site.xml via topology.script.file.name
Property.
&nbsp;
 topology.script.file.name
 /home/hadoop/topology.py
&nbsp;
'</span><span style="color: #483d8b;">''</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">string</span> <span style="color: #ff7700;font-weight:bold;">import</span> join
&nbsp;
DEFAULT_RACK = <span style="color: #483d8b;">'/default/rack0'</span><span style="color: #66cc66;">;</span>
&nbsp;
RACK_MAP = <span style="color: black;">&#123;</span> <span style="color: #483d8b;">'10.72.10.1'</span> : <span style="color: #483d8b;">'/datacenter0/rack0'</span>,
&nbsp;
             <span style="color: #483d8b;">'10.112.110.26'</span> : <span style="color: #483d8b;">'/datacenter1/rack0'</span>,
             <span style="color: #483d8b;">'10.112.110.27'</span> : <span style="color: #483d8b;">'/datacenter1/rack0'</span>,
             <span style="color: #483d8b;">'10.112.110.28'</span> : <span style="color: #483d8b;">'/datacenter1/rack0'</span>,
&nbsp;
             <span style="color: #483d8b;">'10.2.5.1'</span> : <span style="color: #483d8b;">'/datacenter2/rack0'</span>,
             <span style="color: #483d8b;">'10.2.10.1'</span> : <span style="color: #483d8b;">'/datacenter2/rack1'</span>
    <span style="color: black;">&#125;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#41;</span>==<span style="color: #ff4500;">1</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> DEFAULT_RACK
<span style="color: #ff7700;font-weight:bold;">else</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> join<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>RACK_MAP.<span style="color: black;">get</span><span style="color: black;">&#40;</span>i, DEFAULT_RACK<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span><span style="color: black;">&#93;</span>,<span style="color: #483d8b;">&quot; &quot;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Then you need to add a property directive to the hadoop-site.xml you&#8217;re using for your cluster&#8217;s configuration (delete all leading underscores in the tags, this is just for display purposes):</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>topology.script.file.name<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/home/hadoop/topology.py<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Simply restart the namenode&#8217;s process and from now on the Namenode runs the script and looks for a record regarding the datanode everytime a new datanode tries to participate in the cluster.<br />
Keep in mind that taking care of connections between multiple locations via VPN or else and proper DNS resolution is your business and not Hadoop&#8217;s. Make sure that resolving the datanode&#8217;s DNS record is possible and that it&#8217;s accessible within your Hadoop environment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.matejunkie.com/how-to-kick-off-hadoops-rack-awareness/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Apache check plugin for Nagios in Python</title>
		<link>http://www.matejunkie.com/apache-check-plugin-for-nagios-in-python/</link>
		<comments>http://www.matejunkie.com/apache-check-plugin-for-nagios-in-python/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 23:33:02 +0000</pubDate>
		<dc:creator>Mike Adolphs</dc:creator>
				<category><![CDATA[Binary Talks]]></category>
		<category><![CDATA[apache webserver]]></category>
		<category><![CDATA[gplv2]]></category>
		<category><![CDATA[nagios]]></category>
		<category><![CDATA[network monitoring]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.matejunkie.com/?p=879</guid>
		<description><![CDATA[As I&#8217;ve already said yesterday I&#8217;m currently getting warm with Python and for the beginning I&#8217;ve ported my sh compliant Apache check plugin for Nagios to a Python script which is now ready for first tests in production. With options to set a hostname, a port and to define warning/critical thresholds for requests per second [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-193" title="logo_nagios" src="http://www.matejunkie.com/wp-content/uploads/2008/05/logo_nagios.gif" alt="logo_nagios" width="150" height="40" />As I&#8217;ve already <a title="Matejunkie - Porting Nagios Plugins to Python" href="http://www.matejunkie.com/porting-my-nagios-plugins-to-python/" target="_blank">said yesterday</a> I&#8217;m currently getting warm with Python and for the beginning I&#8217;ve ported my sh compliant <a title="Matejunkie - Update: Apache check plugin for Nagios" href="http://www.matejunkie.com/update-apache-check-plugin-for-nagios/" target="_blank">Apache check plugin</a> for Nagios to a Python script which is now ready for first tests in production.</p>
<p>With options to set a hostname, a port and to define warning/critical thresholds for requests per second only, it got less features than the sh script at the moment. In future releases I&#8217;ll integrate more features which are already available in the sh script, but for now that&#8217;ll do the job. Feel free to <a title="NagiosExchange - check_apache2.py" href="http://www.nagiosexchange.org/cgi-bin/page.cgi?g=Detailed%2F3022.html;d=1" target="_blank">visit NagiosExchange</a> to download the script, check it out via svn or simply get along with the copy&#8217;n'paste possibility below.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">user<span style="color: #000000; font-weight: bold;">@</span>host ~ $ <span style="color: #c20cb9; font-weight: bold;">svn</span> <span style="color: #c20cb9; font-weight: bold;">co</span> https:<span style="color: #000000; font-weight: bold;">//</span>svn.matejunkie.com<span style="color: #000000; font-weight: bold;">/</span>svn<span style="color: #000000; font-weight: bold;">/</span>pp-nagios-plugins<span style="color: #000000; font-weight: bold;">/</span>stable<span style="color: #000000; font-weight: bold;">/</span>check_apache2<span style="color: #000000; font-weight: bold;">/</span> check_apache2<span style="color: #000000; font-weight: bold;">/</span>
A    check_apache2<span style="color: #000000; font-weight: bold;">/</span>check_apache2.py
Checked out revision 14.</pre></div></div>

<h3>The script</h3>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#   Author: Mike Adolphs, 2009</span>
<span style="color: #808080; font-style: italic;">#   Blog: http://www.matejunkie.com/</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;">#   This program is free software; you can redistribute it and/or modify</span>
<span style="color: #808080; font-style: italic;">#   it under the terms of the GNU General Public License as published by</span>
<span style="color: #808080; font-style: italic;">#   the Free Software Foundation; version 2 of the License only!</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;">#   This program is distributed in the hope that it will be useful,</span>
<span style="color: #808080; font-style: italic;">#   but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span style="color: #808080; font-style: italic;">#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span style="color: #808080; font-style: italic;">#   GNU General Public License for more details.</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;">#   You should have received a copy of the GNU General Public License</span>
<span style="color: #808080; font-style: italic;">#   along with this program; if not, write to the Free Software</span>
<span style="color: #808080; font-style: italic;">#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">optparse</span> <span style="color: #ff7700;font-weight:bold;">import</span> OptionParser, OptionGroup
&nbsp;
<span style="color: #808080; font-style: italic;"># Nagios return codes</span>
OK = <span style="color: #ff4500;">0</span>
WARNING = <span style="color: #ff4500;">1</span>
CRITICAL = <span style="color: #ff4500;">2</span>
UNKNOWN = <span style="color: #ff4500;">3</span>
&nbsp;
usage = <span style="color: #483d8b;">&quot;Usage: %prog -H HOSTNAME -p PORT [-w] [-c]&quot;</span>
<span style="color: #dc143c;">parser</span> = OptionParser<span style="color: black;">&#40;</span>usage, version=<span style="color: #483d8b;">&quot;%prog 1.0&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">&quot;-H&quot;</span>,
                   <span style="color: #483d8b;">&quot;--hostname&quot;</span>,
                   <span style="color: #008000;">type</span>=<span style="color: #483d8b;">&quot;string&quot;</span>,
                   dest=<span style="color: #483d8b;">&quot;hostname&quot;</span>,
                   default=<span style="color: #483d8b;">&quot;localhost&quot;</span>,
                   <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;You may define a hostname with the -H option. <span style="color: #000099; font-weight: bold;">\</span>
                         Default is: localhost.&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">&quot;-p&quot;</span>,
                   <span style="color: #483d8b;">&quot;--port&quot;</span>,
                   <span style="color: #008000;">type</span>=<span style="color: #483d8b;">&quot;int&quot;</span>,
                   dest=<span style="color: #483d8b;">&quot;port&quot;</span>,
                   default=<span style="color: #ff4500;">80</span>,
                   <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;You may define a port with the -p option. <span style="color: #000099; font-weight: bold;">\</span>
                         Default is: 80.&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
group = OptionGroup<span style="color: black;">&#40;</span><span style="color: #dc143c;">parser</span>, <span style="color: #483d8b;">&quot;Warning/critical thresholds&quot;</span>,
                    <span style="color: #483d8b;">&quot;Use these options to set warning/critical thresholds <span style="color: #000099; font-weight: bold;">\</span>
                     for requests per second served by your Apache.&quot;</span><span style="color: black;">&#41;</span>
group.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">&quot;-w&quot;</span>,
                  <span style="color: #483d8b;">&quot;--warning&quot;</span>,
                  <span style="color: #008000;">type</span>=<span style="color: #483d8b;">&quot;int&quot;</span>,
                  dest=<span style="color: #483d8b;">&quot;warning&quot;</span>,
                  default=-<span style="color: #ff4500;">2</span>,
                  <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;Use this option if you want to use warning/critical <span style="color: #000099; font-weight: bold;">\</span>
                        thresholds. Make sure to set a critical value as <span style="color: #000099; font-weight: bold;">\</span>
                        well. Default is: -1.&quot;</span><span style="color: black;">&#41;</span>
group.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">&quot;-c&quot;</span>,
                  <span style="color: #483d8b;">&quot;--critical&quot;</span>,
                  <span style="color: #008000;">type</span>=<span style="color: #483d8b;">&quot;int&quot;</span>,
                  dest=<span style="color: #483d8b;">&quot;critical&quot;</span>,
                  default=-<span style="color: #ff4500;">1</span>,
                  <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;Use this option if you want to use warning/critical <span style="color: #000099; font-weight: bold;">\</span>
                        thresholds. Make sure to set a warning value as <span style="color: #000099; font-weight: bold;">\</span>
                        well. Default is: -2.&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option_group</span><span style="color: black;">&#40;</span>group<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: black;">&#40;</span>options, args<span style="color: black;">&#41;</span> = <span style="color: #dc143c;">parser</span>.<span style="color: black;">parse_args</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
hostname = options.<span style="color: black;">hostname</span>
port = options.<span style="color: black;">port</span>
warning = options.<span style="color: black;">warning</span>
critical = options.<span style="color: black;">critical</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> end<span style="color: black;">&#40;</span>status, message<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Exits the script with the first argument as the return code and the
       second as the message to generate output.&quot;&quot;&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> status == OK:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;OK: %s&quot;</span> <span style="color: #66cc66;">%</span> message
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">elif</span> status == WARNING:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;WARNING: %s&quot;</span> <span style="color: #66cc66;">%</span> message
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">elif</span> status == CRITICAL:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;CRITICAL: %s&quot;</span> <span style="color: #66cc66;">%</span> message
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;UNKNOWN: %s&quot;</span> <span style="color: #66cc66;">%</span> message
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> validate_thresholds<span style="color: black;">&#40;</span>warning, critical<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Validates warning and critical thresholds in several ways.&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># This is already done by OptionParser.</span>
<span style="color: #808080; font-style: italic;">#    try:</span>
<span style="color: #808080; font-style: italic;">#        warning = int(warning)</span>
<span style="color: #808080; font-style: italic;">#    except ValueError:</span>
<span style="color: #808080; font-style: italic;">#        end(stateUNK, &quot;Warning threshold must be an integer value.&quot;)</span>
<span style="color: #808080; font-style: italic;">#    try:</span>
<span style="color: #808080; font-style: italic;">#        critical = int(critical)</span>
<span style="color: #808080; font-style: italic;">#    except ValueError:</span>
<span style="color: #808080; font-style: italic;">#        end(stateUNK, &quot;Critical threshold must be an integer value.&quot;)</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> critical <span style="color: #66cc66;">!</span>= -<span style="color: #ff4500;">1</span> <span style="color: #ff7700;font-weight:bold;">and</span> warning == -<span style="color: #ff4500;">2</span>:
        end<span style="color: black;">&#40;</span>UNKNOWN, <span style="color: #483d8b;">&quot;Please also set a warning value when using warning/&quot;</span> +
                     <span style="color: #483d8b;">&quot;critical thresholds!&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> critical == -<span style="color: #ff4500;">1</span> <span style="color: #ff7700;font-weight:bold;">and</span> warning <span style="color: #66cc66;">!</span>= -<span style="color: #ff4500;">2</span>:
        end<span style="color: black;">&#40;</span>UNKNOWN, <span style="color: #483d8b;">&quot;Please also set a critical value when using warning/&quot;</span> +
                     <span style="color: #483d8b;">&quot;critical thresholds!&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> critical <span style="color: #66cc66;">&amp;</span>lt<span style="color: #66cc66;">;</span>= warning:
        end<span style="color: black;">&#40;</span>UNKNOWN, <span style="color: #483d8b;">&quot;When using thresholds the critical value has to be &quot;</span> +
                      <span style="color: #483d8b;">&quot;higher than the warning value. Please adjust your &quot;</span> +
                      <span style="color: #483d8b;">&quot;thresholds.&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> retrieve_status_page<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Get's the server's status page and raises an exception if it's not
       accessible.&quot;&quot;&quot;</span>
&nbsp;
    statusPage = <span style="color: #483d8b;">&quot;http://%s:%s/server-status?auto&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>hostname, port<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        retrPage = <span style="color: #dc143c;">urllib</span>.<span style="color: black;">urlretrieve</span><span style="color: black;">&#40;</span>statusPage, <span style="color: #483d8b;">'/tmp/server-status.log'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span>:
        end<span style="color: black;">&#40;</span>CRITICAL, <span style="color: #483d8b;">&quot;Couldn't fetch the server's status page. Please &quot;</span> +
                      <span style="color: #483d8b;">&quot;check given hostname, port or Apache's &quot;</span> +
                      <span style="color: #483d8b;">&quot;configuration. We might not be allowed to access &quot;</span> +
                      <span style="color: #483d8b;">&quot;server-status due to your server's configuration.&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> parse_status_page<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Main parsing function to put the server-status file's content into
       a dictionary.&quot;&quot;&quot;</span>
&nbsp;
    <span style="color: #008000;">file</span> = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/tmp/server-status.log'</span>, <span style="color: #483d8b;">'r'</span><span style="color: black;">&#41;</span>
    line = <span style="color: #008000;">file</span>.<span style="color: #dc143c;">readline</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    dictStatus = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
    counter = <span style="color: #ff4500;">1</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">while</span> line:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #483d8b;">&quot;Total Accesses:&quot;</span> <span style="color: #ff7700;font-weight:bold;">in</span> line:
            key = <span style="color: #483d8b;">&quot;totalAcc&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #483d8b;">&quot;Total kBytes:&quot;</span> <span style="color: #ff7700;font-weight:bold;">in</span> line:
            key = <span style="color: #483d8b;">&quot;totalKb&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #483d8b;">&quot;Uptime:&quot;</span> <span style="color: #ff7700;font-weight:bold;">in</span> line:
            key = <span style="color: #483d8b;">&quot;uptime&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #483d8b;">&quot;ReqPerSec:&quot;</span> <span style="color: #ff7700;font-weight:bold;">in</span> line:
            key = <span style="color: #483d8b;">&quot;reqPSec&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #483d8b;">&quot;BytesPerSec:&quot;</span> <span style="color: #ff7700;font-weight:bold;">in</span> line:
            key = <span style="color: #483d8b;">&quot;bytesPSec&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #483d8b;">&quot;BytesPerReq:&quot;</span> <span style="color: #ff7700;font-weight:bold;">in</span> line:
            key = <span style="color: #483d8b;">&quot;bytesPReq&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #483d8b;">&quot;BusyWorkers:&quot;</span> <span style="color: #ff7700;font-weight:bold;">in</span> line:
            key = <span style="color: #483d8b;">&quot;busyWkrs&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #483d8b;">&quot;IdleWorkers:&quot;</span> <span style="color: #ff7700;font-weight:bold;">in</span> line:
            key = <span style="color: #483d8b;">&quot;idleWkrs&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            key = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>counter<span style="color: black;">&#41;</span>
&nbsp;
        line = line.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        dictStatus<span style="color: black;">&#91;</span>key<span style="color: black;">&#93;</span> = line
        counter = counter + <span style="color: #ff4500;">1</span>
        line = <span style="color: #008000;">file</span>.<span style="color: #dc143c;">readline</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> dictStatus
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> transform_dict<span style="color: black;">&#40;</span>resParse<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Transforms the dictionary to a list and converts variables to proper
       types.&quot;&quot;&quot;</span>
&nbsp;
    totalAcc  = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>resParse<span style="color: black;">&#91;</span><span style="color: #483d8b;">'totalAcc'</span><span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; Total Accesses:&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    totalKb   = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>resParse<span style="color: black;">&#91;</span><span style="color: #483d8b;">'totalKb'</span><span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; Total kBytes:&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    uptime    = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>resParse<span style="color: black;">&#91;</span><span style="color: #483d8b;">'uptime'</span><span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; Uptime:&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    reqPSec   = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>resParse<span style="color: black;">&#91;</span><span style="color: #483d8b;">'reqPSec'</span><span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; ReqPerSec:&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #ff4500;">0</span>
    bytesPSec = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>resParse<span style="color: black;">&#91;</span><span style="color: #483d8b;">'bytesPSec'</span><span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; BytesPerSec:&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> resParse.<span style="color: black;">has_key</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'bytesPReq'</span><span style="color: black;">&#41;</span>:
        bytesPReq = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>resParse<span style="color: black;">&#91;</span><span style="color: #483d8b;">'bytesPReq'</span><span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; BytesPerReq:&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    busyWkrs  = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>resParse<span style="color: black;">&#91;</span><span style="color: #483d8b;">'busyWkrs'</span><span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; BusyWorkers:&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    idleWkrs  = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>resParse<span style="color: black;">&#91;</span><span style="color: #483d8b;">'idleWkrs'</span><span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; IdleWorkers:&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span>reqPSec, busyWkrs, idleWkrs<span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># main</span>
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> critical <span style="color: #66cc66;">!</span>= -<span style="color: #ff4500;">1</span> <span style="color: #ff7700;font-weight:bold;">or</span> warning <span style="color: #66cc66;">!</span>= -<span style="color: #ff4500;">2</span>:
        validate_thresholds<span style="color: black;">&#40;</span>warning, critical<span style="color: black;">&#41;</span>
&nbsp;
    retrieve_status_page<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    resParse = parse_status_page<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    result = transform_dict<span style="color: black;">&#40;</span>resParse<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> critical <span style="color: #66cc66;">!</span>= -<span style="color: #ff4500;">1</span> <span style="color: #ff7700;font-weight:bold;">and</span> warning <span style="color: #66cc66;">!</span>= -<span style="color: #ff4500;">2</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> result<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">&gt;</span>= critical:
            end<span style="color: black;">&#40;</span>CRITICAL, <span style="color: #483d8b;">&quot;Apache serves %f requests per second, exceeding <span style="color: #000099; font-weight: bold;">\</span>
critical threshold! %i busy workers, %i idle workers.&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>result<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, \
result<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, result<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> result<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">&gt;</span>= warning <span style="color: #ff7700;font-weight:bold;">and</span> result<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">&amp;</span>lt<span style="color: #66cc66;">;</span>= critical:
            end<span style="color: black;">&#40;</span>WARNING, <span style="color: #483d8b;">&quot;Apache serves %f requests per second, exceeding <span style="color: #000099; font-weight: bold;">\</span>
warning threshold! %i busy workers, %i idle workers.&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>result<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, \
result<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, result<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            end<span style="color: black;">&#40;</span>OK, <span style="color: #483d8b;">&quot;Apache serves %f requests per second. %i busy workers, <span style="color: #000099; font-weight: bold;">\</span>
%i idle workers.&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>result<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, result<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, result<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        end<span style="color: black;">&#40;</span>OK, <span style="color: #483d8b;">&quot;Apache serves %f requests per second. %i busy workers, %i <span style="color: #000099; font-weight: bold;">\</span>
idle workers.&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>result<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, result<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, result<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<h3>Example Output</h3>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">user<span style="color: #000000; font-weight: bold;">@</span>host $ .<span style="color: #000000; font-weight: bold;">/</span>check_apache2.py <span style="color: #660033;">--version</span>
check_apache2.py <span style="color: #000000;">1.0</span>
user<span style="color: #000000; font-weight: bold;">@</span>host $ .<span style="color: #000000; font-weight: bold;">/</span>check_apache2.py <span style="color: #660033;">-H</span> localhost <span style="color: #660033;">-p</span> <span style="color: #000000;">7001</span>
OK: Apache serves <span style="color: #000000;">9.093710</span> requests per second. <span style="color: #000000;">1</span> busy workers, <span style="color: #000000;">49</span> idle workers.
user<span style="color: #000000; font-weight: bold;">@</span>host $ .<span style="color: #000000; font-weight: bold;">/</span>check_apache2.py <span style="color: #660033;">-H</span> localhost <span style="color: #660033;">-p</span> <span style="color: #000000;">7001</span> <span style="color: #660033;">-w</span> <span style="color: #000000;">2</span> <span style="color: #660033;">-c</span> <span style="color: #000000;">5</span>
CRITICAL: Apache serves <span style="color: #000000;">9.082950</span> requests per second, exceeding critical threshold<span style="color: #000000; font-weight: bold;">!</span> <span style="color: #000000;">1</span> busy workers, <span style="color: #000000;">49</span> idle workers.
user<span style="color: #000000; font-weight: bold;">@</span>host $ .<span style="color: #000000; font-weight: bold;">/</span>check_apache2.py <span style="color: #660033;">-H</span> localhost <span style="color: #660033;">-p</span> <span style="color: #000000;">7001</span> <span style="color: #660033;">-w</span> <span style="color: #000000;">2</span> <span style="color: #660033;">-c</span> <span style="color: #000000;">10</span>
WARNING: Apache serves <span style="color: #000000;">9.077650</span> requests per second, exceeding warning threshold<span style="color: #000000; font-weight: bold;">!</span> <span style="color: #000000;">1</span> busy workers, <span style="color: #000000;">49</span> idle workers.
user<span style="color: #000000; font-weight: bold;">@</span>host $ .<span style="color: #000000; font-weight: bold;">/</span>check_apache2.py <span style="color: #660033;">-H</span> localhost <span style="color: #660033;">-p</span> <span style="color: #000000;">7001</span> <span style="color: #660033;">-w</span> <span style="color: #000000;">10</span> <span style="color: #660033;">-c</span> <span style="color: #000000;">20</span>
OK: Apache serves <span style="color: #000000;">9.073430</span> requests per second. <span style="color: #000000;">1</span> busy workers, <span style="color: #000000;">49</span> idle workers.
user<span style="color: #000000; font-weight: bold;">@</span>host $ .<span style="color: #000000; font-weight: bold;">/</span>check_apache2.py <span style="color: #660033;">-H</span> localhost <span style="color: #660033;">-p</span> <span style="color: #000000;">7001</span> <span style="color: #660033;">-w</span> <span style="color: #000000;">10</span> <span style="color: #660033;">-c</span> <span style="color: #000000;">5</span>
UNKNOWN: When using thresholds the critical value has to be higher than the warning value. Please adjust your thresholds.
user<span style="color: #000000; font-weight: bold;">@</span>host $ .<span style="color: #000000; font-weight: bold;">/</span>check_apache2.py <span style="color: #660033;">-H</span> localhost <span style="color: #660033;">-p</span> <span style="color: #000000;">7001</span> <span style="color: #660033;">-w</span> <span style="color: #000000;">5</span>
UNKNOWN: Please also <span style="color: #000000; font-weight: bold;">set</span> a critical value when using warning<span style="color: #000000; font-weight: bold;">/</span>critical thresholds<span style="color: #000000; font-weight: bold;">!</span>
user<span style="color: #000000; font-weight: bold;">@</span>host $ .<span style="color: #000000; font-weight: bold;">/</span>check_apache2.py <span style="color: #660033;">-H</span> localhost <span style="color: #660033;">-p</span> <span style="color: #000000;">7001</span> <span style="color: #660033;">-c</span> <span style="color: #000000;">5</span>
UNKNOWN: Please also <span style="color: #000000; font-weight: bold;">set</span> a warning value when using warning<span style="color: #000000; font-weight: bold;">/</span>critical thresholds<span style="color: #000000; font-weight: bold;">!</span>
user<span style="color: #000000; font-weight: bold;">@</span>host $ .<span style="color: #000000; font-weight: bold;">/</span>check_apache2.py <span style="color: #660033;">-H</span> localhost <span style="color: #660033;">-p</span> <span style="color: #000000;">7002</span>
CRITICAL: Couldn<span style="color: #ff0000;">'t fetch the server'</span>s status page. Please check given <span style="color: #c20cb9; font-weight: bold;">hostname</span>, port or Apache<span style="color: #ff0000;">'s configuration. We might not be allowed to access server-status due to your server'</span>s configuration.</pre></div></div>

<h3>The License</h3>
<p>As always this little script is ment to be sh-compliant and released under the terms of the GPL Version 2 only. Feel free to subscribe via rss to get updates on this one. More options will be added in the future.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.matejunkie.com/apache-check-plugin-for-nagios-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Porting my Nagios Plugins to Python</title>
		<link>http://www.matejunkie.com/porting-my-nagios-plugins-to-python/</link>
		<comments>http://www.matejunkie.com/porting-my-nagios-plugins-to-python/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 08:33:19 +0000</pubDate>
		<dc:creator>Mike Adolphs</dc:creator>
				<category><![CDATA[Binary Talks]]></category>
		<category><![CDATA[apache webserver]]></category>
		<category><![CDATA[dictionary]]></category>
		<category><![CDATA[draft]]></category>
		<category><![CDATA[nagios]]></category>
		<category><![CDATA[network monitoring]]></category>
		<category><![CDATA[optionparser]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[urllib]]></category>

		<guid isPermaLink="false">http://www.matejunkie.com/?p=873</guid>
		<description><![CDATA[I&#8217;ve just started to rewrite my sh written Nagios Plugins in Python, mainly to get a feeling for writing Python code. First steps with writing object-orientated code were&#8230; &#8220;not so good, Al!&#8221; as Weezer would have said to this. Therefore I&#8217;ve began with some lines of functional programming. That&#8217;ll do the job for now. Below [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just started to rewrite my sh written Nagios Plugins in Python, mainly to get a feeling for writing Python code. First steps with writing object-orientated code were&#8230; &#8220;not so good, Al!&#8221; as Weezer would have said to this. Therefore I&#8217;ve began with some lines of functional programming. That&#8217;ll do the job for now.</p>
<p>Below you&#8217;ll see a rudimentary plugin to check the Apache&#8217;s server-status page. Note that it will never go critical at this time. I was just playing around with retrieving a file, putting the content into a dictionary and then transforming it to a proper output which Nagios would understand.<br />
I&#8217;ll do more work on this within the next days, expect an update here soon. And if you have any suggestions what could have been done better, let me know.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">optparse</span> <span style="color: #ff7700;font-weight:bold;">import</span> OptionParser
&nbsp;
stateOK = <span style="color: #ff4500;">0</span>
stateWARN = <span style="color: #ff4500;">1</span>
stateCRIT = <span style="color: #ff4500;">2</span>
stateUNK = <span style="color: #ff4500;">3</span>
&nbsp;
<span style="color: #dc143c;">parser</span> = OptionParser<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">&quot;-H&quot;</span>,
                   <span style="color: #483d8b;">&quot;--hostname&quot;</span>,
                   <span style="color: #008000;">type</span>=<span style="color: #483d8b;">&quot;string&quot;</span>,
                   dest=<span style="color: #483d8b;">&quot;hostname&quot;</span>,
                   default=<span style="color: #483d8b;">&quot;localhost&quot;</span>,
                   <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;Sets the hostname to check.&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">&quot;-p&quot;</span>,
                   <span style="color: #483d8b;">&quot;--port&quot;</span>,
                   <span style="color: #008000;">type</span>=<span style="color: #483d8b;">&quot;int&quot;</span>,
                   dest=<span style="color: #483d8b;">&quot;port&quot;</span>,
                   default=<span style="color: #ff4500;">80</span>,
                   <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;Sets the port of the host to check.&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: black;">&#40;</span>options, args<span style="color: black;">&#41;</span> = <span style="color: #dc143c;">parser</span>.<span style="color: black;">parse_args</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
hostname = options.<span style="color: black;">hostname</span>
port = options.<span style="color: black;">port</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> retrieve_status_page<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    statusPage = <span style="color: #483d8b;">&quot;http://%s:%s/server-status?auto&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>hostname, port<span style="color: black;">&#41;</span>
    retrPage = <span style="color: #dc143c;">urllib</span>.<span style="color: black;">urlretrieve</span><span style="color: black;">&#40;</span>statusPage, <span style="color: #483d8b;">'/tmp/server-status.log'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> parse_status_page<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #008000;">file</span> = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/tmp/server-status.log'</span>, <span style="color: #483d8b;">'r'</span><span style="color: black;">&#41;</span>
    line = <span style="color: #008000;">file</span>.<span style="color: #dc143c;">readline</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    dictStatus = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
    counter = <span style="color: #ff4500;">1</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">while</span> line:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #483d8b;">&quot;Total Accesses:&quot;</span> <span style="color: #ff7700;font-weight:bold;">in</span> line:
            key = <span style="color: #483d8b;">&quot;totalAcc&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #483d8b;">&quot;Total kBytes:&quot;</span> <span style="color: #ff7700;font-weight:bold;">in</span> line:
            key = <span style="color: #483d8b;">&quot;totalKb&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #483d8b;">&quot;Uptime:&quot;</span> <span style="color: #ff7700;font-weight:bold;">in</span> line:
            key = <span style="color: #483d8b;">&quot;uptime&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #483d8b;">&quot;ReqPerSec:&quot;</span> <span style="color: #ff7700;font-weight:bold;">in</span> line:
            key = <span style="color: #483d8b;">&quot;reqPSec&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #483d8b;">&quot;BytesPerSec:&quot;</span> <span style="color: #ff7700;font-weight:bold;">in</span> line:
            key = <span style="color: #483d8b;">&quot;bytesPSec&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #483d8b;">&quot;BytesPerReq:&quot;</span> <span style="color: #ff7700;font-weight:bold;">in</span> line:
            key = <span style="color: #483d8b;">&quot;bytesPReq&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #483d8b;">&quot;BusyWorkers:&quot;</span> <span style="color: #ff7700;font-weight:bold;">in</span> line:
            key = <span style="color: #483d8b;">&quot;busyWkrs&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #483d8b;">&quot;IdleWorkers:&quot;</span> <span style="color: #ff7700;font-weight:bold;">in</span> line:
            key = <span style="color: #483d8b;">&quot;idleWkrs&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            key = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>counter<span style="color: black;">&#41;</span>
&nbsp;
        line = line.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        dictStatus<span style="color: black;">&#91;</span>key<span style="color: black;">&#93;</span> = line
        counter = counter + <span style="color: #ff4500;">1</span>
        line = <span style="color: #008000;">file</span>.<span style="color: #dc143c;">readline</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> dictStatus
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> transform_dict<span style="color: black;">&#40;</span>resParse<span style="color: black;">&#41;</span>:
    totalAcc  = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>resParse<span style="color: black;">&#91;</span><span style="color: #483d8b;">'totalAcc'</span><span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; Total Accesses:&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    totalKb   = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>resParse<span style="color: black;">&#91;</span><span style="color: #483d8b;">'totalKb'</span><span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; Total kBytes:&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    uptime    = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>resParse<span style="color: black;">&#91;</span><span style="color: #483d8b;">'uptime'</span><span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; Uptime:&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    reqPSec   = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>resParse<span style="color: black;">&#91;</span><span style="color: #483d8b;">'reqPSec'</span><span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; ReqPerSec:&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #ff4500;">0</span>
    bytesPSec = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>resParse<span style="color: black;">&#91;</span><span style="color: #483d8b;">'bytesPSec'</span><span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; BytesPerSec:&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> resParse.<span style="color: black;">has_key</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'bytesPReq'</span><span style="color: black;">&#41;</span>:
        bytesPReq = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>resParse<span style="color: black;">&#91;</span><span style="color: #483d8b;">'bytesPReq'</span><span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; BytesPerReq:&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    busyWkrs  = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>resParse<span style="color: black;">&#91;</span><span style="color: #483d8b;">'busyWkrs'</span><span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; BusyWorkers:&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    idleWkrs  = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>resParse<span style="color: black;">&#91;</span><span style="color: #483d8b;">'idleWkrs'</span><span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; IdleWorkers:&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span>reqPSec, busyWkrs, idleWkrs<span style="color: black;">&#93;</span>
&nbsp;
retrieve_status_page<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
resParse = parse_status_page<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
result = transform_dict<span style="color: black;">&#40;</span>resParse<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;OK: Apache serves %f requests per second. %i busy workers, %i worker &quot;</span> \
      <span style="color: #483d8b;">&quot;idle.&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>result<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, result<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, result<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
exit<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.matejunkie.com/porting-my-nagios-plugins-to-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
