#!/usr/bin/perl
# 
# ***** BEGIN LICENSE BLOCK *****
# Zimbra Collaboration Suite Server
# Copyright (C) 2005, 2006, 2007, 2009, 2010, 2013, 2014, 2016 Synacor, Inc.
#
# 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.
# You should have received a copy of the GNU General Public License along with this program.
# If not, see <https://www.gnu.org/licenses/>.
# ***** END LICENSE BLOCK *****

#
# Generate CSV files from iostat output
#

use strict;
use Date::Manip;
use Getopt::Long;
use Sys::Hostname;
$| = 1;
my $hostname = hostname;
my ($opt_interval, $opt_cpufile, $opt_help);

GetOptions("interval=i" => \$opt_interval,
           "cpu=s" => \$opt_cpufile,
           "help" => \$opt_help) || usage($!);

if (defined($opt_help)) {
    usage();
}

if (!defined($opt_interval)) {
    $opt_interval = 60;
}

sub usage {
    my $error = shift;
    print("Error: ", $error, "\n\n") if (defined($error));
    print <<EOF;
Usage: $0
    -i --interval=secs     Seconds between reports (default 60)
    -c --cpu=file          Also record cpu statistics to specified file
EOF
    exit(1) if (defined($error));
    exit(0);
}

iostat();

sub iostat() {
    open(IOSTAT, "iostat -t -x $opt_interval |");
    if (defined($opt_cpufile)) {
        open(CPU, "> $opt_cpufile") || die "can't open: $opt_cpufile: $!";
    }
    CPU->autoflush(1);
    
    my $time;
    my $inDevices = 0;
    my $expectCPU = 0;
    my $printDeviceHeader = 1;
    my $printCPUHeader = 1;
    
    while (<IOSTAT>) {
        if (/^Time:/) {
            chomp;
            s/^Time:\s*//g;
            $time = UnixDate(ParseDate($_), "%m/%d/%Y %H:%M:%S");
            next;
        }
        
        if (defined($opt_cpufile) && /^avg-cpu:/) {
            $expectCPU = 1;
            if ($printCPUHeader) {
                $printCPUHeader = 0;
                s/^avg-cpu:\s*//g;
                print CPU join(",", "time", "host", split(/\s+/, $_)), "\n";
            }
            next;
        }

        if (defined($opt_cpufile) && $expectCPU) {
            $expectCPU = 0;
            s/^\s*//g;
            print CPU join(",", $time, $hostname, split(/\s+/, $_)), "\n";
            next;
        }

        if (/^Device:/) {
            $inDevices = 1;
            if ($printDeviceHeader) {
                $printDeviceHeader = 0; 
                s/^Device:/device/g;
                print join(",", "time", "host", split(/\s+/, $_)), "\n";
            }
            next;
        }

        if (/^\s*$/ && $inDevices) {
            $inDevices = 0;
            next;
        }
        
        if ($inDevices) {
            print join(",", $time, $hostname, split(/\s+/, $_)), "\n";
            next;
        }     
    }
    if (defined($opt_cpufile)) {
        close(CPU);
    }
    close(IOSTAT);
}
