Projet

Général

Profil

Révision d079f0fa

IDd079f0fa95748c03b2e78a1d538c72cbc690a2dd
Parent 61241b80
Enfant 81b129dd

Ajouté par Neraud il y a plus de 7 ans

Improved the update methods : added the munin mode

Voir les différences:

plugins/git/git_commit_behind
25 25
For example :
26 26
  ln -s /path/to/git_commit_behind /etc/munin/plugins/git_commit_behind
27 27

  
28
You also need to setup a cron job to trigger the git fetches.
29

  
30
The plugin can be called with an "update" mode to handle the fetches :
31
munin-run git_commit_behind update <maxinterval> <probability>
32
It will run the fetches randomly (1 in <probability> chances),
33
and ensure that it is run at least every <maxinterval> seconds.
28
If you wish to update the repositories via cron and not during the plugin
29
execution (cf CONFIGURATION section), you need a dedicated cron job.
34 30

  
35 31
For example, you can use the following cron :
36 32

  
37
# If the git_commit_behind plugin is enabled, fetch git repositories approx.
38
# once an hour (12 invocations an hour, 1 in 12 chance that the update will
39
# happen), but ensure that there will never be more than two hours
33
# If the git_commit_behind plugin is enabled, fetch git repositories randomly
34
# according to the plugin configuration.
35
# By default : once an hour (12 invocations an hour, 1 in 12 chance that the
36
# update will happen), but ensure that there will never be more than two hours
40 37
# (7200 seconds) interval between updates.
41
*/5 * * * * root if [ -x /etc/munin/plugins/git_commit_behind ]; then /usr/sbin/munin-run git_commit_behind update 7200 12 >/dev/null; fi
38
*/5 * * * * root if [ -x /etc/munin/plugins/git_commit_behind ]; then /usr/sbin/munin-run git_commit_behind update >/dev/null; fi
42 39

  
43 40
=head1 CONFIGURATION
44 41

  
......
46 43
    [git_commit_behind]
47 44
    user root
48 45
    env.git_path /path/to/git
46
    env.update.mode [munin|cron]
47
    env.update.probability 12
48
    env.update.maxinterval 7200
49 49

  
50 50
user root : required (to be able to switch to each repo user)
51 51
env.git_path : optional (default : /usr/bin/git), the path to the git binary.
52
env.update.mode : optional (default : munin), the update mode.
53
    munin : repositories are git fetched during the pugin execution
54
    cron : a dedicated cron job needs to be used to update the repositories
55
env.update.probability : optional (default : 12),
56
    runs the update randomly (1 in <probability> chances)
57
env.update.maxinterval : optional (default : 7200),
58
    ensures that the update is run at least every <maxinterval> seconds
59

  
52 60

  
53 61
Then, for each repository you want to check, you need the following
54 62
configuration block under the git_commit_behind section
......
118 126
                        format='%(asctime)s %(levelname)-7s %(message)s')
119 127

  
120 128
conf = {
121
    'git_path':  os.getenv('git_path', '/usr/bin/git'),
122
    'state_file': os.getenv('MUNIN_STATEFILE')
129
    'git_path':            os.getenv('git_path', '/usr/bin/git'),
130
    'state_file':          os.getenv('MUNIN_STATEFILE'),
131
    'update_mode':         os.getenv('update.mode', 'munin'),
132
    'update_probability':  int(os.getenv('update.probability', '12')),
133
    'update_maxinterval':  int(os.getenv('update.maxinterval', '7200'))
123 134
}
124 135

  
125 136
repo_codes = set(re.search('repo\.([^.]+)\..*', elem).group(1)
......
198 209
                          repos_conf[repo_code]['path'])
199 210

  
200 211

  
201
def check_update_repos():
212
def check_update_repos(mode):
202 213
    if not conf['state_file']:
203 214
        logging.error('Munin state file unavailable')
204 215
        sys.exit(1)
205 216

  
206
    if len(sys.argv) > 2:
207
        max_interval = int(sys.argv[2])
208
    else:
209
        max_interval = 7200
210

  
211
    if len(sys.argv) > 3:
212
        probability = int(sys.argv[3])
213
    else:
214
        probability = 12
217
    if mode != conf['update_mode']:
218
        logging.debug('Wrong mode, skipping')
219
        return
215 220

  
216 221
    if not os.path.isfile(conf['state_file']):
217 222
        logging.debug('No state file -> updating')
218 223
        do_update_repos()
219
    elif os.path.getmtime(conf['state_file']) + max_interval < time.time():
224
    elif (os.path.getmtime(conf['state_file']) + conf['update_maxinterval']
225
            < time.time()):
220 226
        logging.debug('State file last modified too long ago -> updating')
221 227
        do_update_repos()
222
    elif randint(1, probability) == 1:
228
    elif randint(1, conf['update_probability']) == 1:
223 229
        logging.debug('Recent state, but random matched -> updating')
224 230
        do_update_repos()
225 231
    else:
......
267 273
        print('Git commit behind Munin plugin, version {0}'.format(
268 274
            plugin_version))
269 275
    elif action == 'update':
270
        check_update_repos()
271
    elif action:
276
        check_update_repos('cron')
277
    else:
272 278
        logging.warn("Unknown argument '%s'" % action)
273 279
        sys.exit(1)
274
    else:
275
        get_info()
276 280
else:
281
    if conf['update_mode'] == 'munin':
282
        check_update_repos('munin')
277 283
    get_info()

Formats disponibles : Unified diff