#!/usr/bin/perl

# http://www.drbd.org/users-guide/ch-admin.html#s-proc-drbd

use strict;
#use Data::Dumper;

my $file="/proc/drbd";
my $store = {};
my $temp;

&crunch;
&display;

sub display{
	if ($ARGV[0] and $ARGV[0] eq "config"){
		print "graph_title DRBD\n";
		print "graph_category DRBD\n";
		print "graph_info Graph DRBD\n";
		print "graph_vlabel Graph DRBD\n";
		#print "graph_scale no\n";
		print "graph_args --base 1024 --lower-limit 0\n";
		#print "graph_args --base 1000\n";

		foreach my $key ( keys %$store ){
			my $drbdname = 'drbd'.$key;
			print $drbdname."dr.label $drbdname Disk Read\n";
			print $drbdname."dw.label $drbdname Disk Write\n";
			print $drbdname."ns.label $drbdname Network Send\n";
			print $drbdname."nr.label $drbdname Network Receive\n";
		}
	}


#print Dumper \$store;

	foreach my $key ( keys %$store ){
		my $drbdname = 'drbd'.$key;
		print $drbdname."dw.value ".$store->{$key}->{'dw'}."\n";
		print $drbdname."dr.value ".$store->{$key}->{'dr'}."\n";
		print $drbdname."ns.value ".$store->{$key}->{'ns'}."\n";
		print $drbdname."nr.value ".$store->{$key}->{'nr'}."\n";
	}
}

sub crunch{
open (IN, $file ) || die "Could not open $file for reading: $!";
	while (<IN>){
		next if /version:|GIT-hash:/;
		chomp;

		my ($drbd) = $_ =~ /^\s+(\d):/;
		$temp = $drbd if $drbd =~ /\d/;

		if (/resync/){
			my ($hits) = $_ =~ /hits:(\d*)/;
			$store->{ $temp }->{'resync'}->{hits} = $hits if $hits ne undef;

			my ($misses) = $_ =~ /misses:(\d*)/;
			$store->{ $temp }->{'resync'}->{misses} = $misses if $misses ne undef;

			my ($starving) = $_ =~ /starving:(\d*)/;
			$store->{ $temp }->{'resync'}->{starving} = $starving if $starving ne undef;

			my ($dirty) = $_ =~ /dirty:(\d*)/;
			$store->{ $temp }->{'resync'}->{dirty} = $dirty if $dirty ne undef;

			my ($changed) = $_ =~ /changed:(\d*)/;
			$store->{ $temp }->{'resync'}->{changed} = $changed if $changed ne undef;
		}

		if (/act_log/){
			my ($hits) = $_ =~ /hits:(\d*)/;
			$store->{ $temp }->{'act_log'}->{hits} = $hits if $hits ne undef;

			my ($misses) = $_ =~ /misses:(\d*)/;
			$store->{ $temp }->{'act_log'}->{misses} = $misses if $misses ne undef;

			my ($starving) = $_ =~ /starving:(\d*)/;
			$store->{ $temp }->{'act_log'}->{starving} = $starving if $starving ne undef;

			my ($dirty) = $_ =~ /dirty:(\d*)/;
			$store->{ $temp }->{'act_log'}->{dirty} = $dirty if $dirty ne undef;

			my ($changed) = $_ =~ /changed:(\d*)/;
			$store->{ $temp }->{'act_log'}->{changed} = $changed if $changed ne undef;
		}
		
		my ($ns) = $_ =~ /ns:(\d*)/;
		$store->{ $temp }->{'ns'} = $ns if $ns ne undef;

		my ($nr) = $_ =~ /nr:(\d*)/;
		$store->{ $temp }->{'nr'} = $nr if $ns ne undef;

		my ($dw) = $_ =~ /dw:(\d*)/;
		$store->{ $temp }->{'dw'} = $dw if $dw ne undef;

		my ($dr) = $_ =~ /dr:(\d*)/;
		$store->{ $temp }->{'dr'} = $dr if $dr ne undef;

		my ($al) = $_ =~ /al:(\d*)/;
		$store->{ $temp }->{'al'} = $al if $dr ne undef;

		my ($bm) = $_ =~ /bm:(\d*)/;
		$store->{ $temp }->{'bm'} = $bm if $dr ne undef;

		my ($lo) = $_ =~ /lo:(\d*)/;
		$store->{ $temp }->{'lo'} = $lo if $dr ne undef;

		my ($pe) = $_ =~ /pe:(\d*)/;
		$store->{ $temp }->{'pe'} = $pe if $dr ne undef;

		my ($ua) = $_ =~ /ua:(\d*)/;
		$store->{ $temp }->{'ua'} = $ua if $dr ne undef;

		my ($ap) = $_ =~ /ap:(\d*)/;
		$store->{ $temp }->{'ap'} = $ap if $ap ne undef;

		my ($hits) = $_ =~ /hits:(\d*)/;
		$store->{ $temp }->{'hits'} = $hits if $hits ne undef;

		my ($used) = $_ =~ /used:(\d*\\\d*)\s/;
		$store->{ $temp }->{'used'} = $used if $used ne undef;
}

close (IN);

#print Dumper \$store;

}

exit 0;
