Merge lp:~benji/charms/precise/juju-gui/bug-1284088 into lp:~juju-gui/charms/precise/juju-gui/trunk

Proposed by Benji York
Status: Merged
Merged at revision: 175
Proposed branch: lp:~benji/charms/precise/juju-gui/bug-1284088
Merge into: lp:~juju-gui/charms/precise/juju-gui/trunk
Diff against target: 117 lines (+48/-9)
2 files modified
scripts/charmsupport/nrpe.py (+27/-2)
scripts/update-nrpe.py (+21/-7)
To merge this branch: bzr merge lp:~benji/charms/precise/juju-gui/bug-1284088
Reviewer Review Type Date Requested Status
charmers Pending
Review via email: mp+211781@code.launchpad.net

Commit message

Handle broken nrpe relations.

Description of the change

Handle broken nrpe relations.

https://codereview.appspot.com/76860047/

To post a comment you must log in.
Revision history for this message
Benji York (benji) wrote :
Download full text (4.3 KiB)

Reviewers: mp+211781_code.launchpad.net,

Message:
Please take a look.

Description:
Handle broken nrpe relations.

https://code.launchpad.net/~benji/charms/precise/juju-gui/bug-1284088/+merge/211781

(do not edit description out of merge proposal)

Please review this at https://codereview.appspot.com/76860047/

Affected files (+51, -7 lines):
   A [revision details]
   M scripts/charmsupport/nrpe.py
   M scripts/update-nrpe.py

Index: [revision details]
=== added file '[revision details]'
--- [revision details] 2012-01-01 00:00:00 +0000
+++ [revision details] 2012-01-01 00:00:00 +0000
@@ -0,0 +1,2 @@
+Old revision: <email address hidden>
+New revision: <email address hidden>

Index: scripts/update-nrpe.py
=== modified file 'scripts/update-nrpe.py'
--- scripts/update-nrpe.py 2014-02-18 12:18:02 +0000
+++ scripts/update-nrpe.py 2014-03-19 16:39:34 +0000
@@ -1,14 +1,29 @@
  #!/usr/bin/env python
-from charmsupport import nrpe
+import sys
+import charmsupport.nrpe
+
+
+def get_nrpe():
+ nrpe = charmsupport.nrpe.NRPE()
+ nrpe.add_check('app-is-accessible', 'Check_the_app_can_be_downloaded',
+ 'check-app-access.sh')
+ return nrpe

  def update_nrpe_config():
- nrpe_compat = nrpe.NRPE()
- nrpe_compat.add_check(
- 'app-is-accessible', 'Check_the_app_can_be_downloaded',
- 'check-app-access.sh')
- nrpe_compat.write()
+ nrpe = get_nrpe()
+ nrpe.write()
+
+
+def remove_nrpe_check():
+ nrpe = get_nrpe()
+ nrpe.remove_checks()

  if __name__ == '__main__':
- update_nrpe_config()
+ hook_name = sys.argv[0]
+ remove_nrpe_check()
+ if 'departed' in hook_name or 'broken' in hook_name:
+ remove_nrpe_check()
+ else:
+ update_nrpe_config()

Index: scripts/charmsupport/nrpe.py
=== modified file 'scripts/charmsupport/nrpe.py'
--- scripts/charmsupport/nrpe.py 2013-07-29 19:02:49 +0000
+++ scripts/charmsupport/nrpe.py 2014-03-19 16:39:34 +0000
@@ -5,12 +5,17 @@
  # Authors:
  # Matthew Wedgwood <email address hidden>

+# XXX This file has been modified to add the ability to remove NRPE checks.
+# The original project that created this code has been abandoned so there
is
+# no upstream to which the modifications can be sent.
+
  import subprocess
  import pwd
  import grp
  import os
  import re
  import shlex
+import errno

  from hookenv import config, local_unit

@@ -105,6 +110,11 @@
          subprocess.call(['juju-log', 'Check command not found:
{}'.format(command[0])])
          return ''

+ def service_file_name(self, nagios_context, hostname):
+ return '{}/service__{}_check_{}.cfg'.format(
+ NRPE.nagios_exportdir, hostname, self.shortname)
+
+
      def write(self, nagios_context, hostname):
          for f in os.listdir(NRPE.nagios_exportdir):
              if re.search('.*check_{}.cfg'.format(self.shortname), f):
@@ -119,6 +129,7 @@
          nrpe_service_text = Check.service_template.format(**templ_vars)
          nrpe_service_file = '{}/service__{}_check_{}.cfg'.format(
              NRPE.nagios_exportdir, hostname, self.shortname)
+ nrpe_service_file = self.servic...

Read more...

Revision history for this message
Francesco Banconi (frankban) wrote :

Thanks for this branch Benji: LGTM with minors.

https://codereview.appspot.com/76860047/diff/1/scripts/charmsupport/nrpe.py
File scripts/charmsupport/nrpe.py (right):

https://codereview.appspot.com/76860047/diff/1/scripts/charmsupport/nrpe.py#newcode113
scripts/charmsupport/nrpe.py:113: def service_file_name(self,
nagios_context, hostname):
nagios_context seems unused in this method.

https://codereview.appspot.com/76860047/diff/1/scripts/charmsupport/nrpe.py#newcode130
scripts/charmsupport/nrpe.py:130: nrpe_service_file =
'{}/service__{}_check_{}.cfg'.format(
The nrpe_service_file is retrieved using the external method in the next
line, so I think we can safely remove this one.

https://codereview.appspot.com/76860047/diff/1/scripts/update-nrpe.py
File scripts/update-nrpe.py (right):

https://codereview.appspot.com/76860047/diff/1/scripts/update-nrpe.py#newcode25
scripts/update-nrpe.py:25: remove_nrpe_check()
So we always remove checks and then add them (or remove them again)
based on the hook name. Is the second removal needed?

https://codereview.appspot.com/76860047/

Revision history for this message
Benji York (benji) wrote :

Thanks for the good review. Read all the comments before replying
because there is a twist ending.

Sincerely,
M. Night Shyamalan

https://codereview.appspot.com/76860047/diff/1/scripts/charmsupport/nrpe.py
File scripts/charmsupport/nrpe.py (right):

https://codereview.appspot.com/76860047/diff/1/scripts/charmsupport/nrpe.py#newcode113
scripts/charmsupport/nrpe.py:113: def service_file_name(self,
nagios_context, hostname):
On 2014/03/19 17:42:04, frankban wrote:
> nagios_context seems unused in this method.

You are exactly right. Given the complete lack of tests and the fact
that this is a dead library plus the extensive testing I've done of the
code as-is, I'm going to leave it be (unless you object strenuously).

https://codereview.appspot.com/76860047/diff/1/scripts/charmsupport/nrpe.py#newcode130
scripts/charmsupport/nrpe.py:130: nrpe_service_file =
'{}/service__{}_check_{}.cfg'.format(
On 2014/03/19 17:42:04, frankban wrote:
> The nrpe_service_file is retrieved using the external method in the
next line,
> so I think we can safely remove this one.

Good catch. Again, I am loathe to touch the known-working code, even to
do something this simple.

https://codereview.appspot.com/76860047/diff/1/scripts/update-nrpe.py
File scripts/update-nrpe.py (right):

https://codereview.appspot.com/76860047/diff/1/scripts/update-nrpe.py#newcode25
scripts/update-nrpe.py:25: remove_nrpe_check()
On 2014/03/19 17:42:04, frankban wrote:
> So we always remove checks and then add them (or remove them again)
based on the
> hook name. Is the second removal needed?

Darn, this is bad code. This really is a stopper, so I'm going to
retract what I said before about changing the code and implement the two
suggestions you made as well as remove this line and then re-test.

https://codereview.appspot.com/76860047/

175. By Benji York

fix bugs found in review

Revision history for this message
Benji York (benji) wrote :

*** Submitted:

Handle broken nrpe relations.

R=frankban
CC=
https://codereview.appspot.com/76860047

https://codereview.appspot.com/76860047/

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added symlink 'hooks/nrpe-external-master-relation-broken'
2=== target is u'../scripts/update-nrpe.py'
3=== added symlink 'hooks/nrpe-external-master-relation-departed'
4=== target is u'../scripts/update-nrpe.py'
5=== modified file 'scripts/charmsupport/nrpe.py'
6--- scripts/charmsupport/nrpe.py 2013-07-29 19:02:49 +0000
7+++ scripts/charmsupport/nrpe.py 2014-03-19 19:36:58 +0000
8@@ -5,12 +5,17 @@
9 # Authors:
10 # Matthew Wedgwood <matthew.wedgwood@canonical.com>
11
12+# XXX This file has been modified to add the ability to remove NRPE checks.
13+# The original project that created this code has been abandoned so there is
14+# no upstream to which the modifications can be sent.
15+
16 import subprocess
17 import pwd
18 import grp
19 import os
20 import re
21 import shlex
22+import errno
23
24 from hookenv import config, local_unit
25
26@@ -105,6 +110,11 @@
27 subprocess.call(['juju-log', 'Check command not found: {}'.format(command[0])])
28 return ''
29
30+ def service_file_name(self, hostname):
31+ return '{}/service__{}_check_{}.cfg'.format(
32+ NRPE.nagios_exportdir, hostname, self.shortname)
33+
34+
35 def write(self, nagios_context, hostname):
36 for f in os.listdir(NRPE.nagios_exportdir):
37 if re.search('.*check_{}.cfg'.format(self.shortname), f):
38@@ -117,8 +127,7 @@
39 'shortname': self.shortname,
40 }
41 nrpe_service_text = Check.service_template.format(**templ_vars)
42- nrpe_service_file = '{}/service__{}_check_{}.cfg'.format(
43- NRPE.nagios_exportdir, hostname, self.shortname)
44+ nrpe_service_file = self.service_file_name(hostname)
45 with open(nrpe_service_file, 'w') as nrpe_service_config:
46 nrpe_service_config.write(str(nrpe_service_text))
47
48@@ -128,9 +137,21 @@
49 nrpe_check_config.write("command[check_{}]={}\n".format(
50 self.shortname, self.check_cmd))
51
52+ def remove(self, hostname):
53+ """Remove the configuration file for this check."""
54+ try:
55+ os.unlink(self.service_file_name(hostname))
56+ except OSError, e:
57+ if e.errno == errno.ENOENT:
58+ # Ignore the fact that the file didn't exist.
59+ pass
60+ else:
61+ raise
62+
63 def run(self):
64 subprocess.call(self.check_cmd)
65
66+
67 class NRPE(object):
68 nagios_logdir = '/var/log/nagios'
69 nagios_exportdir = '/var/lib/nagios/export'
70@@ -167,3 +188,7 @@
71
72 if os.path.isfile('/etc/init.d/nagios-nrpe-server'):
73 subprocess.call(['service', 'nagios-nrpe-server', 'reload'])
74+
75+ def remove_checks(self):
76+ for check in self.checks:
77+ check.remove(self.hostname)
78
79=== modified file 'scripts/update-nrpe.py'
80--- scripts/update-nrpe.py 2014-02-18 12:18:02 +0000
81+++ scripts/update-nrpe.py 2014-03-19 19:36:58 +0000
82@@ -1,14 +1,28 @@
83 #!/usr/bin/env python
84-from charmsupport import nrpe
85+import sys
86+import charmsupport.nrpe
87+
88+
89+def get_nrpe():
90+ nrpe = charmsupport.nrpe.NRPE()
91+ nrpe.add_check('app-is-accessible', 'Check_the_app_can_be_downloaded',
92+ 'check-app-access.sh')
93+ return nrpe
94
95
96 def update_nrpe_config():
97- nrpe_compat = nrpe.NRPE()
98- nrpe_compat.add_check(
99- 'app-is-accessible', 'Check_the_app_can_be_downloaded',
100- 'check-app-access.sh')
101- nrpe_compat.write()
102+ nrpe = get_nrpe()
103+ nrpe.write()
104+
105+
106+def remove_nrpe_check():
107+ nrpe = get_nrpe()
108+ nrpe.remove_checks()
109
110
111 if __name__ == '__main__':
112- update_nrpe_config()
113+ hook_name = sys.argv[0]
114+ if 'departed' in hook_name or 'broken' in hook_name:
115+ remove_nrpe_check()
116+ else:
117+ update_nrpe_config()

Subscribers

People subscribed via source and target branches

to all changes: