#!/usr/bin/perl
#
# aoserv-mrtg - MRTG configured by the AOServ Platform.
# Copyright (C) 2016, 2018, 2026  AO Industries, Inc.
#     support@aoindustries.com
#     7262 Bull Pen Cir
#     Mobile, AL 36695
#
# This file is part of aoserv-mrtg.
#
# aoserv-mrtg is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# aoserv-mrtg 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with aoserv-mrtg.  If not, see <http://www.gnu.org/licenses/>.
#

my $lastbread = 0;
my $lastbwrtn = 0;
my $breadindex = -1;
my $bwrtnindex = -1;
open SAR, "sar -b |";
<SAR>;
<SAR>;
while(<SAR>) {
  chomp;
  if(!/^Average:/ && !/LINUX RESTART/) {
    if(substr($_, 8, 3) eq ' AM' || substr($_, 8, 3) eq ' PM') {
      $_ = substr($_, 11);
    } else {
      $_ = substr($_, 8);
    }
    my @fields = split ' ';
    if (@fields) {
      if($breadindex < 0 || $bwrtnindex < 0) {
        for(my $i = 0; $i < @fields; $i++) {
          if($fields[$i] eq 'bread/s') {
            $breadindex = $i;
          }
          if($fields[$i] eq 'bwrtn/s') {
            $bwrtnindex = $i;
          }
        }
      } elsif(@fields > $breadindex && @fields > $bwrtnindex) {
        $lastbread = $fields[$breadindex];
        $lastbwrtn = $fields[$bwrtnindex];
      }
    }
  }
}
close SAR;
die "Unable to find bread/s column\n" if $breadindex < 0;
die "Unable to find bwrtn/s column\n" if $bwrtnindex < 0;
printf "%.0f\n%.0f\n1\nDisk I/O\n", $lastbread, $lastbwrtn;
