1#!/usr/local/bin/perl -w
2
3=head1 NAME
4
5btrfs_subvol_usage - Plugin to monitor usage of BTRFS subvolumes
6
7=head1 CONFIGURATION
8
9Must be run as root and you have to specify the path to the filesystem
10
11 [btrfs_usage]
12 user root
13 env.fsroot /path/to/btrfs/filesystem
14
15=head1 MAGIC MARKERS
16
17 #%# family=auto
18 #%# capabilities=autoconf
19
20=head1 USAGE
21
22Link/Copy this plugin to /etc/munin/plugins/ and restart the munin-node.
23
24=head1 AUTHOR
25
26Alexander Knöbel <alex@belogy.de>
27
28=head1 LICENSE
29
30GPLv2
31
32=cut
33
34use strict;
35use Munin::Plugin;
36
37if ($ARGV[0] and $ARGV[0] eq 'autoconf') {
38	if (-e "/sbin/btrfs") {
39		print "yes\n";
40	} else {
41		print "no (/sbin/btrfs is missing)\n";
42	}
43	exit 0;
44}
45
46my @fsroot;
47if ( !defined( $ENV{"fsroot"} ) ) {
48	die "No fsroot given! See the manual at top of this plugin file.";
49}
50@fsroot = $ENV{"fsroot"};
51
52# get subvolumes
53my %subvols;
54open(SVS, "btrfs subvolume list --sort=path @fsroot |")
55	or die("Failed to run 'btrfs subvolume list': " . $!);
56while (my $line = <SVS>) {
57	chomp $line;
58	$line =~ s/ID ([0-9]+) gen ([0-9]+) top level ([0-9]+) path (.+)//;
59	$subvols{$1}->{id} = $1;
60	$subvols{$1}->{name} = $4;
61}
62close SVS;
63
64# get sizes from quota
65open(QGS, "btrfs qgroup show --raw @fsroot |")
66	or die("Failed to run 'btrfs qgroup show': " . $!);
67while (my $line = <QGS>) {
68	chomp $line;
69	$line =~ s|([0-9]+)/([0-9]+)\s+([0-9]+)\s+([0-9]+)||;
70	if (defined($2) && defined($subvols{$2})) {
71		$subvols{$2}->{rfer} = $3;
72		$subvols{$2}->{excl} = $4;
73	}
74}
75close QGS;
76
77# print config
78if ($ARGV[0] and $ARGV[0] eq 'config') {
79	print "btrfs_usage\n";
80	print "graph_title BTRFS Subvolume usage\n";
81	print "graph_args --base 1024 --lower-limit 0\n";
82	print "graph_vlabel Bytes\n";
83	print "graph_category disk\n";
84
85	for my $id (sort { $subvols{$a}->{name} cmp $subvols{$b}->{name} } keys %subvols) {
86		print "$id.label " . $subvols{$id}->{name} . "\n";
87		print "$id.type GAUGE\n";
88		print "$id.draw LINE2\n";
89	}
90
91	exit 0;
92}
93
94# print usage
95print "btrfs_subvol_usage\n";
96for my $id (sort { $subvols{$a}->{name} cmp $subvols{$b}->{name} } keys %subvols) {
97	print "$id.value " . $subvols{$id}->{rfer} . "\n";
98}
99