#!/bin/bash
# Munin CLI
# Found on http://munin.scarydevilmonastery.net/

# ----- munin plugin management script -----
# munin [ restart | permissions | active | available | bak | help]
# munin [ <enable | disable | edit | purge> <plugin>]
# munin [ run <plugin [command]>]

# munin restart                  reloads munin config, through init.d script
# munin permissions              edit /etc/munin/plugin-conf.d/munin-node
# munin active                   list enabled plugins
# munin available                list all plugins from repositiories except the active ones
# munin run plugin               execute plugin through munin-run
# munin run plugin config        execute config section of plugin through munin-run
# munin run plugin autoconf      execute config section of plugin through munin-run
# munin enable plugin            symlink to plugin and reload munin config
# munin disable plugin           remove symlink to plugin and reload munin config
# munin purge plugin             remove symlin to plugin, its .rrd files and the associated .html files
# munin edit plugin              open plugin in editor
# munin export plugin            copy plugin to other server(s)
# munin bak                      remove *~ files from plugin repository directories
# munin template plugin          start editing a new plugin
# munin pack                     create archive, containing plugins
# munin help                     print overview of commands
# munin                          print overview of commands

# multiple plugin repositories are supported. 
# running without arguments shows a command overview

# depends on external programs:
# stat, sed, find, sort, uniq.

versions="yes"
plugins=(/usr/local/share/munin/plugins /usr/share/munin/plugins)      # where are plugins kept? First path is local, where new plugins from template are added
munin="/etc/munin/plugins"                                             # munin enabled plugins (symlinks to plugins) are here
trash="$HOME/.Trash"                                                   # where to move files of purged plugins to?
host="localhost"
domain="localdomain"
servers=""                                                             # list of machines to export plugins to
muninlib="/var/lib/munin/$domain"                                      # rrd files store
muninweb="/var/www/munin/$domain"                                      # html files store
muninhost=$host.$domain                                                
permissions="/etc/munin/plugin-conf.d/munin-node"
archive="/var/www/plugins/munin-plugins.tgz"                           # munin pack updates this archive, for downloading through web server
autopack="no"                                                          # update $archive after edit

plugin="$2"
wildcard="$3"

# editor choice:  if not specified in environment variable EDIT, use environment variables EDITOR, then  VISUAL
# if still no success, try executable "editor", and if still no editor found, hardcode to /usr/bin/vi. I know that
# this looks funny, but these are legal bashisms.
: ${EDIT:=${EDITOR:-${VISUAL:-"$(which editor)"}}}
: ${EDIT:="/usr/bin/vi"}



# -------------------------------------------------------------------------

required="chase sort grep sed uniq find stat munin-run scp"
# chase instead of readlink -f because it can handle wildcards

for require in $required; do
   type -t $require > /dev/null ||
   echo "warning: required program $require not found"
done

checkin()  {
    [[ $versions == yes ]] && mkdir -p $(dirname $1)/RCS && ci -l -mautocheckin $1
}

munin_active()     { stat -c%N $munin/*  ;}                            # show enabled plugins
munin_run()        { munin-run $plugin $wildcard ;}                    # run a plugin with munin-run

munin_restart()    {                                                   # restart munin-node
   echo -n restarting munin node...
   /etc/init.d/munin-node restart &> /dev/null
   echo done
}                                                                      

munin_available()  {                                                   # show available, inactive plugins
   (find ${plugins[*]}  -maxdepth 1 -type f ;
   chase $munin/*) |
   sort | uniq -u
}


munin_disable() {                                                      # removes plugin or link from $munin
   if [[ -z "$plugin" ]]        ; then  munin_active              ; exit 1 ; fi
   if [[ ! -z "$wildcard" ]]    ; then  plugin+="_$wildcard"               ; fi
   if [[ ! -h $munin/$plugin ]] ; then
      if [[ ! -f $munin/$plugin ]] ; then  echo "plugin not enabled" ; exit 1 ; fi
   fi
   echo removing $munin/$plugin ; rm $munin/$plugin && munin_restart
}   

findplugin()  {
   for pluginpath in ${plugins[*]} ; do
      if [[ -f "$pluginpath/$1" ]] ; then
         echo "$pluginpath"
         break
      fi
   done
}


munin_enable() {                                                       # creates a link to plugin in $munin
   if [[ -z "$plugin" ]]      ; then munin_available              ; exit 1 ; fi
   if [[ -f $munin/$plugin ]] ; then echo plugin already enabled  ; exit 0 ; fi
   usepath=$(findplugin "$plugin")
   if [[ -z "$usepath" ]]     ; then echo plugin not found        ; exit 1 ; fi
   source="$plugin"
   chmod +x "${usepath}/${source}"
   if [[ ! -z "$wildcard" ]]  ; then  plugin+="$wildcard"                  ; fi
   ln -s "${usepath}/${source}" "${munin}/${plugin}"
   echo "enabled $(stat -c%N ${munin}/${plugin})"
   echo "-------------------------------------------------------------------"
   echo "plugin output:"
   munin-run "${plugin}"
   echo "-------------------------------------------------------------------"
   munin_restart
}


munin_purge()  {    # move link, web files and rrd file to $trash
   if mkdir -p "$trash" ; then 
      munin_disable $plugin
      cleanplugin="${plugin//.-/_}"
      ls $muninlib/$muninhost-$cleanplugin-*.rrd
      mv $muninlib/$muninhost-$cleanplugin-*.rrd "$trash"
   else
      echo "can't access $trash dir"
      exit 1
   fi
}


munin_pack()  {    # pack all plugins into a tgz archive
   tar cpzf "$archive" ${plugins[@]//plugins/plugins/* }
}

munin_edit()  {    # edit plugin source of an active plugin
   plugintarget=$(chase "$munin/$plugin")
   if [[ -z "$plugintarget" ]] ; then
      pluginpath=$(findplugin "$plugin")
      if [[ -z "$pluginpath" ]] ; then
         echo "plugin not found"
         exit 1   
      fi
      plugintarget="$pluginpath/$plugin"
   fi
   $EDIT "$plugintarget"
   checkin "$plugintarget"
   [[ "$autopack" == "yes" ]] && munin_pack
}   

munin_permissions() {   # edit plugin permissions file
   CHANGED=$(stat -c%Y $permissions)
   $EDIT $permissions
   (( CHANGED == $(stat -c%Y $permissions) )) || munin_restart
}

munin_bak()  {          # remove files ending in ~ as left by joe as backup files
   for pluginpath in ${plugins[*]} ; do
      ls $pluginpath/*~ 2> /dev/null && rm $pluginpath/*~
   done
}



munin_template()  {   # start editing a new plugin, using template
   echo $plugin
   if [[ -f ${plugins}/$plugin ]]; then 
      echo "$plugin already exists"
   else
      cp ${plugins}/template ${plugins}/$plugin
      munin edit $plugin
   fi 
}


munin_migrate() {    # move system plugin to local plugins, relink if necessary
   if [[ -f ${plugins[1]}/$plugin ]]; then
      plugintarget=$(chase "$munin/$plugin")
      [[ $plugintarget ]] && munin disable $plugin
      mv ${plugins[1]}/$plugin ${plugins[0]}
      [[ $plugintarget ]] && munin enable $plugin
   fi
}

# -------------------------------------------------------------------------

munin_help()  {                           # shows this
   grep '^munin_.*().*#' $(which $0) |
   sed 's/().*#/ :   /;s/_/ /'
}
      

# -------------------------------------------------------------------------

munin_${1:-help}

# consider to generate a warning with absolute graph styles:
# grep -i "\.type *absolute"

