Selenium, Nagios and Magento– together at last!

Now that I have a server running selenium tests against a magento-powered ecommerce site and logging those tests to a file, we need to integrate with nagios. First, I installed openSSH on that windows server (via copssh), and copied over the nagios user public key to authorized_keys. Then I wrote this nagios script:

#! /usr/bin/php -q
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<a href="http://www.php.net/ini_set"><span style="color: #990000;">ini_set</span></a><span style="color: #009900;">&#40;</span> <span style="">'max_execution_time'</span><span style="color: #339933;">,</span> <span style="">'55'</span> <span style="color: #009900;">&#41;</span>;
<a href="http://www.php.net/ini_set"><span style="color: #990000;">ini_set</span></a><span style="color: #009900;">&#40;</span> <span style="">'display_errors'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">false</span> <span style="color: #009900;">&#41;</span>;
&nbsp;
<a href="http://www.php.net/define"><span style="color: #990000;">define</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;STATUS_OK&quot;</span><span style="color: #339933;">,</span>       <span style="color:#800080;">0</span> <span style="color: #009900;">&#41;</span>;
<a href="http://www.php.net/define"><span style="color: #990000;">define</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;STATUS_WARNING&quot;</span><span style="color: #339933;">,</span>  <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span>;
<a href="http://www.php.net/define"><span style="color: #990000;">define</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;STATUS_CRITICAL&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#41;</span>;
<a href="http://www.php.net/define"><span style="color: #990000;">define</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;STATUS_UNKNOWN&quot;</span><span style="color: #339933;">,</span>  <span style="color: #cc66cc;">3</span> <span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #000088;">$execCommand</span><span style="color: #339933;">=</span><span style="">'ssh WINDOWS_SERVER_IP_GOES_HERE -l nagiosremote &quot;tail -n 1 selenium.txt&quot;'</span>;
<a href="http://www.php.net/exec"><span style="color: #990000;">exec</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$execCommand</span><span style="color: #339933;">,</span><span style="color: #000088;">$output</span><span style="color: #009900;">&#41;</span>;
<a href="http://www.php.net/list"><span style="color: #990000;">list</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #339933;">,</span><span style="color: #000088;">$date</span><span style="color: #339933;">,</span><span style="color: #000088;">$time</span><span style="color: #339933;">,</span><span style="color: #000088;">$status</span><span style="color: #339933;">,</span><span style="color: #000088;">$avg</span><span style="color: #339933;">,</span><span style="color: #000088;">$std</span><span style="color: #339933;">,</span><span style="color: #000088;">$FLAG</span><span style="color: #339933;">,</span><span style="color: #000088;">$flagCnt</span><span style="color: #339933;">,</span><span style="color: #000088;">$statCnt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><a href="http://www.php.net/split"><span style="color: #990000;">split</span></a><span style="color: #009900;">&#40;</span><span style="">','</span><span style="color: #339933;">,</span><span style="color: #000088;">$output</span><span style="color: #009900;">&#91;</span><span style="color:#800080;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span>;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/strpos"><span style="color: #990000;">strpos</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$status</span><span style="color: #339933;">,</span><span style="">'OK'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">===</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>||<span style="color: #009900;">&#40;</span><a href="http://www.php.net/strpos"><span style="color: #990000;">strpos</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$FLAG</span><span style="color: #339933;">,</span><span style="">'ERROR'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!==</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <a href="http://www.php.net/echo"><span style="color: #990000;">echo</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ERROR: $status $FLAGn&quot;</span><span style="color: #009900;">&#41;</span>;
        <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><span style="color: #009900;">&#40;</span> STATUS_CRITICAL <span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
        <a href="http://www.php.net/echo"><span style="color: #990000;">echo</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;OK: time $time, status $status, avg $avg, std $std, FLAG $FLAG, flagCnt $flagCnt, statCnt $statCntn&quot;</span><span style="color: #009900;">&#41;</span>;
        <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><span style="color: #009900;">&#40;</span> STATUS_OK <span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>

Then I defined a check_command, a host, a host_group and a service in nagios and we’re in business!