#!/usr/bin/perl -w
# -*- cperl -*-

=head1 NAME

snmp__screenos_ - SNMP plugin to monitor Juniper ScreenOS based routers

=head1 APPLICABLE SYSTEMS

This has been developed against a Juniper SSG5-Serial router, but
should work with any ScreenOS-based router as manufactured by Juniper.

Using a command such as "munin-node-configure --snmp
switch.langfeldt.net --snmpversion 2c --snmpcommunity public | sh -x"
should identify whether this plugin can apply.


=head1 CONFIGURATION

As a rule SNMP plugins need site specific configuration.  The default
configuration (shown here) will only work on insecure sites/devices:

   [snmp_*]
	env.version 2
        env.community public

In general SNMP is not very secure at all unless you use SNMP version
3 which supports authentication and privacy (encryption).  But in any
case the community string for your devices should not be "public".

Please see 'perldoc Munin::Plugin::SNMP' for further configuration
information.

=head1 MIB INFORMATION

This plugin requires NETSCREEN-RESOURCE-MIB information.

=head1 MAGIC MARKERS

  #%# family=snmpauto
  #%# capabilities=snmpconf

=head1 AUTHOR

Copyright (C) 2012 Diego Elio Pettenò.

=head1 LICENSE

GPLv2

=cut

use strict;
use Munin::Plugin;
use Munin::Plugin::SNMP;

# This corresponds to NETSCREEN-SMI::netscreenResource
my $oidBase = '1.3.6.1.4.1.3224.16';

my $oidCpuAvg       = "$oidBase.1.1";
my $oidCpuLast1Min  = "$oidBase.1.2";
my $oidCpuLast5Min  = "$oidBase.1.3";
my $oidCpuLast15Min = "$oidBase.1.4";
my $oidMemAllocate  = "$oidBase.2.1";
my $oidMemLeft      = "$oidBase.2.2";
my $oidSessAllocate = "$oidBase.3.2";
my $oidSessMaximum  = "$oidBase.3.3";
my $oidSessFailed   = "$oidBase.3.4";

if (defined $ARGV[0] and $ARGV[0] eq "snmpconf") {
    print "require $oidBase.[23].[13].0";
    exit 0;
}

my $session = Munin::Plugin::SNMP->session();
my $data = $session->get_entries(-columns => [$oidCpuAvg,
					      $oidCpuLast1Min,
					      $oidCpuLast5Min,
					      $oidCpuLast15Min,
					      $oidMemAllocate,
					      $oidMemLeft,
					      $oidSessAllocate,
					      $oidSessMaximum,
					      $oidSessFailed ]);

if ($ARGV[0] and $ARGV[0] eq "config") {
  my ($host) = Munin::Plugin::SNMP->config_session();

  my $memTotal = $data->{"$oidMemAllocate.0"} + $data->{"$oidMemLeft.0"};

  # this is the closet I can get to the yellow/red on the ScreenOS
  # interface.
  my $memWarning = int($memTotal * 0.75 + 0.5);
  my $memCritical = int($memTotal * 0.85 + 0.5);
  my $sessWarning = int($data->{"$oidSessMaximum.0"} * 0.75 + 0.5);
  my $sessCritical = int($data->{"$oidSessMaximum.0"} * 0.85 + 0.5);

  print <<END;
host_name $host

multigraph screenos_memory
graph_title Memory allocation
graph_vlabel bytes
graph_args --base 1024
graph_category system

memory.label Memory
memory.min 0
memory.max $memTotal
memory.warning $memWarning
memory.critical $memCritical

multigraph screenos_sessions
graph_title Sessions allocation
graph_vlabel Count
graph_category system

sessions.label Active Sessions
sessions.min 0
sessions.max $data->{"$oidSessMaximum.0"}
sessions.warning $sessWarning
sessions.critical $sessCritical
failed.label Failed Sessions
failed.min 0
failed.critical 1

multigraph screenos_cpu
graph_title CPU Utilization
graph_vlabel Percentage
graph_category system

average.label Average
average.min 0
average.max 100
average.warning 75
average.critical 85
last1.label Last minute
last1.min 0
last1.max 100
last1.warning 75
last1.critical 85
last5.label Last 5 minutes
last5.min 0
last5.max 100
last5.warning 75
last5.critical 85
last15.label Last 15 minutes
last15.min 0
last15.max 100
last15.warning 75
last15.critical 85
END

  exit 0;
}

print <<END;
multigraph screenos_memory
memory.value $data->{"$oidMemAllocate.0"}

multigraph screenos_sessions
sessions.value $data->{"$oidSessAllocate.0"}
failed.value $data->{"$oidSessFailed.0"}

multigraph screenos_cpu
average.value $data->{"$oidCpuAvg.0"}
last1.value $data->{"$oidCpuLast1Min.0"}
last5.value $data->{"$oidCpuLast5Min.0"}
last15.value $data->{"$oidCpuLast15Min.0"}
END
