Agenda: exercises-plugin-disk-space.htm

File exercises-plugin-disk-space.htm, 5.3 KB (added by trac, 6 years ago)
Line 
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml">
3<head>
4  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5  <meta http-equiv="Content-Style-Type" content="text/css" />
6  <meta name="generator" content="pandoc" />
7  <title>Nagios Installation and Configuration</title>
8  <link rel="stylesheet" href="../../style.css" type="text/css" />
9</head>
10<body>
11<div id="header">
12<h1 class="title">Nagios Installation and Configuration</h1>
13</div>
14<div id="TOC">
15<ul>
16<li><a href="#introduction"><span class="toc-section-number">1</span> Introduction</a><ul>
17<li><a href="#goals"><span class="toc-section-number">1.1</span> Goals</a></li>
18<li><a href="#notes"><span class="toc-section-number">1.2</span> Notes</a></li>
19</ul></li>
20<li><a href="#exercises"><span class="toc-section-number">2</span> Exercises</a><ul>
21<li><a href="#create-a-plugin-script"><span class="toc-section-number">2.1</span> 1. Create a plugin script</a></li>
22<li><a href="#configure-the-plugin"><span class="toc-section-number">2.2</span> 2. Configure the plugin</a></li>
23<li><a href="#add-a-service-check"><span class="toc-section-number">2.3</span> 3. Add a service check</a></li>
24</ul></li>
25</ul>
26</div>
27<h1 id="introduction"><a href="#TOC"><span class="header-section-number">1</span> Introduction</a></h1>
28<h2 id="goals"><a href="#TOC"><span class="header-section-number">1.1</span> Goals</a></h2>
29<ul>
30<li>Install and configure Nagios disk space plugin</li>
31</ul>
32<h2 id="notes"><a href="#TOC"><span class="header-section-number">1.2</span> Notes</a></h2>
33<ul>
34<li>Commands preceded with &quot;$&quot; imply that you should execute the command as a general user - not as root.</li>
35<li>Commands preceded with &quot;#&quot; imply that you should be working as root.</li>
36<li>Commands with more specific command lines (e.g. &quot;rtrX&gt;&quot; or &quot;mysql&gt;&quot;) imply that you are executing commands on remote equipment, or within another program.</li>
37</ul>
38<h1 id="exercises"><a href="#TOC"><span class="header-section-number">2</span> Exercises</a></h1>
39<p>It is possible to alert on disk space thresholds with the existing check_snmp plugin, but it is rather fiddly to set up (because, for example, you have to manually map the filesystem mountpoint to the OID index). So the approach given here is to install a new plugin, which handles this for you.</p>
40<h2 id="create-a-plugin-script"><a href="#TOC"><span class="header-section-number">2.1</span> 1. Create a plugin script</a></h2>
41<p>Create a file /usr/local/bin/check_hrstorage.pl with the following contents (the #! line must be the very first line in the file)</p>
42<pre><code>#!/usr/bin/perl -w
43
44unless ($ARGV[2]) {
45  print STDERR &quot;Usage: $0 &lt;host&gt; &lt;community&gt; &lt;mountpoint&gt; [&lt;warn%&gt; [&lt;crit%&gt;]]\n&quot;;
46  exit 3;
47}
48
49my $res = `snmpwalk -On -Oq -r 2 -t 1 -c &#39;$ARGV[1]&#39; -v2c &#39;$ARGV[0]&#39; .1.3.6.1.2.1.25.2.3`;
50unless ($res =~ /^\.1.3.6/) {
51  print &quot;Unable to contact via SNMP\n&quot;;
52  exit 3;
53}
54
55my $table = {};
56my $index;
57foreach my $line (split(/\n/,$res)) {
58  my ($k,$v) = split(/ /,$line,2);
59  $table-&gt;{$k} = $v;
60  if ($v eq $ARGV[2] &amp;&amp; $k =~ /^.1.3.6.1.2.1.25.2.3.1.3\.(\d+)$/) {
61    $index = $1;
62  }
63}
64
65unless ($index) {
66  print &quot;Unable to find mountpoint $ARGV[2]\n&quot;;
67  exit 3;
68}
69
70my $size = $table-&gt;{&quot;.1.3.6.1.2.1.25.2.3.1.5.$index&quot;};
71$size += 4294967296 if $size &lt; 0;
72my $used = $table-&gt;{&quot;.1.3.6.1.2.1.25.2.3.1.6.$index&quot;};
73$used += 4294967296 if $used &lt; 0;
74my $percent = $used * 100.0 / $size;
75
76my $block = $table-&gt;{&quot;.1.3.6.1.2.1.25.2.3.1.4.$index&quot;};
77if ($block =~ /^(\d+)/) {
78  $block = $1;
79  $size = $size * 1.0 * $block / 1024 / 1024;
80  $used = $used * 1.0 * $block / 1024 / 1024;
81}
82printf &quot;%.2f%% used (%d out of %d MB)\n&quot;, $percent, $used, $size;
83if ($ARGV[4] &amp;&amp; $percent &gt;= $ARGV[4]) {
84  exit 2;  # Critical
85}
86if ($ARGV[3] &amp;&amp; $percent &gt;= $ARGV[3]) {
87  exit 1;  # Warning
88}
89exit 0;
90</code></pre>
91<p>Make it executable using the following command:</p>
92<pre><code># chmod +x /usr/local/bin/check_hrstorage.pl</code></pre>
93<h2 id="configure-the-plugin"><a href="#TOC"><span class="header-section-number">2.2</span> 2. Configure the plugin</a></h2>
94<p>Create a new configuration file:</p>
95<pre><code># editor /etc/nagios-plugins/config/hrstorage.cfg</code></pre>
96<pre><code>define command {
97    command_name check_hrstorage
98    command_line /usr/local/bin/check_hrstorage.pl &#39;$HOSTADDRESS$&#39; &#39;$ARG1$&#39; &#39;$ARG2$&#39; &#39;$ARG3$&#39; &#39;$ARG4$&#39;
99}</code></pre>
100<h2 id="add-a-service-check"><a href="#TOC"><span class="header-section-number">2.3</span> 3. Add a service check</a></h2>
101<p>A service check can now be added to an individual host, or to a hostgroup. For example:</p>
102<pre><code>define service {
103        host_name                       noc
104        service_description             Disk space /
105        check_command                   check_hrstorage!NetManage!/!80!90
106        use                             generic-service
107        notification_interval           0
108}</code></pre>
109<p>The parameters to this check_hrstorage are:</p>
110<ul>
111<li>SNMP community</li>
112<li>Filesystem mount point</li>
113<li>Warning threshold (%)</li>
114<li>Critical threshold (%)</li>
115</ul>
116</body>
117</html>