<?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>Router and Switch Monitoring &#8211; Nagios Library</title>
	<atom:link href="https://library.nagios.com/tag/router-and-switch-monitoring/feed/" rel="self" type="application/rss+xml" />
	<link>https://library.nagios.com</link>
	<description>Complete Nagios monitoring resources and documentation</description>
	<lastBuildDate>Tue, 31 Mar 2026 15:47:16 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://library.nagios.com/wp-content/uploads/2024/11/Nagios-Blue-N.svg</url>
	<title>Router and Switch Monitoring &#8211; Nagios Library</title>
	<link>https://library.nagios.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Route Hops: How to Create a Nagios Plugin to Monitor Them</title>
		<link>https://library.nagios.com/training/route-hops-nagios-plugin/</link>
		
		<dc:creator><![CDATA[Tyler Larson]]></dc:creator>
		<pubDate>Tue, 04 Nov 2025 14:45:00 +0000</pubDate>
				<category><![CDATA[Training]]></category>
		<category><![CDATA[Network Monitoring]]></category>
		<category><![CDATA[Router and Switch Monitoring]]></category>
		<guid isPermaLink="false">https://library.nagios.com/?p=41190</guid>

					<description><![CDATA[An important metric in network diagnostics is the number of hops it takes to reach a target host. In this article, we will create a plugin for Nagios using Python to monitor route hops to a target host. Step 1: Prerequisites Step 2: Set Up the Python Script Create a new Python file named check_route_hops.py [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>An important metric in network diagnostics is the number of hops it takes to reach a target host. In this article, we will create a plugin for Nagios using Python to monitor route hops to a target host.</p>



<h2 class="wp-block-heading">Step 1: Prerequisites</h2>



<ul class="wp-block-list">
<li>Python 3: The script uses Python, so make sure you have it installed so you can test your plugin as you develop it. It can be downloaded <a href="https://www.python.org/downloads/" target="_blank" rel="noreferrer noopener">here</a>.</li>



<li>A code editor: To create this plugin I used VS Code, which can be downloaded <a href="https://code.visualstudio.com/" data-type="link" data-id="https://code.visualstudio.com/" target="_blank" rel="noreferrer noopener">here</a>.</li>
</ul>



<h2 class="wp-block-heading">Step 2: Set Up the Python Script</h2>



<p>Create a new Python file named <em>check_route_hops.py</em> and begin with a shebang line and essential import statements.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-Geist-Mono" style="font-size:.875rem;font-family:Code-Pro-Geist-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.5rem;--cbp-tab-width:2"><span role="button" data-code="#!/usr/bin/env python3
import sys
import traceback
import argparse
import subprocess
def main():
if __name__ == &quot;__main__&quot;:
    main()" style="color:#adbac7" aria-label="Copy" class="code-block-pro-copy-button"></span><pre class="shiki github-dark-dimmed" style="background-color: #22272e"><code><span class="line"><span style="color: #768390">#!/usr/bin/env python3</span></span>
<span class="line"></span>
<span class="line"><span style="color: #F47067">import</span><span style="color: #ADBAC7"> sys</span></span>
<span class="line"><span style="color: #ADBAC7">import traceback</span></span>
<span class="line"><span style="color: #ADBAC7">import argparse</span></span>
<span class="line"><span style="color: #ADBAC7">import subprocess</span></span>
<span class="line"></span>
<span class="line"><span style="color: #ADBAC7">def main():</span></span>
<span class="line"></span>
<span class="line"><span style="color: #ADBAC7">if __name__ == </span><span style="color: #96D0FF">&quot;__main__&quot;</span><span style="color: #ADBAC7">:</span></span>
<span class="line"><span style="color: #ADBAC7">    main()</span></span></code></pre></div>



<p>Define the Nagios status codes to make the code more readable.</p>



<pre class="wp-block-code has-adbac-7-color has-text-color has-875-rem-font-size"><code>OK = 0
WARNING = 1
CRITICAL = 2
UNKNOWN = 3</code></pre>



<p>Now, create a global variable for the command line arguments.</p>



<pre class="wp-block-code has-adbac-7-color has-text-color has-875-rem-font-size"><code>_args = None</code></pre>



<h2 class="wp-block-heading">Step 3: Parse Command-Line Arguments</h2>



<p>The plugin should accept arguments to specify the target host, thresholds, and other options. Use Python&#8217;s <code>argparse</code> module to define and handle these arguments. This code should be placed within the main function.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-Geist-Mono" style="font-size:.875rem;font-family:Code-Pro-Geist-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.5rem;--cbp-tab-width:2"><span role="button" data-code="parser = argparse.ArgumentParser(
 description='This script checks the number of hops to a target host using traceroute.'
 'You can define warning and critical thresholds for hop counts. '
 'If the host is unreachable with the default protocol (UDP) other protocols will be       tried.')
        
        # Define the arguments
        parser.add_argument('-H', '--host', required=True, type=str, help='Target host IP to check hop count')
        parser.add_argument('-t', '--timeout', default=3, type=int, help='Timeout duration in seconds for the check (default is 3)')
        parser.add_argument('-w', '--warning', required=False, type=int, help='Warning threshold for hop count')
        parser.add_argument('-c', '--critical', required=False, type=int, help='Critical threshold for hop count')
        parser.add_argument('-v', '--verbose', required=False, type=int, default=0, help=(
            'The verbosity level of the output. '
            '(0: Single line summary, '
            '1: Single line with additional information, '
            '2: Multi line with configuration debug output)'
        ))
        parser.add_argument('-p', '--protocol', type=str, default='UDP', help='First protocol to attempt for traceroute (default: UDP)')
        parser.add_argument('--debug', action='store_true', help='Enable debug mode')
        
        _args = parser.parse_args(sys.argv[1:])" style="color:#adbac7" aria-label="Copy" class="code-block-pro-copy-button"></span><pre class="shiki github-dark-dimmed" style="background-color: #22272e"><code><span class="line"><span style="color: #ADBAC7">parser </span><span style="color: #F47067">=</span><span style="color: #ADBAC7"> argparse.</span><span style="color: #DCBDFB">ArgumentParser</span><span style="color: #ADBAC7">(</span></span>
<span class="line"><span style="color: #ADBAC7"> description</span><span style="color: #F47067">=</span><span style="color: #96D0FF">&#039;This script checks the number of hops to a target host using traceroute.&#039;</span></span>
<span class="line"><span style="color: #ADBAC7"> </span><span style="color: #96D0FF">&#039;You can define warning and critical thresholds for hop counts. &#039;</span></span>
<span class="line"><span style="color: #ADBAC7"> </span><span style="color: #96D0FF">&#039;If the host is unreachable with the default protocol (UDP) other protocols will be       tried.&#039;</span><span style="color: #ADBAC7">)</span></span>
<span class="line"><span style="color: #ADBAC7">        </span></span>
<span class="line"><span style="color: #ADBAC7">        # Define the </span><span style="color: #6CB6FF">arguments</span></span>
<span class="line"><span style="color: #ADBAC7">        parser.</span><span style="color: #DCBDFB">add_argument</span><span style="color: #ADBAC7">(</span><span style="color: #96D0FF">&#039;-H&#039;</span><span style="color: #ADBAC7">, </span><span style="color: #96D0FF">&#039;--host&#039;</span><span style="color: #ADBAC7">, required</span><span style="color: #F47067">=</span><span style="color: #ADBAC7">True, type</span><span style="color: #F47067">=</span><span style="color: #ADBAC7">str, help</span><span style="color: #F47067">=</span><span style="color: #96D0FF">&#039;Target host IP to check hop count&#039;</span><span style="color: #ADBAC7">)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #ADBAC7">        parser.</span><span style="color: #DCBDFB">add_argument</span><span style="color: #ADBAC7">(</span><span style="color: #96D0FF">&#039;-t&#039;</span><span style="color: #ADBAC7">, </span><span style="color: #96D0FF">&#039;--timeout&#039;</span><span style="color: #ADBAC7">, default</span><span style="color: #F47067">=</span><span style="color: #6CB6FF">3</span><span style="color: #ADBAC7">, type</span><span style="color: #F47067">=</span><span style="color: #ADBAC7">int, help</span><span style="color: #F47067">=</span><span style="color: #96D0FF">&#039;Timeout duration in seconds for the check (default is 3)&#039;</span><span style="color: #ADBAC7">)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #ADBAC7">        parser.</span><span style="color: #DCBDFB">add_argument</span><span style="color: #ADBAC7">(</span><span style="color: #96D0FF">&#039;-w&#039;</span><span style="color: #ADBAC7">, </span><span style="color: #96D0FF">&#039;--warning&#039;</span><span style="color: #ADBAC7">, required</span><span style="color: #F47067">=</span><span style="color: #ADBAC7">False, type</span><span style="color: #F47067">=</span><span style="color: #ADBAC7">int, help</span><span style="color: #F47067">=</span><span style="color: #96D0FF">&#039;Warning threshold for hop count&#039;</span><span style="color: #ADBAC7">)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #ADBAC7">        parser.</span><span style="color: #DCBDFB">add_argument</span><span style="color: #ADBAC7">(</span><span style="color: #96D0FF">&#039;-c&#039;</span><span style="color: #ADBAC7">, </span><span style="color: #96D0FF">&#039;--critical&#039;</span><span style="color: #ADBAC7">, required</span><span style="color: #F47067">=</span><span style="color: #ADBAC7">False, type</span><span style="color: #F47067">=</span><span style="color: #ADBAC7">int, help</span><span style="color: #F47067">=</span><span style="color: #96D0FF">&#039;Critical threshold for hop count&#039;</span><span style="color: #ADBAC7">)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #ADBAC7">        parser.</span><span style="color: #DCBDFB">add_argument</span><span style="color: #ADBAC7">(</span><span style="color: #96D0FF">&#039;-v&#039;</span><span style="color: #ADBAC7">, </span><span style="color: #96D0FF">&#039;--verbose&#039;</span><span style="color: #ADBAC7">, required</span><span style="color: #F47067">=</span><span style="color: #ADBAC7">False, type</span><span style="color: #F47067">=</span><span style="color: #ADBAC7">int, default</span><span style="color: #F47067">=</span><span style="color: #6CB6FF">0</span><span style="color: #ADBAC7">, </span><span style="color: #DCBDFB">help</span><span style="color: #F47067">=</span><span style="color: #ADBAC7">(</span></span>
<span class="line"><span style="color: #ADBAC7">            </span><span style="color: #96D0FF">&#039;The verbosity level of the output. &#039;</span></span>
<span class="line"><span style="color: #ADBAC7">            </span><span style="color: #96D0FF">&#039;(0: Single line summary, &#039;</span></span>
<span class="line"><span style="color: #ADBAC7">            </span><span style="color: #96D0FF">&#039;1: Single line with additional information, &#039;</span></span>
<span class="line"><span style="color: #ADBAC7">            </span><span style="color: #96D0FF">&#039;2: Multi line with configuration debug output)&#039;</span></span>
<span class="line"><span style="color: #ADBAC7">        ))</span></span>
<span class="line"></span>
<span class="line"><span style="color: #ADBAC7">        parser.</span><span style="color: #DCBDFB">add_argument</span><span style="color: #ADBAC7">(</span><span style="color: #96D0FF">&#039;-p&#039;</span><span style="color: #ADBAC7">, </span><span style="color: #96D0FF">&#039;--protocol&#039;</span><span style="color: #ADBAC7">, type</span><span style="color: #F47067">=</span><span style="color: #ADBAC7">str, default</span><span style="color: #F47067">=</span><span style="color: #96D0FF">&#039;UDP&#039;</span><span style="color: #ADBAC7">, help</span><span style="color: #F47067">=</span><span style="color: #96D0FF">&#039;First protocol to attempt for traceroute (default: UDP)&#039;</span><span style="color: #ADBAC7">)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #ADBAC7">        parser.</span><span style="color: #DCBDFB">add_argument</span><span style="color: #ADBAC7">(</span><span style="color: #96D0FF">&#039;--debug&#039;</span><span style="color: #ADBAC7">, action</span><span style="color: #F47067">=</span><span style="color: #96D0FF">&#039;store_true&#039;</span><span style="color: #ADBAC7">, help</span><span style="color: #F47067">=</span><span style="color: #96D0FF">&#039;Enable debug mode&#039;</span><span style="color: #ADBAC7">)</span></span>
<span class="line"><span style="color: #ADBAC7">        </span></span>
<span class="line"><span style="color: #ADBAC7">        _args </span><span style="color: #F47067">=</span><span style="color: #ADBAC7"> parser.</span><span style="color: #DCBDFB">parse_args</span><span style="color: #ADBAC7">(sys.argv[</span><span style="color: #6CB6FF">1</span><span style="color: #ADBAC7">:])</span></span></code></pre></div>



<h2 class="wp-block-heading">Step 4: Implement the Traceroute Logic</h2>



<p>Create a helper function to execute traceroute and calculate the number of hops to the host. Use subprocess.run to call the  traceroute command. The stdout result of the command is then used to count the number of hops to the host. Different protocols are used by adding additional arguments to the command.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-Geist-Mono" style="font-size:.875rem;font-family:Code-Pro-Geist-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>def get_route_hops(protocol):
    global _args

    try:
        result = None
        if protocol == "UDP":
            result = subprocess.run(&#91;'traceroute', _args.host&#93;, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=_args.timeout)
        elif protocol == "ICMP":
            result = subprocess.run(&#91;'traceroute', _args.host, '-I'&#93;, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=_args.timeout)
        
        # Count the number of hops (lines returned by traceroute)
        hops = len(result.stdout.splitlines()) - 1

        return hops if hops > 0 else 0

    except subprocess.TimeoutExpired:
        return 0
    except Exception as e:
        return 0</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #9CDCFE">def</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">get_route_hops</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">protocol</span><span style="color: #D4D4D4">):</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #9CDCFE">global</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">_args</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #C586C0">try</span><span style="color: #D4D4D4">:</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #9CDCFE">result</span><span style="color: #D4D4D4"> = </span><span style="color: #9CDCFE">None</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #C586C0">if</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">protocol</span><span style="color: #D4D4D4"> == </span><span style="color: #CE9178">&quot;UDP&quot;</span><span style="color: #D4D4D4">:</span></span>
<span class="line"><span style="color: #D4D4D4">            </span><span style="color: #9CDCFE">result</span><span style="color: #D4D4D4"> = </span><span style="color: #9CDCFE">subprocess</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">run</span><span style="color: #D4D4D4">(&#91;</span><span style="color: #CE9178">&#39;traceroute&#39;</span><span style="color: #D4D4D4">, </span><span style="color: #9CDCFE">_args</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">host</span><span style="color: #D4D4D4">&#93;, </span><span style="color: #9CDCFE">stdout</span><span style="color: #D4D4D4">=</span><span style="color: #9CDCFE">subprocess</span><span style="color: #D4D4D4">.</span><span style="color: #4FC1FF">PIPE</span><span style="color: #D4D4D4">, </span><span style="color: #9CDCFE">stderr</span><span style="color: #D4D4D4">=</span><span style="color: #9CDCFE">subprocess</span><span style="color: #D4D4D4">.</span><span style="color: #4FC1FF">PIPE</span><span style="color: #D4D4D4">, </span><span style="color: #9CDCFE">text</span><span style="color: #D4D4D4">=</span><span style="color: #9CDCFE">True</span><span style="color: #D4D4D4">, </span><span style="color: #9CDCFE">timeout</span><span style="color: #D4D4D4">=</span><span style="color: #9CDCFE">_args</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">timeout</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #9CDCFE">elif</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">protocol</span><span style="color: #D4D4D4"> == </span><span style="color: #CE9178">&quot;ICMP&quot;</span><span style="color: #D4D4D4">:</span></span>
<span class="line"><span style="color: #D4D4D4">            </span><span style="color: #9CDCFE">result</span><span style="color: #D4D4D4"> = </span><span style="color: #9CDCFE">subprocess</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">run</span><span style="color: #D4D4D4">(&#91;</span><span style="color: #CE9178">&#39;traceroute&#39;</span><span style="color: #D4D4D4">, </span><span style="color: #9CDCFE">_args</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">host</span><span style="color: #D4D4D4">, </span><span style="color: #CE9178">&#39;-I&#39;</span><span style="color: #D4D4D4">&#93;, </span><span style="color: #9CDCFE">stdout</span><span style="color: #D4D4D4">=</span><span style="color: #9CDCFE">subprocess</span><span style="color: #D4D4D4">.</span><span style="color: #4FC1FF">PIPE</span><span style="color: #D4D4D4">, </span><span style="color: #9CDCFE">stderr</span><span style="color: #D4D4D4">=</span><span style="color: #9CDCFE">subprocess</span><span style="color: #D4D4D4">.</span><span style="color: #4FC1FF">PIPE</span><span style="color: #D4D4D4">, </span><span style="color: #9CDCFE">text</span><span style="color: #D4D4D4">=</span><span style="color: #9CDCFE">True</span><span style="color: #D4D4D4">, </span><span style="color: #9CDCFE">timeout</span><span style="color: #D4D4D4">=</span><span style="color: #9CDCFE">_args</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">timeout</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">        </span></span>
<span class="line"><span style="color: #D4D4D4">        # </span><span style="color: #9CDCFE">Count</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">the</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">number</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">of</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">hops</span><span style="color: #D4D4D4"> (</span><span style="color: #9CDCFE">lines</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">returned</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">by</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">traceroute</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #9CDCFE">hops</span><span style="color: #D4D4D4"> = </span><span style="color: #DCDCAA">len</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">result</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">stdout</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">splitlines</span><span style="color: #D4D4D4">()) - </span><span style="color: #B5CEA8">1</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">hops</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">if</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">hops</span><span style="color: #D4D4D4"> &gt; </span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">else</span><span style="color: #D4D4D4"> </span><span style="color: #B5CEA8">0</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #9CDCFE">except</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">subprocess</span><span style="color: #D4D4D4">.</span><span style="color: #9CDCFE">TimeoutExpired</span><span style="color: #D4D4D4">:</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #B5CEA8">0</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #9CDCFE">except</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">Exception</span><span style="color: #D4D4D4"> </span><span style="color: #C586C0">as</span><span style="color: #D4D4D4"> </span><span style="color: #4EC9B0">e</span><span style="color: #D4D4D4">:</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #B5CEA8">0</span></span></code></pre></div>



<h2 class="wp-block-heading">Step 5: Combine Logic in the Main Function</h2>



<p>The main function shall do the following:</p>



<ol class="wp-block-list">
<li>Parse arguments.</li>



<li>Attempt traceroute on the specified protocol.</li>



<li>Fallback to alternative protocols if the specified protocol fails.</li>



<li>Compare the hop counts against the thresholds.</li>



<li>Build and print a Nagios status message.</li>



<li>Exit with the proper status code.</li>
</ol>



<p>For testing, you can add debug statements that print out additional information if the &#8211;debug argument exists.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-Geist-Mono" style="font-size:.875rem;font-family:Code-Pro-Geist-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" style="color:#adbac7;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>    # Find the number of hops to the host using different protocols    
    protocols = &#91;'UDP', 'ICMP'&#93;    
    if _args.protocol in protocols:        
      protocols.remove(_args.protocol)       
      protocols.insert(0, _args.protocol)   
      
      hops = 0   
      used_protocol = _args.protocol  
      
      # Check the default protocol first   
      hops = get_route_hops(_args.protocol)  
      
      # If the default protocol fails, try the other protocols   
      if hops == 0:     
      for protocol in protocols&#91;1:&#93;:        
        hops = get_route_hops(protocol)
             
        if hops > 0:            
          used_protocol = protocol             
          break
        if hops == 0:       
          print("CRITICAL - Failed to determine hop count for all protocols")            
          sys.exit(CRITICAL)  
            
        # Set the status code based on the number of hops to the host    
        status_code = OK   
        if _args.warning and hops >= _args.warning:       
          status_code = WARNING   
        if _args.critical and hops >= _args.critical:       
          status_code = CRITICAL
          status_dict = {      
            0: "OK",       
            1: "WARNING",      
            2: "CRITICAL",   
          }
         
        # Build the status message   
        message = ''   
        if (hops == 1):      
          message = f"{status_dict&#91;status_code&#93;} - {hops} hop was counted to {_args.host}" 
        else:       
        message = f"{status_dict&#91;status_code&#93;} - {hops} hops were counted to {_args.host}"
         
        print(message)    
        sys.exit(status_code)</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki github-dark-dimmed" style="background-color: #22272e" tabindex="0"><code><span class="line"><span style="color: #ADBAC7">    # Find the number </span><span style="color: #F47067">of</span><span style="color: #ADBAC7"> hops to the host </span><span style="color: #F47067">using</span><span style="color: #ADBAC7"> </span><span style="color: #6CB6FF">different</span><span style="color: #ADBAC7"> protocols    </span></span>
<span class="line"><span style="color: #ADBAC7">    protocols </span><span style="color: #F47067">=</span><span style="color: #ADBAC7"> &#91;</span><span style="color: #96D0FF">&#39;UDP&#39;</span><span style="color: #ADBAC7">, </span><span style="color: #96D0FF">&#39;ICMP&#39;</span><span style="color: #ADBAC7">&#93;    </span></span>
<span class="line"><span style="color: #ADBAC7">    </span><span style="color: #F47067">if</span><span style="color: #ADBAC7"> _args.protocol </span><span style="color: #F47067">in</span><span style="color: #ADBAC7"> </span><span style="color: #F69D50">protocols</span><span style="color: #ADBAC7">:        </span></span>
<span class="line"><span style="color: #ADBAC7">      protocols.</span><span style="color: #DCBDFB">remove</span><span style="color: #ADBAC7">(_args.protocol)       </span></span>
<span class="line"><span style="color: #ADBAC7">      protocols.</span><span style="color: #DCBDFB">insert</span><span style="color: #ADBAC7">(</span><span style="color: #6CB6FF">0</span><span style="color: #ADBAC7">, _args.protocol)   </span></span>
<span class="line"><span style="color: #ADBAC7">      </span></span>
<span class="line"><span style="color: #ADBAC7">      hops </span><span style="color: #F47067">=</span><span style="color: #ADBAC7"> </span><span style="color: #6CB6FF">0</span><span style="color: #ADBAC7">   </span></span>
<span class="line"><span style="color: #ADBAC7">      used_protocol </span><span style="color: #F47067">=</span><span style="color: #ADBAC7"> _args.protocol  </span></span>
<span class="line"><span style="color: #ADBAC7">      </span></span>
<span class="line"><span style="color: #ADBAC7">      # Check the </span><span style="color: #F47067">default</span><span style="color: #ADBAC7"> protocol first   </span></span>
<span class="line"><span style="color: #ADBAC7">      hops </span><span style="color: #F47067">=</span><span style="color: #ADBAC7"> </span><span style="color: #DCBDFB">get_route_hops</span><span style="color: #ADBAC7">(_args.protocol)  </span></span>
<span class="line"><span style="color: #ADBAC7">      </span></span>
<span class="line"><span style="color: #ADBAC7">      # If the </span><span style="color: #F47067">default</span><span style="color: #ADBAC7"> protocol fails, </span><span style="color: #F47067">try</span><span style="color: #ADBAC7"> the other protocols   </span></span>
<span class="line"><span style="color: #ADBAC7">      </span><span style="color: #F47067">if</span><span style="color: #ADBAC7"> hops </span><span style="color: #F47067">==</span><span style="color: #ADBAC7"> </span><span style="color: #6CB6FF">0</span><span style="color: #ADBAC7">:     </span></span>
<span class="line"><span style="color: #ADBAC7">      for protocol </span><span style="color: #F47067">in</span><span style="color: #ADBAC7"> protocols&#91;</span><span style="color: #6CB6FF">1</span><span style="color: #ADBAC7">:&#93;:        </span></span>
<span class="line"><span style="color: #ADBAC7">        hops </span><span style="color: #F47067">=</span><span style="color: #ADBAC7"> </span><span style="color: #DCBDFB">get_route_hops</span><span style="color: #ADBAC7">(protocol)</span></span>
<span class="line"><span style="color: #ADBAC7">             </span></span>
<span class="line"><span style="color: #ADBAC7">        </span><span style="color: #F47067">if</span><span style="color: #ADBAC7"> hops </span><span style="color: #F47067">&gt;</span><span style="color: #ADBAC7"> </span><span style="color: #6CB6FF">0</span><span style="color: #ADBAC7">:            </span></span>
<span class="line"><span style="color: #ADBAC7">          used_protocol </span><span style="color: #F47067">=</span><span style="color: #ADBAC7"> protocol             </span></span>
<span class="line"><span style="color: #ADBAC7">          </span><span style="color: #F47067">break</span></span>
<span class="line"><span style="color: #ADBAC7">        </span><span style="color: #F47067">if</span><span style="color: #ADBAC7"> hops </span><span style="color: #F47067">==</span><span style="color: #ADBAC7"> </span><span style="color: #6CB6FF">0</span><span style="color: #ADBAC7">:       </span></span>
<span class="line"><span style="color: #ADBAC7">          </span><span style="color: #DCBDFB">print</span><span style="color: #ADBAC7">(</span><span style="color: #96D0FF">&quot;CRITICAL - Failed to determine hop count for all protocols&quot;</span><span style="color: #ADBAC7">)            </span></span>
<span class="line"><span style="color: #ADBAC7">          sys.</span><span style="color: #DCBDFB">exit</span><span style="color: #ADBAC7">(</span><span style="color: #6CB6FF">CRITICAL</span><span style="color: #ADBAC7">)  </span></span>
<span class="line"><span style="color: #ADBAC7">            </span></span>
<span class="line"><span style="color: #ADBAC7">        # Set the status code based on the number </span><span style="color: #F47067">of</span><span style="color: #ADBAC7"> hops to the host    </span></span>
<span class="line"><span style="color: #ADBAC7">        status_code </span><span style="color: #F47067">=</span><span style="color: #ADBAC7"> </span><span style="color: #6CB6FF">OK</span><span style="color: #ADBAC7">   </span></span>
<span class="line"><span style="color: #ADBAC7">        </span><span style="color: #F47067">if</span><span style="color: #ADBAC7"> _args.warning and hops </span><span style="color: #F47067">&gt;=</span><span style="color: #ADBAC7"> _args.warning:       </span></span>
<span class="line"><span style="color: #ADBAC7">          status_code </span><span style="color: #F47067">=</span><span style="color: #ADBAC7"> </span><span style="color: #6CB6FF">WARNING</span><span style="color: #ADBAC7">   </span></span>
<span class="line"><span style="color: #ADBAC7">        </span><span style="color: #F47067">if</span><span style="color: #ADBAC7"> _args.critical and hops </span><span style="color: #F47067">&gt;=</span><span style="color: #ADBAC7"> _args.critical:       </span></span>
<span class="line"><span style="color: #ADBAC7">          status_code </span><span style="color: #F47067">=</span><span style="color: #ADBAC7"> </span><span style="color: #6CB6FF">CRITICAL</span></span>
<span class="line"><span style="color: #ADBAC7">          status_dict </span><span style="color: #F47067">=</span><span style="color: #ADBAC7"> {      </span></span>
<span class="line"><span style="color: #ADBAC7">            </span><span style="color: #6CB6FF">0</span><span style="color: #ADBAC7">: </span><span style="color: #96D0FF">&quot;OK&quot;</span><span style="color: #ADBAC7">,       </span></span>
<span class="line"><span style="color: #ADBAC7">            </span><span style="color: #6CB6FF">1</span><span style="color: #ADBAC7">: </span><span style="color: #96D0FF">&quot;WARNING&quot;</span><span style="color: #ADBAC7">,      </span></span>
<span class="line"><span style="color: #ADBAC7">            </span><span style="color: #6CB6FF">2</span><span style="color: #ADBAC7">: </span><span style="color: #96D0FF">&quot;CRITICAL&quot;</span><span style="color: #ADBAC7">,   </span></span>
<span class="line"><span style="color: #ADBAC7">          }</span></span>
<span class="line"><span style="color: #ADBAC7">         </span></span>
<span class="line"><span style="color: #ADBAC7">        # Build the status message   </span></span>
<span class="line"><span style="color: #ADBAC7">        message </span><span style="color: #F47067">=</span><span style="color: #ADBAC7"> </span><span style="color: #96D0FF">&#39;&#39;</span><span style="color: #ADBAC7">   </span></span>
<span class="line"><span style="color: #ADBAC7">        </span><span style="color: #F47067">if</span><span style="color: #ADBAC7"> (hops </span><span style="color: #F47067">==</span><span style="color: #ADBAC7"> </span><span style="color: #6CB6FF">1</span><span style="color: #ADBAC7">):      </span></span>
<span class="line"><span style="color: #ADBAC7">          message </span><span style="color: #F47067">=</span><span style="color: #ADBAC7"> f</span><span style="color: #96D0FF">&quot;{status_dict&#91;status_code&#93;} - {hops} hop was counted to {_args.host}&quot;</span><span style="color: #ADBAC7"> </span></span>
<span class="line"><span style="color: #ADBAC7">        </span><span style="color: #F47067">else</span><span style="color: #ADBAC7">:       </span></span>
<span class="line"><span style="color: #ADBAC7">        message </span><span style="color: #F47067">=</span><span style="color: #ADBAC7"> f</span><span style="color: #96D0FF">&quot;{status_dict&#91;status_code&#93;} - {hops} hops were counted to {_args.host}&quot;</span></span>
<span class="line"><span style="color: #ADBAC7">         </span></span>
<span class="line"><span style="color: #ADBAC7">        </span><span style="color: #DCBDFB">print</span><span style="color: #ADBAC7">(message)    </span></span>
<span class="line"><span style="color: #ADBAC7">        sys.</span><span style="color: #DCBDFB">exit</span><span style="color: #ADBAC7">(status_code)</span></span></code></pre></div>



<h2 class="wp-block-heading">Step 6: Test the Plugin</h2>



<p>Run your script manually to verify that your plugin functions as expected. Try different combinations of arguments to ensure everything functions as expected.</p>



<p>Here is an example of what you could run in your terminal:</p>



<pre class="wp-block-code"><code>python3 check_route_hops.py -H example.com -w 10 -c 20 -p ICMP --debug</code></pre>



<h2 class="wp-block-heading">Step 7: Upload your plugin to Nagios XI</h2>



<p>Follow the steps <a href="https://assets.nagios.com/downloads/nagiosxi/docs/Managing-Plugins-in-Nagios-XI-2024.pdf" target="_blank" rel="noreferrer noopener">here</a> to add your new plugin to Nagios XI!</p>



<h2 class="wp-block-heading">Video</h2>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Create A Nagios Plugin And Wizard To Monitor Hops Between Hosts Using Traceroute - Tyler Larson" width="800" height="450" src="https://www.youtube.com/embed/EPRfBj3W_oY?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<h2 class="wp-block-heading">Conclusion</h2>



<p>You’ve successfully created a custom Nagios plugin to monitor route hops. This plugin is highly customizable, allowing you to modify thresholds and protocols as needed. It’s a solid tool for monitoring network paths and troubleshooting connectivity issues.</p>



<p></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to Use the Meraki Switch Wizard in Nagios XI</title>
		<link>https://library.nagios.com/tutorials/how-to-use-the-meraki-switch-wizard-in-nagios-xi/</link>
		
		<dc:creator><![CDATA[Ayub Huruse]]></dc:creator>
		<pubDate>Thu, 25 Sep 2025 13:57:47 +0000</pubDate>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Router and Switch Monitoring]]></category>
		<category><![CDATA[Wizards]]></category>
		<guid isPermaLink="false">https://library.nagios.com/?p=64264</guid>

					<description><![CDATA[Nagios XI includes a Meraki Switch Wizard that discovers Cisco Meraki switches via the Meraki API or SNMP, auto-detects interfaces, and lets you quickly set up per-port monitoring for status and bandwidth (Rate In/Rate Out) with bulk selection and warning/critical thresholds across one or many switches. You can also refer to this documentation for full [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>Nagios XI includes a Meraki Switch Wizard that discovers Cisco Meraki switches via the Meraki API or SNMP, auto-detects interfaces, and lets you quickly set up per-port monitoring for status and bandwidth (Rate In/Rate Out) with bulk selection and warning/critical thresholds across one or many switches.</p>



<p>You can also refer to this documentation for full details:</p>


<div class="wp-block-pdfemb-pdf-embedder-viewer"><a href="https://library.nagios.com/wp-content/uploads/2025/09/Monitoring-Meraki-Switches-with-Nagios-XI.pdf" class="pdfemb-viewer" style="" data-width="max" data-height="max" data-toolbar="bottom" data-toolbar-fixed="off">Monitoring-Meraki-Switches-with-Nagios-XI</a></div>


<div style="height:29px" aria-hidden="true" class="wp-block-spacer"></div>



<p>Here is a direct link to the PDF as well:</p>



<p><a href="https://assets.nagios.com/downloads/nagiosxi/docs/Monitoring-Meraki-Switches-with-Nagios-XI.pdf" target="_blank" rel="noopener">How To Monitor Meraki Switches With Nagios XI 2026</a></p>



<p><canvas width="821" height="1063"></canvas></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Configuring Switches And Routers To Send Netflow Data To Nagios Network Analyzer</title>
		<link>https://library.nagios.com/documentation/configuring-switches-and-routers-to-send-netflow-data-to-nagios-network-analyzer/</link>
		
		<dc:creator><![CDATA[Ethan Galstad]]></dc:creator>
		<pubDate>Fri, 06 Sep 2013 22:31:52 +0000</pubDate>
				<category><![CDATA[Documentation]]></category>
		<category><![CDATA[Router and Switch Monitoring]]></category>
		<guid isPermaLink="false">https://libtest.nagios.com/library/products/nagios-network-analyzer/documentation/configuring-switches-and-routers-to-send-netflow-data-to-nagios-network-analyzer/</guid>

					<description><![CDATA[This document describes how to setup Netflow data collection on enterprise grade switches and routers, and relay that information to Nagios Network Analyzer (NNA). It is intended for use by network administrators who are doing the initial setup of their NNA software. Network Analyzer 2026 Configuring Switches &#38; Routers to Send Flow Data to NNA [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>This document describes how to setup Netflow data collection on enterprise grade switches and routers, and relay that information to Nagios Network Analyzer (NNA). It is intended for use by network administrators who are doing the initial setup of their NNA software.</p>



<p><strong>Network Analyzer 2026</strong></p>



<p><a href="https://assets.nagios.com/downloads/nagios-network-analyzer/docs/Configuring-Switches-and-Routers-in-NNA-2026.pdf" target="_blank" rel="noopener">Configuring Switches &amp; Routers to Send Flow Data to NNA &#8211; 2026</a></p>



<p><strong>Network Analyzer 2024</strong></p>



<p><a href="https://assets.nagios.com/downloads/nagios-network-analyzer/docs/Configuring-Switches-and-Routers-in-NNA-2024.pdf" target="_blank" rel="noreferrer noopener">Configuring Switches &amp; Routers to Send Flow Data to NNA &#8211; 2024</a></p>



<p><strong>Network Analyzer v2 (Legacy) </strong></p>



<p><a href="https://assets.nagios.com/downloads/nagios-network-analyzer/docs/Configuring_Switches_And_Routers_To_Send_Netflow_Data_To_Network_Analyzer.pdf" target="_blank" rel="noreferrer noopener">Configuring Switches &amp; Routers to Send Flow Data to NNA &#8211; v2 (Legacy)</a></p>



<p></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Nagios XI &#8211; Adding Service Switch or Router</title>
		<link>https://library.nagios.com/documentation/nagios-xi-adding-service-switch-or-router/</link>
		
		<dc:creator><![CDATA[Ethan Galstad]]></dc:creator>
		<pubDate>Fri, 17 Aug 2012 02:13:33 +0000</pubDate>
				<category><![CDATA[Documentation]]></category>
		<category><![CDATA[Router and Switch Monitoring]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[Setup & Installation]]></category>
		<guid isPermaLink="false">https://libtest.nagios.com/library/products/nagios-xi/documentation/nagios-xi-adding-service-switch-or-router/</guid>

					<description><![CDATA[Often times, adding ports to a switch, or interfaces to a router must be done dynamically. This document shows how to add ports or interfaces to your monitoring switch or router using Nagios XI&#8217;s Switch/Router Monitoring wizard. This document is intended for use by Nagios XI Administrators who have dynamic environments and must adjust their [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>Often times, adding ports to a switch, or interfaces to a router must be done dynamically. This document shows how to add ports or interfaces to your monitoring switch or router using Nagios XI&#8217;s Switch/Router Monitoring wizard.</p>



<p>This document is intended for use by Nagios XI Administrators who have dynamic environments and must adjust their switch infrastructure monitoring to reflect those changes.</p>



<p><a href="https://assets.nagios.com/downloads/nagiosxi/docs/Adding_Services_To_Preexisting_Switch_or_Router.pdf" target="_blank" rel="noopener">Adding Service to Switch or Router</a></p>



<h2 class="wp-block-heading">Note: No Longer Necessary</h2>



<p>As of XI 2014 these steps are no longer necessary to add new services to an existing switch or<br>router, as MRTG now stores configurations in per-device .cfg files. The XI Switch / Router Wizard<br>handles all of the logic for you.</p>



<p>Simply re-run the wizard, using the exact same hostname as the existing host in Step 2, and add an additional services you wish. </p>



<p></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
