#!/usr/pkg/bin/perl -w

# libstatgrab
# https://libstatgrab.org
# Copyright (C) 2003-2004 Peter Saunders
# Copyright (C) 2003-2019 Tim Bishop
# Copyright (C) 2003-2013 Adam Sampson
# Copyright (C) 2012-2019 Jens Rehsack
# 
# 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; either version 2
# of the License, or (at your option) any later version.
# 
# 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.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

use strict;
use Getopt::Long;

my $progname = "statgrab-make-mrtg-config";
my $statgrab = "statgrab";
my $workdir = undef;

my $kib = 1024;
my $mib = $kib * $kib;

# Type 0 is plain integers.
my $KIBIBYTES = 1;
my $PERCENT = 2;
my $FLOAT = 3;

# Print an entry in the MRTG config file.
sub entry ($$$$$$$$$$) {
	my ($title, $vali, $valo, $max, $ylegend, $yunit, $legendi, $legendo, $gauge, $type) = @_;
	my $name = $vali;
	my $options = "";
	$options .= " noo" unless defined $valo;
	$options .= " gauge" if $gauge;
	my $sgoptions = "";
	$sgoptions .= " -o -p" if $type == $PERCENT;
	$sgoptions .= " -f 1000" if $type == $FLOAT;

	print "\n";
	print "Title[$name]: $title\n";
	print "PageTop[$name]: $title\n";
	print "MaxBytes[$name]: $max\n";
	print "YLegend[$name]: $ylegend\n";
	print "ShortLegend[$name]: $yunit\n";
	print "LegendI[$name]: $legendi\n";
	print "LegendO[$name]: $legendo\n" if defined $valo;
	if ($type == $KIBIBYTES) {
		print "kMG[$name]: Ki,Mi,Gi,Ti\n";
		$sgoptions .= " -K";
	}
	$valo = "const.0" unless defined $valo;
	print "Options[$name]:$options\n" if $options ne "";
	print "Target[$name]: `$statgrab$sgoptions -m $vali $valo`\n";
}

my $package_version = '0.92.1';
my $package_bugreport = 'https://libstatgrab.org/issues';
my $help_text = <<EOF;
Usage: $progname [OPTION]...
Generate MRTG configuration from statgrab output and write it to stdout.

--no-header                  Don't print MRTG global options; useful if you
                             want to include the output of this script in
                             another MRTG config file
--workdir PATH               Use PATH for MRTG's WorkDir option
--statgrab PATH              Specify location of statgrab binary
                             (default "statgrab")
--help                       Display this help and exit

Version $package_version - report bugs to $package_bugreport.
EOF

sub fatal ($) {
	my ($message) = @_;
	die "$progname: $message\n";
}

sub main () {
	GetOptions('statgrab=s' => \$statgrab,
	           'workdir=s' => \$workdir,
	           'no-header' => \my $no_header,
	           'help' => \my $help) or die $help_text;
	if ($help) {
		print "$help_text";
		exit 0;
	}

	unless ($no_header or defined $workdir) {
		fatal "must specify --workdir or --no-header"
	}

	my %stats = ();
	my %toplevel = ();
	my %disks = ();
	my %fss = ();
	my %nets = ();
	open STATGRAB, "$statgrab|" or fatal "can't run statgrab";
	while (<STATGRAB>) {
		chomp;
		/^([^=]*) = (.*)$/ or fatal "bad line in statgrab output";
		$stats{$1} = $2;

		my @parts = split /\./, $1;
		$toplevel{$parts[0]} = 1;
		$disks{$parts[1]} = 1 if $parts[0] eq "disk";
		$fss{$parts[1]} = 1 if $parts[0] eq "fs";
		$nets{$parts[1]} = 1 if $parts[0] eq "net";
	}
	close STATGRAB;

	unless ($no_header) {
		print "WorkDir: $workdir\n";
		print "Options[^]: growright\n";
		print "WriteExpires: Yes\n";
	}

	if (exists $toplevel{"cpu"}) {
		entry("CPU idle", "cpu.idle", undef, "100", "Idle", "%", "idle", undef, 1, $PERCENT);
		entry("CPU iowait", "cpu.iowait", undef, "100", "iowait", "%", "iowait", undef, 1, $PERCENT);
		entry("CPU kernel", "cpu.kernel", undef, "100", "Kernel", "%", "kernel", undef, 1, $PERCENT);
		entry("CPU nice", "cpu.nice", undef, "100", "Nice", "%", "nice", undef, 1, $PERCENT);
		entry("CPU swap", "cpu.swap", undef, "100", "Swap", "%", "swap", undef, 1, $PERCENT);
		entry("CPU user", "cpu.user", undef, "100", "User", "%", "user", undef, 1, $PERCENT);
	}

	foreach my $disk (sort keys %disks) {
		my $name = $stats{"disk.$disk.disk_name"};
		entry("Disk $name IO", "disk.$disk.read_bytes", "disk.$disk.write_bytes", 100*$mib, "IO rate", "KiB/s", "read", "write", 0, $KIBIBYTES);
	}

	foreach my $fs (sort keys %fss) {
		my $name = $stats{"fs.$fs.mnt_point"};
		my $size = $stats{"fs.$fs.size"};
		my $inodes = $stats{"fs.$fs.total_inodes"};
		entry("Filesystem $name space usage", "fs.$fs.used", undef, $size, "Space used", "KiB", "used", undef, 1, $KIBIBYTES);
		entry("Filesystem $name inode usage", "fs.$fs.used_inodes", undef, $inodes, "Inodes used", "inodes", "used", undef, 1, 0);
	}

	if (exists $toplevel{"load"}) {
		entry("Load average over 1 minute", "load.min1", undef, 100, "Load average", "running * 1000", "load", undef, 1, $FLOAT);
		entry("Load average over 5 minutes", "load.min5", undef, 100, "Load average", "running * 1000", "load", undef, 1, $FLOAT);
		entry("Load average over 15 minutes", "load.min15", undef, 100, "Load average", "running * 1000", "load", undef, 1, $FLOAT);
	}

	if (exists $toplevel{"mem"}) {
		my $total = $stats{"mem.total"};
		entry("Memory usage", "mem.used", "mem.cache", $total, "Memory usage", "KiB", "total", "cache", 1, $KIBIBYTES);
	}

	foreach my $net (sort keys %nets) {
		my $name = $stats{"net.$net.interface_name"};
		my $speed = int($stats{"net.$net.speed"});
		$speed = 100 if $speed == 0;

		# The speed is reported in Mbit/s; we want KiB/s.
		$speed = int(($speed * 1000000) / (8 * $kib));

		entry("Network interface $name IO", "net.$net.rx", "net.$net.tx", $speed, "Network IO", "KiB/s", "rx", "tx", 0, $KIBIBYTES);
	}

	if (exists $toplevel{"page"}) {
		# FIXME what's a sensible maximum?
		entry("Paging IO", "page.in", "page.out", 1000, "Paging IO", "pages", "in", "out", 0, 0);
	}

	if (exists $toplevel{"proc"}) {
		# FIXME mildly silly assumption
		my $maxproc = 65536;
		entry("Processes running", "proc.running", undef, $maxproc, "Running", "procs", "running", undef, 1, 0);
		entry("Processes sleeping", "proc.sleeping", undef, $maxproc, "Sleeping", "procs", "running", undef, 1, 0);
		entry("Processes stopped", "proc.stopped", undef, $maxproc, "Stopped", "procs", "running", undef, 1, 0);
		entry("Processes", "proc.total", undef, $maxproc, "Total", "procs", "running", undef, 1, 0);
		entry("Processes zombie", "proc.zombie", undef, $maxproc, "Zombie", "procs", "running", undef, 1, 0);
	}

	if (exists $toplevel{"swap"}) {
		my $swapsize = $stats{"swap.total"};
		if ($swapsize ne "0") {
			entry("Swap usage", "swap.used", undef, $swapsize, "Swap usage", "KiB", "used", undef, 1, $KIBIBYTES);
		}
	}

	if (exists $toplevel{"user"}) {
		entry("Users", "user.num", undef, 1000, "Users", "users", "users", undef, 1, 0);
	}
}

main();
