www/database.php
[monitor.git] / nodecommon.py
index cef1247..ba67625 100644 (file)
@@ -2,6 +2,8 @@
 import struct
 import reboot
 import time
+import util.file
+import plc
 from monitor import database
 from unified_model import PersistFlags
 esc = struct.pack('i', 27)
@@ -37,7 +39,6 @@ def get_current_state(fbnode):
        return l
 
 def color_pcu_state(fbnode):
-       import plc
 
        if 'plcnode' in fbnode and 'pcu_ids' in fbnode['plcnode'] and len(fbnode['plcnode']['pcu_ids']) > 0 :
                values = reboot.get_pcu_values(fbnode['plcnode']['pcu_ids'][0])
@@ -151,3 +152,35 @@ def datetime_fromstr(str):
                tup = time.strptime(str, "%m/%d/%Y")
        ret = datetime.fromtimestamp(time.mktime(tup))
        return ret
+
+def get_nodeset(config):
+       """
+               Given the config values passed in, return the set of hostnames that it
+               evaluates to.
+       """
+       api = plc.getAuthAPI()
+       l_nodes = database.dbLoad("l_plcnodes")
+
+       if config.nodelist:
+               f_nodes = util.file.getListFromFile(config.nodelist)
+               l_nodes = filter(lambda x: x['hostname'] in f_nodes, l_nodes)
+       elif config.node:
+               f_nodes = [config.node]
+               l_nodes = filter(lambda x: x['hostname'] in f_nodes, l_nodes)
+       elif config.nodegroup:
+               ng = api.GetNodeGroups({'name' : config.nodegroup})
+               l_nodes = api.GetNodes(ng[0]['node_ids'], ['hostname'])
+       elif config.site:
+               site = api.GetSites(config.site)
+               l_nodes = api.GetNodes(site[0]['node_ids'], ['hostname'])
+               
+       l_nodes = [node['hostname'] for node in l_nodes]
+
+       # perform this query after the above options, so that the filter above
+       # does not break.
+       if config.nodeselect:
+               fb = database.dbLoad("findbad")
+               l_nodes = node_select(config.nodeselect, fb['nodes'].keys(), fb)
+
+       return l_nodes
+