#!/usr/bin/perl -w
############################# process-service-data ##############
# Version : 0.1
# Date :  2007 02 16
# Author  : Paul Vogt (  paul dot unix at gmail.com )
# Licence : GPL - http://www.fsf.org/licenses/gpl.txt
# TODO : make nice dynamic config structure
#################################################################
#
#
# First test if an rrd file exists, if not create it.
# secondly put perf data in rrd file
# config file determines what per data is processed.
# perf data input is expectedto have the format
# $LASTSERVICECHECK$\t$HOSTNAME$\t$SERVICEDESC$\t$SERVICESTATE$\t
# $SERVICEATTEMPT$\t$SERVICESTATETYPE$\t$SERVICEEXECUTIONTIME$\t
# $SERVICELATENCY$\t$SERVICEOUTPUT$\t$SERVICEPERFDATA$\n

use strict;
use RRDs;
use RRDp;

my $program="process-service-perfdata.pl";
my $rrd_dir="/var/lib/opennms/nagios/rrd";


sub check_host_dir
{
   my $hostfile=$_[0];
   if ( ! -d "$rrd_dir/$hostfile")
   {
     mkdir "$rrd_dir/$hostfile" ;
   }
}


my $rrderror;

if ( @ARGV < 1 )
{
  die "\nUsage: $program \"perf data\"\n\n";
}

my @perfdata = split(/\|/,$ARGV[0]);

my ($time,$host,$user,$system,$idle,$nice,$wait,$kernel,$interupt);
my ($rrdload,$rrdcpu);
my @loadperf;
my ($load1m,$warn1m,$crit1m);
my ($load5m,$warn5m,$crit5m);
my ($load15m,$warn15m,$crit15m);

#
# process load and cpu data
#
if ( $perfdata[2] =~ /check_load_hpux/ && $perfdata[3] =~ /OK|WARN|CRIT/ )
{
   $time = $perfdata[0];
   $host = $perfdata[1];
   @loadperf = split / /,$perfdata[9];
   $loadperf[0] =~ /load1m=(\d+\.*\d*);(\d+\.*\d*);(\d+\.*\d*)/ ;
   ($load1m,$warn1m,$crit1m) = ($1,$2,$3);
   $loadperf[1] =~ /load5m=(\d+\.*\d*);(\d+\.*\d*);(\d+\.*\d*)/ ;
   ($load5m,$warn5m,$crit5m) = ($1,$2,$3);
   $loadperf[2] =~ /load15m=(\d+\.*\d*);(\d+\.*\d*);(\d+\.*\d*)/ ;
   ($load15m,$warn15m,$crit15m) = ($1,$2,$3);
   $loadperf[3] =~ /user=(\d+)/;
   $user = $1;
   $loadperf[4] =~ /system=(\d+)/;
   $system = $1;
   $loadperf[5] =~ /idle=(\d+)/;
   $idle = $1;
   $loadperf[6] =~ /nice=(\d+)/;
   $nice = $1;
   
   $rrdload = "$rrd_dir/$host/load.rrd";
   if ( ! -f $rrdload )
   {
      print "huh $rrdload\n";
      check_host_dir("$host");
      
        print "  Generating load rrd for host $host\n";
        RRDs::create(
            "$rrdload",
            "-s 300",
	    "DS:load1m:GAUGE:600:0:U",
	    "DS:warn1m:GAUGE:600:0:U",
	    "DS:crit1m:GAUGE:600:0:U",
	    "DS:load5m:GAUGE:600:0:U",
	    "DS:warn5m:GAUGE:600:0:U",
	    "DS:crit5m:GAUGE:600:0:U",
	    "DS:load15m:GAUGE:600:0:U",
	    "DS:warn15m:GAUGE:600:0:U",
	    "DS:crit15m:GAUGE:600:0:U",
            "RRA:AVERAGE:0.5:1:210240"      # 2years 5 min resolution
       );
       $rrderror = RRDs::error;
       die "Problem while creating rrd: $rrderror\n" if ($rrderror);
   }
   RRDs::update("$rrdload", "$time:$load1m:$warn1m:$crit1m:$load5m:$warn5m:$crit5m:$load15m:$warn15m:$crit15m");
   $rrderror = RRDs::error;
   die "Problem while updating rrd: $rrderror\n" if ($rrderror);
   
   $rrdcpu = "$rrd_dir/$host/cpu.rrd";
   if ( ! -f $rrdcpu )
   {
      check_host_dir($host);
      
        print "  Generating cpu rrd for host $host\n";
        RRDs::create(
            "$rrdcpu",
            "-s 300",
	    "DS:user:COUNTER:600:0:U",
	    "DS:system:COUNTER:600:0:U",
	    "DS:idle:COUNTER:600:0:U",
	    "DS:nice:COUNTER:600:0:U",
            "RRA:AVERAGE:0.5:1:210240"      # 2years 5 min resolution
       );
       $rrderror = RRDs::error;
       die "Problem while creating rrd: $rrderror\n" if ($rrderror);
   }
   RRDs::update("$rrdcpu", "$time:$user:$system:$idle:$nice");
   print "update $host :$user:$system:$idle:$nice\n";
   $rrderror = RRDs::error;
   die "Problem while updating rrd: $rrderror\n" if ($rrderror);
}
elsif ( $perfdata[2] =~ /check_load_netsnmp/ && $perfdata[3] =~ /OK|WARN|CRIT/ )
{
   $time = $perfdata[0];
   $host = $perfdata[1];
   @loadperf = split / /,$perfdata[9];
   $loadperf[0] =~ /load1m=(\d+\.*\d*);(\d+\.*\d*);(\d+\.*\d*)/ ;
   ($load1m,$warn1m,$crit1m) = ($1,$2,$3);
   $loadperf[1] =~ /load5m=(\d+\.*\d*);(\d+\.*\d*);(\d+\.*\d*)/ ;
   ($load5m,$warn5m,$crit5m) = ($1,$2,$3);
   $loadperf[2] =~ /load15m=(\d+\.*\d*);(\d+\.*\d*);(\d+\.*\d*)/ ;
   ($load15m,$warn15m,$crit15m) = ($1,$2,$3);
   if ( $loadperf[3] =~ /user=(\d+)/ )
   { $user = $1; } else {$user = 0 ; }
   $loadperf[4] =~ /system=(\d+)/;
   $system = $1;
   $loadperf[5] =~ /idle=(\d+)/;
   $idle = $1;
   if ( $loadperf[6] =~ /nice=(\d+)/ )
   { $nice = $1; } else { $nice = 0 ; }
   $loadperf[7] =~ /wait=(\d+)/;
   $wait = $1;
   $loadperf[9] =~ /kernel=(\d+)/;
   $kernel = $1;
   if ( $loadperf[10] =~ /interupt=(\d+)/ )
   { $interupt = $1; } else { $interupt = 0 ; }
   
   $rrdload = "$rrd_dir/$host/load.rrd";
   if ( ! -f $rrdload )
   {
      print "huh $rrdload\n";
      check_host_dir("$host");
      
        print "  Generating load rrd for host $host\n";
        RRDs::create(
            "$rrdload",
            "-s 300",
	    "DS:load1m:GAUGE:600:0:U",
	    "DS:warn1m:GAUGE:600:0:U",
	    "DS:crit1m:GAUGE:600:0:U",
	    "DS:load5m:GAUGE:600:0:U",
	    "DS:warn5m:GAUGE:600:0:U",
	    "DS:crit5m:GAUGE:600:0:U",
	    "DS:load15m:GAUGE:600:0:U",
	    "DS:warn15m:GAUGE:600:0:U",
	    "DS:crit15m:GAUGE:600:0:U",
            "RRA:AVERAGE:0.5:1:210240"      # 2years 5 min resolution
       );
       $rrderror = RRDs::error;
       die "Problem while creating rrd: $rrderror\n" if ($rrderror);
   }
   RRDs::update("$rrdload", "$time:$load1m:$warn1m:$crit1m:$load5m:$warn5m:$crit5m:$load15m:$warn15m:$crit15m");
   $rrderror = RRDs::error;
   die "Problem while updating rrd: $rrderror\n" if ($rrderror);
   
   $rrdcpu = "$rrd_dir/$host/cpu.rrd";
   if ( ! -f $rrdcpu )
   {
      check_host_dir($host);
      
        print "  Generating cpu rrd for host $host\n";
        RRDs::create(
            "$rrdcpu",
            "-s 300",
	    "DS:user:COUNTER:600:0:U",
	    "DS:system:COUNTER:600:0:U",
	    "DS:idle:COUNTER:600:0:U",
	    "DS:nice:COUNTER:600:0:U",
	    "DS:wait:COUNTER:600:0:U",
	    "DS:kernel:COUNTER:600:0:U",
	    "DS:interupt:COUNTER:600:0:U",
            "RRA:AVERAGE:0.5:1:210240"      # 2years 5 min resolution
       );
       $rrderror = RRDs::error;
       die "Problem while creating rrd: $rrderror\n" if ($rrderror);
   }
   RRDs::update("$rrdcpu", "$time:$user:$system:$idle:$nice:$wait:$kernel:$interupt");
   print "update: $host :$user:$system:$idle:$nice:$wait:$kernel:$interupt\n";
   $rrderror = RRDs::error;
   die "Problem while updating rrd: $rrderror\n" if ($rrderror);
}