<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../../assets/xml/rss.xsl" media="all"?><rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Hobgoblin's Master (monitoring)</title><link>http://hobgoblins-master.info/</link><description></description><atom:link href="http://hobgoblins-master.info/en/categories/monitoring.xml" type="application/rss+xml" rel="self"></atom:link><language>en</language><lastBuildDate>Tue, 25 Aug 2015 11:16:03 GMT</lastBuildDate><generator>http://getnikola.com/</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>check_by_ssh_wrapper</title><link>http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html</link><dc:creator>Landry MINOZA</dc:creator><description>&lt;div&gt;&lt;p&gt;Trying to mutualise Nagios services (or Shinken in my case) and NRPE configuration, sometimes a check returns to many data for NRPE or a host in DMZ don't have NRPE port accessible.&lt;/p&gt;
&lt;p&gt;On one infrastructure I monitor, while my NRPE configuration can check free disk space on local file systems (among other), I have a NAS in one DMZ with a lot of file systems (one LVM volume by share). The size of returned data, with Nagios perf data was above NRPE limit (1 kB). Rather than moving NRPE command line in Shinken services configuration and having to handle character escaping, rights and complexify Shinken configuration, I wrote this little script which use via &lt;tt class="docutils literal"&gt;check_by_ssh&lt;/tt&gt; the same parameters than &lt;tt class="docutils literal"&gt;check_nrpe&lt;/tt&gt; and read NRPE configuration one the client host to validate the command and execute it.&lt;/p&gt;
&lt;!-- TEASER_END --&gt;
&lt;div class="section" id="the-code"&gt;
&lt;h2&gt;The code&lt;/h2&gt;
&lt;p&gt;It is a simple bash script, hosted on &lt;a class="reference external" href="https://github.com/hobgoblinsmaster/check_by_ssh_wrapper"&gt;GitHub&lt;/a&gt;. At time of writing this post, the code is:&lt;/p&gt;
&lt;table class="codetable"&gt;&lt;tr&gt;&lt;td class="linenos"&gt;&lt;div class="linenodiv"&gt;&lt;pre&gt;&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-1"&gt; 1&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-2"&gt; 2&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-3"&gt; 3&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-4"&gt; 4&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-5"&gt; 5&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-6"&gt; 6&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-7"&gt; 7&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-8"&gt; 8&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-9"&gt; 9&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-10"&gt;10&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-11"&gt;11&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-12"&gt;12&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-13"&gt;13&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-14"&gt;14&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-15"&gt;15&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-16"&gt;16&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-17"&gt;17&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-18"&gt;18&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-19"&gt;19&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-20"&gt;20&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-21"&gt;21&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-22"&gt;22&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-23"&gt;23&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-24"&gt;24&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-25"&gt;25&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-26"&gt;26&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-27"&gt;27&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-28"&gt;28&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-29"&gt;29&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-30"&gt;30&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-31"&gt;31&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-32"&gt;32&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-33"&gt;33&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-34"&gt;34&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-35"&gt;35&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-36"&gt;36&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-37"&gt;37&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-38"&gt;38&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-39"&gt;39&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-40"&gt;40&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-41"&gt;41&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-42"&gt;42&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-43"&gt;43&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-44"&gt;44&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-45"&gt;45&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-46"&gt;46&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-47"&gt;47&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-48"&gt;48&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-49"&gt;49&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-50"&gt;50&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-51"&gt;51&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-52"&gt;52&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-53"&gt;53&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-54"&gt;54&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-55"&gt;55&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-56"&gt;56&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-57"&gt;57&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-58"&gt;58&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-59"&gt;59&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-60"&gt;60&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-61"&gt;61&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-62"&gt;62&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-63"&gt;63&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-64"&gt;64&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-65"&gt;65&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-66"&gt;66&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-67"&gt;67&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-68"&gt;68&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-69"&gt;69&lt;/a&gt;
&lt;a href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#rest_code_d298a16652fa41c3861c19b857501c1b-70"&gt;70&lt;/a&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;pre class="code bash"&gt;&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-1"&gt;&lt;/a&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-2"&gt;&lt;/a&gt;&lt;span class="c"&gt;#&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-3"&gt;&lt;/a&gt;&lt;span class="c"&gt;# check_by_ssh_wrapper&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-4"&gt;&lt;/a&gt;&lt;span class="c"&gt;#&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-5"&gt;&lt;/a&gt;&lt;span class="c"&gt;# This script provides access to commands declared in nrpe to check_by_ssh&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-6"&gt;&lt;/a&gt;&lt;span class="c"&gt;# Please set the nrpeconf variable to the right value&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-7"&gt;&lt;/a&gt;&lt;span class="c"&gt;# You must generate a ssh key on the nagios server, copy it in .ssh/authorized_keys&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-8"&gt;&lt;/a&gt;&lt;span class="c"&gt;# in nagios (or nrpe) user home directory on distant server, then add this at&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-9"&gt;&lt;/a&gt;&lt;span class="c"&gt;# the begining of the line juste before the key:&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-10"&gt;&lt;/a&gt;&lt;span class="c"&gt;# no-pty,no-port-forwarding,command="/usr/local/bin/check_by_ssh_wrapper $SSH_ORIGINAL_COMMAND"&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-11"&gt;&lt;/a&gt;&lt;span class="c"&gt;#&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-12"&gt;&lt;/a&gt;&lt;span class="c"&gt;# You can the use $USER1$/check_by_ssh -H &amp;lt;distant host&amp;gt; -l nagios -C 'check_cmd [args]'&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-13"&gt;&lt;/a&gt;&lt;span class="c"&gt;#&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-14"&gt;&lt;/a&gt;&lt;span class="c"&gt;# Freely inspired by nagios_ssh_framework. Can be used when nrpe port can't be accessed&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-15"&gt;&lt;/a&gt;&lt;span class="c"&gt;# or check_command returns to many text for nrpe and you want to keep facilities of nrpe&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-16"&gt;&lt;/a&gt;&lt;span class="c"&gt;# config files.&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-17"&gt;&lt;/a&gt;&lt;span class="c"&gt;#&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-18"&gt;&lt;/a&gt;&lt;span class="c"&gt;# Changes an Modifications:&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-19"&gt;&lt;/a&gt;&lt;span class="c"&gt;# 2011-10-12 - Landry MINOZA - V 1.0&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-20"&gt;&lt;/a&gt;&lt;span class="c"&gt;# Created because check_disks returns to many lines for nrpe with one of my servers&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-21"&gt;&lt;/a&gt;&lt;span class="c"&gt;#&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-22"&gt;&lt;/a&gt;&lt;span class="c"&gt;# Copyright: Telbase Formation Conseil, MUSEE D'ORSAY 2011&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-23"&gt;&lt;/a&gt;&lt;span class="c"&gt;#&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-24"&gt;&lt;/a&gt;&lt;span class="c"&gt;# This program is free software: you can redistribute it and/or modify&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-25"&gt;&lt;/a&gt;&lt;span class="c"&gt;# it under the terms of the GNU General Public License as published by&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-26"&gt;&lt;/a&gt;&lt;span class="c"&gt;# the Free Software Foundation, either version 3 of the License, or&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-27"&gt;&lt;/a&gt;&lt;span class="c"&gt;# (at your option) any later version.&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-28"&gt;&lt;/a&gt;&lt;span class="c"&gt;#&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-29"&gt;&lt;/a&gt;&lt;span class="c"&gt;# This program is distributed in the hope that it will be useful,&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-30"&gt;&lt;/a&gt;&lt;span class="c"&gt;# but WITHOUT ANY WARRANTY; without even the implied warranty of&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-31"&gt;&lt;/a&gt;&lt;span class="c"&gt;# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-32"&gt;&lt;/a&gt;&lt;span class="c"&gt;# GNU General Public License for more details.&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-33"&gt;&lt;/a&gt;&lt;span class="c"&gt;#&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-34"&gt;&lt;/a&gt;&lt;span class="c"&gt;# You should have received a copy of the GNU General Public License&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-35"&gt;&lt;/a&gt;&lt;span class="c"&gt;# along with this program.  If not, see &amp;lt;http://www.gnu.org/licenses/&amp;gt;.&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-36"&gt;&lt;/a&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-37"&gt;&lt;/a&gt;&lt;span class="c"&gt;# Set main nrpe.cfg file&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-38"&gt;&lt;/a&gt;&lt;span class="nv"&gt;nrpeconf&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/etc/nagios/nrpe.cfg
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-39"&gt;&lt;/a&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-40"&gt;&lt;/a&gt;&lt;span class="c"&gt;# Get cmdname&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-41"&gt;&lt;/a&gt;&lt;span class="nv"&gt;cmdname&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-42"&gt;&lt;/a&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; -z &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$cmdname&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-43"&gt;&lt;/a&gt;        &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"WARNING, no command name provided"&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-44"&gt;&lt;/a&gt;        &lt;span class="nb"&gt;exit &lt;/span&gt;1
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-45"&gt;&lt;/a&gt;&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-46"&gt;&lt;/a&gt;&lt;span class="nb"&gt;shift&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-47"&gt;&lt;/a&gt;&lt;span class="c"&gt;# Get optionnal args&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-48"&gt;&lt;/a&gt;&lt;span class="nv"&gt;cmdargs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$@&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-49"&gt;&lt;/a&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-50"&gt;&lt;/a&gt;&lt;span class="c"&gt;# Declare an array of commands&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-51"&gt;&lt;/a&gt;&lt;span class="nb"&gt;declare&lt;/span&gt; -A &lt;span class="nb"&gt;command&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-52"&gt;&lt;/a&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-53"&gt;&lt;/a&gt;&lt;span class="c"&gt;# find all nrpe conf files&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-54"&gt;&lt;/a&gt;&lt;span class="nv"&gt;include&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$(&lt;/span&gt;sed -ne &lt;span class="s1"&gt;'/^include=/ s/^.*=// p'&lt;/span&gt; &lt;span class="nv"&gt;$nrpeconf&lt;/span&gt;&lt;span class="k"&gt;)&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-55"&gt;&lt;/a&gt;&lt;span class="nv"&gt;include_dir&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$(&lt;/span&gt;sed -ne &lt;span class="s1"&gt;'/^include_dir=/ s/^.*=// p'&lt;/span&gt; &lt;span class="nv"&gt;$nrpeconf&lt;/span&gt;&lt;span class="k"&gt;)&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-56"&gt;&lt;/a&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-57"&gt;&lt;/a&gt;&lt;span class="c"&gt;# Parse all nrpe conf files for command[*]= arguments&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-58"&gt;&lt;/a&gt;&lt;span class="k"&gt;for&lt;/span&gt; cfgfile in &lt;span class="k"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; -e &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$nrpeconf&lt;/span&gt;&lt;span class="s2"&gt;\n&lt;/span&gt;&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;include&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;\n&lt;/span&gt;&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;include_dir&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;*.cfg"&lt;/span&gt;&lt;span class="k"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="k"&gt;do&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-59"&gt;&lt;/a&gt;        &lt;span class="nb"&gt;eval&lt;/span&gt; &lt;span class="k"&gt;$(&lt;/span&gt;sed -ne &lt;span class="s1"&gt;'/^command\[/ s/=\(.*\)$/="\1"/ p'&lt;/span&gt; &lt;span class="nv"&gt;$cfgfile&lt;/span&gt;&lt;span class="k"&gt;)&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-60"&gt;&lt;/a&gt;&lt;span class="k"&gt;done&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-61"&gt;&lt;/a&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-62"&gt;&lt;/a&gt;&lt;span class="c"&gt;# Check if command[$cmdname] exists&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-63"&gt;&lt;/a&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; -z &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;command&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;$cmdname&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-64"&gt;&lt;/a&gt;        &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"WARNING, command &lt;/span&gt;&lt;span class="nv"&gt;$cmdname&lt;/span&gt;&lt;span class="s2"&gt; does not exist"&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-65"&gt;&lt;/a&gt;        &lt;span class="nb"&gt;exit &lt;/span&gt;1
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-66"&gt;&lt;/a&gt;&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-67"&gt;&lt;/a&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-68"&gt;&lt;/a&gt;&lt;span class="c"&gt;# Launch check&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-69"&gt;&lt;/a&gt;&lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;command&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;$cmdname&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="nv"&gt;$cmdargs&lt;/span&gt;
&lt;a name="rest_code_d298a16652fa41c3861c19b857501c1b-70"&gt;&lt;/a&gt;&lt;span class="nb"&gt;exit&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;p&gt;The code can be divided in 8 parts:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Line 38, to customize, this is the path of NRPE configuration file (for Debian stable here).&lt;/li&gt;
&lt;li&gt;Line 40 to 48, command line parameters validation.&lt;/li&gt;
&lt;li&gt;Line 51, explicit (and mandatory) declaration of an associative array of commands, it is used to store for each command named in NRPE configuration the related command line.&lt;/li&gt;
&lt;li&gt;Line 54 and 55, retrieval of include and include_dir statement in nrpe.conf file to take into account the referenced files. &lt;strong&gt;Take care&lt;/strong&gt;, this include files research is not yet recusive, the statements are only analyzed in the main configuration file.&lt;/li&gt;
&lt;li&gt;Line 58 and 60, loop on list on found files.&lt;/li&gt;
&lt;li&gt;Line 59, this line is the core of the script, this is a trick base on NRPE configuration syntax, indeed, to declare a command in NRPE, one writes: &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;command[command_name]=command_line&lt;/span&gt; options&lt;/tt&gt;. Lines simply have to be extracted and the command line part (after “=”) have to be quoted, then this have to be executed via eval to fill the array &lt;em&gt;command&lt;/em&gt; declared before.&lt;/li&gt;
&lt;li&gt;Line 63, testing if a command whith the name given as parameter to the script exists.&lt;/li&gt;
&lt;li&gt;At last, line 69, execution of the specified command with his parameters. The explicit exit at the end, ensure that the exit state of the script is the same that the one of the executed command, and so transmited to Nagios.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="installation"&gt;
&lt;h2&gt;Installation&lt;/h2&gt;
&lt;p&gt;This script have to be installed on the monitored host, not on the monitoring server, for instance in &lt;tt class="docutils literal"&gt;/usr/local/bin&lt;/tt&gt;. The user executing NRPe daemon (on Debian, it is named nagios) must have execution rights on the script. By default, this user have no password set, and we do not want to set one. Anyway, this is generaly a bad idea to have passwords in Nagios configuration files. First, generate a SSH key pair on the monitoring server using the command &lt;tt class="docutils literal"&gt;ssh_keygen&lt;/tt&gt; as the monitoring daemon user. By default, this commande generates a RSA key, stores the private key in &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;$HOME/.ssh/id_rsa&lt;/span&gt;&lt;/tt&gt; and the public one in &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;$HOME/.ssh/id_rsa.pub&lt;/span&gt;&lt;/tt&gt;. This last is the one we want.&lt;/p&gt;
&lt;p&gt;On the monitored host, create in the &lt;tt class="docutils literal"&gt;$HOME&lt;/tt&gt; of the NRPE daemon user (&lt;tt class="docutils literal"&gt;/var/lib/nagios&lt;/tt&gt; on Debian) a directory named &lt;tt class="docutils literal"&gt;.ssh&lt;/tt&gt; whith unix rights &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;rwx------&lt;/span&gt;&lt;/tt&gt;&lt;/p&gt;
&lt;pre class="code bash"&gt;&lt;a name="rest_code_bf97a56ca0b4439db5c45bfcb551329f-1"&gt;&lt;/a&gt;mkdir /var/lib/nagios/.ssh
&lt;a name="rest_code_bf97a56ca0b4439db5c45bfcb551329f-2"&gt;&lt;/a&gt;chown nagios:nagios /var/lib/nagios/.ssh
&lt;a name="rest_code_bf97a56ca0b4439db5c45bfcb551329f-3"&gt;&lt;/a&gt;chmod &lt;span class="nv"&gt;u&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;rwx,go&lt;span class="o"&gt;=&lt;/span&gt; /var/lib/nagios/.ssh
&lt;/pre&gt;&lt;p&gt;In the folder, create a file named &lt;tt class="docutils literal"&gt;authorized_keys&lt;/tt&gt; containing the following line:&lt;/p&gt;
&lt;pre class="code text"&gt;&lt;a name="rest_code_ec6fb29d6f934bd696fb759a3fe98e0c-1"&gt;&lt;/a&gt;no-pty,no-port-forwarding,command="/usr/local/bin/check_by_ssh_wrapper $SSH_ORIGINAL_COMMAND" ssh-rsa AAAA… user@server
&lt;/pre&gt;&lt;p&gt;Replace &lt;tt class="docutils literal"&gt;AAAA… user@server&lt;/tt&gt; with the content of the file &lt;tt class="docutils literal"&gt;id_rsa.pub&lt;/tt&gt; generated on the server.
This line &lt;a class="footnote-reference" href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#id2" id="id1"&gt;[1]&lt;/a&gt; permits to allow only the command &lt;tt class="docutils literal"&gt;chaeck_by_ssh_wrapper&lt;/tt&gt; (only place here an absolute path) to be executed when connectiong with this SSH key and forbid ports forwarding and pseudo-tty allocation. The original command passed to ssh will be provided as parameter to our script via the variable &lt;tt class="docutils literal"&gt;$SSH_ORIGINAL_COMMAND&lt;/tt&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="using"&gt;
&lt;h2&gt;Using&lt;/h2&gt;
&lt;p&gt;In Nagios configuration, just create a new command object:&lt;/p&gt;
&lt;pre class="code text"&gt;&lt;a name="rest_code_125039f3beb34a71943b7c10c836a29c-1"&gt;&lt;/a&gt;define command{
&lt;a name="rest_code_125039f3beb34a71943b7c10c836a29c-2"&gt;&lt;/a&gt;        command_name check_by_ssh
&lt;a name="rest_code_125039f3beb34a71943b7c10c836a29c-3"&gt;&lt;/a&gt;        command_line $USER1$/check_by_ssh -H $HOSTADDRESS$ -t 60 -l nagios -C '$ARG1$ $ARG2$'
&lt;a name="rest_code_125039f3beb34a71943b7c10c836a29c-4"&gt;&lt;/a&gt;}
&lt;/pre&gt;&lt;p&gt;This can be used the same way than check_nrpe in services definitions:&lt;/p&gt;
&lt;pre class="code text"&gt;&lt;a name="rest_code_268095b07e9a4c769d3e06a59672871c-1"&gt;&lt;/a&gt;define service{
&lt;a name="rest_code_268095b07e9a4c769d3e06a59672871c-2"&gt;&lt;/a&gt;        use                     base-service
&lt;a name="rest_code_268095b07e9a4c769d3e06a59672871c-3"&gt;&lt;/a&gt;        service_description     check_disks
&lt;a name="rest_code_268095b07e9a4c769d3e06a59672871c-4"&gt;&lt;/a&gt;        display_name            Check Disks
&lt;a name="rest_code_268095b07e9a4c769d3e06a59672871c-5"&gt;&lt;/a&gt;        check_command           check_by_ssh!check_disks
&lt;a name="rest_code_268095b07e9a4c769d3e06a59672871c-6"&gt;&lt;/a&gt;}
&lt;/pre&gt;&lt;/div&gt;
&lt;div class="section" id="conclusion"&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;This is not perfect, it does not handle recusrive inclusions, but it work as I want since 2 years and permited me to simplify a lot the use of &lt;tt class="docutils literal"&gt;check_by_ssh&lt;/tt&gt; plugin, securing it as only commands declared in NRPE configuration are usable.&lt;/p&gt;
&lt;table class="docutils footnote" frame="void" id="id2" rules="none"&gt;
&lt;colgroup&gt;&lt;col class="label"&gt;&lt;col&gt;&lt;/colgroup&gt;
&lt;tbody valign="top"&gt;
&lt;tr&gt;&lt;td class="label"&gt;&lt;a class="fn-backref" href="http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html#id1"&gt;[1]&lt;/a&gt;&lt;/td&gt;&lt;td&gt;I'm preparing a more complete post on ssh (and PuTTY).&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;&lt;/div&gt;</description><category>monitoring</category><category>shinken</category><category>ssh</category><guid>http://hobgoblins-master.info/en/posts/check_by_ssh_wrapper.html</guid><pubDate>Sun, 27 Oct 2013 17:00:00 GMT</pubDate></item></channel></rss>