Monitor disk space with nagios

Discussion in 'Hacking' started by aleatorvb, Dec 10, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    aleatorvb

    Member Since:
    Nov 14, 2012
    Messages:
    10
    Message Count:
    10
    Likes Received:
    0
    Trophy Points:
    1
    aleatorvb, Dec 10, 2012

    You can monitor remote disk with nagios over ssh.


    Ssh script to be copied on the freenas server, I used /mnt/pool1/users/nagios/check_diskfree.sh
    Code (text):
    1.  
    2.  
    3. case $1 in
    4.   --help | -h )
    5.          echo "Usage: check_diskfree [dev] [warn] [crit]"
    6.          echo " [warn] and [crit] as int"
    7.          echo " Example: check_diskfree hda1 70 90"
    8.          exit 3
    9.          ;;
    10.   * )
    11.     ;;
    12. esac
    13.  
    14. if [ ! "$1" -o ! "$2" -o ! "$3" ]; then
    15.         echo "Usage: check_diskfree [dev] [warn] [crit]"
    16.         echo " [warn] and [crit] as int"
    17.         echo " Example: check_diskfree hda1 70 90"
    18.         echo "Unknown: Options missing"
    19.         exit 3
    20. fi
    21.  
    22. used=`df /$1 | tail -n1 | sed -r 's/\ +/\ /g' | cut -d \  -f3`
    23. free=`df /$1 | tail -n1 | sed -r 's/\ +/\ /g' | cut -d \  -f4`
    24. full=`echo $(($used+$free))`
    25. percent=`echo $((( $free * 100 ) / $full))`
    26. warn=`echo $((( $full * $2 ) / 100 ))`
    27. crit=`echo $((( $full * $3 ) / 100 ))`
    28.  
    29. if [ "$warn" -gt "$crit" -o "$warn" -eq "$crit" ]; then
    30.    echo "Unknown: [crit] must be larger than [warn]"
    31.         exit 3
    32. fi
    33.  
    34. if [ "$used" -lt "$warn" -o "$used" -eq "$warn" ]; then
    35.         echo "OK. Free Space: `df -h /$1 | tail -n1 | sed -r 's/\ +/\ /g' | cut -d \  -f4`B, $percent%"
    36.         exit 0
    37.  elif [ "$used" -gt "$warn" -a "$used" -lt "$crit" ]; then
    38.         echo "Warning. Free Space: `df -h /$1 | tail -n1 | sed -r 's/\ +/\ /g' | cut -d \  -f4`B, $percent%"
    39.         exit 1
    40.  elif [ "$used" -gt "$crit" ]; then
    41.         echo "Critical. Free Space: `df -h /$1 | tail -n1 | sed -r 's/\ +/\ /g' | cut -d \  -f4`B, $percent%"
    42.         exit 2
    43.  else
    44.    echo "Unknown"
    45.    exit 3
    46. fi
    47.  
    Configure nagios to monitor
    - using check_diskfree.sh script
    - dataset /mnt/pool1
    - warning at 90%, critical at 95%
    Code (text):
    1.  
    2. define service{
    3.         use                             local-service
    4.         host_name                       freenas_host_name
    5.         service_description             Disk Usage
    6.         check_command                   check_by_ssh!22!/mnt/pool1/users/nagios/check_diskfree.sh!mnt/pool1!90!95$
    7.         max_check_attempts              3
    8.         check_interval                  5
    9.         retry_interval                  3
    10.         check_period                    24x7
    11. }
    12.  
    13.  
    Works like a charm.


    Attention!
    This requires having the nagios user being able to ssh to the freenas_host_ip_address with passwordless login using ssh-keys.
    Create a nagios user on freenas and allow it to remotely login via ssh.
    Also select a home folder for it.

    Create public/private key pair
    Code (text):
    1.  
    2. # login to nagios server with the nagios user
    3. cd ~
    4. mkdir .ssh
    5. chmod -R 700 .ssh
    6. cd .ssh
    7. ssh-keygen -t rsa
    8. # do not enter any password if you want passwordless login
    9.  
    ..and then copy the key to freenas (using ubuntu as example here)
    Code (text):
    1.  
    2. ssh-copy-id freenas_host_ip
    3. #enter password
    4. exit
    5. exit
    6.  
    .. now we're back on nagios server. To test passwordless login type
    Code (text):
    1.  
    2. ssh freenas_host_ip
    3.  
  2. Offline

    MaIakai

    Member Since:
    Jan 24, 2013
    Messages:
    16
    Message Count:
    16
    Likes Received:
    0
    Trophy Points:
    1
    MaIakai, Feb 2, 2013

    nice, i will be doing this
  3. Offline

    jeronsoenmans

    Member Since:
    Mar 28, 2013
    Messages:
    11
    Message Count:
    11
    Likes Received:
    0
    Trophy Points:
    1
  4. Offline

    jgreco Resident Grinch

    Member Since:
    May 29, 2011
    Messages:
    3,524
    Message Count:
    3,524
    Likes Received:
    334
    Trophy Points:
    83
    Location:
    WHO(1)ville, Unixland
    jgreco, Dec 26, 2013

    Why? You expect FreeNAS to support every other monitoring system available as part of the base system? collectd not good enough?
  5. Offline

    jgreco Resident Grinch

    Member Since:
    May 29, 2011
    Messages:
    3,524
    Message Count:
    3,524
    Likes Received:
    334
    Trophy Points:
    83
    Location:
    WHO(1)ville, Unixland
    jgreco, Dec 27, 2013

    Since you don't understand my question, then you won't be teaching me anything, you are quite correct.

    FreeNAS uses collectd to gather those stats. You can either work within that powerful, networked framework to collect the requested stats, or you can hack your Nagios to directly monitor it in any of many ways. Since there already exist bridges from collectd to Nagios, there is no real need to hack this, but it MAY be more convenient to omit the intermediate collectd or required complications of configuration.

    Of course, if Nagios was the only network monitoring package, then maybe it would be reasonable to also support direct monitoring. But since Nagios is only one of dozens (or even hundreds) of monitoring packages, special-casing for your preferred monitoring package is unlikely.

    Most people in the hacking section are able to come to this sort of understanding without much explanation. We've mostly all cobbled together some hacky cheats dozens or thousands of times over the years; criticizing FreeNAS in the tone you did for not idolizing your preferred monitoring tool may not go over too well.

    School's out and I obviously need more coffee 'coz I am clearly cranky.
  6. Offline

    jgreco Resident Grinch

    Member Since:
    May 29, 2011
    Messages:
    3,524
    Message Count:
    3,524
    Likes Received:
    334
    Trophy Points:
    83
    Location:
    WHO(1)ville, Unixland
    jgreco, Dec 27, 2013

    Oh and PS the OP did a wonderful job at this. Nicely done.
Thread Status:
Not open for further replies.

Share This Page