5 from datetime import datetime, timedelta
8 from monitor.database.info.model import *
9 from monitor.database.info.interface import *
12 from PLC.Parameter import Parameter, Mixed
14 def Parameter(a = None, b = None): pass
15 def Mixed(a = None, b = None, c = None): pass
17 def export_to_docbook(**kwargs):
30 # Inspect method. Remove self from the argument list.
31 max_args = method.func_code.co_varnames[0:method.func_code.co_argcount]
32 defaults = method.func_defaults
35 min_args = max_args[0:len(max_args) - len(defaults)]
37 defaults = tuple([None for arg in min_args]) + defaults
38 return (min_args, max_args, defaults)
40 keywords['name'] = method.__name__
41 keywords['args'] = args
43 method.__setattr__(arg, keywords[arg])
46 method.__setattr__(arg, kwargs[arg])
52 class MonitorXmlrpcServerMethods:
54 def listMethods(self):
55 mod = MonitorXmlrpcServer()
58 if isinstance(mod.__getattribute__(f),type(mod.__getattribute__('addDowntime'))):
62 def convert_datetime(d, keys=None):
68 if type(d[k]) == type(n):
69 ret[k] = time.mktime(d[k].utctimetuple())
73 class MonitorXmlrpcServer(object):
76 def listMethods(self):
77 mod = MonitorXmlrpcServer()
80 if isinstance(mod.__getattribute__(f),type(mod.__getattribute__('addDowntime'))):
86 params, method = xmlrpclib.loads(cherrypy.request.body.read())
88 if method == "xmlrpc":
90 raise AssertionError("method cannot be 'xmlrpc'")
91 # Get the function and make sure it's exposed.
92 method = getattr(self, method, None)
93 # Use the same error message to hide private method names
94 if method is None or not getattr(method, "exposed", False):
95 raise AssertionError("method does not exist")
98 # Call the method, convert it into a 1-element tuple
99 # as expected by dumps
100 response = method(*params)
103 response = xmlrpclib.dumps((response,), methodresponse=1, allow_none=1)
104 except xmlrpclib.Fault, fault:
105 # Can't marshal the result
106 response = xmlrpclib.dumps(fault, allow_none=1)
108 # Some other error; send back some error info
109 response = xmlrpclib.dumps(
110 xmlrpclib.Fault(1, "%s:%s" % (sys.exc_type, sys.exc_value))
113 cherrypy.response.headers["Content-Type"] = "text/xml"
116 # User-defined functions must use cherrypy.expose; turbogears.expose
117 # does additional checking of the response type that we don't want.
119 @export_to_docbook(roles=['tech', 'user', 'pi', 'admin'],
121 returns=Parameter(bool, 'True is successful'))
122 def upAndRunning(self):
125 # SITES ------------------------------------------------------------
128 def getSiteStatus(self, auth):
130 sites = HistorySiteRecord.query.all()
132 d = q.to_dict(exclude=['timestamp', 'version', ])
133 d = convert_datetime(d, ['last_checked', 'last_changed', 'message_created'])
138 def clearSitePenalty(self, auth, loginbase):
139 sitehist = SiteInterface.get_or_make(loginbase=loginbase)
140 sitehist.clearPenalty()
141 #sitehist.applyPenalty()
142 #sitehist.sendMessage('clear_penalty')
143 sitehist.closeTicket()
147 def increaseSitePenalty(self, auth, loginbase):
148 sitehist = SiteInterface.get_or_make(loginbase=loginbase)
149 sitehist.increasePenalty()
150 #sitehist.applyPenalty()
151 #sitehist.sendMessage('increase_penalty')
154 # NODES ------------------------------------------------------------
157 def getNodeStatus(self, auth):
159 sites = HistoryNodeRecord.query.all()
161 d = q.to_dict(exclude=['timestamp', 'version', ])
162 d = convert_datetime(d, ['last_checked', 'last_changed',])
167 def getRecentActions(self, auth, loginbase=None, hostname=None):
171 # BLACKLIST ------------------------------------------------------------
174 def getBlacklist(self, auth):
175 bl = BlacklistRecord.query.all()
178 d = q.to_dict(exclude=['timestamp', 'version', 'id', ])
179 d = convert_datetime(d, ['date_created'])
183 # datetime.datetime.fromtimestamp(time.mktime(time.strptime(mytime, time_format)))
186 def addHostToBlacklist(self, auth, hostname, expires=0):
187 bl = BlacklistRecord.findby_or_create(hostname=hostname, expires=expires)
191 def addSiteToBlacklist(self, auth, loginbase, expires=0):
192 bl = BlacklistRecord.findby_or_create(hostname=hostname, expires=expires)
196 def deleteFromBlacklist(self, auth, loginbase=None, hostname=None):
197 if (loginbase==None and hostname == None) or (loginbase != None and hostname != None):
198 raise Exception("Please specify a single record to delete: either hostname or loginbase")
199 elif loginbase != None:
200 bl = BlacklistRecord.get_by(loginbase=loginbase)
202 elif hostname != None:
203 bl = BlacklistRecord.get_by(hostname=hostname)