pause penalties or apply them, not both...
authorStephen Soltesz <soltesz@cs.princeton.edu>
Fri, 19 Jun 2009 03:36:49 +0000 (03:36 +0000)
committerStephen Soltesz <soltesz@cs.princeton.edu>
Fri, 19 Jun 2009 03:36:49 +0000 (03:36 +0000)
plcquery tool frame.

plcquery.py [new file with mode: 0755]
policy.py

diff --git a/plcquery.py b/plcquery.py
new file mode 100755 (executable)
index 0000000..0540098
--- /dev/null
@@ -0,0 +1,79 @@
+#!/usr/bin/python
+
+
+from monitor.wrapper import plc
+api = plc.api
+
+from monitor import config
+
+def parse_fields(fields):
+    if fields:
+        s = fields.split(",")
+        if s == []:
+            return None
+        else:
+            return s
+    else:
+        return None
+
+def parse_filter(filter):
+    if filter:
+        s = filter.split("=")
+        if len(s) == 2:
+            return {s[0] : s[1]}
+        else:
+            raise Exception("filter format should be name=value")
+    else:
+        return None
+
+def print_fields(obj, fields):
+    for f in fields:
+        if f in obj:
+            print obj[f],
+    print ""
+
+def main():
+
+    from monitor import parser as parsermodule
+    parser = parsermodule.getParser()
+
+    parser.set_defaults(get=True,
+                        type='node',
+                        filter=None,
+                        fields='node_id,hostname,last_contact'
+                        )
+
+    parser.add_option("", "--get", dest="get", action="store_true",
+                        help="just get values for object")
+
+    parser.add_option("", "--type", dest="type", metavar="[node|person|site]", 
+                        help="object type to query")
+
+    parser.add_option("", "--fields", dest="fields", metavar="key,list,...", 
+                        help="a list of keys to display for each object.")
+
+    parser.add_option("", "--filter", dest="filter", metavar="name=value", 
+                        help="Filter passed to Get* calls")
+
+    parser.add_option("", "--nodelist", dest="nodelist", metavar="nodelist.txt", 
+                        help="A list of nodes to bring out of debug mode.")
+    parser.add_option("", "--listkeys", dest="listkeys", action="store_true",
+                        help="A list of nodes to bring out of debug mode.")
+
+    #parser = parsermodule.getParser(['defaults'], parser)
+    config = parsermodule.parse_args(parser)
+    
+    if config.nodelist:
+        nodelist = utilfile.getListFromFile(config.nodelist)
+
+    if config.get:
+        f = parse_filter(config.filter)
+        fields = parse_fields(config.fields)
+
+    if config.type == 'node': 
+        n = api.GetNodes(f, fields)
+        for i in n:
+            print_fields(i, fields)
+
+if __name__ == "__main__":
+    main()
index c1f69a5..edff7bd 100755 (executable)
--- a/policy.py
+++ b/policy.py
@@ -144,33 +144,7 @@ def main(hostnames, sitenames):
                recent_actions = sitehist.getRecentActions(loginbase=site)
 
                print "%s %s %s" % (i, sitehist.db.loginbase, sitehist.db.status)
-               if sitehist.db.status == 'down':
-                       if  not found_within(recent_actions, 'pause_penalty', 30) and \
-                               not found_within(recent_actions, 'increase_penalty', 7) and \
-                               changed_greaterthan(sitehist.db.last_changed, 7):
 
-                               # TODO: catch errors
-                               sitehist.increasePenalty()
-                               sitehist.applyPenalty()
-                               sitehist.sendMessage('increase_penalty')
-
-                               print "send message for site %s penalty increase" % site
-
-               if sitehist.db.status == 'good':
-                       # clear penalty
-                       # NOTE: because 'all clear' should have an indefinite status, we
-                       #               have a boolean value rather than a 'recent action'
-                       if sitehist.db.penalty_applied:
-                               # send message that penalties are cleared.
-
-                               sitehist.clearPenalty()
-                               sitehist.applyPenalty()
-                               sitehist.sendMessage('clear_penalty')
-                               sitehist.closeTicket()
-
-                               print "send message for site %s penalty cleared" % site
-
-               # find all ticket ids for site ( could be on the site record? )
                # determine if there are penalties within the last 30 days?
                # if so, add a 'pause_penalty' action.
                if sitehist.db.message_id != 0 and sitehist.db.message_status == 'open' and \
@@ -178,6 +152,34 @@ def main(hostnames, sitenames):
                        #       pause escalation
                        print "Pausing penalties for %s" % site
                        sitehist.pausePenalty()
+               else:
+
+                       if sitehist.db.status == 'down':
+                               if  not found_within(recent_actions, 'pause_penalty', 30) and \
+                                       not found_within(recent_actions, 'increase_penalty', 7) and \
+                                       changed_greaterthan(sitehist.db.last_changed, 7):
+
+                                       # TODO: catch errors
+                                       sitehist.increasePenalty()
+                                       sitehist.applyPenalty()
+                                       sitehist.sendMessage('increase_penalty')
+
+                                       print "send message for site %s penalty increase" % site
+
+                       if sitehist.db.status == 'good':
+                               # clear penalty
+                               # NOTE: because 'all clear' should have an indefinite status, we
+                               #               have a boolean value rather than a 'recent action'
+                               if sitehist.db.penalty_applied:
+                                       # send message that penalties are cleared.
+
+                                       sitehist.clearPenalty()
+                                       sitehist.applyPenalty()
+                                       sitehist.sendMessage('clear_penalty')
+                                       sitehist.closeTicket()
+
+                                       print "send message for site %s penalty cleared" % site
+
 
                site_count = site_count + 1