Display partitions with facter and the inventory database

166 views
Skip to first unread message

Stephan

unread,
Sep 4, 2012, 12:39:08 PM9/4/12
to puppet...@googlegroups.com
Hi,

if anyone is interested, here's a way to get kind of a report of hard disks that are spilling over with the help of facter and the use of the facts inventory DB.

I'm using this fact called mounts.rb (can't remember where I got it from, just remember that I had to change df #{mount} to df -P #{mount} to make it work with long mount points):

mounts = []
mntpoints=`mount -t ext2,ext3,ext4,reiserfs,xfs`
mntpoints.split(/\n/).each do |m|
    mount = m.split(/ /)[2]
    mounts << mount
end

Facter.add("mounts") do
    confine :kernel => :linux

    setcode do
        mounts.join(',')
    end
end

mounts.each do |mount|
    output = %x{df -P #{mount}}
    output.each do |str|
        dsk_size = nil
        dsk_used = nil
        dsk_avail = nil
        if str =~ /^\S+\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+/
            dsk_size = $1
            dsk_used = $2
            dsk_avail = $3
            Facter.add("mount_#{mount}_size") do
                setcode do
                    dsk_size
                end
            end
            Facter.add("mount_#{mount}_used") do
                setcode do
                    dsk_used
                end
            end
            Facter.add("mount_#{mount}_avail") do
                setcode do
                    dsk_avail
                end
            end
        end
    end
end


Then in the inventory MySQL database I created these two views, which are prereqs for the actual query:

create view mount_extended as (select node_id, substring_index(name,"_",2) as name, value, case when name regexp "mount_.*_avail" then value end as avail, case when name regexp "mount_.*_size" then value end as size, case when name regexp "mount_.*_used" then value end as used from inventory_facts where name regexp "mount_");

create view mount_pivot as (select inventory_nodes.name as hostname, trim(leading "mount_" from mount_extended.name) as name, sum(avail) as avail, sum(size) as size, sum(used) as used from mount_extended, inventory_nodes where (mount_extended.node_id = inventory_nodes.id) group by node_id, mount_extended.name);

And the actual query looks like this:


select hostname as Hostname, name as Mountname, round(avail/1000,0) as "Free in MB", round(size/1000,0) as "Total in MB", round(used/1000,0) as "Used in MB", round(100-100*avail/size,1) as "% used" from mount_pivot where round(100-100*avail/size) >= 80 order by avail;

I know this is a bit hacky, but it works for my purposes.
Reply all
Reply to author
Forward
0 new messages