← Back to team overview

nagios-charmers team mailing list archive

[Merge] ~xavpaice/nagios-charm:parents into nagios-charm:master

 

Xav Paice has proposed merging ~xavpaice/nagios-charm:parents into nagios-charm:master.

Requested reviews:
  Nagios Charm developers (nagios-charmers)

For more details, see:
https://code.launchpad.net/~xavpaice/nagios-charm/+git/nagios-charm/+merge/332127

Depends on https://code.launchpad.net/~xavpaice/nrpe-charm/+git/nrpe-charm/+merge/332126 to be of much use
-- 
Your team Nagios Charm developers is requested to review the proposed merge of ~xavpaice/nagios-charm:parents into nagios-charm:master.
diff --git a/hooks/monitors-relation-changed b/hooks/monitors-relation-changed
index 7547a96..7bb355b 100755
--- a/hooks/monitors-relation-changed
+++ b/hooks/monitors-relation-changed
@@ -89,16 +89,32 @@ def main(argv):
     all_relations = new_all_relations
 
     initialize_inprogress_config()
+    # make a dict of machine ids to target-id hostnames
+    all_hosts = {}
     for relid, units in all_relations.items():
-        apply_relation_config(relid, units)
+        for unit, relation_settings in units.iteritems():
+            machine_id = relation_settings['machine_id']
+            all_hosts[machine_id] = relation_settings['target-id']
+    for relid, units in all_relations.items():
+        apply_relation_config(relid, units, all_hosts)
     refresh_hostgroups()
     flush_inprogress_config()
     os.system('service nagios3 reload')
 
-def apply_relation_config(relid, units):
+
+def apply_relation_config(relid, units, all_hosts):
     for unit, relation_settings in units.iteritems():
         monitors = relation_settings['monitors']
         target_id = relation_settings['target-id']
+        machine_id = relation_settings.get('machine_id', None)
+        parent_host = None
+        if machine_id:
+            container_regex = re.compile("(\d*)/lx[cd]/\d*")
+            if container_regex.search(machine_id):
+                parent_machine = container_regex.search(machine_id).group(1)
+                if all_hosts[parent_machine]:
+                    parent_host = all_hosts[parent_machine]
+
         # If not set, we don't mess with it, as multiple services may feed
         # monitors in for a particular address. Generally a primary will set this
         # to its own private-address
@@ -109,10 +125,13 @@ def apply_relation_config(relid, units):
 
         # Output nagios config
         host = get_pynag_host(target_id)
-
         if not target_address:
             raise Exception("No Target Address provied by NRPE service!")
         host.set_attribute('address', target_address)
+        if parent_host:
+            # We assume that we only want one parent and will overwrite any
+            # existing parents for this host.
+            host.set_attribute('parents', parent_host)
         host.save()
 
         for mon_family, mons in monitors['monitors']['remote'].iteritems():
@@ -125,5 +144,6 @@ def apply_relation_config(relid, units):
                     print('Ignoring %s due to unknown family %s' % (mon_name,
                                                                     mon_family))
 
+
 if __name__ == '__main__':
     main(sys.argv)

Follow ups