#!/usr/bin/perl
###############################################################################
#
# Copyright (c) 2016  Akira Inamori <akira.inamori@tf-ebina.com>
#
# pandora_ cpu
#   Retrieve filesystem cpu load
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
###############################################################################
use strict;
use warnings;
use Getopt::Long qw(:config posix_default no_ignore_case gnu_compat);

my $opt_module_name_prefix = 'CPU Usage';
my $opt_module_type = 'generic_data';
my $opt_module_group = 'none';
my $opt_module_description_prefix = 'CPU Usage';
my $opt_module_max = '100';
my $opt_module_min = '0';
my $opt_module_min_warning = '70';
my $opt_module_max_warning = '80';
my $opt_module_min_critical = '81';
my $opt_module_max_critical = '100';
my $opt_module_unit = '%';
my $cmd = "echo \$(( 100 - `vmstat 1 2 | tail -1 | awk '{ print \$15 }'`))";

sub usage {
    my $prog = $0;
    $prog =~ s[\./][];
    (my $usage = <<"    __HDT__") =~ s/^    //;
    Usage: $prog [--site_name=<SITE_NAME>]

    Options:
     -n, --name
        Name of the module. This is the module ID.

     -g, --group
        This is the name of the module group.

     -d, --description
        This guideline will be employed to add a comment to the module.

     -mi, --min
        This is the minimum valid value to generated data within this module.

     -ma, --max
        This is the maximum valid value for generated data in this module.

     -wi, --w_min
        This is the minimum value which will make the module state go to the 'warning' status.

     -wa, --w_max
        This is the maximum value which will make the module go to 'warning' status.

     -ci, --c_min
        This is the minimum value which will make the module state go to 'critical' status.

     -ca, --c_max
        This is the maximum value which will make the module state go to 'critical' status.

     -u, --unit
        This is a unit of the value retrieved by the module.

     -h, --help
        Print detailed help screen

    __HDT__
    print STDERR $usage;
    exit;
}

sub print_module {

    my $module_data = `$cmd` ;
    chop ($module_data);

    # Print module output
    print "<module>\n";
    print "<name><![CDATA[" . $opt_module_name_prefix . "]]></name>\n";
    print "<module_group><![CDATA[" . $opt_module_group . "]]></module_group>\n";
    print "<description><![CDATA[" . $opt_module_description_prefix . "]]></description>\n";
    print "<type><![CDATA[" . $opt_module_type . "]]></type>\n";
    print "<data><![CDATA[" . $module_data . "]]></data>\n";
    print "<min><![CDATA[" . $opt_module_min . "]]></min>\n";
    print "<max><![CDATA[" . $opt_module_max . "]]></max>\n";
    print "<min_warning><![CDATA[" . $opt_module_min_warning . "]]></min_warning>\n";
    print "<max_warning><![CDATA[" . $opt_module_max_warning . "]]></max_warning>\n";
    print "<min_critical><![CDATA[" . $opt_module_min_critical . "]]></min_critical>\n";
    print "<max_critical><![CDATA[" . $opt_module_max_critical . "]]></max_critical>\n";
    print "<unit><![CDATA[" . $opt_module_unit . "]]></unit>\n";
    print "</module>\n";
}

GetOptions(
    "n|name=s"        => \$opt_module_name_prefix,
    "g|group=s"       => \$opt_module_group,
    "d|description=s" => \$opt_module_description_prefix,
    "ma|max=s"        => \$opt_module_max,
    "mi|min=s"        => \$opt_module_min,
    "wi|w_min=s"      => \$opt_module_min_warning,
    "wa|w_max=s"      => \$opt_module_max_warning,
    "ci|c_min=s"      => \$opt_module_min_critical,
    "ca|c_max=s"      => \$opt_module_max_critical,
    "u|unit=s"        => \$opt_module_unit,
    "h|help=s"        => \&usage
) or usage();

print_module;

exit 0;