+ All Categories
Home > Technology > Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

Date post: 08-Jun-2015
Category:
Upload: jose-luis-martinez
View: 1,999 times
Download: 0 times
Share this document with a friend
Description:
Presentation for CAPSiDE Tech Talks about how to write great Nagios plugins in Perl for the Nagios-compatible monitoring solution of your choice
Popular Tags:
35
itects of the digital society www.capside.com Writing plugins for Nagios and Opsview Jose Luis Martinez CTO 2014-02-11 Barcelona
Transcript
Page 1: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Writing plugins for Nagios and Opsview

Jose Luis MartinezCTO

2014-02-11 Barcelona

Page 2: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Nagios

What is Nagios?

Opsview? Icinga? Naemon? Shinken?

Page 3: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Opsview

Configures and runs Nagios for you

Presents a frendlier and more powerfull user interface

Implements Nagios best practices

Distributed Monitoring

Datawarehouse

Page 4: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Back to Nagios

Monitoring tool that doesn’t know how to monitor anything!?!?

• The real monitoring is done by plugins• Just external programs with a defined interface to communicate

with Nagios• Written in any language

Page 5: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Plugins

Page 6: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Writing your plugins

Page 7: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

First rule

Is it already done?• www.monitoring-plugins.org

• Plugins project

• www.nagiosplugins.org• Nagios official plugins

• www.monitoringexchange.org• User contributed

• exchange.nagios.org• User contributed

• Google “xxx nagios”

Page 8: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

We’ll take a look at

Nagios::Plugin (Monitoring::Plugin)Nagios::Plugin::DieNicelyNagios::Plugin::WWW::MechanizeNagios::Plugin::SNMPNagios::Plugin::Differences

Page 9: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Nagios::Plugin

Lots of common functionality for free!• When writing a plugin you have to

Calculate state (OK, CRITICAL, …)

Get and manipulate the data

Handle Arguments

Page 10: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Nagios::Plugin

Lots of common functionality for free!• When writing a GREAT plugin you have to

Output performance data

Calculate state (OK, CRITICAL, …)In a flexible way (in f(x) of arguments)

Get and manipulate the data

Handle Arguments-h –v arguments -h has to output

documentation

Page 11: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

That’s a lot of work

I just wanted to monitor my app!

Page 12: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Nagios::Plugin

Lots of common functionality for free!• When writing a GREAT plugin you have to

Output performance data

Calculate state (OK, CRITICAL, …)In a flexible way (in f(x) of arguments)

Get and manipulate the data

Handle Arguments-h –v arguments -h has to output

documentation

Page 13: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

3 Simple Steps

SetupData collectionState calculation

Page 14: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Setup

Just make an instance of N::P

You’ve just obtained• -t option (timeout)• -v option (verbose)• --help option

#!/usr/bin/env perl

use Nagios::Plugin;my $np= Nagios::Plugin->new( 'usage' => 'Usage: %s');$np->getopts;

Page 15: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Setup (II) Constructor options

usage ("Usage: %s --foo --bar")version <- Version stringurl <- Help and Versionblurb <- Help description license <- Helpextra <- Helpplugin <- overrides auto detected name

Page 16: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Setup: GetOpt magic

$np->add_arg( spec=> 'warning|w=s', help=> "-w, --warning=RANGE", required=> 1);$np->add_arg( spec => 'user|u=s', help => 'Username', default => 'www-data');$np->getopts;if($np->opts->user) { … }

Page 17: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Collect data

Now you have to work

Page 18: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

State calculation

Page 19: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Ranges

Most plugins suppose that• OK<WARNING<CRITICAL

But… what if…

Page 20: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Ranges

Range definition Generate alert if x…

10 Is not between 0 and 10

10: Is not between 10 and infinity

~:10 Is not between –Inf and 10

10:20 Is not between 10 and 20

@10:20 Is between 10 and 20

$code= $np->check_threshold( check => $value, warning => $warning_threshold, critical => $critical_threshold); $np->nagios_exit( $code, "Thresholdcheckfailed" ) if ($code!= OK);

Page 21: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Output status

$np->nagios_exit(CRITICAL, “Too many connections”);

$np->nagios_exit(OK, “Everything went fine”);

$np->nagios_exit(WARNING, “Too few connections”);

$np->nagios_exit(UNKNOWN, “Bad options”);

Page 22: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Performance Data

$np->add_perfdata( label => "size", value => $value, uom => "kB", warning => $warning, critical => $critical);

UOM Unit of measurement Is for

No unit specified Assume a number of things

s,ms,us econds, miliseconds, nanoseconds

% Percentage

B,KB,MB,TB Bytes

c A continuous counter

Page 23: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Success!

Enjoy your shiny new plugin

Page 24: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Nagios::Plugin::DieNicely

Page 25: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Nagios::Plugin::DieNicely

Software fails, and sometimes you don’t control it

Page 26: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Nagios::Plugin::DieNicely

What happened?

Page 27: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Nagios::Plugin::DieNicely

What happened?• Output went to STDERR

• Nagios doesn’t care

• Exit code follows Perls rules• Nagios understands 0-3

Page 28: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Nagios::Plugin::DieNicely

use Nagios::Plugin::DieNicely

Page 29: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Nagios::Plugin::WWW::Mechanize

Nagios::Plugin+

WWW::Mechanize

Page 30: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Nagios::Plugin::WWW::Mechanize

Nagios::Plugin+

WWW::Mechanize

You where going to do it anyway!

Page 31: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Nagios::Plugin::WWW::Mechanize

Again... Just create an instance. Use it as a Nagios::Plugin object

Automatically tracks response time for you

$np->mech$np->content

$np->get, $np->submit_form

Page 32: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Nagios::Plugin::WWW::Mechanize

A couple of tricks• Gzipped content

• Proxy

my $np = Nagios::Plugin::WWW::Mechanize->new( 'mech' => WWW::Mechanize::GZip->new(autocheck => 0) );

my $proxy = $np->opts->proxy; if (defined $proxy){ $np->mech->proxy(['http', 'https'], $proxy); }

Page 33: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Nagios::Plugin::SNMP

Nagios::Plugin+

Net::SNMP

Page 34: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Nagios::Plugin::SNMP

Again... Just create an instance. Use it as a Nagios::Plugin object

Sets up a lot of argumentsDoes delta between values of

counters

Page 35: Writing plugins for Nagios and Opsview - CAPSiDE Tech Talks

architects of the digital society www.capside.com

Any Questions?

Thanks to:The Monitoring::Plugin module maintainers

Icanhazcheezburger for the cats


Recommended