Projet

Général

Profil

Révision d8208bfa

IDd8208bfa86293da8f64bd224fe4c117ce03b37de
Parent ad72df15
Enfant 921de335

Ajouté par Bushmills il y a presque 14 ans

Adding a cli for munin

Voir les différences:

tools/munin-contrib-cli/munin
1
#!/bin/bash
2
# Munin CLI
3
# Found on http://munin.scarydevilmonastery.net/
4

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

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

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

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

  
34
versions="yes"
35
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
36
munin="/etc/munin/plugins"                                             # munin enabled plugins (symlinks to plugins) are here
37
trash="$HOME/.Trash"                                                   # where to move files of purged plugins to?
38
host="localhost"
39
domain="localdomain"
40
servers=""                                                             # list of machines to export plugins to
41
muninlib="/var/lib/munin/$domain"                                      # rrd files store
42
muninweb="/var/www/munin/$domain"                                      # html files store
43
muninhost=$host.$domain                                                
44
permissions="/etc/munin/plugin-conf.d/munin-node"
45
archive="/var/www/plugins/munin-plugins.tgz"                           # munin pack updates this archive, for downloading through web server
46
autopack="no"                                                          # update $archive after edit
47

  
48
plugin="$2"
49
wildcard="$3"
50

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

  
57

  
58

  
59
# -------------------------------------------------------------------------
60

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

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

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

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

  
76
munin_restart()    {                                                   # restart munin-node
77
   echo -n restarting munin node...
78
   /etc/init.d/munin-node restart &> /dev/null
79
   echo done
80
}                                                                      
81

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

  
88

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

  
98
findplugin()  {
99
   for pluginpath in ${plugins[*]} ; do
100
      if [[ -f "$pluginpath/$1" ]] ; then
101
         echo "$pluginpath"
102
         break
103
      fi
104
   done
105
}
106

  
107

  
108
munin_enable() {                                                       # creates a link to plugin in $munin
109
   if [[ -z "$plugin" ]]      ; then munin_available              ; exit 1 ; fi
110
   if [[ -f $munin/$plugin ]] ; then echo plugin already enabled  ; exit 0 ; fi
111
   usepath=$(findplugin "$plugin")
112
   if [[ -z "$usepath" ]]     ; then echo plugin not found        ; exit 1 ; fi
113
   source="$plugin"
114
   chmod +x "${usepath}/${source}"
115
   if [[ ! -z "$wildcard" ]]  ; then  plugin+="$wildcard"                  ; fi
116
   ln -s "${usepath}/${source}" "${munin}/${plugin}"
117
   echo "enabled $(stat -c%N ${munin}/${plugin})"
118
   echo "-------------------------------------------------------------------"
119
   echo "plugin output:"
120
   munin-run "${plugin}"
121
   echo "-------------------------------------------------------------------"
122
   munin_restart
123
}
124

  
125

  
126
munin_purge()  {    # move link, web files and rrd file to $trash
127
   if mkdir -p "$trash" ; then 
128
      munin_disable $plugin
129
      cleanplugin="${plugin//.-/_}"
130
      ls $muninlib/$muninhost-$cleanplugin-*.rrd
131
      mv $muninlib/$muninhost-$cleanplugin-*.rrd "$trash"
132
   else
133
      echo "can't access $trash dir"
134
      exit 1
135
   fi
136
}
137

  
138

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

  
143
munin_edit()  {    # edit plugin source of an active plugin
144
   plugintarget=$(chase "$munin/$plugin")
145
   if [[ -z "$plugintarget" ]] ; then
146
      pluginpath=$(findplugin "$plugin")
147
      if [[ -z "$pluginpath" ]] ; then
148
         echo "plugin not found"
149
         exit 1   
150
      fi
151
      plugintarget="$pluginpath/$plugin"
152
   fi
153
   $EDIT "$plugintarget"
154
   checkin "$plugintarget"
155
   [[ "$autopack" == "yes" ]] && munin_pack
156
}   
157

  
158
munin_permissions() {   # edit plugin permissions file
159
   CHANGED=$(stat -c%Y $permissions)
160
   $EDIT $permissions
161
   (( CHANGED == $(stat -c%Y $permissions) )) || munin_restart
162
}
163

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

  
170

  
171

  
172
munin_template()  {   # start editing a new plugin, using template
173
   echo $plugin
174
   if [[ -f ${plugins}/$plugin ]]; then 
175
      echo "$plugin already exists"
176
   else
177
      cp ${plugins}/template ${plugins}/$plugin
178
      munin edit $plugin
179
   fi 
180
}
181

  
182

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

  
192
# -------------------------------------------------------------------------
193

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

  
200
# -------------------------------------------------------------------------
201

  
202
munin_${1:-help}
203

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

  

Formats disponibles : Unified diff