Merge ~hloeung/charm-nrpe:master into ~nrpe-charmers/charm-nrpe:master

Proposed by Haw Loeung
Status: Merged
Merged at revision: 79c24209d5babecec1de668219c24c828ee747b1
Proposed branch: ~hloeung/charm-nrpe:master
Merge into: ~nrpe-charmers/charm-nrpe:master
Diff against target: 40 lines (+19/-3)
1 file modified
hooks/nrpe_helpers.py (+19/-3)
Reviewer Review Type Date Requested Status
Paul Collins lgtm Approve
Review via email: mp+334720@code.launchpad.net

Description of the change

Use the correct IP address in exported host cfgs

Work around LP: #1736050 and use socket connect to work out the
correct IP address for the exported host cfgs.

To post a comment you must log in.
Revision history for this message
Paul Collins (pjdc) wrote :

see inline

review: Needs Fixing
Revision history for this message
Paul Collins (pjdc) :
review: Approve (lgtm)
Revision history for this message
Paul Gear (paulgear) wrote :

I think it would be good to move the socket connect code into charmhelpers so that it can be used from multiple charms. This seems like something we'll likely have to do again.

Revision history for this message
Haw Loeung (hloeung) wrote :

At present, the code tries to connect to what's configured as the "nagios_master", I'm not sure what would we use in charmhelpers. Feel free to open a bug/wishlist against charmhelpers though.

Revision history for this message
Paul Gear (paulgear) wrote :

The protocol, host, and port would be parameters to the method:

def get_outbound_address(host='ntp.ubuntu.com', port=123, proto=socket.SOCK_DGRAM):
    s = socket.socket(socket.AF_INET, proto)
    s.connect(host, port)
    address = s.getsockname()[0]
    s.close()
    return address

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/hooks/nrpe_helpers.py b/hooks/nrpe_helpers.py
2index cfcb348..32ee7b6 100644
3--- a/hooks/nrpe_helpers.py
4+++ b/hooks/nrpe_helpers.py
5@@ -1,7 +1,7 @@
6 import glob
7 import socket
8-import yaml
9 import subprocess
10+import yaml
11
12 from charmhelpers.core.services import helpers
13 from charmhelpers.core import hookenv
14@@ -205,8 +205,24 @@ class NagiosInfo(dict):
15 "{},{}".format(self['external_nagios_master'],
16 hookenv.config('nagios_master'))
17 self['nagios_hostname'] = self.principal_relation.nagios_hostname()
18- ip_key = hookenv.config('nagios_address_type') + '-address'
19- self['nagios_ipaddress'] = hookenv.unit_get(ip_key)
20+
21+ address = None
22+ if hookenv.config('nagios_address_type').lower() == 'public':
23+ address = hookenv.unit_get('public-address')
24+ elif hookenv.config('nagios_master') != 'None':
25+ # Try to work out the correct interface/IP. We can't use both
26+ # network-get nor 'unit-get private-address' because both can
27+ # return the wrong IP on systems with more than one interface
28+ # (LP: #1736050).
29+ s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
30+ s.connect((hookenv.config('nagios_master'), 80))
31+ address = s.getsockname()[0]
32+ s.close()
33+ # Fallback to unit-get private-address
34+ if not address:
35+ address = hookenv.unit_get('private-address')
36+
37+ self['nagios_ipaddress'] = address
38
39 self['dont_blame_nrpe'] = '1' if hookenv.config('dont_blame_nrpe') else '0'
40 self['debug'] = '1' if hookenv.config('debug') else '0'

Subscribers

People subscribed via source and target branches