~jsing/charms/trusty/nrpe/principle-unit-fix

« back to all changes in this revision

Viewing changes to hooks/nrpe_helpers.py

  • Committer: Joel Sing
  • Date: 2016-04-06 06:28:55 UTC
  • Revision ID: joel.sing@canonical.com-20160406062855-4l8dlognti7eg88x
[jsing] use principle unit for nagios hostname
      
Currently the first relation is considered to be the principle unit
and this is not always the case.

Show diffs side-by-side

added added

removed removed

Lines of Context:
145
145
            return False
146
146
        return '__unit__' in self[self.name][0]
147
147
 
 
148
    def principle_unit(self):
 
149
        for rel in self[self.name]:
 
150
            if 'primary' in rel and rel['primary'] == 'True':
 
151
                return rel
 
152
 
 
153
        # This is not necessarily the primary relation...
 
154
        return rel[0]
 
155
 
148
156
    def nagios_hostname(self):
149
157
        """ Return the string that nagios will use to identify this host """
150
158
        host_context = hookenv.config('nagios_host_context')
152
160
        if hostname_type == 'host' or not self.is_ready():
153
161
            return socket.gethostname()
154
162
        else:
155
 
            principle_unitname = self[self.name][0]['__unit__']
 
163
            principle_unit = self.principle_unit()
 
164
            principle_unitname = principle_unit['__unit__']
156
165
            nagios_hostname = "{}-{}".format(host_context, principle_unitname)
157
166
            nagios_hostname = nagios_hostname.replace('/', '-')
158
167
            return nagios_hostname