Developer's Guide

  • Docs Home
  • Community Home

2. MySQL Event database

MySQL is an open-source relational database that Zenoss uses to store Zenoss events. Configuration information about the MySQL database can be maintained by going to the Event Manager link from the navigation bar when you are logged in as a user with ZenManager privileges.

MySQL-level performance tweaking can substantially improve Zenoss' ability to handle events. One tool that can be used to improve your database performance is MySQLTuner.

If you need a connection to the MySQL events database, here is how to retrieve a connection and how to put it back into the pool.

DbConnectionPool is hidden and is accessed through DbAccessBase. It follows the Singleton design pattern, so it'll only actually create one DbConnectionPool. It extends the Python class Queue, so DbConnectionPool is also a synchronized queue and should be thread-safe. DbAccessBase is extended by EventManagerBase>?, so if you have access to the ZenEventManager (located at /zport/dmd/ZenEventManager) you'll have the ability to get a database connection.

2.1. Connecting to the Database

First you'll need to get an instance of ZenEventManager OR an instance of a class that extends DbAccessBase. Within Zenoss, a ZenEventManager should already be instantiated.

Next is the try block which should include ANY database calls. This is where you'll get a connection from the pool with the connect() method. You may pass this around to other methods or create a cursor and make some database transactions. The try block MUST be completed with a finally block that includes the close() method. You MUST pass the connection object to the close() method. This will ensure that even if the code within the try breaks, we are not leaking database connections. If you create more than one connection (ie more than one connect() call in your try block) you will need to have a corresponding close() call. There is ALWAYS a one-to-one relationship between connect() and close() calls.

Here is a block of code that illustrates best practices for using the DbConnectionPool

...
zem = self.dmd.ZenEventManager
try:
	conn1 = zem.connect()
	conn2 = zem.connect()
	curs1 = conn1.cursor()
	...
	curs2 = conn2.cursor()
	...
	# do work
	...
	curs3 = conn1.cursor()
	...
finally:
	zem.close(conn1)
	zem.close(conn2)
	...
...

Take a look at EventManagerBase.py for some examples of code using the DbConnectionPool.

2.2. MySQL in 60 Seconds

To start an interactive session with MySQL, run the mysql as the zenoss user. The following example is from a default install of Zenoss where there is no password for the MySQL root user.

$ mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17799
Server version: 5.0.45 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

Once we've logged into MySQL, we can see the various databases and see the tables that are available. The events database is maintained by Zenoss.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| events             |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.03 sec)
mysql> use events;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+------------------+
| Tables_in_events |
+------------------+
| alert_state      |
| detail           |
| heartbeat        |
| history          |
| log              |
| status           |
+------------------+
6 rows in set (0.00 sec)

From here we can determine what information is in what table. For instance, the log table.

mysql> describe log;
+----------+-------------+------+-----+-------------------+-------+
| Field    | Type        | Null | Key | Default           | Extra |
+----------+-------------+------+-----+-------------------+-------+
| evid     | char(25)    | NO   | MUL |                   |       |
| userName | varchar(64) | YES  |     | NULL              |       |
| ctime    | timestamp   | NO   |     | CURRENT_TIMESTAMP |       |
| text     | text        | YES  |     | NULL              |       |
+----------+-------------+------+-----+-------------------+-------+
4 rows in set (0.00 sec)