5 from datetime import datetime, timedelta
8 from monitor.database.info.model import *
9 from monitor.database.info.interface import *
11 class MonitorXmlrpcServerMethods:
13 def listMethods(self):
14 mod = MonitorXmlrpcServer()
17 if isinstance(mod.__getattribute__(f),type(mod.__getattribute__('addDowntime'))):
21 def convert_datetime(d, keys=None):
27 if type(d[k]) == type(n):
28 ret[k] = time.mktime(d[k].utctimetuple())
32 class MonitorXmlrpcServer(object):
35 def listMethods(self):
36 mod = MonitorXmlrpcServer()
39 if isinstance(mod.__getattribute__(f),type(mod.__getattribute__('addDowntime'))):
45 params, method = xmlrpclib.loads(cherrypy.request.body.read())
47 if method == "xmlrpc":
49 raise AssertionError("method cannot be 'xmlrpc'")
50 # Get the function and make sure it's exposed.
51 method = getattr(self, method, None)
52 # Use the same error message to hide private method names
53 if method is None or not getattr(method, "exposed", False):
54 raise AssertionError("method does not exist")
57 # Call the method, convert it into a 1-element tuple
58 # as expected by dumps
59 response = method(*params)
62 response = xmlrpclib.dumps((response,), methodresponse=1, allow_none=1)
63 except xmlrpclib.Fault, fault:
64 # Can't marshal the result
65 response = xmlrpclib.dumps(fault, allow_none=1)
67 # Some other error; send back some error info
68 response = xmlrpclib.dumps(
69 xmlrpclib.Fault(1, "%s:%s" % (sys.exc_type, sys.exc_value))
72 cherrypy.response.headers["Content-Type"] = "text/xml"
75 # User-defined functions must use cherrypy.expose; turbogears.expose
76 # does additional checking of the response type that we don't want.
78 def upAndRunning(self):
81 # SITES ------------------------------------------------------------
84 def getSiteStatus(self, auth):
86 sites = HistorySiteRecord.query.all()
88 d = q.to_dict(exclude=['timestamp', 'version', ])
89 d = convert_datetime(d, ['last_checked', 'last_changed', 'message_created'])
94 def clearSitePenalty(self, auth, loginbase):
95 sitehist = SiteInterface.get_or_make(loginbase=loginbase)
96 sitehist.clearPenalty()
97 #sitehist.applyPenalty()
98 #sitehist.sendMessage('clear_penalty')
99 sitehist.closeTicket()
103 def increaseSitePenalty(self, auth, loginbase):
104 sitehist = SiteInterface.get_or_make(loginbase=loginbase)
105 sitehist.increasePenalty()
106 #sitehist.applyPenalty()
107 #sitehist.sendMessage('increase_penalty')
110 # NODES ------------------------------------------------------------
113 def getNodeStatus(self, auth):
115 sites = HistoryNodeRecord.query.all()
117 d = q.to_dict(exclude=['timestamp', 'version', ])
118 d = convert_datetime(d, ['last_checked', 'last_changed',])
123 def getRecentActions(self, auth, loginbase=None, hostname=None):
127 # BLACKLIST ------------------------------------------------------------
130 def getBlacklist(self, auth):
131 bl = BlacklistRecord.query.all()
134 d = q.to_dict(exclude=['timestamp', 'version', 'id', ])
135 d = convert_datetime(d, ['date_created'])
139 # datetime.datetime.fromtimestamp(time.mktime(time.strptime(mytime, time_format)))
142 def addHostToBlacklist(self, auth, hostname, expires=0):
143 bl = BlacklistRecord.findby_or_create(hostname=hostname, expires=expires)
147 def addSiteToBlacklist(self, auth, loginbase, expires=0):
148 bl = BlacklistRecord.findby_or_create(hostname=hostname, expires=expires)
152 def deleteFromBlacklist(self, auth, loginbase=None, hostname=None):
153 if (loginbase==None and hostname == None) or (loginbase != None and hostname != None):
154 raise Exception("Please specify a single record to delete: either hostname or loginbase")
155 elif loginbase != None:
156 bl = BlacklistRecord.get_by(loginbase=loginbase)
158 elif hostname != None:
159 bl = BlacklistRecord.get_by(hostname=hostname)