~ubuntuone-pqm-team/charm-haproxy/snap-store

« back to all changes in this revision

Viewing changes to hooks/hooks.py

  • Committer: William Grant
  • Date: 2021-05-17 04:53:41 UTC
  • mfrom: (108.2.35 haproxy-charm)
  • Revision ID: william.grant@canonical.com-20210517045341-zkatebaaapqbalcx
Merge trunk, mostly for focal support

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
 
1
#!/usr/bin/env python2
2
2
 
3
3
import base64
4
4
import errno
59
59
    "deb http://archive.ubuntu.com/ubuntu %(release)s-backports "
60
60
    "main restricted universe multiverse")
61
61
haproxy_preferences_path = "/etc/apt/preferences.d/haproxy"
62
 
 
 
62
nrpe_scripts_dest = "/usr/lib/nagios/plugins"
63
63
 
64
64
dupe_options = [
65
65
    "mode tcp",
1195
1195
def install_nrpe_scripts():
1196
1196
    scripts_src = os.path.join(os.environ["CHARM_DIR"], "files",
1197
1197
                               "nrpe")
1198
 
    scripts_dst = "/usr/lib/nagios/plugins"
1199
 
    if not os.path.exists(scripts_dst):
1200
 
        os.makedirs(scripts_dst)
 
1198
    if not os.path.exists(nrpe_scripts_dest):
 
1199
        os.makedirs(nrpe_scripts_dest)
1201
1200
    for fname in glob.glob(os.path.join(scripts_src, "*.sh")):
1202
1201
        shutil.copy2(fname,
1203
 
                     os.path.join(scripts_dst, os.path.basename(fname)))
1204
 
 
1205
 
 
1206
 
def update_nrpe_config():
1207
 
    install_nrpe_scripts()
 
1202
                     os.path.join(nrpe_scripts_dest, os.path.basename(fname)))
 
1203
 
 
1204
 
 
1205
def remove_nrpe_scripts():
 
1206
    scripts_src = os.path.join(os.environ["CHARM_DIR"], "files",
 
1207
                               "nrpe")
 
1208
    for fname in glob.glob(os.path.join(scripts_src, "*.sh")):
 
1209
        try:
 
1210
            os.remove(os.path.join(nrpe_scripts_dest,
 
1211
                      os.path.basename(fname)))
 
1212
        except OSError:
 
1213
            pass
 
1214
 
 
1215
 
 
1216
def  update_nrpe_config():
 
1217
    config_data = config_get()
1208
1218
    nrpe_compat = nrpe.NRPE()
1209
 
    nrpe_compat.add_check('haproxy', 'Check HAProxy', 'check_haproxy.sh')
1210
 
    nrpe_compat.add_check('haproxy_queue', 'Check HAProxy queue depth',
1211
 
                          'check_haproxy_queue_depth.sh')
 
1219
    checks_args = [
 
1220
        ('haproxy', 'Check HAProxy', 'check_haproxy.sh'),
 
1221
        ('haproxy_queue', 'Check HAProxy queue depth', 'check_haproxy_queue_depth.sh'),
 
1222
    ]
 
1223
    if config_data['enable_monitoring'] is True:
 
1224
        install_nrpe_scripts()
 
1225
        for check_args in checks_args:
 
1226
            nrpe_compat.add_check(*check_args)
 
1227
    else:
 
1228
        for check_args in checks_args:
 
1229
            if os.path.isfile(nrpe_scripts_dest + '/' + check_args[2]):
 
1230
                nrpe_compat.remove_check(shortname=check_args[0],
 
1231
                                         description=check_args[1],
 
1232
                                         check_cmd=check_args[2])
 
1233
        remove_nrpe_scripts()
1212
1234
    nrpe_compat.write()
1213
1235
 
1214
1236