5 from datetime import datetime, timedelta
9 from monitor.database.info.model import *
10 from monitor.database.info.interface import *
15 from PLC.Parameter import Parameter, Mixed
17 def Parameter(a = None, b = None): pass
18 def Mixed(a = None, b = None, c = None): pass
20 def export_to_docbook(**kwargs):
33 # Inspect method. Remove self from the argument list.
34 max_args = method.func_code.co_varnames[0:method.func_code.co_argcount]
35 defaults = method.func_defaults
38 min_args = max_args[0:len(max_args) - len(defaults)]
40 defaults = tuple([None for arg in min_args]) + defaults
41 return (min_args, max_args, defaults)
43 keywords['name'] = method.__name__
44 keywords['args'] = args
46 method.__setattr__(arg, keywords[arg])
49 method.__setattr__(arg, kwargs[arg])
55 class MonitorXmlrpcServerMethods:
57 def listMethods(self):
58 mod = MonitorXmlrpcServer()
61 if isinstance(mod.__getattribute__(f),type(mod.__getattribute__('addDowntime'))):
65 def convert_datetime(d, keys=None):
71 if type(d[k]) == type(n):
72 ret[k] = time.mktime(d[k].utctimetuple())
76 class MonitorXmlrpcServer(object):
79 def listMethods(self):
80 mod = MonitorXmlrpcServer()
83 if isinstance(mod.__getattribute__(f),type(mod.__getattribute__('addDowntime'))):
89 params, method = xmlrpclib.loads(cherrypy.request.body.read())
91 if method == "xmlrpc":
93 raise AssertionError("method cannot be 'xmlrpc'")
94 # Get the function and make sure it's exposed.
95 method = getattr(self, method, None)
96 # Use the same error message to hide private method names
97 if method is None or not getattr(method, "exposed", False):
98 raise AssertionError("method does not exist")
101 # Call the method, convert it into a 1-element tuple
102 # as expected by dumps
103 response = method(*params)
106 response = xmlrpclib.dumps((response,), methodresponse=1, allow_none=1)
107 except xmlrpclib.Fault, fault:
108 # Can't marshal the result
109 response = xmlrpclib.dumps(fault, allow_none=1)
111 # Some other error; send back some error info
112 response = xmlrpclib.dumps(
113 xmlrpclib.Fault(1, "%s:%s" % (sys.exc_type, sys.exc_value))
116 cherrypy.response.headers["Content-Type"] = "text/xml"
119 # User-defined functions must use cherrypy.expose; turbogears.expose
120 # does additional checking of the response type that we don't want.
122 @export_to_docbook(roles=['tech', 'user', 'pi', 'admin'],
124 returns=Parameter(bool, 'True is successful'))
125 def upAndRunning(self):
126 """ This is a test """
129 # SITES ------------------------------------------------------------
132 def getSiteStatus(self, auth):
134 sites = HistorySiteRecord.query.all()
136 d = q.to_dict(exclude=['timestamp', 'version', ])
137 d = convert_datetime(d, ['last_checked', 'last_changed', 'message_created'])
142 def clearSitePenalty(self, auth, loginbase):
143 sitehist = SiteInterface.get_or_make(loginbase=loginbase)
144 sitehist.clearPenalty()
145 #sitehist.applyPenalty()
146 #sitehist.sendMessage('clear_penalty')
147 sitehist.closeTicket()
151 def increaseSitePenalty(self, auth, loginbase):
152 sitehist = SiteInterface.get_or_make(loginbase=loginbase)
153 sitehist.increasePenalty()
154 #sitehist.applyPenalty()
155 #sitehist.sendMessage('increase_penalty')
158 # NODES ------------------------------------------------------------
161 def getNodeStatus(self, auth):
163 sites = HistoryNodeRecord.query.all()
165 d = q.to_dict(exclude=['timestamp', 'version', ])
166 d = convert_datetime(d, ['last_checked', 'last_changed',])
171 def getRecentActions(self, auth, loginbase=None, hostname=None):
175 # BLACKLIST ------------------------------------------------------------
178 def getBlacklist(self, auth):
179 bl = BlacklistRecord.query.all()
182 d = q.to_dict(exclude=['timestamp', 'version', 'id', ])
183 d = convert_datetime(d, ['date_created'])
187 # datetime.datetime.fromtimestamp(time.mktime(time.strptime(mytime, time_format)))
190 def addHostToBlacklist(self, auth, hostname, expires=0):
191 bl = BlacklistRecord.findby_or_create(hostname=hostname, expires=expires)
195 def addSiteToBlacklist(self, auth, loginbase, expires=0):
196 bl = BlacklistRecord.findby_or_create(hostname=hostname, expires=expires)
200 def deleteFromBlacklist(self, auth, loginbase=None, hostname=None):
201 if (loginbase==None and hostname == None) or (loginbase != None and hostname != None):
202 raise Exception("Please specify a single record to delete: either hostname or loginbase")
203 elif loginbase != None:
204 bl = BlacklistRecord.get_by(loginbase=loginbase)
206 elif hostname != None:
207 bl = BlacklistRecord.get_by(hostname=hostname)