As you know from the database section, the server database has two tables that contain system configuration properties. Although I have created a data structure that allows for future expansion and is capable of holding a virtually unlimited number of configuration parameters, at this time it is going to serve only one purpose: to define the monitoring server address. The entry that is responsible for this parameter has the key value of ' monitor_url'. It is possible to override this setting for each individual node, and this is basically a way of distributing the load between multiple monitoring servers.
When the client gets an instruction to retrieve new data, it will connect back to the server and supply its hostname. The server code (Listing 9-3) first tries to look up its own address and port number from the CherryPy configuration class. To read the CherryPy configuration, you call the following function and provide the configuration item key as a parameter:
Bear in mind that you will get the result back only if it is defined in the configuration; therefore I have fallback statements that assume the default values.
The next step is to find the host specific settings and if they are not found, use the system-wide or default values. It is also possible that they are not defined; if so, we will send either the CherryPy configuration or, failing that, the assumed defaults.
Listing 9-3. Supplying a new server address @cherrypy.expose def cmd_get_new_monitor_url(self, host):
port = cherrypy.config.get('server.socket_port') if cherrypy.config.get('server.socket_port') else 8080 host = cherrypy.config.get('server.socket_host') if cherrypy.config.get('server.socket_host') else '127.0.0.1' server_url = "http://%s:%s/xmlrpc/M % (host, str(port)) con = sqlite3.connect('monitor.db')
res = con.execute( SELECT hostparams.value
FROM hostparams, host, systemparams WHERE host.id = hostparams.host_id AND systemparams.name = 'monitor_url' AND hostparams.param_id = systemparams.id AND host.address = ? , (host,) ).fetchone()
if not res:
res = con.execute( SELECT value FROM systemparams WHERE name =
server_url = res return server_url
Was this article helpful?